Previous 199869 Revisions Next

r18355 Monday 8th October, 2012 at 10:58:03 UTC by smf
Spaces are converted to tabs at beginning of lines and tabs are converted to spaces everywhere else. Comments in .lst files are cleaned. Extra blank lines are trimmed from ends of source files [smf]
[src/tools]srcclean.c

trunk/src/tools/srcclean.c
r18354r18355
4848    CONSTANTS & DEFINES
4949***************************************************************************/
5050
51#define MAX_FILE_SIZE   (32 * 1024 * 1024)
51#define MAX_FILE_SIZE   (32 * 1024 * 1024)
5252
5353
5454
r18354r18355
6767
6868int main(int argc, char *argv[])
6969{
70   int removed_tabs = 0, removed_spaces = 0, fixed_mac_style = 0, fixed_nix_style = 0, added_newline = 0;
70   int removed_tabs = 0, added_tabs = 0, removed_spaces = 0, fixed_mac_style = 0, fixed_nix_style = 0, added_newline = 0, removed_newlines = 0;
7171   int src = 0, dst = 0, in_c_comment = FALSE, in_cpp_comment = FALSE, in_c_string = FALSE;
7272   int hichars = 0;
7373   int is_c_file, is_xml_file;
r18354r18355
7676   int bytes;
7777   int col = 0;
7878   int escape = 0;
79   const int tab_size = 4;
7980
8081   /* print usage info */
8182   if (argc != 2)
r18354r18355
9697
9798   /* determine if we are a C file */
9899   ext = strrchr(argv[1], '.');
99   is_c_file = (ext && (core_stricmp(ext, ".c") == 0 || core_stricmp(ext, ".h") == 0 || core_stricmp(ext, ".cpp") == 0));
100   is_c_file = (ext && (core_stricmp(ext, ".c") == 0 || core_stricmp(ext, ".h") == 0 || core_stricmp(ext, ".cpp") == 0 || core_stricmp(ext, ".lst") == 0));
100101   is_xml_file = (ext && core_stricmp(ext, ".xml") == 0);
101102
102103   /* rip through it */
r18354r18355
194195      /* if we hit a tab... */
195196      else if (ch == 0x09)
196197      {
197         int spaces = 4 - col % 4;
198         int spaces = tab_size - (col % tab_size);
198199
199         /* Remove invisible spaces */
200         while ((spaces & 3) != 0 && dst > 0 && modified[dst-1] == ' ')
201         {
202            removed_spaces++;
203            spaces++;
204            col--;
205            dst--;
206         }
207200         col += spaces;
208201
209         /* if inside a comment, expand it */
210         if (in_c_comment || in_cpp_comment)
202         /* if inside a comment or in the middle of a line, expand it */
203         if (in_c_comment || in_cpp_comment || (col - spaces > 0 && modified[dst-1] != 0x09))
211204         {
212205            while (spaces--) modified[dst++] = ' ';
213206            removed_tabs++;
r18354r18355
218211         }
219212      }
220213
214      /* convert spaces to tabs at beginning of lines */
215      else if (ch == 0x20 && !in_c_comment && !in_cpp_comment && (col == 0 || modified[dst-1] == 0x09))
216      {
217         int spaces = 1;
218
219         while (original[src]==32)
220         {
221            spaces++;
222            src++;
223         }
224
225         while (spaces > 0)
226         {
227            modified[dst++] = 0x09;
228            spaces -= tab_size;
229            added_tabs++;
230            col += tab_size;
231         }
232      }
233
221234      /* otherwise, copy the source character */
222235      else
223236      {
r18354r18355
233246      return 1;
234247   }
235248
236   if (is_c_file && modified[dst - 1] != 0x0a)
249   if (is_c_file)
237250   {
238      modified[dst++] = 0x0d;
239      modified[dst++] = 0x0a;
240      added_newline = 1;
251      if (modified[dst - 1] != 0x0a)
252      {
253         modified[dst++] = 0x0d;
254         modified[dst++] = 0x0a;
255         added_newline = 1;
256      }
257      else
258      {
259         while (dst >= 4 && modified[dst - 4] == 0x0d && modified[dst - 3] == 0x0a)
260         {
261            dst -= 2;
262            removed_newlines++;
263         }
264      }
241265   }
242266
243267   /* if the result == original, skip it */
r18354r18355
246270      /* explain what we did */
247271      printf("Cleaned up %s:", argv[1]);
248272      if (added_newline) printf(" added newline at end of file");
273      if (removed_newlines) printf(" removed %d newline(s) at end of file", removed_newlines);
249274      if (removed_spaces) printf(" removed %d space(s)", removed_spaces);
250275      if (removed_tabs) printf(" removed %d tab(s)", removed_tabs);
276      if (added_tabs) printf(" added %d tab(s)", added_tabs);
251277      if (hichars) printf(" fixed %d high-ASCII char(s)", hichars);
252278      if (fixed_nix_style) printf(" fixed *nix-style line-ends");
253279      if (fixed_mac_style) printf(" fixed Mac-style line-ends");

Previous 199869 Revisions Next


© 1997-2024 The MAME Team