1 | /* |
---|
2 | Copyright or © or Copr. Charles SORROR (2011/01/13) |
---|
3 | |
---|
4 | mail of the author: cslod@locean-ipsl.upmc.fr |
---|
5 | |
---|
6 | This software is a framework for variational data assimilation in |
---|
7 | numerical models. |
---|
8 | |
---|
9 | This software is governed by the CeCILL license under French law and |
---|
10 | abiding by the rules of distribution of free software. You can use, |
---|
11 | modify and/ or redistribute the software under the terms of the CeCILL |
---|
12 | license as circulated by CEA, CNRS and INRIA at the following URL |
---|
13 | "http://www.cecill.info". |
---|
14 | |
---|
15 | As a counterpart to the access to the source code and rights to copy, |
---|
16 | modify and redistribute granted by the license, users are provided only |
---|
17 | with a limited warranty and the software's author, the holder of the |
---|
18 | economic rights, and the successive licensors have only limited |
---|
19 | liability. |
---|
20 | |
---|
21 | In this respect, the user's attention is drawn to the risks associated |
---|
22 | with loading, using, modifying and/or developing or reproducing the |
---|
23 | software by the user in light of its specific status of free software, |
---|
24 | that may mean that it is complicated to manipulate, and that also |
---|
25 | therefore means that it is reserved for developers and experienced |
---|
26 | professionals having in-depth computer knowledge. Users are therefore |
---|
27 | encouraged to load and test the software's suitability as regards their |
---|
28 | requirements in conditions enabling the security of their systems and/or |
---|
29 | data to be ensured and, more generally, to use and operate it in the |
---|
30 | same conditions as regards security. |
---|
31 | |
---|
32 | The fact that you are presently reading this means that you have had |
---|
33 | knowledge of the CeCILL license and that you accept its terms. |
---|
34 | */ |
---|
35 | |
---|
36 | //gcc yrcmt.c -Wall -o yrcmt |
---|
37 | |
---|
38 | #include <stdlib.h> |
---|
39 | #include <stdio.h> |
---|
40 | #include <string.h> |
---|
41 | |
---|
42 | #define BUFSIZE 2048 |
---|
43 | |
---|
44 | FILE *fpin, *fpout; |
---|
45 | char buffer[BUFSIZE+1]; |
---|
46 | char *pstr1, *pstr2, *pstr3, *pstr4; |
---|
47 | short in_cmt_star, end; |
---|
48 | short in_string; |
---|
49 | int nbenrlus=0; |
---|
50 | |
---|
51 | //========================================================== |
---|
52 | int main(int argc, char *argv[]) |
---|
53 | { //pour virer les commentaires |
---|
54 | |
---|
55 | /* debut : verif arg, ouverture fichier ... */ |
---|
56 | if (argc<2 || argc>3) |
---|
57 | { printf ("syntaxe error: yrcmy file_in [file_out]}\n"); |
---|
58 | exit(-9); |
---|
59 | } |
---|
60 | if ((fpin = fopen(argv[1], "r")) <= 0) |
---|
61 | { printf ("problem when opening %s \n", argv[1]); |
---|
62 | exit(-9); |
---|
63 | } |
---|
64 | if (argc==3) |
---|
65 | { if ((fpout = fopen(argv[2], "w")) <= 0) |
---|
66 | { printf ("problem when opening %s \n", argv[2]); |
---|
67 | exit(-9); |
---|
68 | } |
---|
69 | } |
---|
70 | else fpout = stdout; |
---|
71 | |
---|
72 | while ( (fgets(buffer, BUFSIZE+1, fpin)) != NULL) |
---|
73 | { ++nbenrlus; |
---|
74 | pstr1 = buffer; |
---|
75 | end = 0; |
---|
76 | while (!end) |
---|
77 | { |
---|
78 | if (in_cmt_star) |
---|
79 | { //si on est dans un commentaire, on regarde |
---|
80 | //si on en trouve la fin |
---|
81 | pstr1 = strstr(pstr1, "*/"); |
---|
82 | if (pstr1 != NULL) |
---|
83 | { pstr1+=2; |
---|
84 | in_cmt_star = 0; |
---|
85 | } |
---|
86 | else |
---|
87 | end=1; |
---|
88 | } |
---|
89 | |
---|
90 | if (!in_cmt_star) |
---|
91 | { //si on est pas (ou plus) dans commentaire, on regarde si |
---|
92 | //y'en a |
---|
93 | pstr2 = pstr3 = pstr1; |
---|
94 | pstr2 = strstr(pstr1, "/*"); |
---|
95 | pstr3 = strstr(pstr1, "//"); |
---|
96 | pstr4 = strstr(pstr1, "\""); |
---|
97 | |
---|
98 | if (pstr4!=NULL) |
---|
99 | { //if ( (pstr4<pstr2 && pstr2!=NULL) && (pstr4<pstr3 && pstr3!=NULL) ) //" |
---|
100 | in_string=1; |
---|
101 | if (pstr2<pstr4 && pstr2!=NULL) in_string=0; |
---|
102 | if (pstr3<pstr4 && pstr3!=NULL) in_string=0; |
---|
103 | if (in_string) //on a detected le debut d'une chaine de carateres |
---|
104 | { //on doit en trouver la fin sur la meme ligne !!! |
---|
105 | ++pstr4; |
---|
106 | pstr4 = strstr(pstr4, "\""); |
---|
107 | if (pstr4==NULL) |
---|
108 | { printf(" > yrcmt error a line %i: a string must end on the same line !!!?\n", nbenrlus); |
---|
109 | exit(-9); |
---|
110 | } |
---|
111 | //on va ecrire y compris jusqu'a la fin de la chaine de carateres |
---|
112 | pstr4[0]='\0'; |
---|
113 | printf("%s", pstr1); |
---|
114 | printf("\""); |
---|
115 | pstr1=pstr4+1; |
---|
116 | if (pstr1[0]=='\0') |
---|
117 | end=1; |
---|
118 | continue; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | if (pstr2!=NULL && pstr3!=NULL) |
---|
123 | { //c'est l'inf qui compte |
---|
124 | if (pstr2<pstr3) //commentaire star (/*) |
---|
125 | { in_cmt_star=1; |
---|
126 | //il faut écrire ce qu'il y a avant |
---|
127 | pstr2[0]='\0'; |
---|
128 | printf("%s", pstr1); |
---|
129 | pstr1=pstr2+2; //pour avancer |
---|
130 | } |
---|
131 | if (pstr3<pstr2) //commentaire slash (//) |
---|
132 | { //il faut écrire ce qu'il y a avant |
---|
133 | pstr3[0]='\0'; |
---|
134 | printf("%s", pstr1); |
---|
135 | end=1; |
---|
136 | } |
---|
137 | } |
---|
138 | else if (pstr2!=NULL) //commentaire star (/*) |
---|
139 | { in_cmt_star=1; |
---|
140 | //il faut écrire ce qu'il y a avant |
---|
141 | pstr2[0]='\0'; |
---|
142 | printf("%s", pstr1); |
---|
143 | pstr1=pstr2+2; //pour avancer |
---|
144 | } |
---|
145 | else if (pstr3!=NULL) //commentaire slash (//) |
---|
146 | { //il faut écrire ce qu'il y a avant |
---|
147 | pstr3[0]='\0'; |
---|
148 | printf("%s", pstr1); |
---|
149 | end=1; |
---|
150 | } |
---|
151 | else //pas de commentaire, on reconduit |
---|
152 | { printf("%s", pstr1); |
---|
153 | end=1; |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | return(0); |
---|
160 | } |
---|
161 | //========================================================= |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | |
---|