trunk/src/tools/srcclean.c
| r18354 | r18355 | |
| 48 | 48 | CONSTANTS & DEFINES |
| 49 | 49 | ***************************************************************************/ |
| 50 | 50 | |
| 51 | | #define MAX_FILE_SIZE (32 * 1024 * 1024) |
| 51 | #define MAX_FILE_SIZE (32 * 1024 * 1024) |
| 52 | 52 | |
| 53 | 53 | |
| 54 | 54 | |
| r18354 | r18355 | |
| 67 | 67 | |
| 68 | 68 | int main(int argc, char *argv[]) |
| 69 | 69 | { |
| 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; |
| 71 | 71 | int src = 0, dst = 0, in_c_comment = FALSE, in_cpp_comment = FALSE, in_c_string = FALSE; |
| 72 | 72 | int hichars = 0; |
| 73 | 73 | int is_c_file, is_xml_file; |
| r18354 | r18355 | |
| 76 | 76 | int bytes; |
| 77 | 77 | int col = 0; |
| 78 | 78 | int escape = 0; |
| 79 | const int tab_size = 4; |
| 79 | 80 | |
| 80 | 81 | /* print usage info */ |
| 81 | 82 | if (argc != 2) |
| r18354 | r18355 | |
| 96 | 97 | |
| 97 | 98 | /* determine if we are a C file */ |
| 98 | 99 | 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)); |
| 100 | 101 | is_xml_file = (ext && core_stricmp(ext, ".xml") == 0); |
| 101 | 102 | |
| 102 | 103 | /* rip through it */ |
| r18354 | r18355 | |
| 194 | 195 | /* if we hit a tab... */ |
| 195 | 196 | else if (ch == 0x09) |
| 196 | 197 | { |
| 197 | | int spaces = 4 - col % 4; |
| 198 | int spaces = tab_size - (col % tab_size); |
| 198 | 199 | |
| 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 | | } |
| 207 | 200 | col += spaces; |
| 208 | 201 | |
| 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)) |
| 211 | 204 | { |
| 212 | 205 | while (spaces--) modified[dst++] = ' '; |
| 213 | 206 | removed_tabs++; |
| r18354 | r18355 | |
| 218 | 211 | } |
| 219 | 212 | } |
| 220 | 213 | |
| 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 | |
| 221 | 234 | /* otherwise, copy the source character */ |
| 222 | 235 | else |
| 223 | 236 | { |
| r18354 | r18355 | |
| 233 | 246 | return 1; |
| 234 | 247 | } |
| 235 | 248 | |
| 236 | | if (is_c_file && modified[dst - 1] != 0x0a) |
| 249 | if (is_c_file) |
| 237 | 250 | { |
| 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 | } |
| 241 | 265 | } |
| 242 | 266 | |
| 243 | 267 | /* if the result == original, skip it */ |
| r18354 | r18355 | |
| 246 | 270 | /* explain what we did */ |
| 247 | 271 | printf("Cleaned up %s:", argv[1]); |
| 248 | 272 | 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); |
| 249 | 274 | if (removed_spaces) printf(" removed %d space(s)", removed_spaces); |
| 250 | 275 | if (removed_tabs) printf(" removed %d tab(s)", removed_tabs); |
| 276 | if (added_tabs) printf(" added %d tab(s)", added_tabs); |
| 251 | 277 | if (hichars) printf(" fixed %d high-ASCII char(s)", hichars); |
| 252 | 278 | if (fixed_nix_style) printf(" fixed *nix-style line-ends"); |
| 253 | 279 | if (fixed_mac_style) printf(" fixed Mac-style line-ends"); |