Rewrote hresultwrap.cpp with the new changes, fixing some build errors too. Now to fix the remaining build errors.

This commit is contained in:
Pietro Gagliardi 2018-05-27 16:32:33 -04:00
parent 4a7e7ed983
commit cf945367a3
3 changed files with 69 additions and 67 deletions

View File

@ -4,97 +4,98 @@
#include <string.h> #include <string.h>
#include "lib.hpp" #include "lib.hpp"
struct item { namespace {
Slice *name;
Slice *callingConvention; class items {
Slice **params; public:
size_t nParams; ByteSlice name;
Slice *returns; ByteSlice callingConvention;
std::vector<ByteSlice> params;
ByteSlice returns;
bool keepReturn; bool keepReturn;
Slice *cond[2]; ByteSlice cond[2];
}; };
bool generate(const char *line, size_t n, FILE *fout) #define noutbuf 2048
bool generate(ByteSlice line, FILE *fout)
{ {
std::vector<char> genout; ByteSlice genout;
std::vector<Slice *> *tokens; std::vector<ByteSlice> tokens;
size_t i, j; size_t i, j;
struct item item; items item;
size_t nw; size_t nw;
tokens = TokenizeWhitespace(line, n); tokens = ByteSliceFields(line);
memset(&item, 0, sizeof (struct item)); new (&item) items;
i = 0; i = 0;
item.returns = tokens->at(i); item.returns = tokens.at(i);
if (item.returns->Data()[0] == '*') { item.keepReturn = false;
item.returns = new Slice(item.returns->Data() + 1, item.returns->Len() - 1); if (item.returns.Data()[0] == '*') {
item.returns = item.returns.Slice(1, item.returns.Len());
item.keepReturn = true; item.keepReturn = true;
} }
i++; i++;
if (tokens->size() % 2 == 1) { if (tokens.size() % 2 == 1) {
item.callingConvention = tokens->at(i); item.callingConvention = tokens.at(i);
i++; i++;
} }
item.name = tokens->at(i); item.name = tokens.at(i);
i++; i++;
item.cond[0] = tokens->at(tokens->size() - 2); item.cond[0] = tokens.at(tokens.size() - 2);
item.cond[1] = tokens->at(tokens->size() - 1); item.cond[1] = tokens.at(tokens.size() - 1);
item.nParams = (tokens->size() - 2) - i; item.params.reserve((tokens.size() - 2) - i);
item.params = new Slice *[item.nParams]; for (j = 0; j < item.params.capacity(); j++) {
for (j = 0; j < item.nParams; j++) { item.params.push_back(tokens.at(i));
item.params[j] = tokens->at(i);
i++; i++;
} }
AppendString(&genout, "HRESULT "); genout = ByteSlice(0, noutbuf);
if (item.callingConvention != NULL) { genout = genout.AppendString("HRESULT ");
AppendSlice(&genout, item.callingConvention); if (item.callingConvention.Len() != 0) {
genout.push_back(' '); genout = genout.Append(item.callingConvention);
genout = genout.AppendString(" ");
} }
AppendSlice(&genout, item.name); genout = genout.Append(item.name);
genout.push_back('('); genout = genout.AppendString("(");
for (i = 0; i < item.nParams; i += 2) { for (i = 0; i < item.params.size(); i += 2) {
AppendSlice(&genout, item.params[i]); genout = genout.Append(item.params[i]);
genout.push_back(' '); genout = genout.AppendString(" ");
AppendSlice(&genout, item.params[i + 1]); genout = genout.Append(item.params[i + 1]);
genout.push_back(','); genout = genout.AppendString(", ");
genout.push_back(' ');
} }
if (item.keepReturn) { if (item.keepReturn) {
AppendSlice(&genout, item.returns); genout = genout.Append(item.returns);
AppendString(&genout, " *ret"); genout = genout.AppendString(" *ret");
} else if (item.nParams != 0) { } else if (item.params.size() != 0)
// remove the trailing comma and space // remove the trailing comma and space
genout.pop_back(); genout = genout.Slice(0, genout.Len() - 2);
genout.pop_back(); else
} else genout = genout.AppendString("void");
AppendString(&genout, "void"); genout = genout.AppendString(")\n");
genout.push_back(')');
genout.push_back('\n');
delete[] item.params; item.~items();
if (item.keepReturn)
delete item.returns;
FreeTokenized(tokens);
genout.push_back('\n'); genout = genout.AppendString("\n");
nw = fwrite(genout.data(), sizeof (char), genout.size(), fout); nw = fwrite(genout.Data(), sizeof (char), genout.Len(), fout);
return nw == genout.size(); return nw == genout.Len();
} }
bool process(const char *line, size_t n, FILE *fout) bool process(ByteSlice line, FILE *fout)
{ {
size_t nw; size_t nw;
if (n > 0 && line[0] == '@') if (line.Len() > 0 && line.Data()[0] == '@')
return generate(line + 1, n - 1, fout); return generate(line.Slice(1, line.Len()), fout);
nw = fwrite(line, sizeof (char), n, fout); nw = fwrite(line.Data(), sizeof (char), line.Len(), fout);
if (nw != n) if (nw != line.Len())
return false; return false;
return fwrite("\n", sizeof (char), 1, fout) == 1; return fwrite("\n", sizeof (char), 1, fout) == 1;
} }
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
ReadCloser *fin = NULL; ReadCloser *fin = NULL;
@ -120,17 +121,11 @@ int main(int argc, char *argv[])
} }
s = new Scanner(fin); s = new Scanner(fin);
while (s->Scan()) { while (s->Scan())
const char *line; if (!process(s->Bytes(), fout)) {
size_t n;
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 (s->Err() != 0) { if (s->Err() != 0) {
fprintf(stderr, "error reading from %s: %s\n", argv[1], s->Err()->String()); fprintf(stderr, "error reading from %s: %s\n", argv[1], s->Err()->String());
goto done; goto done;

View File

@ -159,6 +159,12 @@ ByteSlice::ByteSlice(size_t len, size_t cap)
this->cap = cap; this->cap = cap;
} }
ByteSlice::ByteSlice(int len, size_t cap) :
ByteSlice::ByteSlice((size_t) len, cap)
{
// do nothing else
}
ByteSlice::~ByteSlice(void) ByteSlice::~ByteSlice(void)
{ {
sliceRelease(this->data); sliceRelease(this->data);
@ -305,7 +311,7 @@ bool Scanner::Scan(void)
// 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
} }
// need to refill the buffer // need to refill the buffer
this->err = this->r->Read(this->buf, nbuf * sizeof (char), &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;

View File

@ -24,6 +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(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)