diff --git a/windows/tools/hresultwrap.cpp b/windows/tools/hresultwrap.cpp index e8675937..35be883d 100644 --- a/windows/tools/hresultwrap.cpp +++ b/windows/tools/hresultwrap.cpp @@ -6,13 +6,19 @@ bool generate(const char *line, size_t n, FILE *fout) { std::vector genout; + std::vector *tokens; + std::vector::const_iterator i; size_t nw; - genout.push_back('/'); - genout.push_back('/'); - genout.push_back(' '); - genout.insert(genout.end(), line, line + n); - genout.push_back('\n'); + tokens = TokenizeWhitespace(line, n); + for (i = tokens->begin(); i < tokens->end(); i++) { + genout.push_back('/'); + genout.push_back('/'); + genout.push_back(' '); + AppendSlice(&genout, *i); + genout.push_back('\n'); + } + FreeTokenized(tokens); genout.push_back('\n'); nw = fwrite(genout.data(), sizeof (char), genout.size(), fout); diff --git a/windows/tools/lib.cpp b/windows/tools/lib.cpp index b548f3c5..a881cf7e 100644 --- a/windows/tools/lib.cpp +++ b/windows/tools/lib.cpp @@ -13,6 +13,7 @@ #else #endif #include +#include #include "lib.hpp" class eofError : public Error { @@ -65,6 +66,11 @@ bool IsEOF(Error *e) return typeid (*e) == typeid (eofError); } +Error *WriteVector(WriteCloser *w, std::vector *v) +{ + return w->Write(v->data(), v->size()); +} + #define nbuf 1024 Scanner::Scanner(ReadCloser *r) @@ -139,3 +145,55 @@ Error *Scanner::Err(void) const return this->err; return NULL; } + +Slice::Slice(const char *p, size_t n) +{ + this->p = p; + this->n = n; +} + +const char *Slice::Data(void) const +{ + return this->p; +} + +size_t Slice::Len(void) const +{ + return this->n; +} + +std::vector *TokenizeWhitespace(const char *buf, size_t n) +{ + std::vector *ret; + const char *p, *q; + const char *end; + + ret = new std::vector; + p = buf; + end = buf + n; + while (p < end) { + if (*p == ' ' || *p == '\t') { + p++; + continue; + } + for (q = p; q < end; q++) + if (*q == ' ' || *q == '\t') + break; + ret->push_back(new Slice(p, q - p)); + p = q; + } + return ret; +} + +void FreeTokenized(std::vector *v) +{ + std::for_each(v->begin(), v->end(), [](Slice *s) { + delete s; + }); + delete v; +} + +void AppendSlice(std::vector *v, Slice *s) +{ + v->insert(v->end(), s->Data(), s->Data() + s->Len()); +} diff --git a/windows/tools/lib.hpp b/windows/tools/lib.hpp index b8b8f3bf..d1f9ca50 100644 --- a/windows/tools/lib.hpp +++ b/windows/tools/lib.hpp @@ -27,6 +27,8 @@ public: }; extern Error *OpenRead(const char *filename, ReadCloser **r); +extern Error *CreateWrite(const char *filename, WriteCloser **w); +extern Error *WriteVector(WriteCloser *w, std::vector *v); class Scanner { ReadCloser *r; @@ -44,3 +46,17 @@ public: size_t Len(void) const; Error *Err(void) const; }; + +class Slice { + const char *p; + size_t n; +public: + Slice(const char *p, size_t n); + + const char *Data(void) const; + size_t Len(void) const; +}; + +extern std::vector *TokenizeWhitespace(const char *buf, size_t n); +extern void FreeTokenized(std::vector *v); +extern void AppendSlice(std::vector *v, Slice *s); diff --git a/windows/tools/lib_posix.cpp b/windows/tools/lib_posix.cpp index a743af9b..528cc35e 100644 --- a/windows/tools/lib_posix.cpp +++ b/windows/tools/lib_posix.cpp @@ -104,3 +104,15 @@ Error *OpenRead(const char *filename, ReadCloser **r) *r = new posixReadCloser(fd); return NULL; } + +Error *CreateWrite(const char *filename, WriteCloser **w) +{ + int fd; + + *w = NULL; + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + return new posixError(errno); + *w = new posixWriteCloser(fd); + return NULL; +}