avance dans replica avec fonction de clonage:
This commit is contained in:
Binary file not shown.
+43
-32
@@ -54,7 +54,7 @@ static char *escape_double_quote(const char *str)
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int search_end(FILE* file){
|
static int search_landmark(FILE* file){
|
||||||
fseek(file, 0, SEEK_SET);
|
fseek(file, 0, SEEK_SET);
|
||||||
char* line = NULL;
|
char* line = NULL;
|
||||||
size_t rded = 0;
|
size_t rded = 0;
|
||||||
@@ -62,7 +62,7 @@ static int search_end(FILE* file){
|
|||||||
int line_count = 0;
|
int line_count = 0;
|
||||||
while (getline(&line, &rded, file) != -1) {
|
while (getline(&line, &rded, file) != -1) {
|
||||||
printf("%s", line);
|
printf("%s", line);
|
||||||
found = strstr(line, "//END");
|
found = strstr(line, "//LANDMARK");
|
||||||
if (found != NULL)
|
if (found != NULL)
|
||||||
{
|
{
|
||||||
printf("MOTIF TROUVE: ligne %d\n", line_count);
|
printf("MOTIF TROUVE: ligne %d\n", line_count);
|
||||||
@@ -77,39 +77,55 @@ static int search_end(FILE* file){
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int insert_into_section(FILE* file, const char* name_out, const char* toInsert){
|
static int insert_into_section(char* arg, const char* name_out){
|
||||||
|
FILE *file = fopen(arg, "r");
|
||||||
fseek(file, 0, SEEK_SET);
|
fseek(file, 0, SEEK_SET);
|
||||||
if (strlen(name_out) > 48)
|
int landmark= search_landmark(file);
|
||||||
return -1;
|
|
||||||
int end = search_end(file);
|
FILE* out = fopen(name_out, "a+");
|
||||||
char name_concat[50] = {0};
|
|
||||||
for (int i = 0; name_out[i] != 0; i++){
|
|
||||||
name_concat[i] = name_out[i];
|
|
||||||
if (name_out[i + 1] == 0){
|
|
||||||
name_concat[i + 1] = '.';
|
|
||||||
name_concat[i + 2] = 'i';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("%s: name_concat for the moment: %s\n", __FUNCTION__, name_concat);
|
|
||||||
FILE* out = fopen(name_concat, "w");
|
|
||||||
printf("%s: ecriture du fichier avec %s a insere a la ligne %d\n", __FUNCTION__, toInsert, end);
|
|
||||||
fseek(file, 0, SEEK_SET);
|
fseek(file, 0, SEEK_SET);
|
||||||
int count = 0;
|
int count = 0;
|
||||||
char* line = NULL;
|
char* line = NULL;
|
||||||
size_t rded = 0;
|
size_t rded = 0;
|
||||||
|
char* toInsert = NULL;
|
||||||
|
printf("%s: quining begining\n", __FUNCTION__);
|
||||||
while(1){
|
while(1){
|
||||||
if (count == end)
|
|
||||||
fprintf(out, "%s", toInsert);
|
}
|
||||||
if (getline(&line, &rded, file) == -1)
|
fclose(out);
|
||||||
break;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char* create_name_out(const char* fileToBeCloned){
|
||||||
|
if (strlen(fileToBeCloned) > 48)
|
||||||
|
return NULL;
|
||||||
|
char *name_concat = malloc(50);
|
||||||
|
name_concat[0] = 0;
|
||||||
|
for (int i = 0; fileToBeCloned[i] != 0; i++){
|
||||||
|
name_concat[i] = fileToBeCloned[i];
|
||||||
|
if (fileToBeCloned[i + 1] == 0){
|
||||||
|
name_concat[i + 1] = '.';
|
||||||
|
name_concat[i + 2] = 'i';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return name_concat;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cloning_file(char* fileToBeCloned, char* name_out){
|
||||||
|
FILE* in = fopen(fileToBeCloned, "r");
|
||||||
|
FILE* out = fopen(name_out, "w");
|
||||||
|
char* line = NULL;
|
||||||
|
size_t rded = 0;
|
||||||
|
int line_count = 0;
|
||||||
|
while (getline(&line, &rded, in) != -1) {
|
||||||
fprintf(out, "%s", line);
|
fprintf(out, "%s", line);
|
||||||
free(line);
|
free(line);
|
||||||
line = NULL;
|
line = NULL;
|
||||||
count++;
|
line_count++;
|
||||||
}
|
}
|
||||||
/* FAIRE LE QUINE AUTOMATIQUE */
|
printf("%s: clonage finis\n", __FUNCTION__);
|
||||||
|
free(line);
|
||||||
fclose(out);
|
fclose(out);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
@@ -117,13 +133,8 @@ int main(int argc, char **argv)
|
|||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
FILE *in = fopen(argv[1], "r+");
|
char* name_out = create_name_out(argv[1]);
|
||||||
char* line = NULL;
|
cloning_file(argv[1], name_out);
|
||||||
size_t rded = 0;
|
//insert_into_section(argv[1], name_out);
|
||||||
if (!in)
|
|
||||||
return 1;
|
|
||||||
insert_into_section(in, argv[1], "INSERTED\n");
|
|
||||||
free(line);
|
|
||||||
fclose(in);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user