And fixed the remaining errors, including runtime erorrs. It works!

This commit is contained in:
Pietro Gagliardi 2018-05-27 19:48:39 -04:00
parent cf945367a3
commit 620b03f442
4 changed files with 13 additions and 22 deletions

View File

@ -28,7 +28,6 @@ bool generate(ByteSlice line, FILE *fout)
tokens = ByteSliceFields(line);
new (&item) items;
i = 0;
item.returns = tokens.at(i);
item.keepReturn = false;
@ -75,8 +74,6 @@ bool generate(ByteSlice line, FILE *fout)
genout = genout.AppendString("void");
genout = genout.AppendString(")\n");
item.~items();
genout = genout.AppendString("\n");
nw = fwrite(genout.Data(), sizeof (char), genout.Len(), fout);
return nw == genout.Len();

View File

@ -267,9 +267,8 @@ void ByteSlice::CopyFrom(const ByteSlice &b)
Scanner::Scanner(ReadCloser *r)
{
this->r = r;
this->buf = new char[nbuf];
this->p = this->buf;
this->n = 0;
this->buf = ByteSlice(nbuf, nbuf);
this->p = ByteSlice();
this->line = ByteSlice(0, nbuf);
this->err = NULL;
}
@ -278,7 +277,6 @@ Scanner::~Scanner(void)
{
if (this->err != NULL)
delete this->err;
delete[] this->buf;
}
bool Scanner::Scan(void)
@ -289,23 +287,21 @@ bool Scanner::Scan(void)
return false;
this->line = this->line.Slice(0, 0);
for (;;) {
if (this->n > 0) {
if (this->p.Len() > 0) {
size_t j;
bool haveline;
haveline = false;
for (j = 0; j < this->n; j++)
if (this->p[j] == '\n') {
for (j = 0; j < this->p.Len(); j++)
if (this->p.Data()[j] == '\n') {
haveline = true;
break;
}
this->line = this->line.Append(this->p, j);
this->p += j;
this->n -= j;
this->line = this->line.Append(this->p.Slice(0, j));
this->p = this->p.Slice(j, this->p.Len());
if (haveline) {
// swallow the \n for the next time through
this->p++;
this->n--;
this->p = this->p.Slice(1, this->p.Len());
return true;
}
// otherwise, the buffer was exhausted in the middle of a line, so fall through
@ -314,8 +310,7 @@ bool Scanner::Scan(void)
this->err = this->r->Read(this->buf, &n);
if (this->err != NULL)
return false;
this->p = this->buf;
this->n = n;
this->p = this->buf.Slice(0, n);
}
}

View File

@ -24,7 +24,7 @@ public:
ByteSlice(ByteSlice &&b); // move constructor; sets b to ByteSlice()
ByteSlice(const char *b, size_t n);
ByteSlice(size_t len, size_t cap);
ByteSlice(int len, size_t cap); // deal with stupid rule about 0
ByteSlice(int len, size_t cap); // deal with stupid rule about 0 (see https://stackoverflow.com/a/4610586/3408572)
~ByteSlice(void);
// note: copy assignment does not use copy-and-swap because I get neither copy-and-swap nor ADL public friend swap functions (https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom, https://stackoverflow.com/questions/5695548/public-friend-swap-member-function)
@ -64,9 +64,8 @@ extern Error *CreateWrite(const char *filename, WriteCloser **w);
class Scanner {
ReadCloser *r;
char *buf;
const char *p;
size_t n;
ByteSlice buf;
ByteSlice p;
ByteSlice line;
Error *err;
public:

View File

@ -81,7 +81,7 @@ posixWriteCloser::~posixWriteCloser(void)
close(this->fd);
}
Error *posixWriteCloser::Write(const ByteSlice &b)
Error *posixWriteCloser::Write(const ByteSlice b)
{
ssize_t ret;