Extracted class IndentedPrintDecorator from PrettyPrintDecorator

This commit is contained in:
Benoit Blanchon
2014-08-26 09:28:41 +02:00
parent d877d77b63
commit 6539c6982c
12 changed files with 128 additions and 31 deletions

View File

@ -0,0 +1,40 @@
#include "IndentedPrintDecorator.h"
void IndentedPrintDecorator::indent()
{
currentTabCount++;
}
void IndentedPrintDecorator::unindent()
{
currentTabCount--;
}
size_t IndentedPrintDecorator::write(uint8_t c)
{
size_t n = 0;
if (isNewLine)
n += writeCurrentTabs();
n += sink.write(c);
isNewLine = c == '\n';
return n;
}
size_t IndentedPrintDecorator::writeCurrentTabs()
{
return writeTabs(currentTabCount);
}
size_t IndentedPrintDecorator::writeTabs(int count)
{
size_t n = 0;
for (int i = 0; i<count; i++)
n += sink.write(' ');
return n;
}

View File

@ -0,0 +1,36 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "Print.h"
class IndentedPrintDecorator : public Print
{
public:
IndentedPrintDecorator(Print& p)
: sink(p)
{
pendingTabCount = 0;
currentTabCount = 0;
isNewLine = true;
}
void indent();
void unindent();
bool isNewLine;
virtual size_t write(uint8_t);
private:
Print& sink;
int currentTabCount;
int pendingTabCount;
size_t writeCurrentTabs();
size_t writeTabs(int count);
};

View File

@ -12,6 +12,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="EscapedString.h" />
<ClInclude Include="IndentedPrintDecorator.h" />
<ClInclude Include="PrettyPrintDecorator.h" />
<ClInclude Include="JsonArray.h" />
<ClInclude Include="JsonArrayBase.h" />
@ -25,6 +26,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="EscapedString.cpp" />
<ClCompile Include="IndentedPrintDecorator.cpp" />
<ClCompile Include="PrettyPrintDecorator.cpp" />
<ClCompile Include="JsonArrayBase.cpp" />
<ClCompile Include="JsonObjectBase.cpp" />

View File

@ -48,6 +48,9 @@
<ClInclude Include="PrettyPrintDecorator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="IndentedPrintDecorator.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="EscapedString.cpp">
@ -71,5 +74,8 @@
<ClCompile Include="PrettyPrintDecorator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="IndentedPrintDecorator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -26,12 +26,18 @@ namespace ArduinoJson
return printTo(sb);
}
size_t prettyPrintTo(Print& p) const
size_t prettyPrintTo(IndentedPrintDecorator& p) const
{
PrettyPrintDecorator decorator(p);
return printTo(decorator);
}
size_t prettyPrintTo(Print& p) const
{
IndentedPrintDecorator decorator(p);
return printTo(decorator);
}
virtual size_t printTo(Print& p) const = 0;
};
}

View File

@ -51,18 +51,20 @@ size_t PrettyPrintDecorator::handleMarkupChar(uint8_t c)
size_t PrettyPrintDecorator::handleBlockOpen(uint8_t c)
{
size_t n = inEmptyBlock() ? breakThenWrite(c) : writeChar(c);
indent++;
return n;
return indentIfNeeded() + writeChar(c);
}
size_t PrettyPrintDecorator::handleBlockClose(uint8_t c)
{
indent--;
return inEmptyBlock() ? writeChar(c) : breakThenWrite(c);
if (inEmptyBlock())
{
return writeChar(c);
}
else
{
sink.unindent();
return breakThenWrite(c);
}
}
size_t PrettyPrintDecorator::handleColumn()
@ -77,7 +79,7 @@ size_t PrettyPrintDecorator::handleComma()
size_t PrettyPrintDecorator::handleQuoteOpen()
{
size_t n = inEmptyBlock() ? breakThenWrite('"') : writeChar('"');
size_t n = indentIfNeeded() + writeChar('"');
inString = true;
@ -86,15 +88,13 @@ size_t PrettyPrintDecorator::handleQuoteOpen()
size_t PrettyPrintDecorator::handleNormalChar(uint8_t c)
{
return inEmptyBlock() ? breakThenWrite(c) : writeChar(c);
return indentIfNeeded() + writeChar(c);
}
size_t PrettyPrintDecorator::breakAndIndent()
size_t PrettyPrintDecorator::indentIfNeeded()
{
size_t n = writeChar('\n');
if (!inEmptyBlock()) return 0;
for (int i = 0; i < indent; i++)
n += writeChar(' ');
return n;
sink.indent();
return sink.println();
}

View File

@ -1,4 +1,3 @@
#pragma once
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
@ -7,6 +6,7 @@
#pragma once
#include "Print.h"
#include "IndentedPrintDecorator.h"
namespace ArduinoJson
{
@ -16,8 +16,8 @@ namespace ArduinoJson
{
public:
PrettyPrintDecorator(Print& p)
: indent(0), sink(p)
PrettyPrintDecorator(IndentedPrintDecorator& p)
: sink(p)
{
previousChar = 0;
inString = false;
@ -26,9 +26,8 @@ namespace ArduinoJson
virtual size_t write(uint8_t);
private:
int indent;
uint8_t previousChar;
Print& sink;
IndentedPrintDecorator& sink;
bool inString;
bool inEmptyBlock()
@ -45,17 +44,16 @@ namespace ArduinoJson
size_t handleComma();
size_t handleQuoteOpen();
size_t handleNormalChar(uint8_t);
size_t breakAndIndent();
size_t indentIfNeeded();
size_t breakThenWrite(uint8_t c)
{
return breakAndIndent() + writeChar(c);
return sink.println() + writeChar(c);
}
size_t writeThenBreak(uint8_t c)
{
return writeChar(c) + breakAndIndent();
return writeChar(c) + sink.println();
}
size_t writeChar(uint8_t c)

View File

@ -32,4 +32,9 @@ size_t Print::print(long value)
return print(tmp);
}
size_t Print::println()
{
return /*write('\r') +*/ write('\n');
}
#endif

View File

@ -19,6 +19,7 @@ public:
size_t print(const char[]);
size_t print(double, int = 2);
size_t print(long);
size_t println();
};
#else

View File

@ -76,7 +76,8 @@ namespace JsonGeneratorTests
void whenInputIs(const char input[])
{
StringBuilder sb(buffer, sizeof(buffer));
PrettyPrintDecorator decorator(sb);
IndentedPrintDecorator indentedPrint(sb);
PrettyPrintDecorator decorator(indentedPrint);
returnValue = decorator.print(input);
}

View File

@ -74,7 +74,8 @@ namespace JsonGeneratorTests
void whenInputIs(const char input[])
{
StringBuilder sb(buffer, sizeof(buffer));
PrettyPrintDecorator decorator(sb);
IndentedPrintDecorator indentedPrint(sb);
PrettyPrintDecorator decorator(indentedPrint);
returnValue = decorator.print(input);
}

View File

@ -61,7 +61,8 @@ namespace JsonGeneratorTests
void whenInputIs(const char input[])
{
StringBuilder sb(buffer, sizeof(buffer));
PrettyPrintDecorator decorator(sb);
IndentedPrintDecorator indentedPrint(sb);
PrettyPrintDecorator decorator(indentedPrint);
returnValue = decorator.print(input);
}