/* Skrevet av Dag Langmyhr (dag@ifi.uio.no) til NM i programmering 2000. */ #include #define FALSE 0 #define TRUE 1 FILE *in_f, *out_f; /* The line being constructed, and the number of characters entered. */ char out_l[256]; int out_x = 0; /* Function `put' prints a character `c' to the output file (after buffering it in `out_l'). Sequences of spaces are compressed unless `keep' is true. */ void put(char c, int keep) { if (c == '\n') { if (out_x == 0) { /* Ignore empty lines. */ } else { if (out_l[out_x-1] == ' ') --out_x; out_l[out_x] = '\0'; fprintf(out_f, "%s\n", out_l); out_x = 0; } } else if (c == ' ') { if (keep || out_x>0 && out_l[out_x-1]!=' ') out_l[out_x++] = c; } else { out_l[out_x++] = c; } } /* The character currently being examined, and the next. */ int curc, nextc; /* This macro is used to read characters: */ #define GET curc = nextc; nextc = fgetc(in_f); /* The main program */ int main(void) { int comment = FALSE, string = FALSE; in_f = fopen("infil.C","r"); out_f = fopen("utfil.C","w"); GET; GET; while (curc != EOF) { if (comment) { if (comment == '{' && curc == '}') { comment = 0; } else if (comment == '*' && curc == '*' && nextc == ')') { comment = 0; GET; } } else if (string) { if (curc == '\'') { if (nextc == '\'') { GET; put('\'',TRUE); put('\'',TRUE); } else { put('\'',TRUE); string = 0; } } else put(curc,TRUE); } else if (curc == '{') { put(' ',FALSE); comment = '{'; } else if (curc == '(' && nextc == '*') { put(' ',FALSE); comment = '*'; GET; } else if (curc == '\'') { put('\'',TRUE); string = '\''; } else { put(curc,FALSE); } GET; } fclose(in_f); fclose(out_f); return 0; }