2018-05-21 21:42:19 -05:00
|
|
|
// 21 may 2018
|
2018-05-24 15:11:58 -05:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
|
|
|
|
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT 1
|
|
|
|
#include <io.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#define openfunc _open
|
|
|
|
#define openflags (_O_RDONLY | _O_BINARY)
|
|
|
|
#define openmode (_S_IREAD)
|
|
|
|
#define readfunc _read
|
|
|
|
#define readtype int
|
|
|
|
#define closefunc _close
|
|
|
|
#else
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#define openfunc open
|
|
|
|
#define openflags (O_RDONLY)
|
|
|
|
#define openmode 0644
|
|
|
|
#define readfunc read
|
|
|
|
#define readtype ssize_t
|
|
|
|
#define closefunc close
|
|
|
|
#endif
|
2018-05-21 21:42:19 -05:00
|
|
|
#include <vector>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2018-05-24 15:11:58 -05:00
|
|
|
#include <errno.h>
|
2018-05-21 21:42:19 -05:00
|
|
|
|
2018-05-22 20:03:54 -05:00
|
|
|
class Scanner {
|
2018-05-24 15:11:58 -05:00
|
|
|
int fd;
|
2018-05-22 20:03:54 -05:00
|
|
|
char *buf;
|
|
|
|
const char *p;
|
|
|
|
size_t n;
|
|
|
|
std::vector<char> *line;
|
|
|
|
bool eof;
|
2018-05-24 15:11:58 -05:00
|
|
|
int error;
|
2018-05-22 20:03:54 -05:00
|
|
|
public:
|
2018-05-24 15:11:58 -05:00
|
|
|
Scanner(int fd);
|
2018-05-22 20:03:54 -05:00
|
|
|
~Scanner(void);
|
|
|
|
|
|
|
|
bool Scan(void);
|
2018-05-22 20:19:54 -05:00
|
|
|
const char *Bytes(void) const;
|
|
|
|
size_t Len(void) const;
|
2018-05-24 15:11:58 -05:00
|
|
|
int Error(void) const;
|
2018-05-21 21:42:19 -05:00
|
|
|
};
|
|
|
|
|
2018-05-22 20:03:54 -05:00
|
|
|
#define nbuf 1024
|
|
|
|
|
2018-05-24 15:11:58 -05:00
|
|
|
Scanner::Scanner(int fd)
|
2018-05-21 21:42:19 -05:00
|
|
|
{
|
2018-05-24 15:11:58 -05:00
|
|
|
this->fd = fd;
|
2018-05-22 20:03:54 -05:00
|
|
|
this->buf = new char[nbuf];
|
|
|
|
this->p = this->buf;
|
|
|
|
this->n = 0;
|
|
|
|
this->line = new std::vector<char>;
|
|
|
|
this->eof = false;
|
2018-05-24 15:11:58 -05:00
|
|
|
this->error = 0;
|
2018-05-21 21:42:19 -05:00
|
|
|
}
|
|
|
|
|
2018-05-22 20:03:54 -05:00
|
|
|
Scanner::~Scanner(void)
|
2018-05-21 21:42:19 -05:00
|
|
|
{
|
2018-05-22 20:03:54 -05:00
|
|
|
delete this->line;
|
|
|
|
delete[] this->buf;
|
2018-05-21 21:42:19 -05:00
|
|
|
}
|
|
|
|
|
2018-05-22 20:03:54 -05:00
|
|
|
bool Scanner::Scan(void)
|
2018-05-21 21:42:19 -05:00
|
|
|
{
|
2018-05-24 15:11:58 -05:00
|
|
|
readtype n;
|
|
|
|
|
|
|
|
if (this->eof || this->error != 0)
|
2018-05-22 20:03:54 -05:00
|
|
|
return false;
|
|
|
|
this->line->clear();
|
|
|
|
for (;;) {
|
|
|
|
if (this->n > 0) {
|
|
|
|
size_t j;
|
|
|
|
bool haveline;
|
|
|
|
|
|
|
|
haveline = false;
|
|
|
|
for (j = 0; j < this->n; j++)
|
|
|
|
if (this->p[j] == '\n') {
|
|
|
|
haveline = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this->line->insert(this->line->end(), this->p, this->p + j);
|
|
|
|
this->p += j;
|
|
|
|
this->n -= j;
|
|
|
|
if (haveline) {
|
|
|
|
// swallow the \n for the next time through
|
|
|
|
this->p++;
|
|
|
|
this->n--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// otherwise, the buffer was exhausted in the middle of a line, so fall through
|
2018-05-21 21:42:19 -05:00
|
|
|
}
|
2018-05-22 20:03:54 -05:00
|
|
|
// need to refill the buffer
|
2018-05-24 15:11:58 -05:00
|
|
|
n = readfunc(this->fd, this->buf, nbuf * sizeof (char));
|
|
|
|
if (n < 0) {
|
|
|
|
this->error = errno;
|
2018-05-22 20:23:38 -05:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-24 15:11:58 -05:00
|
|
|
if (n == 0) {
|
|
|
|
this->eof = true;
|
|
|
|
return false;
|
2018-05-22 20:03:54 -05:00
|
|
|
}
|
|
|
|
this->p = this->buf;
|
2018-05-24 15:11:58 -05:00
|
|
|
this->n = n;
|
2018-05-21 21:42:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 20:19:54 -05:00
|
|
|
const char *Scanner::Bytes(void) const
|
2018-05-21 21:42:19 -05:00
|
|
|
{
|
2018-05-22 20:19:54 -05:00
|
|
|
return this->line->data();
|
2018-05-21 21:42:19 -05:00
|
|
|
}
|
|
|
|
|
2018-05-22 20:19:54 -05:00
|
|
|
size_t Scanner::Len(void) const
|
2018-05-21 21:42:19 -05:00
|
|
|
{
|
2018-05-22 20:19:54 -05:00
|
|
|
return this->line->size();
|
2018-05-21 21:42:19 -05:00
|
|
|
}
|
|
|
|
|
2018-05-24 15:11:58 -05:00
|
|
|
int Scanner::Error(void) const
|
2018-05-21 21:42:19 -05:00
|
|
|
{
|
2018-05-22 20:03:54 -05:00
|
|
|
return this->error;
|
2018-05-21 21:42:19 -05:00
|
|
|
}
|
|
|
|
|
2018-05-22 20:19:54 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:42:19 -05:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-05-24 15:11:58 -05:00
|
|
|
int fin = -1;
|
|
|
|
FILE *fout = NULL;
|
2018-05-22 20:19:54 -05:00
|
|
|
Scanner *s = NULL;
|
2018-05-21 21:42:19 -05:00
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-05-24 15:11:58 -05:00
|
|
|
fin = openfunc(argv[1], openflags, openmode);
|
|
|
|
if (fin < 0) {
|
|
|
|
fprintf(stderr, "error opening %s: %s\n", argv[1], strerror(errno));
|
2018-05-21 21:42:19 -05:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
fout = fopen(argv[2], "wb");
|
|
|
|
if (fout == NULL) {
|
|
|
|
fprintf(stderr, "error creating %s\n", argv[2]);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2018-05-22 20:19:54 -05:00
|
|
|
s = new Scanner(fin);
|
|
|
|
while (s->Scan()) {
|
|
|
|
const char *line;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
line = s->Bytes();
|
|
|
|
n = s->Len();
|
|
|
|
if (!process(line, n, fout)) {
|
2018-05-21 21:42:19 -05:00
|
|
|
fprintf(stderr, "error writing to %s\n", argv[2]);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 15:11:58 -05:00
|
|
|
if (s->Error() != 0) {
|
|
|
|
fprintf(stderr, "error reading from %s: %s\n", argv[1], strerror(s->Error()));
|
2018-05-21 21:42:19 -05:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
done:
|
2018-05-22 20:19:54 -05:00
|
|
|
if (s != NULL)
|
|
|
|
delete s;
|
2018-05-21 21:42:19 -05:00
|
|
|
if (fout != NULL)
|
|
|
|
fclose(fout);
|
2018-05-24 15:11:58 -05:00
|
|
|
if (fin >= 0)
|
|
|
|
closefunc(fin);
|
2018-05-21 21:42:19 -05:00
|
|
|
return ret;
|
|
|
|
}
|