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

View File

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

View File

@ -24,7 +24,7 @@ public:
ByteSlice(ByteSlice &&b); // move constructor; sets b to ByteSlice() ByteSlice(ByteSlice &&b); // move constructor; sets b to ByteSlice()
ByteSlice(const char *b, size_t n); ByteSlice(const char *b, size_t n);
ByteSlice(size_t len, size_t cap); 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); ~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) // 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 { class Scanner {
ReadCloser *r; ReadCloser *r;
char *buf; ByteSlice buf;
const char *p; ByteSlice p;
size_t n;
ByteSlice line; ByteSlice line;
Error *err; Error *err;
public: public:

View File

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