6
typedef unsigned int uint;
8
typedef unsigned char byte;
35
int tabsize = TABSIZE;
40
Memory allocation functions vary from one environment to
41
the next, and experience shows that wrapping the local
42
mechanisms up provides for greater flexibility and allows
43
out of memory conditions to be detected in one place.
45
void *MemAlloc(unsigned int size)
53
fprintf(stderr, "***** Out of memory! *****\n");
60
void *MemRealloc(void *old, unsigned int size)
64
p = realloc(old, size);
68
fprintf(stderr, "***** Out of memory! *****\n");
81
Stream *NewStream(FILE *fp)
85
in = (Stream *)MemAlloc(sizeof(Stream));
87
memset(in, 0, sizeof(Stream));
92
void FreeStream(Stream *in)
100
void AddByte(Stream *in, uint c)
102
if (in->size + 1 >= in->length)
104
while (in->size + 1 >= in->length)
109
in->length = in->length * 2;
112
in->buf = (char *)MemRealloc(in->buf, in->length*sizeof(char));
115
in->buf[in->size++] = (char)c;
116
in->buf[in->size] = '\0'; /* debug */
122
Read a character from a stream, keeping track
123
of lines, columns etc. This is used for parsing
124
markup and plain text etc. A single level
125
pushback is allowed with UngetChar(c, in).
126
Returns EndOfStream if there's nothing more to read.
128
int ReadChar(Stream *in)
136
if (in->pushed_char == '\n')
139
return in->pushed_char;
142
in->lastcol = in->curcol;
144
/* expanding tab ? */
152
/* Else go on with normal buffer: */
161
/* coerce \r\n and isolated \r as equivalent to \n : */
174
if (in->maxcol < in->curcol)
175
in->maxcol = in->curcol;
185
in->curcol += tabsize - ((in->curcol - 1) % tabsize);
186
else /* expand to spaces */
188
in->tabs = tabsize - ((in->curcol - 1) % tabsize) - 1;
199
/* strip control characters including '\r' */
211
Stream *ReadFile(FILE *fin)
214
Stream *in = NewStream(fin);
216
while ((c = ReadChar(in)) >= 0)
217
AddByte(in, (uint)c);
222
void WriteFile(Stream *in, FILE *fout)
241
else if (endline == UNIX)
243
else /* Macs which use CR */
253
void HelpText(FILE *errout, char *prog)
255
fprintf(errout, "%s: file1 file2 ...\n", prog);
256
fprintf(errout, "Utility to expand tabs and ensure consistent line ends\n");
257
fprintf(errout, "options for tab2space vers: 3rd January 2002\n");
258
fprintf(errout, " -t8 set tabs to 8 (default is 4)\n");
259
fprintf(errout, " -crlf set line ends to CRLF (PC-DOS/Windows - default)\n");
260
fprintf(errout, " -unix or -lf set line ends to LF (Unix)\n");
261
fprintf(errout, " -cr set line ends to CR (classic Mac OS)\n");
262
fprintf(errout, " -tabs preserve tabs, e.g. for Makefile\n");
263
fprintf(errout, " -help or -h display this help message\n");
264
fprintf(errout, "\nNote this utility doesn't map spaces to tabs!\n");
267
int main(int argc, char **argv)
277
if (argc > 1 && argv[1][0] == '-')
279
if (strcmp(argv[1], "-help") == 0 || argv[1][1] == 'h')
281
HelpText(stdout, prog);
285
if (strncmp(argv[1], "-t", 2) == 0)
287
sscanf(argv[1]+2, "%d", &tabsize);
293
if (strcmp(argv[1], "-unix") == 0 ||
294
strcmp(argv[1], "-lf") == 0)
302
if (strcmp(argv[1], "-crlf") == 0)
310
if (strcmp(argv[1], "-cr") == 0)
318
if (strcmp(argv[1], "-tabs") == 0)
334
fin = fopen(file, "rb");
352
fout = fopen(file, "wb");
366
fprintf(stderr, "%s - can't open \"%s\" for writing\n", prog, file);
372
fprintf(stderr, "%s - can't open \"%s\" for reading\n", prog, file);