drh | 36ce6d2 | 2012-08-20 15:46:08 +0000 | [diff] [blame^] | 1 | /* |
| 2 | ** This program checks for formatting problems in source code: |
| 3 | ** |
| 4 | ** * Any use of tab characters |
| 5 | ** * White space at the end of a line |
| 6 | ** * Blank lines at the end of a file |
| 7 | ** |
| 8 | ** Any violations are reported. |
| 9 | */ |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | static void checkSpacing(const char *zFile, int crok){ |
| 15 | FILE *in = fopen(zFile, "rb"); |
| 16 | int i; |
| 17 | int seenSpace; |
| 18 | int seenTab; |
| 19 | int ln = 0; |
| 20 | int lastNonspace = 0; |
| 21 | char zLine[2000]; |
| 22 | if( in==0 ){ |
| 23 | printf("cannot open %s\n", zFile); |
| 24 | return; |
| 25 | } |
| 26 | while( fgets(zLine, sizeof(zLine), in) ){ |
| 27 | seenSpace = 0; |
| 28 | seenTab = 0; |
| 29 | ln++; |
| 30 | for(i=0; zLine[i]; i++){ |
| 31 | if( zLine[i]=='\t' && seenTab==0 ){ |
| 32 | printf("%s:%d: tab (\\t) character\n", zFile, ln); |
| 33 | seenTab = 1; |
| 34 | }else if( zLine[i]=='\r' ){ |
| 35 | if( !crok ){ |
| 36 | printf("%s:%d: carriage-return (\\r) character\n", zFile, ln); |
| 37 | } |
| 38 | }else if( zLine[i]==' ' ){ |
| 39 | seenSpace = 1; |
| 40 | }else if( zLine[i]!='\n' ){ |
| 41 | lastNonspace = ln; |
| 42 | seenSpace = 0; |
| 43 | } |
| 44 | } |
| 45 | if( seenSpace ){ |
| 46 | printf("%s:%d: whitespace at end-of-line\n", zFile, ln); |
| 47 | } |
| 48 | } |
| 49 | fclose(in); |
| 50 | if( lastNonspace<ln ){ |
| 51 | printf("%s:%d: blank lines at end of file (%d)\n", |
| 52 | zFile, ln, ln - lastNonspace); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | int main(int argc, char **argv){ |
| 57 | int i; |
| 58 | int crok = 0; |
| 59 | for(i=1; i<argc; i++){ |
| 60 | if( strcmp(argv[i], "--crok")==0 ){ |
| 61 | crok = 1; |
| 62 | }else{ |
| 63 | checkSpacing(argv[i], crok); |
| 64 | } |
| 65 | } |
| 66 | return 0; |
| 67 | } |