File:  [local] / rpl / rpltags / rpltags.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Fri Aug 6 15:32:56 2010 UTC (13 years, 7 months ago) by bertrand
Branches: MAIN
CVS tags: rpl-4_1_9, rpl-4_1_8, rpl-4_1_7, rpl-4_1_6, rpl-4_1_5, rpl-4_1_4, rpl-4_1_35, rpl-4_1_34, rpl-4_1_33, rpl-4_1_32, rpl-4_1_31, rpl-4_1_30, rpl-4_1_3, rpl-4_1_29, rpl-4_1_28, rpl-4_1_27, rpl-4_1_26, rpl-4_1_25, rpl-4_1_24, rpl-4_1_23, rpl-4_1_22, rpl-4_1_21, rpl-4_1_20, rpl-4_1_2, rpl-4_1_19, rpl-4_1_18, rpl-4_1_17, rpl-4_1_16, rpl-4_1_15, rpl-4_1_14, rpl-4_1_13, rpl-4_1_12, rpl-4_1_11, rpl-4_1_10, rpl-4_1_1, rpl-4_1_0, rpl-4_0_24, rpl-4_0_22, rpl-4_0_21, rpl-4_0_20, rpl-4_0_19, rpl-4_0_18, rpl-4_0, HEAD
Cohérence

    1: /*
    2: ================================================================================
    3:   RPLtags
    4: ================================================================================
    5: */
    6: 
    7: #include "rpltags.h"
    8: 
    9: char ***
   10: traitement(char *nom_fichier, char ***tab, int *nb_lignes)
   11: {
   12:     char                    *chaine;
   13:     char                    *ptr;
   14:     char                    *ptr2;
   15:     char                    *nom;
   16:     char                    *text;
   17: 
   18:     FILE                    *fichier;
   19: 
   20:     int                     drapeau_chaine;
   21:     int                     ligne_valide;
   22:     int                     niveau;
   23: 
   24:     unsigned long           i;
   25:     unsigned long           ligne;
   26:     unsigned long           nombre_caracteres_source;
   27: 
   28:     if ((fichier = fopen(nom_fichier, "r")) == NULL)
   29:     {
   30:         exit(EXIT_FAILURE);
   31:     }
   32: 
   33:     nombre_caracteres_source = 0;
   34: 
   35:     while(getc(fichier) != EOF)
   36:     {
   37:         nombre_caracteres_source++;
   38:     }
   39: 
   40:     if ((chaine = malloc((nombre_caracteres_source + 1) * sizeof(char)))
   41:             == NULL)
   42:     {
   43:         exit(EXIT_FAILURE);
   44:     }
   45: 
   46:     rewind(fichier);
   47:     ptr = chaine;
   48: 
   49:     for(i = 0; i < nombre_caracteres_source; i++)
   50:     {
   51:         *ptr++ = getc(fichier);
   52:     }
   53: 
   54:     *ptr = 0;
   55:     ptr = chaine;
   56:     text = NULL;
   57:     nom = NULL;
   58: 
   59:     drapeau_chaine = 0;
   60:     ligne = 1;
   61:     niveau = 0;
   62: 
   63:     while((*ptr) != 0)
   64:     {
   65:         if ((*ptr) == '\"')
   66:         {
   67:             drapeau_chaine = (drapeau_chaine == 0) ? -1 : 0;
   68:         }
   69: 
   70:         if (drapeau_chaine == 0)
   71:         {
   72:             if ((*ptr) == '#')
   73:             {
   74:                 if (ptr == chaine)
   75:                 {
   76:                     while(((*ptr) != '\n') && ((*ptr) != 0))
   77:                     {
   78:                         ptr++;
   79:                     }
   80:                 }
   81:                 else if ((*(ptr - 1)) == '\n')
   82:                 {
   83:                     while(((*ptr) != '\n') && ((*ptr) != 0))
   84:                     {
   85:                         ptr++;
   86:                     }
   87:                 }
   88:             }
   89:             else if ((*ptr) == '<')
   90:             {
   91:                 if ((*(ptr + 1)) == '<')
   92:                 {
   93:                     if (text == NULL)
   94:                     {
   95:                         if ((text = malloc(3 * sizeof(char))) == NULL)
   96:                         {
   97:                             exit(EXIT_FAILURE);
   98:                         }
   99: 
  100:                         text[0] = (*ptr);
  101:                         text[1] = (*(ptr + 1));
  102:                         text[2] = 0;
  103:                     }
  104:                     else
  105:                     {
  106:                         if ((text = realloc(text, (strlen(text) + 3) *
  107:                                 sizeof(char))) == NULL)
  108:                         {
  109:                             exit(EXIT_FAILURE);
  110:                         }
  111: 
  112:                         ptr2 = &text[strlen(text)];
  113:                         (*ptr2++) = (*ptr);
  114:                         (*ptr2++) = (*(ptr + 1));
  115:                         (*ptr2++) = 0;
  116:                     }
  117: 
  118:                     niveau++;
  119:                     ptr += 2;
  120:                 }
  121:             }
  122:             else if ((*ptr) == '>')
  123:             {
  124:                 if ((*(ptr + 1)) == '>')
  125:                 {
  126:                     if (text == NULL)
  127:                     {
  128:                         if ((text = malloc(3 * sizeof(char))) == NULL)
  129:                         {
  130:                             exit(EXIT_FAILURE);
  131:                         }
  132: 
  133:                         text[0] = (*ptr);
  134:                         text[1] = (*(ptr + 1));
  135:                         text[2] = 0;
  136:                     }
  137:                     else
  138:                     {
  139:                         if ((text = realloc(text, (strlen(text) + 3) *
  140:                                 sizeof(char))) == NULL)
  141:                         {
  142:                             exit(EXIT_FAILURE);
  143:                         }
  144: 
  145:                         ptr2 = &text[strlen(text)];
  146:                         (*ptr2++) = (*ptr);
  147:                         (*ptr2++) = (*(ptr + 1));
  148:                         (*ptr2++) = 0;
  149:                     }
  150: 
  151:                     niveau--;
  152:                     ptr += 2;
  153:                 }
  154:             }
  155:             else if ((*ptr) == '/')
  156:             {
  157:                 if ((*(ptr + 1)) == '/')
  158:                 {
  159:                     while(((*ptr) != '\n') && ((*ptr) != 0))
  160:                     {
  161:                         ptr++;
  162:                     }
  163:                 }
  164:                 else if ((*(ptr + 1)) == '*')
  165:                 {
  166:                     ptr += 2;
  167: 
  168:                     for(;;)
  169:                     {
  170:                         while(((*ptr) != '*') && ((*ptr) != 0))
  171:                         {
  172:                             if ((*ptr) == '\n')
  173:                             {
  174:                                 ligne++;
  175:                             }
  176: 
  177:                             ptr++;
  178:                         }
  179: 
  180:                         if ((*ptr) == 0)
  181:                         {
  182:                             break;
  183:                         }
  184: 
  185:                         if ((*(ptr + 1) == '/'))
  186:                         {
  187:                             ptr += 2;
  188:                             break;
  189:                         }
  190: 
  191:                         ptr++;
  192:                     }
  193:                 }
  194:             }
  195:         }
  196: 
  197:         if (niveau == 0)
  198:         {
  199:             if (nom == NULL)
  200:             {
  201:                 if ((nom = malloc(2 * sizeof(char))) == NULL)
  202:                 {
  203:                     exit(EXIT_FAILURE);
  204:                 }
  205: 
  206:                 nom[0] = (*ptr);
  207:                 nom[1] = 0;
  208:             }
  209:             else
  210:             {
  211:                 if ((nom = realloc(nom, (strlen(nom) + 2) * sizeof(char)))
  212:                         == NULL)
  213:                 {
  214:                     exit(EXIT_FAILURE);
  215:                 }
  216: 
  217:                 ptr2 = &nom[strlen(nom)];
  218:                 (*ptr2++) = (*ptr);
  219:                 (*ptr2++) = 0;
  220:             }
  221:         }
  222: 
  223:         if (text == NULL)
  224:         {
  225:             if ((text = malloc(2 * sizeof(char))) == NULL)
  226:             {
  227:                 exit(EXIT_FAILURE);
  228:             }
  229: 
  230:             text[0] = (*ptr);
  231:             text[1] = 0;
  232:         }
  233:         else
  234:         {
  235:             if ((text = realloc(text, (strlen(text) + 2) * sizeof(char)))
  236:                     == NULL)
  237:             {
  238:                 exit(EXIT_FAILURE);
  239:             }
  240: 
  241:             text[strlen(text) + 1] = 0;
  242:             text[strlen(text)] = (*ptr);
  243:         }
  244: 
  245:         if ((*ptr) == '\n')
  246:         {
  247:             ptr2 = nom;
  248:             ligne_valide = 0;
  249: 
  250:             if (nom != NULL)
  251:             {
  252:                 while((*ptr2) != 0)
  253:                 {
  254:                     if (((*ptr2) != ' ') && ((*ptr2) != '\n'))
  255:                     {
  256:                         ligne_valide = -1;
  257:                     }
  258: 
  259:                     ptr2++;
  260:                 }
  261:             }
  262: 
  263:             if (ligne_valide == -1)
  264:             {
  265:                 if (nom[strlen(nom) - 1] == '\n')
  266:                 {
  267:                     nom[strlen(nom) - 1] = 0;
  268:                 }
  269: 
  270:                 if (text[strlen(text) - 1] == '\n')
  271:                 {
  272:                     text[strlen(text) - 1] = 0;
  273:                 }
  274: 
  275:                 if ((*nb_lignes) == 0)
  276:                 {
  277:                     if ((tab = malloc(sizeof(char **))) == NULL)
  278:                     {
  279:                         exit(EXIT_FAILURE);
  280:                     }
  281: 
  282:                     if ((tab[0] = malloc(3 * sizeof(char *))) == NULL)
  283:                     {
  284:                         exit(EXIT_FAILURE);
  285:                     }
  286: 
  287:                     if ((tab[0][0] = malloc((strlen(nom) + 1) * sizeof(char)))
  288:                             == NULL)
  289:                     {
  290:                         exit(EXIT_FAILURE);
  291:                     }
  292: 
  293:                     strcpy(tab[0][0], nom);
  294: 
  295:                     if ((tab[0][1] = malloc((strlen(nom_fichier) + 1) *
  296:                             sizeof(char))) == NULL)
  297:                     {
  298:                         exit(EXIT_FAILURE);
  299:                     }
  300: 
  301:                     strcpy(tab[0][1], nom_fichier);
  302: 
  303:                     if ((tab[0][2] = malloc((strlen(text) + 1) * sizeof(char)))
  304:                             == NULL)
  305:                     {
  306:                         exit(EXIT_FAILURE);
  307:                     }
  308: 
  309:                     strcpy(tab[0][2], text);
  310:                 }
  311:                 else
  312:                 {
  313:                     if ((tab = realloc(tab, ((*nb_lignes) + 1) *
  314:                             sizeof(char **))) == NULL)
  315:                     {
  316:                         exit(EXIT_FAILURE);
  317:                     }
  318: 
  319:                     if ((tab[(*nb_lignes)] = malloc(3 * sizeof(char *)))
  320:                             == NULL)
  321:                     {
  322:                         exit(EXIT_FAILURE);
  323:                     }
  324: 
  325:                     if ((tab[(*nb_lignes)][0] = malloc((strlen(nom) + 1) *
  326:                             sizeof(char))) == NULL)
  327:                     {
  328:                         exit(EXIT_FAILURE);
  329:                     }
  330: 
  331:                     strcpy(tab[(*nb_lignes)][0], nom);
  332: 
  333:                     if ((tab[(*nb_lignes)][1] =
  334:                             malloc((strlen(nom_fichier) + 1) *
  335:                             sizeof(char))) == NULL)
  336:                     {
  337:                         exit(EXIT_FAILURE);
  338:                     }
  339: 
  340:                     strcpy(tab[(*nb_lignes)][1], nom_fichier);
  341: 
  342:                     if ((tab[(*nb_lignes)][2] = malloc((strlen(text) + 1) *
  343:                             sizeof(char))) == NULL)
  344:                     {
  345:                         exit(EXIT_FAILURE);
  346:                     }
  347: 
  348:                     strcpy(tab[(*nb_lignes)][2], text);
  349:                 }
  350: 
  351:                 (*nb_lignes)++;
  352:             }
  353: 
  354:             ligne++;
  355:             free(text);
  356:             free(nom);
  357:             text = NULL;
  358:             nom = NULL;
  359:         }
  360: 
  361:         ptr++;
  362:     }
  363: 
  364:     free(text);
  365:     free(chaine);
  366: 
  367:     fclose(fichier);
  368: 
  369:     return(tab);
  370: }
  371: 
  372: int
  373: main(int argc, char *argv[])
  374: {
  375:     char        ***tab;
  376: 
  377:     FILE        *tags;
  378: 
  379:     int         drapeau_desordre;
  380:     int         i;
  381:     int         j;
  382:     int         nb_lignes;
  383:     int         p;
  384: 
  385:     void        *tmp;
  386: 
  387:     if (argc == 1)
  388:     {
  389:         printf("No file specified\n");
  390:         return(EXIT_FAILURE);
  391:     }
  392:     else
  393:     {
  394:         p = 1;
  395:         argc--;
  396: 
  397:         tab = NULL;
  398:         nb_lignes = 0;
  399: 
  400:         while(argc > 0)
  401:         {
  402:             tab = traitement(argv[p], tab, &nb_lignes);
  403:             argc--;
  404:             p++;
  405:         }
  406: 
  407:         if ((tags = fopen("tags", "w+")) == NULL)
  408:         {
  409:             return(EXIT_FAILURE);
  410:         }
  411: 
  412:         i = 0;
  413:         j = 0;
  414:         drapeau_desordre = -1;
  415: 
  416:         for(i = 0; (i < nb_lignes) && drapeau_desordre; i++)
  417:         {
  418:             drapeau_desordre = 0;
  419:             for(j = 1; j < nb_lignes - i; j++)
  420:             {
  421:                 if (strcmp(tab[j][0], tab[j - 1][0]) < 0)
  422:                 {
  423:                     tmp = tab[j - 1];
  424:                     tab[j - 1] = tab[j];
  425:                     tab[j] = tmp;
  426: 
  427:                     drapeau_desordre = -1;
  428:                 }
  429:             }
  430:         }
  431: 
  432:         for(i = 0; i < nb_lignes; i++)
  433:         {
  434:             for(j = 0; j < strlen(tab[i][0]); j++)
  435:                 if (tab[i][0][j] == ' ') tab[i][0][j] = 0;
  436:             for(j = 0; j < strlen(tab[i][1]); j++)
  437:                 if (tab[i][1][j] == ' ') tab[i][1][j] = 0;
  438:             for(j = 0; j < strlen(tab[i][0]); j++)
  439:                 if (tab[i][2][j] == ' ') tab[i][2][j] = 0;
  440: 
  441:             fprintf(tags, "%s\t%s\t/^%s/;\n", tab[i][0], tab[i][1], tab[i][2]);
  442:         }
  443: 
  444:         fclose(tags);
  445: 
  446:         for(i = 0; i < nb_lignes; i++)
  447:         {
  448:             free(tab[i][0]);
  449:             free(tab[i][1]);
  450:             free(tab[i][2]);
  451:             free(tab[i]);
  452:         }
  453: 
  454:         free(tab);
  455:     }
  456: 
  457:     return(EXIT_SUCCESS);
  458: }

CVSweb interface <joel.bertrand@systella.fr>