1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
# COMMENT.D
# dient zum Umwandeln von Kommentaren:
# Inputzeile:
# text # comment
# Outputzeile:
# text / * comment * /
# Bruno Haible 9.7.1990, 6.8.1990, 15.8.1990, 22.9.1990, 17.1.1991, 31.8.1991
# Aufrufmglichkeiten, um program.d in program.c umzuwandeln:
# comment program
# comment und dann von Hand 'program' eingeben
# comment program.d program.c
# comment program.d > program.c
# Methode:
# Das Inputfile wird Zeile fr Zeile umgewandelt. Ein '#', gefolgt von ' ',
# leitet einen Kommentar ein, der bis Zeilenende geht.
# '#'' ' wird durch '/''*'' ' ersetzt, und vors Zeilenende kommt ' ''*''/'.
# Ein '\' unmittelbar am Ende einer Zeile wird dabei zum Zeilenende gerechnet.
# Als erste Zeile wird '#line 1 "sourcefile"' eingefgt.
# Syntax eines Inputfile: #
# -------> Char ----> '#' -> ' ' ----> Char ----> '\\' ---> '\n' -----> #
# / / \ / \ \ / \ #
# | \ / \ / --->--- | #
# \ --<------ --<------ / #
# -------------------<----------------------------------------- #
#include <stdio.h>
#define fopen_read_ascii "r"
#define fopen_write_ascii "w"
#define fputc putc
#define fgetc getc
#ifdef __cplusplus
extern "C" void exit(int);
#endif
#if defined(__STDC__) || defined(__cplusplus)
int main (int argc, char** argv)
#else
int main(argc,argv)
int argc;
char** argv;
#endif
{ char infilenamebuffer[1000];
char outfilenamebuffer[1000];
FILE * infile;
FILE * outfile;
if (argc==3)
{ # Aufruf der Form 'comment source destination'
{ char* p1 = argv[1]; char* p2 = infilenamebuffer; while (*p2++ = *p1++); }
{ char* p1 = argv[2]; char* p2 = outfilenamebuffer; while (*p2++ = *p1++); }
}
else
{ char filenamebuffer[1000];
char* filename;
if (argc==2)
{ filename = argv[1]; }
else
{ printf("Filename: "); filename = gets(filenamebuffer);
if (filename==NULL) { exit(1); }
}
# infilename basteln: filename+".d"
{ char* p = infilenamebuffer;
{ char* p2 = filename; char c; while (c = *p2++) { *p++ = c; } }
# Endet filename bereits mit ".d" ?
if ((&p[-2] >= infilenamebuffer) && (p[-2]=='.') && (p[-1]=='d'))
{ *p++ = '\0'; goto to_stdout; } # ja -> Output nach stdout
*p++ = '.'; *p++ = 'd';
*p++ = '\0';
}
# outfilename basteln: filename+".c"
{ char* p = outfilenamebuffer;
{ char* p2 = filename; char c; while (c = *p2++) { *p++ = c; } }
*p++ = '.'; *p++ = 'c';
*p++ = '\0';
}
}
# infile ffnen:
if ((infile = fopen(infilenamebuffer,fopen_read_ascii))==NULL) { exit(1); }
# outfile ffnen:
if ((outfile = fopen(outfilenamebuffer,fopen_write_ascii))==NULL)
{ fclose(infile); exit(1); }
if (0)
{ to_stdout:
# infile ffnen:
if ((infile = fopen(infilenamebuffer,fopen_read_ascii))==NULL) { exit(1); }
outfile = stdout; # outfile = Standard-Output
}
# Header in outfile schreiben:
{ fputs("#line 1 \"",outfile);
fputs(infilenamebuffer,outfile);
fputs("\"\n",outfile);
}
# infile in outfile kopieren:
#define fput_startcomment(outfile) \
{ fputc('/',outfile); fputc('*',outfile); fputc(' ',outfile); }
#define fput_endcomment(outfile) \
{ fputc(' ',outfile); fputc('*',outfile); fputc('/',outfile); }
{ register int c;
L1: # innerhalb einer Zeile, vor Kommentar
c = fgetc(infile) ;
L1a: if (c==EOF){ goto L3; }
if (!(c=='#')) { fputc(c,outfile); goto L1; }
# innerhalb einer Zeile, nach '#', vor ' '
c = fgetc(infile) ;
if (!(c==' ')) { fputc('#',outfile); goto L1a; }
fput_startcomment(outfile);
L2: # innerhalb eines Kommentars
c = fgetc(infile) ;
L2a: if (c==EOF) { fput_endcomment(outfile); goto L3; }
if (c=='\n') { fput_endcomment(outfile); fputc(c,outfile); goto L1; }
if (!(c=='\\')) { fputc(c,outfile); goto L2; }
# innerhalb eines Kommentars, nach '\\'
c = fgetc(infile) ;
if (!(c=='\n')) { fputc('\\',outfile); goto L2a; }
fput_endcomment(outfile); fputc('\\',outfile); fputc(c,outfile);
goto L1;
L3: ; # am File-Ende
}
# Files schlieen:
if (ferror(infile) || ferror(outfile))
{ fclose(infile); fclose(outfile); exit(1); }
fclose(infile);
fclose(outfile);
exit(0); # alles OK
}
|