minor improvements to documentation and style

This commit is contained in:
Guenter Obiltschnig
2017-02-13 22:33:48 +01:00
parent ba6a7900e2
commit bd82bb1306
21 changed files with 392 additions and 322 deletions

View File

@@ -48,7 +48,7 @@ Var Array::get(unsigned int index) const
{
value = _values.at(index);
}
catch(std::out_of_range&)
catch (std::out_of_range&)
{
//Ignore, we return an empty value
}
@@ -61,7 +61,7 @@ Array::Ptr Array::getArray(unsigned int index) const
Array::Ptr result;
Var value = get(index);
if ( value.type() == typeid(Array::Ptr) )
if (value.type() == typeid(Array::Ptr))
{
result = value.extract<Array::Ptr>();
}
@@ -74,7 +74,7 @@ Object::Ptr Array::getObject(unsigned int index) const
Object::Ptr result;
Var value = get(index);
if ( value.type() == typeid(Object::Ptr) )
if (value.type() == typeid(Object::Ptr))
{
result = value.extract<Object::Ptr>();
}
@@ -84,7 +84,7 @@ Object::Ptr Array::getObject(unsigned int index) const
bool Array::isNull(unsigned int index) const
{
if ( index < _values.size() )
if (index < _values.size())
{
Dynamic::Var value = _values[index];
return value.isEmpty();
@@ -122,11 +122,11 @@ void Array::stringify(std::ostream& out, unsigned int indent, int step) const
for (ValueVec::const_iterator it = _values.begin(); it != _values.end();)
{
for(int i = 0; i < indent; i++) out << ' ';
for (int i = 0; i < indent; i++) out << ' ';
Stringifier::stringify(*it, out, indent + step, step);
if ( ++it != _values.end() )
if (++it != _values.end())
{
out << ",";
if (step > 0) out << '\n';

View File

@@ -25,4 +25,4 @@ namespace JSON {
POCO_IMPLEMENT_EXCEPTION(JSONException, Exception, "JSON Exception")
} } // Namespace Poco::JSON
} } // namespace Poco::JSON

View File

@@ -92,7 +92,7 @@ Object::Ptr Object::getObject(const std::string& key) const
void Object::getNames(std::vector<std::string>& names) const
{
names.clear();
for(ValueMap::const_iterator it = _values.begin(); it != _values.end(); ++it)
for (ValueMap::const_iterator it = _values.begin(); it != _values.end(); ++it)
{
names.push_back(it->first);
}
@@ -103,7 +103,7 @@ void Object::stringify(std::ostream& out, unsigned int indent, int step) const
{
if (step < 0) step = indent;
if(!_preserveInsOrder)
if (!_preserveInsOrder)
doStringify(_values, out, indent, step);
else
doStringify(_keys, out, indent, step);

View File

@@ -48,7 +48,7 @@ void ParseHandler::startObject()
{
Object::Ptr newObj = new Object(_preserveObjectOrder);
if ( _stack.empty() ) // The first object
if (_stack.empty()) // The first object
{
_result = newObj;
}
@@ -56,12 +56,12 @@ void ParseHandler::startObject()
{
Var parent = _stack.top();
if ( parent.type() == typeid(Array::Ptr) )
if (parent.type() == typeid(Array::Ptr))
{
Array::Ptr arr = parent.extract<Array::Ptr>();
arr->add(newObj);
}
else if ( parent.type() == typeid(Object::Ptr) )
else if (parent.type() == typeid(Object::Ptr))
{
poco_assert_dbg(!_key.empty());
Object::Ptr obj = parent.extract<Object::Ptr>();
@@ -84,7 +84,7 @@ void ParseHandler::startArray()
{
Array::Ptr newArr = new Array();
if ( _stack.empty() ) // The first array
if (_stack.empty()) // The first array
{
_result = newArr;
}
@@ -92,12 +92,12 @@ void ParseHandler::startArray()
{
Var parent = _stack.top();
if ( parent.type() == typeid(Array::Ptr) )
if (parent.type() == typeid(Array::Ptr))
{
Array::Ptr arr = parent.extract<Array::Ptr>();
arr->add(newArr);
}
else if ( parent.type() == typeid(Object::Ptr) )
else if (parent.type() == typeid(Object::Ptr))
{
poco_assert_dbg(!_key.empty());
Object::Ptr obj = parent.extract<Object::Ptr>();
@@ -126,12 +126,12 @@ void ParseHandler::setValue(const Var& value)
{
Var parent = _stack.top();
if ( parent.type() == typeid(Array::Ptr) )
if (parent.type() == typeid(Array::Ptr))
{
Array::Ptr arr = parent.extract<Array::Ptr>();
arr->add(value);
}
else if ( parent.type() == typeid(Object::Ptr) )
else if (parent.type() == typeid(Object::Ptr))
{
Object::Ptr obj = parent.extract<Object::Ptr>();
obj->set(_key, value);

View File

@@ -34,7 +34,8 @@ namespace JSON {
static const unsigned char UTF8_LEAD_BITS[4] = { 0x00, 0xC0, 0xE0, 0xF0 };
const int Parser::_asciiClass[] = {
const int Parser::_asciiClass[] =
{
xx, xx, xx, xx, xx, xx, xx, xx,
xx, C_WHITE, C_WHITE, xx, xx, C_WHITE, xx, xx,
xx, xx, xx, xx, xx, xx, xx, xx,
@@ -57,7 +58,8 @@ const int Parser::_asciiClass[] = {
};
const int Parser::_stateTransitionTable[NR_STATES][NR_CLASSES] = {
const int Parser::_stateTransitionTable[NR_STATES][NR_CLASSES] =
{
/*
white 1-9 ABCDF etc
space | { } [ ] : , " \ / + - . 0 | a b c d e f l n r s t u | E | * */
@@ -100,7 +102,7 @@ const int Parser::_stateTransitionTable[NR_STATES][NR_CLASSES] = {
};
Parser::Parser(const Handler::Ptr& pHandler, std::size_t bufSize) :
Parser::Parser(const Handler::Ptr& pHandler, std::size_t bufSize):
_pHandler(pHandler),
_state(GO),
_beforeCommentState(0),
@@ -149,7 +151,7 @@ Dynamic::Var Parser::parse(const std::string& json)
Source<std::string::const_iterator> source(it, end);
int c = 0;
while(source.nextChar(c))
while (source.nextChar(c))
{
if (0 == parseChar(c, source))
throw SyntaxException("JSON syntax error");
@@ -169,7 +171,7 @@ Dynamic::Var Parser::parse(std::istream& in)
Source<std::istreambuf_iterator<char> > source(it, end);
int c = 0;
while(source.nextChar(c))
while (source.nextChar(c))
{
if (0 == parseChar(c, source)) throw JSONException("JSON syntax error");
}
@@ -237,7 +239,7 @@ void Parser::addEscapedCharToParseBuffer(CharIntType nextChar)
// remove the backslash
parseBufferPopBackChar();
switch(nextChar)
switch (nextChar)
{
case 'b':
parseBufferPushBackChar('\b');
@@ -313,7 +315,7 @@ Parser::CharIntType Parser::decodeUnicodeChar()
uc |= x << i;
}
if ( !_allowNullByte && uc == 0 ) return 0;
if (!_allowNullByte && uc == 0) return 0;
// clear UTF-16 char from buffer
_parseBuffer.resize(_parseBuffer.size() - 6);
@@ -379,7 +381,7 @@ void Parser::parseBuffer()
{
assertNonContainer();
switch(type)
switch (type)
{
case JSON_T_TRUE:
{
@@ -399,7 +401,7 @@ void Parser::parseBuffer()
case JSON_T_FLOAT:
{
// Float can't end with a dot
if (_parseBuffer[_parseBuffer.size() - 1] == '.' ) throw SyntaxException("JSON syntax error");
if (_parseBuffer[_parseBuffer.size() - 1] == '.') throw SyntaxException("JSON syntax error");
double float_value = NumberParser::parseFloat(std::string(_parseBuffer.begin(), _parseBuffer.size()));
_pHandler->value(float_value);
@@ -415,7 +417,7 @@ void Parser::parseBuffer()
Int64 value = NumberParser::parse64(numStr);
// if number is 32-bit, then handle as such
if (value > std::numeric_limits<int>::max()
|| value < std::numeric_limits<int>::min() )
|| value < std::numeric_limits<int>::min())
{
_pHandler->value(value);
}
@@ -425,11 +427,11 @@ void Parser::parseBuffer()
}
}
// try to handle error as unsigned in case of overflow
catch ( const SyntaxException& )
catch (const SyntaxException&)
{
UInt64 value = NumberParser::parseUnsigned64(numStr);
// if number is 32-bit, then handle as such
if ( value > std::numeric_limits<unsigned>::max() )
if (value > std::numeric_limits<unsigned>::max())
{
_pHandler->value(value);
}
@@ -445,7 +447,7 @@ void Parser::parseBuffer()
_pHandler->value(value);
}
// try to handle error as unsigned in case of overflow
catch ( const SyntaxException& )
catch (const SyntaxException&)
{
unsigned value = NumberParser::parseUnsigned(numStr);
_pHandler->value(value);
@@ -465,11 +467,12 @@ void Parser::parseBuffer()
clearBuffer();
}
int Parser::utf8CheckFirst(char byte)
{
unsigned char u = (unsigned char) byte;
if(u < 0x80)
if (u < 0x80)
return 1;
if (0x80 <= u && u <= 0xBF)
@@ -478,22 +481,22 @@ int Parser::utf8CheckFirst(char byte)
// sequence, i.e. a "continuation byte"
return 0;
}
else if(u == 0xC0 || u == 0xC1)
else if (u == 0xC0 || u == 0xC1)
{
// overlong encoding of an ASCII byte
return 0;
}
else if(0xC2 <= u && u <= 0xDF)
else if (0xC2 <= u && u <= 0xDF)
{
// 2-byte sequence
return 2;
}
else if(0xE0 <= u && u <= 0xEF)
else if (0xE0 <= u && u <= 0xEF)
{
// 3-byte sequence
return 3;
}
else if(0xF0 <= u && u <= 0xF4)
else if (0xF0 <= u && u <= 0xF4)
{
// 4-byte sequence
return 4;
@@ -506,4 +509,5 @@ int Parser::utf8CheckFirst(char byte)
}
}
} } // namespace Poco::JSON

View File

@@ -88,7 +88,7 @@ void PrintHandler::startObject()
void PrintHandler::endObject()
{
if( _tab.length() >= indent())
if (_tab.length() >= indent())
_tab.erase(_tab.length() - indent());
_out << endLine() << _tab << '}';
@@ -207,7 +207,8 @@ void PrintHandler::comma()
void PrintHandler::arrayValue()
{
if (!_objStart) comma();
if (array()) {
if (array())
{
_out << _tab;
}
}

View File

@@ -104,7 +104,7 @@ Var Query::find(const std::string& path) const
{
Var result = _source;
StringTokenizer tokenizer(path, ".");
for(StringTokenizer::Iterator token = tokenizer.begin(); token != tokenizer.end(); token++)
for (StringTokenizer::Iterator token = tokenizer.begin(); token != tokenizer.end(); token++)
{
if (!result.isEmpty())
{
@@ -113,7 +113,7 @@ Var Query::find(const std::string& path) const
int firstOffset = -1;
int offset = 0;
RegularExpression regex("\\[([0-9]+)\\]");
while(regex.match(*token, offset, matches) > 0)
while (regex.match(*token, offset, matches) > 0)
{
if (firstOffset == -1)
{
@@ -149,7 +149,7 @@ Var Query::find(const std::string& path) const
if (!result.isEmpty() && !indexes.empty())
{
for(std::vector<int>::iterator it = indexes.begin(); it != indexes.end(); ++it)
for (std::vector<int>::iterator it = indexes.begin(); it != indexes.end(); ++it)
{
if (result.type() == typeid(Array::Ptr))
{
@@ -171,4 +171,4 @@ Var Query::find(const std::string& path) const
}
} } // Namespace Poco::JSON
} } // namespace Poco::JSON

View File

@@ -31,31 +31,31 @@ void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int inde
{
if (step == -1) step = indent;
if ( any.type() == typeid(Object) )
if (any.type() == typeid(Object))
{
const Object& o = any.extract<Object>();
o.stringify(out, indent == 0 ? 0 : indent, step);
}
else if ( any.type() == typeid(Array) )
else if (any.type() == typeid(Array))
{
const Array& a = any.extract<Array>();
a.stringify(out, indent == 0 ? 0 : indent, step);
}
else if ( any.type() == typeid(Object::Ptr) )
else if (any.type() == typeid(Object::Ptr))
{
const Object::Ptr& o = any.extract<Object::Ptr>();
o->stringify(out, indent == 0 ? 0 : indent, step);
}
else if ( any.type() == typeid(Array::Ptr) )
else if (any.type() == typeid(Array::Ptr))
{
const Array::Ptr& a = any.extract<Array::Ptr>();
a->stringify(out, indent == 0 ? 0 : indent, step);
}
else if ( any.isEmpty() )
else if (any.isEmpty())
{
out << "null";
}
else if ( any.isNumeric() || any.isBoolean() )
else if (any.isNumeric() || any.isBoolean())
{
out << any.convert<std::string>();
}
@@ -75,19 +75,19 @@ void Stringifier::formatString(const std::string& value, std::ostream& out)
{
switch (*it)
{
case '\\': out << "\\\\"; break;
case '"': out << "\\\""; break;
case '/': out << "\\/"; break;
case '\b': out << "\\b"; break;
case '\f': out << "\\f"; break;
case '\n': out << "\\n"; break;
case '\r': out << "\\r"; break;
case '\t': out << "\\t"; break;
default: out << *it; break;
case '\\': out << "\\\\"; break;
case '"': out << "\\\""; break;
case '/': out << "\\/"; break;
case '\b': out << "\\b"; break;
case '\f': out << "\\f"; break;
case '\n': out << "\\n"; break;
case '\r': out << "\\r"; break;
case '\t': out << "\\t"; break;
default: out << *it; break;
}
}
out << '"';
}
} } // Namespace Poco::JSON
} } // namespace Poco::JSON

View File

@@ -51,11 +51,11 @@ public:
class StringPart: public Part
{
public:
StringPart() : Part()
StringPart(): Part()
{
}
StringPart(const std::string& content) : Part(), _content(content)
StringPart(const std::string& content): Part(), _content(content)
{
}
@@ -101,7 +101,7 @@ public:
void render(const Var& data, std::ostream& out) const
{
for(VectorParts::const_iterator it = _parts.begin(); it != _parts.end(); ++it)
for (VectorParts::const_iterator it = _parts.begin(); it != _parts.end(); ++it)
{
(*it)->render(data, out);
}
@@ -115,7 +115,7 @@ protected:
class EchoPart: public Part
{
public:
EchoPart(const std::string& query) : Part(), _query(query)
EchoPart(const std::string& query): Part(), _query(query)
{
}
@@ -128,7 +128,7 @@ public:
Query query(data);
Var value = query.find(_query);
if ( ! value.isEmpty() )
if (!value.isEmpty())
{
out << value.convert<std::string>();
}
@@ -142,7 +142,7 @@ private:
class LogicQuery
{
public:
LogicQuery(const std::string& query) : _queryString(query)
LogicQuery(const std::string& query): _queryString(query)
{
}
@@ -157,14 +157,14 @@ public:
Query query(data);
Var value = query.find(_queryString);
if ( ! value.isEmpty() ) // When empty, logic will be false
if (!value.isEmpty()) // When empty, logic will be false
{
if ( value.isString() )
if (value.isString())
// An empty string must result in false, otherwise true
// Which is not the case when we convert to bool with Var
{
std::string s = value.convert<std::string>();
logic = ! s.empty();
logic = !s.empty();
}
else
{
@@ -183,10 +183,10 @@ protected:
};
class LogicExistQuery : public LogicQuery
class LogicExistQuery: public LogicQuery
{
public:
LogicExistQuery(const std::string& query) : LogicQuery(query)
LogicExistQuery(const std::string& query): LogicQuery(query)
{
}
@@ -204,10 +204,10 @@ public:
};
class LogicElseQuery : public LogicQuery
class LogicElseQuery: public LogicQuery
{
public:
LogicElseQuery() : LogicQuery("")
LogicElseQuery(): LogicQuery("")
{
}
@@ -222,10 +222,10 @@ public:
};
class LogicPart : public MultiPart
class LogicPart: public MultiPart
{
public:
LogicPart() : MultiPart()
LogicPart(): MultiPart()
{
}
@@ -248,9 +248,9 @@ public:
void render(const Var& data, std::ostream& out) const
{
int count = 0;
for(std::vector<SharedPtr<LogicQuery> >::const_iterator it = _queries.begin(); it != _queries.end(); ++it, ++count)
for (std::vector<SharedPtr<LogicQuery> >::const_iterator it = _queries.begin(); it != _queries.end(); ++it, ++count)
{
if( (*it)->apply(data) && _parts.size() > count )
if ((*it)->apply(data) && _parts.size() > count)
{
_parts[count]->render(data, out);
break;
@@ -266,7 +266,7 @@ private:
class LoopPart: public MultiPart
{
public:
LoopPart(const std::string& name, const std::string& query) : MultiPart(), _name(name), _query(query)
LoopPart(const std::string& name, const std::string& query): MultiPart(), _name(name), _query(query)
{
}
@@ -278,13 +278,13 @@ public:
{
Query query(data);
if ( data.type() == typeid(Object::Ptr) )
if (data.type() == typeid(Object::Ptr))
{
Object::Ptr dataObject = data.extract<Object::Ptr>();
Array::Ptr array = query.findArray(_query);
if ( ! array.isNull() )
if (!array.isNull())
{
for(int i = 0; i < array->size(); i++)
for (int i = 0; i < array->size(); i++)
{
Var value = array->get(i);
dataObject->set(_name, value);
@@ -305,19 +305,19 @@ class IncludePart: public Part
{
public:
IncludePart(const Path& parentPath, const Path& path)
: Part()
, _path(path)
IncludePart(const Path& parentPath, const Path& path):
Part(),
_path(path)
{
// When the path is relative, try to make it absolute based
// on the path of the parent template. When the file doesn't
// exist, we keep it relative and hope that the cache can
// resolve it.
if ( _path.isRelative() )
if (_path.isRelative())
{
Path templatePath(parentPath, _path);
File templateFile(templatePath);
if ( templateFile.exists() )
if (templateFile.exists())
{
_path = templatePath;
}
@@ -331,7 +331,7 @@ public:
void render(const Var& data, std::ostream& out) const
{
TemplateCache* cache = TemplateCache::instance();
if ( cache == NULL )
if (cache == 0)
{
Template tpl(_path);
tpl.parse();
@@ -349,17 +349,17 @@ private:
};
Template::Template(const Path& templatePath)
: _parts(0)
, _currentPart(0)
, _templatePath(templatePath)
Template::Template(const Path& templatePath):
_parts(0),
_currentPart(0),
_templatePath(templatePath)
{
}
Template::Template()
: _parts(0)
, _currentPart(0)
Template::Template():
_parts(0),
_currentPart(0)
{
}
@@ -373,7 +373,7 @@ Template::~Template()
void Template::parse()
{
File file(_templatePath);
if ( file.exists() )
if (file.exists())
{
FileInputStream fis(_templatePath.toString());
parse(fis);
@@ -385,48 +385,48 @@ void Template::parse(std::istream& in)
{
_parseTime.update();
_parts = new MultiPart();
_parts = new MultiPart;
_currentPart = _parts;
while(in.good())
while (in.good())
{
std::string text = readText(in); // Try to read text first
if ( text.length() > 0 )
if (text.length() > 0)
{
_currentPart->addPart(new StringPart(text));
}
if ( in.bad() )
if (in.bad())
break; // Nothing to do anymore
std::string command = readTemplateCommand(in); // Try to read a template command
if ( command.empty() )
if (command.empty())
{
break;
}
readWhiteSpace(in);
if ( command.compare("echo") == 0 )
if (command.compare("echo") == 0)
{
std::string query = readQuery(in);
if ( query.empty() )
if (query.empty())
{
throw JSONTemplateException("Missing query in <? echo ?>");
}
_currentPart->addPart(new EchoPart(query));
}
else if ( command.compare("for") == 0 )
else if (command.compare("for") == 0)
{
std::string loopVariable = readWord(in);
if ( loopVariable.empty() )
if (loopVariable.empty())
{
throw JSONTemplateException("Missing variable in <? for ?> command");
}
readWhiteSpace(in);
std::string query = readQuery(in);
if ( query.empty() )
if (query.empty())
{
throw JSONTemplateException("Missing query in <? for ?> command");
}
@@ -437,15 +437,15 @@ void Template::parse(std::istream& in)
_currentPart->addPart(part);
_currentPart = part;
}
else if ( command.compare("else") == 0 )
else if (command.compare("else") == 0)
{
if ( _partStack.size() == 0 )
if (_partStack.size() == 0)
{
throw JSONTemplateException("Unexpected <? else ?> found");
}
_currentPart = _partStack.top();
LogicPart* lp = dynamic_cast<LogicPart*>(_currentPart);
if ( lp == NULL )
if (lp == 0)
{
throw JSONTemplateException("Missing <? if ?> or <? ifexist ?> for <? else ?>");
}
@@ -453,23 +453,22 @@ void Template::parse(std::istream& in)
lp->addPart(part);
_currentPart = part;
}
else if ( command.compare("elsif") == 0
|| command.compare("elif") == 0 )
else if (command.compare("elsif") == 0 || command.compare("elif") == 0)
{
std::string query = readQuery(in);
if ( query.empty() )
if (query.empty())
{
throw JSONTemplateException("Missing query in <? " + command + " ?>");
}
if ( _partStack.size() == 0 )
if (_partStack.size() == 0)
{
throw JSONTemplateException("Unexpected <? elsif / elif ?> found");
}
_currentPart = _partStack.top();
LogicPart* lp = dynamic_cast<LogicPart*>(_currentPart);
if ( lp == NULL )
if (lp == 0)
{
throw JSONTemplateException("Missing <? if ?> or <? ifexist ?> for <? elsif / elif ?>");
}
@@ -477,15 +476,15 @@ void Template::parse(std::istream& in)
lp->addPart(new LogicQuery(query), part);
_currentPart = part;
}
else if ( command.compare("endfor") == 0 )
else if (command.compare("endfor") == 0)
{
if ( _partStack.size() < 2 )
if (_partStack.size() < 2)
{
throw JSONTemplateException("Unexpected <? endfor ?> found");
}
MultiPart* loopPart = _partStack.top();
LoopPart* lp = dynamic_cast<LoopPart*>(loopPart);
if ( lp == NULL )
if (lp == 0)
{
throw JSONTemplateException("Missing <? for ?> command");
}
@@ -493,16 +492,16 @@ void Template::parse(std::istream& in)
_currentPart = _partStack.top();
_partStack.pop();
}
else if ( command.compare("endif") == 0 )
else if (command.compare("endif") == 0)
{
if ( _partStack.size() < 2 )
if (_partStack.size() < 2)
{
throw JSONTemplateException("Unexpected <? endif ?> found");
}
_currentPart = _partStack.top();
LogicPart* lp = dynamic_cast<LogicPart*>(_currentPart);
if ( lp == NULL )
if (lp == 0)
{
throw JSONTemplateException("Missing <? if ?> or <? ifexist ?> for <? endif ?>");
}
@@ -511,11 +510,10 @@ void Template::parse(std::istream& in)
_currentPart = _partStack.top();
_partStack.pop();
}
else if ( command.compare("if") == 0
|| command.compare("ifexist") == 0 )
else if (command.compare("if") == 0 || command.compare("ifexist") == 0)
{
std::string query = readQuery(in);
if ( query.empty() )
if (query.empty())
{
throw JSONTemplateException("Missing query in <? " + command + " ?>");
}
@@ -524,7 +522,7 @@ void Template::parse(std::istream& in)
_partStack.push(lp);
_currentPart->addPart(lp);
_currentPart = new MultiPart();
if ( command.compare("ifexist") == 0 )
if (command.compare("ifexist") == 0)
{
lp->addPart(new LogicExistQuery(query), _currentPart);
}
@@ -533,11 +531,11 @@ void Template::parse(std::istream& in)
lp->addPart(new LogicQuery(query), _currentPart);
}
}
else if ( command.compare("include") == 0 )
else if (command.compare("include") == 0)
{
readWhiteSpace(in);
std::string filename = readString(in);
if ( filename.empty() )
if (filename.empty())
{
throw JSONTemplateException("Missing filename in <? include ?>");
}
@@ -556,17 +554,17 @@ void Template::parse(std::istream& in)
readWhiteSpace(in);
int c = in.get();
if ( c == '?' && in.peek() == '>' )
if (c == '?' && in.peek() == '>')
{
in.get(); // forget '>'
if ( command.compare("echo") != 0 )
if (command.compare("echo") != 0)
{
if ( in.peek() == '\r' )
if (in.peek() == '\r')
{
in.get();
}
if ( in.peek() == '\n' )
if (in.peek() == '\n')
{
in.get();
}
@@ -584,11 +582,11 @@ std::string Template::readText(std::istream& in)
{
std::string text;
int c = in.get();
while(c != -1)
while (c != -1)
{
if ( c == '<' )
if (c == '<')
{
if ( in.peek() == '?' )
if (in.peek() == '?')
{
in.get(); // forget '?'
break;
@@ -609,18 +607,18 @@ std::string Template::readTemplateCommand(std::istream& in)
readWhiteSpace(in);
int c = in.get();
while(c != -1)
while (c != -1)
{
if ( Ascii::isSpace(c) )
if (Ascii::isSpace(c))
break;
if ( c == '?' && in.peek() == '>' )
if (c == '?' && in.peek() == '>')
{
in.putback(c);
break;
}
if ( c == '=' && command.length() == 0 )
if (c == '=' && command.length() == 0)
{
command = "echo";
break;
@@ -639,7 +637,7 @@ std::string Template::readWord(std::istream& in)
{
std::string word;
int c;
while((c = in.peek()) != -1 && ! Ascii::isSpace(c))
while ((c = in.peek()) != -1 && !Ascii::isSpace(c))
{
in.get();
word += c;
@@ -652,15 +650,15 @@ std::string Template::readQuery(std::istream& in)
{
std::string word;
int c;
while((c = in.get()) != -1)
while ((c = in.get()) != -1)
{
if ( c == '?' && in.peek() == '>' )
if (c == '?' && in.peek() == '>')
{
in.putback(c);
break;
}
if ( Ascii::isSpace(c) )
if (Ascii::isSpace(c))
{
break;
}
@@ -673,7 +671,7 @@ std::string Template::readQuery(std::istream& in)
void Template::readWhiteSpace(std::istream& in)
{
int c;
while((c = in.peek()) != -1 && Ascii::isSpace(c))
while ((c = in.peek()) != -1 && Ascii::isSpace(c))
{
in.get();
}
@@ -685,9 +683,9 @@ std::string Template::readString(std::istream& in)
std::string str;
int c = in.get();
if ( c == '"' )
if (c == '"')
{
while((c = in.get()) != -1 && c != '"')
while ((c = in.get()) != -1 && c != '"')
{
str += c;
}
@@ -702,4 +700,4 @@ void Template::render(const Var& data, std::ostream& out) const
}
} } // Namespace Poco::JSON
} } // namespace Poco::JSON

View File

@@ -22,10 +22,10 @@ namespace Poco {
namespace JSON {
TemplateCache* TemplateCache::_instance = NULL;
TemplateCache* TemplateCache::_pInstance = 0;
TemplateCache::TemplateCache() : _logger(NULL)
TemplateCache::TemplateCache(): _pLogger(0)
{
setup();
}
@@ -33,40 +33,44 @@ TemplateCache::TemplateCache() : _logger(NULL)
TemplateCache::~TemplateCache()
{
_instance = NULL;
_pInstance = 0;
}
void TemplateCache::setup()
{
poco_assert (_instance == NULL);
_instance = this;
poco_assert (_pInstance == 0);
_pInstance = this;
}
Template::Ptr TemplateCache::getTemplate(const Path& path)
{
if ( _logger )
if (_pLogger)
{
poco_trace_f1(*_logger, "Trying to load %s", path.toString());
poco_trace_f1(*_pLogger, "Trying to load %s", path.toString());
}
Path templatePath = resolvePath(path);
std::string templatePathname = templatePath.toString();
if ( _logger )
if (_pLogger)
{
poco_trace_f1(*_logger, "Path resolved to %s", templatePathname);
poco_trace_f1(*_pLogger, "Path resolved to %s", templatePathname);
}
File templateFile(templatePathname);
Template::Ptr tpl;
std::map<std::string, Template::Ptr>::iterator it = _cache.find(templatePathname);
if ( it == _cache.end() )
if (it == _cache.end())
{
if ( templateFile.exists() )
if (templateFile.exists())
{
if ( _logger )
if (_pLogger)
{
poco_information_f1(*_logger, "Loading template %s", templatePath.toString());
poco_information_f1(*_pLogger, "Loading template %s", templatePath.toString());
}
tpl = new Template(templatePath);
@@ -76,19 +80,19 @@ Template::Ptr TemplateCache::getTemplate(const Path& path)
tpl->parse();
_cache[templatePathname] = tpl;
}
catch(JSONTemplateException& jte)
catch (JSONTemplateException& jte)
{
if ( _logger )
if (_pLogger)
{
poco_error_f2(*_logger, "Template %s contains an error: %s", templatePath.toString(), jte.message());
poco_error_f2(*_pLogger, "Template %s contains an error: %s", templatePath.toString(), jte.message());
}
}
}
else
{
if ( _logger )
if (_pLogger)
{
poco_error_f1(*_logger, "Template file %s doesn't exist", templatePath.toString());
poco_error_f1(*_pLogger, "Template file %s doesn't exist", templatePath.toString());
}
throw FileNotFoundException(templatePathname);
}
@@ -96,11 +100,11 @@ Template::Ptr TemplateCache::getTemplate(const Path& path)
else
{
tpl = it->second;
if ( tpl->parseTime() < templateFile.getLastModified() )
if (tpl->parseTime() < templateFile.getLastModified())
{
if ( _logger )
if (_pLogger)
{
poco_information_f1(*_logger, "Reloading template %s", templatePath.toString());
poco_information_f1(*_pLogger, "Reloading template %s", templatePath.toString());
}
tpl = new Template(templatePath);
@@ -110,11 +114,11 @@ Template::Ptr TemplateCache::getTemplate(const Path& path)
tpl->parse();
_cache[templatePathname] = tpl;
}
catch(JSONTemplateException& jte)
catch (JSONTemplateException& jte)
{
if ( _logger )
if (_pLogger)
{
poco_error_f2(*_logger, "Template %s contains an error: %s", templatePath.toString(), jte.message());
poco_error_f2(*_pLogger, "Template %s contains an error: %s", templatePath.toString(), jte.message());
}
}
}
@@ -126,25 +130,25 @@ Template::Ptr TemplateCache::getTemplate(const Path& path)
Path TemplateCache::resolvePath(const Path& path) const
{
if ( path.isAbsolute() )
if (path.isAbsolute())
return path;
for(std::vector<Path>::const_iterator it = _includePaths.begin(); it != _includePaths.end(); ++it)
for (std::vector<Path>::const_iterator it = _includePaths.begin(); it != _includePaths.end(); ++it)
{
Path templatePath(*it, path);
File templateFile(templatePath);
if ( templateFile.exists() )
if (templateFile.exists())
{
if ( _logger )
if (_pLogger)
{
poco_trace_f2(*_logger, "%s template file resolved to %s", path.toString(), templatePath.toString());
poco_trace_f2(*_pLogger, "%s template file resolved to %s", path.toString(), templatePath.toString());
}
return templatePath;
}
if ( _logger )
if (_pLogger)
{
poco_trace_f1(*_logger, "%s doesn't exist", templatePath.toString());
poco_trace_f1(*_pLogger, "%s doesn't exist", templatePath.toString());
}
}