campo-sirio/projects/fastrip.cpp

58 lines
933 B
C++
Raw Normal View History

#include <ctype.h>
#include <string.h>
#include <stdio.h>
void strip(FILE * i, FILE * o)
{
char lin[513];
while (fgets(lin, 512, i) != NULL)
{
char * c = lin;
for(; isspace(*c); c++); // salta spazi iniziali
char instring = '\0';
char wasspace = 0;
for (; *c; c++)
{
if (*c == '/' && *(c+1) == '/') break;
if (*c == '#')
{
fputc(*c, o);
while (isspace(*(++c)));
}
if (*c == '"' || *c == '\'')
{
if (instring == *c) instring = '\0';
else
if (instring == '\0') instring = *c;
}
if (isspace(*c))
{
if (!instring)
{
if (wasspace) continue;
wasspace = 1;
}
} else wasspace = 0;
fputc(*c, o);
}
}
}
int main(int argc, char ** argv)
{
// cerr << "Strip 0.93 - White spaces filter by Guy '93" << endl;
FILE * i = argc > 1 ? fopen(argv[1], "r") : stdin;
FILE * o = argc > 2 ? fopen(argv[2], "w") : stdout;
strip(i, o);
return 0;
}