/* ================================================================================ RPLtags ================================================================================ */ #include "rpltags.h" char *** traitement(char *nom_fichier, char ***tab, int *nb_lignes) { char *chaine; char *ptr; char *ptr2; char *nom; char *text; FILE *fichier; int drapeau_chaine; int ligne_valide; int niveau; unsigned long i; unsigned long ligne; unsigned long nombre_caracteres_source; if ((fichier = fopen(nom_fichier, "r")) == NULL) { exit(EXIT_FAILURE); } nombre_caracteres_source = 0; while(getc(fichier) != EOF) { nombre_caracteres_source++; } if ((chaine = malloc((nombre_caracteres_source + 1) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } rewind(fichier); ptr = chaine; for(i = 0; i < nombre_caracteres_source; i++) { *ptr++ = getc(fichier); } *ptr = 0; ptr = chaine; text = NULL; nom = NULL; drapeau_chaine = 0; ligne = 1; niveau = 0; while((*ptr) != 0) { if ((*ptr) == '\"') { drapeau_chaine = (drapeau_chaine == 0) ? -1 : 0; } if (drapeau_chaine == 0) { if ((*ptr) == '#') { if (ptr == chaine) { while(((*ptr) != '\n') && ((*ptr) != 0)) { ptr++; } } else if ((*(ptr - 1)) == '\n') { while(((*ptr) != '\n') && ((*ptr) != 0)) { ptr++; } } } else if ((*ptr) == '<') { if ((*(ptr + 1)) == '<') { if (text == NULL) { if ((text = malloc(3 * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } text[0] = (*ptr); text[1] = (*(ptr + 1)); text[2] = 0; } else { if ((text = realloc(text, (strlen(text) + 3) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } ptr2 = &text[strlen(text)]; (*ptr2++) = (*ptr); (*ptr2++) = (*(ptr + 1)); (*ptr2++) = 0; } niveau++; ptr += 2; } } else if ((*ptr) == '>') { if ((*(ptr + 1)) == '>') { if (text == NULL) { if ((text = malloc(3 * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } text[0] = (*ptr); text[1] = (*(ptr + 1)); text[2] = 0; } else { if ((text = realloc(text, (strlen(text) + 3) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } ptr2 = &text[strlen(text)]; (*ptr2++) = (*ptr); (*ptr2++) = (*(ptr + 1)); (*ptr2++) = 0; } niveau--; ptr += 2; } } else if ((*ptr) == '/') { if ((*(ptr + 1)) == '/') { while(((*ptr) != '\n') && ((*ptr) != 0)) { ptr++; } } else if ((*(ptr + 1)) == '*') { ptr += 2; for(;;) { while(((*ptr) != '*') && ((*ptr) != 0)) { if ((*ptr) == '\n') { ligne++; } ptr++; } if ((*ptr) == 0) { break; } if ((*(ptr + 1) == '/')) { ptr += 2; break; } ptr++; } } } } if (niveau == 0) { if (nom == NULL) { if ((nom = malloc(2 * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } nom[0] = (*ptr); nom[1] = 0; } else { if ((nom = realloc(nom, (strlen(nom) + 2) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } ptr2 = &nom[strlen(nom)]; (*ptr2++) = (*ptr); (*ptr2++) = 0; } } if (text == NULL) { if ((text = malloc(2 * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } text[0] = (*ptr); text[1] = 0; } else { if ((text = realloc(text, (strlen(text) + 2) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } text[strlen(text) + 1] = 0; text[strlen(text)] = (*ptr); } if ((*ptr) == '\n') { ptr2 = nom; ligne_valide = 0; if (nom != NULL) { while((*ptr2) != 0) { if (((*ptr2) != ' ') && ((*ptr2) != '\n')) { ligne_valide = -1; } ptr2++; } } if (ligne_valide == -1) { if (nom[strlen(nom) - 1] == '\n') { nom[strlen(nom) - 1] = 0; } if (text[strlen(text) - 1] == '\n') { text[strlen(text) - 1] = 0; } if ((*nb_lignes) == 0) { if ((tab = malloc(sizeof(char **))) == NULL) { exit(EXIT_FAILURE); } if ((tab[0] = malloc(3 * sizeof(char *))) == NULL) { exit(EXIT_FAILURE); } if ((tab[0][0] = malloc((strlen(nom) + 1) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } strcpy(tab[0][0], nom); if ((tab[0][1] = malloc((strlen(nom_fichier) + 1) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } strcpy(tab[0][1], nom_fichier); if ((tab[0][2] = malloc((strlen(text) + 1) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } strcpy(tab[0][2], text); } else { if ((tab = realloc(tab, ((*nb_lignes) + 1) * sizeof(char **))) == NULL) { exit(EXIT_FAILURE); } if ((tab[(*nb_lignes)] = malloc(3 * sizeof(char *))) == NULL) { exit(EXIT_FAILURE); } if ((tab[(*nb_lignes)][0] = malloc((strlen(nom) + 1) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } strcpy(tab[(*nb_lignes)][0], nom); if ((tab[(*nb_lignes)][1] = malloc((strlen(nom_fichier) + 1) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } strcpy(tab[(*nb_lignes)][1], nom_fichier); if ((tab[(*nb_lignes)][2] = malloc((strlen(text) + 1) * sizeof(char))) == NULL) { exit(EXIT_FAILURE); } strcpy(tab[(*nb_lignes)][2], text); } (*nb_lignes)++; } ligne++; free(text); free(nom); text = NULL; nom = NULL; } ptr++; } free(text); free(chaine); fclose(fichier); return(tab); } int main(int argc, char *argv[]) { char ***tab; FILE *tags; int drapeau_desordre; int i; int j; int nb_lignes; int p; void *tmp; if (argc == 1) { printf("No file specified\n"); return(EXIT_FAILURE); } else { p = 1; argc--; tab = NULL; nb_lignes = 0; while(argc > 0) { tab = traitement(argv[p], tab, &nb_lignes); argc--; p++; } if ((tags = fopen("tags", "w+")) == NULL) { return(EXIT_FAILURE); } i = 0; j = 0; drapeau_desordre = -1; for(i = 0; (i < nb_lignes) && drapeau_desordre; i++) { drapeau_desordre = 0; for(j = 1; j < nb_lignes - i; j++) { if (strcmp(tab[j][0], tab[j - 1][0]) < 0) { tmp = tab[j - 1]; tab[j - 1] = tab[j]; tab[j] = tmp; drapeau_desordre = -1; } } } for(i = 0; i < nb_lignes; i++) { for(j = 0; j < strlen(tab[i][0]); j++) if (tab[i][0][j] == ' ') tab[i][0][j] = 0; for(j = 0; j < strlen(tab[i][1]); j++) if (tab[i][1][j] == ' ') tab[i][1][j] = 0; for(j = 0; j < strlen(tab[i][0]); j++) if (tab[i][2][j] == ' ') tab[i][2][j] = 0; fprintf(tags, "%s\t%s\t/^%s/;\n", tab[i][0], tab[i][1], tab[i][2]); } fclose(tags); for(i = 0; i < nb_lignes; i++) { free(tab[i][0]); free(tab[i][1]); free(tab[i][2]); free(tab[i]); } free(tab); } return(EXIT_SUCCESS); }