Previous 199869 Revisions Next

r20199 Friday 11th January, 2013 at 16:58:02 UTC by smf
srcclean now processes XML & C comments the same, so they can be indented but within the comment itself spaces are used [smf]
[src/tools]srcclean.c

trunk/src/tools/srcclean.c
r20198r20199
7676   int removed_newlines = 0;
7777   int src = 0;
7878   int dst = 0;
79   bool in_c_comment = false;
80   bool in_cpp_comment = false;
81   int indent_c_comment = 0;
79   bool in_multiline_comment = false;
80   bool in_singleline_comment = false;
81   int indent_multiline_comment = 0;
8282   int in_c_string = FALSE;
8383   int hichars = 0;
8484   bool is_c_file;
r20198r20199
8888   int bytes;
8989   int col = 0;
9090   int escape = 0;
91   int consume = 0;
9192   const int tab_size = 4;
9293
9394   /* print usage info */
r20198r20199
117118   {
118119      UINT8 ch = original[src++];
119120
120      /* check for invalid upper-ASCII chars, but only for non-xml files (swlists might contain UTF-8 chars) */
121      if (!is_xml_file && ch != 13 && ch != 10 && ch != 9 && (ch > 127 || ch < 32))
121      if (consume == 0)
122122      {
123         ch = '?';
124         hichars++;
125      }
123         /* C-specific handling */
124         if (is_c_file)
125         {
126            /* check for string/char literals */
127            if ((ch == '"' || ch == '\'') && !in_multiline_comment && !in_singleline_comment )
128            {
129               if (ch == in_c_string && !escape)
130                  in_c_string = 0;
131               else if (!in_c_string)
132                  in_c_string = ch;
133            }
126134
127      /* C-specific handling */
128      if (is_c_file)
129      {
130         /* check for string/char literals */
131         if ((ch == '"' || ch == '\'') && !in_c_comment && !in_cpp_comment )
132         {
133            if (ch == in_c_string && !escape)
134               in_c_string = 0;
135            else if (!in_c_string)
136               in_c_string = ch;
135            /* Update escape state */
136            if (in_c_string)
137               escape = (ch == '\\') ? !escape : 0;
138
139            if (!in_c_string && !in_singleline_comment)
140            {
141               /* track whether or not we are within a C-style comment */
142               if (!in_multiline_comment && ch == '/' && original[src] == '*')
143               {
144                  in_multiline_comment = true;
145                  if (col > 0 && modified[dst-1] == 0x09)
146                  {
147                     indent_multiline_comment = col;
148                  }
149                  else
150                  {
151                     indent_multiline_comment = 0;
152                  }
153                  consume = 2;
154               }
155               else if (in_multiline_comment && ch == '*' && original[src] == '/')
156               {
157                  in_multiline_comment = false;
158                  indent_multiline_comment = 0;
159                  consume = 2;
160               }
161
162               /* track whether or not we are within a C++-style comment */
163               else if (!in_multiline_comment && ch == '/' && original[src] == '/')
164               {
165                  in_singleline_comment = true;
166                  consume = 2;
167               }
168            }
137169         }
138170
139         /* Update escape state */
140         if (in_c_string)
141            escape = (ch == '\\') ? !escape : 0;
142
143         if (!in_c_string && !in_cpp_comment)
171         if (is_xml_file)
144172         {
145            int consume = TRUE;
146
147            /* track whether or not we are within a C-style comment */
148            if (!in_c_comment && ch == '/' && original[src] == '*')
173            /* track whether or not we are within a XML comment */
174            if (!in_multiline_comment && ch == '<' && original[src] == '!' && original[src+1] == '-' && original[src+2] == '-')
149175            {
150               in_c_comment = true;
176               in_multiline_comment = true;
151177               if (col > 0 && modified[dst-1] == 0x09)
152178               {
153                  indent_c_comment = col;
179                  indent_multiline_comment = col;
154180               }
155181               else
156182               {
157                  indent_c_comment = 0;
183                  indent_multiline_comment = 0;
158184               }
185               consume = 4;
159186            }
160            else if (in_c_comment && ch == '*' && original[src] == '/')
187            else if (in_multiline_comment && ch == '-' && original[src] == '-' && original[src+1] == '>')
161188            {
162               in_c_comment = false;
163               indent_c_comment = 0;
189               in_multiline_comment = false;
190               indent_multiline_comment = 0;
191               consume = 3;
164192            }
165
166            /* track whether or not we are within a C++-style comment */
167            else if (!in_c_comment && ch == '/' && original[src] == '/')
168               in_cpp_comment = true;
169            else
170               consume = FALSE;
171
172            if (consume)
173            {
174               modified[dst++] = ch;
175               col++;
176               ch = original[src++];
177            }
178193         }
179194      }
180195
181      /* if we hit a LF without a CR, back up and act like we hit a CR */
182      if (ch == 0x0a)
196      if (consume != 0)
183197      {
184         src--;
185         ch = 0x0d;
186         fixed_nix_style = 1;
198         modified[dst++] = ch;
199         col++;
200         consume--;
187201      }
188202
189      /* if we hit a CR, clean up from there */
190      if (ch == 0x0d)
203      /* if we hit a CR or LF, clean up from there */
204      else if (ch == 0x0d || ch == 0x0a)
191205      {
192206         /* remove all extra spaces/tabs at the end */
193207         while (dst > 0 && (modified[dst-1] == ' ' || modified[dst-1] == 0x09))
r20198r20199
202216         col = 0;
203217
204218         /* skip over any LF in the source file */
205         if (original[src] == 0x0a)
219         if (ch == 0x0d && original[src] == 0x0a)
206220            src++;
221         else if (ch == 0x0a)
222            fixed_nix_style = 1;
207223         else
208224            fixed_mac_style = 1;
209225
210226         /* we are no longer in a C++-style comment */
211         in_cpp_comment = false;
227         in_singleline_comment = false;
212228
213229         if (in_c_string)
214230         {
r20198r20199
223239         int spaces = tab_size - (col % tab_size);
224240
225241         /* convert tabs to spaces, if not used for indenting */
226         if ((in_c_comment && col >= indent_c_comment) || (col != 0 && modified[dst-1] != 0x09))
242         if ((in_multiline_comment && col >= indent_multiline_comment) || (col != 0 && modified[dst-1] != 0x09))
227243         {
228244            while (spaces > 0)
229245            {
r20198r20199
261277         }
262278
263279         /* convert spaces to tabs, if used for indenting */
264         while (spaces > 0 && (!in_c_comment || col < indent_c_comment) && (col == 0 || modified[dst-1] == 0x09))
280         while (spaces > 0 && (!in_multiline_comment || col < indent_multiline_comment) && (col == 0 || modified[dst-1] == 0x09))
265281         {
266282            modified[dst++] = 0x09;
267283            spaces -= tab_size;
r20198r20199
280296      /* otherwise, copy the source character */
281297      else
282298      {
299         /* check for invalid upper-ASCII chars, but only for non-xml files (swlists might contain UTF-8 chars) */
300         if (!is_xml_file && (ch < 32 || ch > 127))
301         {
302            ch = '?';
303            hichars++;
304         }
305
283306         modified[dst++] = ch;
284307         col++;
285308      }
286309   }
287310
288311   /* if we didn't find an end of comment, we screwed up */
289   if (in_c_comment)
312   if (in_multiline_comment)
290313   {
291      printf("Error: unmatched C-style comment (%s)!\n", argv[1]);
314      printf("Error: unmatched multi-line comment (%s)!\n", argv[1]);
292315      return 1;
293316   }
294317

Previous 199869 Revisions Next


© 1997-2024 The MAME Team