From 039a9db2e5d3ddebfe39b5699a3dd5659f33e185 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 28 May 2018 22:57:52 -0400 Subject: [PATCH] Wrapped the processing in a class and collapsed consecutive blank lines. --- windows/tools/hresultwrap.cpp | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/windows/tools/hresultwrap.cpp b/windows/tools/hresultwrap.cpp index d91fca70..82450204 100644 --- a/windows/tools/hresultwrap.cpp +++ b/windows/tools/hresultwrap.cpp @@ -153,14 +153,31 @@ ByteSlice Function::Body(void) const return out; } +class Processor { + bool previousLineBlank; + + ByteSlice generate(ByteSlice line); +public: + Processor(void); + + ByteSlice Process(ByteSlice line); +}; + +Processor::Processor(void) +{ + this->previousLineBlank = false; +} + #define noutbuf 2048 -ByteSlice generate(ByteSlice line) +ByteSlice Processor::generate(ByteSlice line) { ByteSlice genout; Function *f; genout = ByteSlice(0, noutbuf); + if (!this->previousLineBlank) + genout = genout.AppendString("\n"); f = new Function(line); genout = genout.Append(f->Signature()); @@ -169,14 +186,20 @@ ByteSlice generate(ByteSlice line) delete f; genout = genout.AppendString(u8"\n"); - genout = genout.AppendString(u8"\n"); + this->previousLineBlank = false; return genout; } -ByteSlice process(ByteSlice line) +ByteSlice Processor::Process(ByteSlice line) { if (line.Len() > 0 && line.Data()[0] == '@') - return generate(line.Slice(1, line.Len())); + return this->generate(line.Slice(1, line.Len())); + if (line.Len() == 0) { + if (this->previousLineBlank) + return ByteSlice(); + this->previousLineBlank = true; + } else + this->previousLineBlank = false; return line.AppendString("\n"); } @@ -187,6 +210,7 @@ int main(int argc, char *argv[]) ReadCloser *fin = NULL; WriteCloser *fout = NULL; Scanner *s = NULL; + Processor p; ByteSlice b; int ret = 1; Error *err = NULL; @@ -209,7 +233,7 @@ int main(int argc, char *argv[]) s = new Scanner(fin); while (s->Scan()) { - b = process(s->Bytes()); + b = p.Process(s->Bytes()); err = fout->Write(b); if (err != NULL) { fprintf(stderr, "error writing to %s: %s\n", argv[2], err->String());