[1] | 1 | //gcc yrcmt.c -Wall -o yrcmt |
---|
| 2 | |
---|
| 3 | #include <stdlib.h> |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include <string.h> |
---|
| 6 | |
---|
| 7 | #define BUFSIZE 2048 |
---|
| 8 | |
---|
| 9 | FILE *fpin, *fpout; |
---|
| 10 | char buffer[BUFSIZE+1]; |
---|
| 11 | char *pstr1, *pstr2, *pstr3, *pstr4; |
---|
| 12 | short in_cmt_star, end; |
---|
| 13 | short in_string; |
---|
| 14 | int nbenrlus=0; |
---|
| 15 | |
---|
| 16 | //========================================================== |
---|
| 17 | int main(int argc, char *argv[]) |
---|
| 18 | { //pour virer les commentaires |
---|
| 19 | |
---|
| 20 | /* debut : verif arg, ouverture fichier ... */ |
---|
| 21 | if (argc<2 || argc>3) |
---|
| 22 | { printf ("syntaxe error: yrcmy file_in [file_out]}\n"); |
---|
| 23 | exit(-9); |
---|
| 24 | } |
---|
| 25 | if ((fpin = fopen(argv[1], "r")) <= 0) |
---|
| 26 | { printf ("problem when opening %s \n", argv[1]); |
---|
| 27 | exit(-9); |
---|
| 28 | } |
---|
| 29 | if (argc==3) |
---|
| 30 | { if ((fpout = fopen(argv[2], "w")) <= 0) |
---|
| 31 | { printf ("problem when opening %s \n", argv[2]); |
---|
| 32 | exit(-9); |
---|
| 33 | } |
---|
| 34 | } |
---|
| 35 | else fpout = stdout; |
---|
| 36 | |
---|
| 37 | while ( (fgets(buffer, BUFSIZE+1, fpin)) != NULL) |
---|
| 38 | { ++nbenrlus; |
---|
| 39 | pstr1 = buffer; |
---|
| 40 | end = 0; |
---|
| 41 | while (!end) |
---|
| 42 | { |
---|
| 43 | if (in_cmt_star) |
---|
| 44 | { //si on est dans un commentaire, on regarde |
---|
| 45 | //si on en trouve la fin |
---|
| 46 | pstr1 = strstr(pstr1, "*/"); |
---|
| 47 | if (pstr1 != NULL) |
---|
| 48 | { pstr1+=2; |
---|
| 49 | in_cmt_star = 0; |
---|
| 50 | } |
---|
| 51 | else |
---|
| 52 | end=1; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | if (!in_cmt_star) |
---|
| 56 | { //si on est pas (ou plus) dans commentaire, on regarde si |
---|
| 57 | //y'en a |
---|
| 58 | pstr2 = pstr3 = pstr1; |
---|
| 59 | pstr2 = strstr(pstr1, "/*"); |
---|
| 60 | pstr3 = strstr(pstr1, "//"); |
---|
| 61 | pstr4 = strstr(pstr1, "\""); |
---|
| 62 | |
---|
| 63 | if (pstr4!=NULL) |
---|
| 64 | { //if ( (pstr4<pstr2 && pstr2!=NULL) && (pstr4<pstr3 && pstr3!=NULL) ) //" |
---|
| 65 | in_string=1; |
---|
| 66 | if (pstr2<pstr4 && pstr2!=NULL) in_string=0; |
---|
| 67 | if (pstr3<pstr4 && pstr3!=NULL) in_string=0; |
---|
| 68 | if (in_string) //on a detected le debut d'une chaine de carateres |
---|
| 69 | { //on doit en trouver la fin sur la meme ligne !!! |
---|
| 70 | ++pstr4; |
---|
| 71 | pstr4 = strstr(pstr4, "\""); |
---|
| 72 | if (pstr4==NULL) |
---|
| 73 | { printf(" > yrcmt error a line %i: a string must end on the same line !!!?\n", nbenrlus); |
---|
| 74 | exit(-9); |
---|
| 75 | } |
---|
| 76 | //on va ecrire y compris jusqu'a la fin de la chaine de carateres |
---|
| 77 | pstr4[0]='\0'; |
---|
| 78 | printf("%s", pstr1); |
---|
| 79 | printf("\""); |
---|
| 80 | pstr1=pstr4+1; |
---|
| 81 | if (pstr1[0]=='\0') |
---|
| 82 | end=1; |
---|
| 83 | continue; |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | if (pstr2!=NULL && pstr3!=NULL) |
---|
| 88 | { //c'est l'inf qui compte |
---|
| 89 | if (pstr2<pstr3) //commentaire star (/*) |
---|
| 90 | { in_cmt_star=1; |
---|
| 91 | //il faut écrire ce qu'il y a avant |
---|
| 92 | pstr2[0]='\0'; |
---|
| 93 | printf("%s", pstr1); |
---|
| 94 | pstr1=pstr2+2; //pour avancer |
---|
| 95 | } |
---|
| 96 | if (pstr3<pstr2) //commentaire slash (//) |
---|
| 97 | { //il faut écrire ce qu'il y a avant |
---|
| 98 | pstr3[0]='\0'; |
---|
| 99 | printf("%s", pstr1); |
---|
| 100 | end=1; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | else if (pstr2!=NULL) //commentaire star (/*) |
---|
| 104 | { in_cmt_star=1; |
---|
| 105 | //il faut écrire ce qu'il y a avant |
---|
| 106 | pstr2[0]='\0'; |
---|
| 107 | printf("%s", pstr1); |
---|
| 108 | pstr1=pstr2+2; //pour avancer |
---|
| 109 | } |
---|
| 110 | else if (pstr3!=NULL) //commentaire slash (//) |
---|
| 111 | { //il faut écrire ce qu'il y a avant |
---|
| 112 | pstr3[0]='\0'; |
---|
| 113 | printf("%s", pstr1); |
---|
| 114 | end=1; |
---|
| 115 | } |
---|
| 116 | else //pas de commentaire, on reconduit |
---|
| 117 | { printf("%s", pstr1); |
---|
| 118 | end=1; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | return(0); |
---|
| 125 | } |
---|
| 126 | //========================================================= |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | |
---|