From 7c7b4def93d2e71a301f22090bbdb07fadf65f70 Mon Sep 17 00:00:00 2001 From: Alessandro Bonazzi Date: Wed, 24 Jun 2020 18:30:35 +0200 Subject: [PATCH] Patch level : 12.0 no-patch Files correlati : Commento : rivisti patchdef e fastrip --- src/utilities/fastrip.c | 73 --------------------------------- src/utilities/fastrip.cpp | 77 +++++++++++++++++++++++++++++++++++ src/utilities/patchdef.cpp | 82 ++++++++++++++++++++++---------------- 3 files changed, 125 insertions(+), 107 deletions(-) delete mode 100644 src/utilities/fastrip.c create mode 100644 src/utilities/fastrip.cpp diff --git a/src/utilities/fastrip.c b/src/utilities/fastrip.c deleted file mode 100644 index be1ef951e..000000000 --- a/src/utilities/fastrip.c +++ /dev/null @@ -1,73 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS - -#include -#include - - -void strip(FILE * i, FILE * o) -{ - char lin[513]; - - while (fgets(lin, 512, i) != NULL) - { - char instring = '\0'; - char wasspace = 0; - - char* c = lin; - for(; isspace(*c); c++); // salta spazi iniziali - - for (; *c; c++) - { - if (*c == '/' && *(c+1) == '/') - { - fputc('\n', o); - 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 && *c != '\n') - continue; - wasspace = 1; - } - } - else - wasspace = 0; - - fputc(*c, o); - } - } -} - - -int main(int argc, char ** argv) -{ -// cerr << "Strip 1.2 - White spaces filter by Guy 2012" << endl; - FILE* i = argc > 1 ? fopen(argv[1], "r") : stdin; - FILE* o = argc > 2 ? fopen(argv[2], "w") : stdout; - if (i && o) - strip(i, o); - if (o) - fclose(o); - if (i) - fclose(i); - return 0; -} diff --git a/src/utilities/fastrip.cpp b/src/utilities/fastrip.cpp new file mode 100644 index 000000000..32ff027b4 --- /dev/null +++ b/src/utilities/fastrip.cpp @@ -0,0 +1,77 @@ +#define _CRT_SECURE_NO_WARNINGS + +#include +#include +#include +#include +using namespace std; + +void strip(istream & i, ostream & o) +{ + unsigned char lin[2048]; + unsigned char* c; + + while (i.good()) + { + bool instring = false; + bool wasspace = false; + bool full = false; + + i.getline((char *)lin, 2048); + for (c = lin; isspace(*c); c++); // salta spazi iniziali + for (; *c; c++) + { + full = true; + if (*c == '/' && *(c+1) == '/') + { + o << '\n'; + break; + } + if (*c == '#') + { + o << *c; + while (isspace(*(++c))); + // c--; + } + else + if (*c == '"' || *c == '\'') + instring = !instring; + if (isspace(*c)) + { + if (!instring) + { + if (wasspace && *c != '\n') + continue; + wasspace = true; + } + } + else + wasspace = false; + o << *c; + } + if (full) + o << '\n'; + } +} + + +int main(int argc, char ** argv) +{ +// cerr << "Strip 1.2 - White spaces filter by Guy 2012" << endl; + if (argc == 1) + strip(cin, cout); + else + { + ifstream fin(argv[1]); + + if (argc == 2) + strip(fin, cout); + else + { + ofstream fout(argv[2]); + + strip(fin, fout); + } + } + return 0; +} diff --git a/src/utilities/patchdef.cpp b/src/utilities/patchdef.cpp index 1c2b15dd8..1fe95d431 100644 --- a/src/utilities/patchdef.cpp +++ b/src/utilities/patchdef.cpp @@ -1,51 +1,65 @@ #include #include -#include +#include +#include +using namespace std; - -void patch(istream& i, ostream& o) +void patch(istream & i, ostream & o) { - char lin[513]; + unsigned char lin[2048]; + unsigned char * c; while (i.good()) { - i.getline(lin, 512); - - for(char* c = lin; isspace(*c); c++); // salta spazi iniziali - - char instring = '\0'; - char wasspace = 0; - - for (char* cur = lin; *c; c++) + char instring = '\0'; + bool full = false; + + i.getline((char *) lin, 2048); + for(c = lin; isspace(*c); c++); // salta spazi iniziali + for (c ; *c; c++) { - if (*c == '!') + full = true; + if (*c == '!' && *(c + 1) == '!') { - if (*(c + 1) == '!') - { - c++; - *c = '#'; - } - } - if (*c == '#') - { - *cur++ = *c; - while isspace(*(++c)); - } - if (*c == ';') - *cur++ = '\n'; - else - *cur++ = *c; - } - *cur = '\0'; + o << '#'; + c++; - if (*lin) o << lin << endl; + } + else + if (*c == '#') + { + o << *c; + while (isspace(*(++c))); + c--; + } + else + if (*c == ';') + o << '\n'; + else + o << *c; + } + if (full) + o << '\n'; } } - -int main() +int main(int argc, char ** argv) { - patch(cin, cout); + if (argc == 1) + patch(cin, cout); + else + { + ifstream fin(argv[1]); + + if (argc == 2) + patch(fin, cout); + else + { + ofstream fout(argv[2]); + + patch(fin, fout); + } + } return 0; }  \ No newline at end of file