trunk/src/tools/srcclean.c
| r26763 | r26764 | |
| 31 | 31 | static UINT8 modified[MAX_FILE_SIZE]; |
| 32 | 32 | |
| 33 | 33 | |
| 34 | static int le_convert(char *buffer, int size) |
| 35 | { |
| 36 | char *pos; |
| 37 | char *end = buffer + size; |
| 34 | 38 | |
| 39 | /* brute force */ |
| 40 | *end = 0; |
| 41 | pos = strchr(buffer, 0x0d); |
| 42 | while (pos != NULL) |
| 43 | { |
| 44 | memmove(pos, pos+1,end - pos + 1); |
| 45 | size--; |
| 46 | buffer = pos + 1; |
| 47 | pos = strchr(buffer, 0x0d); |
| 48 | } |
| 49 | return size; |
| 50 | } |
| 51 | |
| 35 | 52 | /*************************************************************************** |
| 36 | 53 | MAIN |
| 37 | 54 | ***************************************************************************/ |
| r26763 | r26764 | |
| 95 | 112 | bytes = fread(original, 1, MAX_FILE_SIZE, file); |
| 96 | 113 | fclose(file); |
| 97 | 114 | |
| 115 | /* check whether we have dos line endings and are in unix mode */ |
| 116 | if (unix_le && (strchr((char *) original, 0x0d) != NULL)) |
| 117 | fixed_dos_style = 1; |
| 118 | |
| 98 | 119 | /* determine if we are a C file */ |
| 99 | 120 | ext = strrchr(argv[1], '.'); |
| 100 | 121 | is_c_file = (ext && (core_stricmp(ext, ".c") == 0 || core_stricmp(ext, ".h") == 0 || core_stricmp(ext, ".cpp") == 0 || core_stricmp(ext, ".inc") == 0 || core_stricmp(ext, ".lst") == 0)); |
| r26763 | r26764 | |
| 217 | 238 | } |
| 218 | 239 | |
| 219 | 240 | /* insert a proper CR/LF */ |
| 220 | | if (!unix_le) |
| 221 | | modified[dst++] = 0x0d; |
| 241 | modified[dst++] = 0x0d; |
| 222 | 242 | modified[dst++] = 0x0a; |
| 223 | 243 | col = 0; |
| 224 | 244 | |
| 225 | 245 | /* skip over any LF in the source file */ |
| 226 | 246 | if (ch == 0x0d && original[src] == 0x0a) |
| 227 | | { |
| 228 | 247 | src++; |
| 229 | | if (unix_le) |
| 230 | | fixed_dos_style = 1; |
| 231 | | } |
| 232 | 248 | else if (ch == 0x0a) |
| 233 | 249 | fixed_nix_style = 1; |
| 234 | 250 | else |
| r26763 | r26764 | |
| 344 | 360 | } |
| 345 | 361 | } |
| 346 | 362 | |
| 363 | /* convert to unix_le if requested */ |
| 364 | |
| 365 | if (unix_le) |
| 366 | dst = le_convert((char *) modified, dst); |
| 367 | |
| 347 | 368 | /* if the result == original, skip it */ |
| 348 | 369 | if (dst != bytes || memcmp(original, modified, bytes)) |
| 349 | 370 | { |
| r26763 | r26764 | |
| 356 | 377 | if (removed_tabs) printf(" removed %d tab(s)", removed_tabs); |
| 357 | 378 | if (added_tabs) printf(" added %d tab(s)", added_tabs); |
| 358 | 379 | if (hichars) printf(" fixed %d high-ASCII char(s)", hichars); |
| 359 | | if (fixed_nix_style) printf(" fixed *nix-style line-ends"); |
| 380 | if (fixed_nix_style && !unix_le) printf(" fixed *nix-style line-ends"); |
| 360 | 381 | if (fixed_mac_style) printf(" fixed Mac-style line-ends"); |
| 361 | 382 | if (fixed_dos_style) printf(" fixed Dos-style line-ends"); |
| 362 | 383 | printf("\n"); |
| 363 | 384 | |
| 364 | 385 | /* write the file */ |
| 365 | 386 | file = fopen(argv[1], "wb"); |
| 366 | | fwrite(modified, 1, dst, file); |
| 387 | fwrite(modified, 1, dst, file); |
| 367 | 388 | fclose(file); |
| 368 | 389 | } |
| 369 | 390 | |