And integrated Scanner into the mix. C's EOF behavior is gonna make things harder...

This commit is contained in:
Pietro Gagliardi 2018-05-22 21:19:54 -04:00
parent b769b37189
commit c38564a8f7
1 changed files with 48 additions and 34 deletions

View File

@ -3,22 +3,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
bool generate(std::vector<char> *genline, FILE *fout)
{
std::vector<char> genout;
size_t nw;
genout.push_back('/');
genout.push_back('/');
genout.push_back(' ');
genout.insert(genout.end(), genline->begin(), genline->end());
genout.push_back('\n');
genout.push_back('\n');
nw = fwrite(genout.data(), sizeof (char), genout.size(), fout);
return nw == genout.size();
}
class Scanner { class Scanner {
FILE *fin; FILE *fin;
char *buf; char *buf;
@ -32,8 +16,8 @@ public:
~Scanner(void); ~Scanner(void);
bool Scan(void); bool Scan(void);
std::vector<char>::const_iterator BytesBegin(void) const; const char *Bytes(void) const;
std::vector<char>::const_iterator BytesEnd(void) const; size_t Len(void) const;
bool Error(void) const; bool Error(void) const;
}; };
@ -97,14 +81,14 @@ bool Scanner::Scan(void)
} }
} }
std::vector<char>::const_iterator Scanner::BytesBegin(void) const const char *Scanner::Bytes(void) const
{ {
return this->line->cbegin(); return this->line->data();
} }
std::vector<char>::const_iterator Scanner::BytesEnd(void) const size_t Scanner::Len(void) const
{ {
return this->line->cend(); return this->line->size();
} }
bool Scanner::Error(void) const bool Scanner::Error(void) const
@ -112,12 +96,38 @@ bool Scanner::Error(void) const
return this->error; return this->error;
} }
bool generate(const char *line, size_t n, FILE *fout)
{
std::vector<char> genout;
size_t nw;
genout.push_back('/');
genout.push_back('/');
genout.push_back(' ');
genout.insert(genout.end(), line, line + n);
genout.push_back('\n');
genout.push_back('\n');
nw = fwrite(genout.data(), sizeof (char), genout.size(), fout);
return nw == genout.size();
}
bool process(const char *line, size_t n, FILE *fout)
{
size_t nw;
if (n > 0 && line[0] == '@')
return generate(line + 1, n - 1, fout);
nw = fwrite(line, sizeof (char), n, fout);
if (nw != n)
return false;
return fwrite("\n", sizeof (char), 1, fout) == 1;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
FILE *fin = NULL, *fout = NULL; FILE *fin = NULL, *fout = NULL;
char buf[nbuf]; Scanner *s = NULL;
size_t n;
struct process p;
int ret = 1; int ret = 1;
if (argc != 3) { if (argc != 3) {
@ -136,26 +146,30 @@ int main(int argc, char *argv[])
goto done; goto done;
} }
processInit(&p); s = new Scanner(fin);
for (;;) { while (s->Scan()) {
n = fread(buf, sizeof (char), nbuf, fin); const char *line;
if (n == 0) size_t n;
break;
if (!process(&p, buf, n, fout)) { line = s->Bytes();
n = s->Len();
if (!process(line, n, fout)) {
fprintf(stderr, "error writing to %s\n", argv[2]); fprintf(stderr, "error writing to %s\n", argv[2]);
goto done; goto done;
} }
} }
if (!feof(fin)) { if (s->Error()) {
fprintf(stderr, "error reading from %s\n", argv[1]); fprintf(stderr, "error reading from %s\n", argv[1]);
goto done; goto done;
} }
ret = 0; ret = 0;
done: done:
if (fin != NULL) if (s != NULL)
fclose(fin); delete s;
if (fout != NULL) if (fout != NULL)
fclose(fout); fclose(fout);
if (fin != NULL)
fclose(fin);
return ret; return ret;
} }