Skip to content

Commit

Permalink
COMMON: Remove vfprintf call from XML parser
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Jun 6, 2011
1 parent ac1c212 commit a6e5b97
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
22 changes: 7 additions & 15 deletions common/xmlparser.cpp
Expand Up @@ -23,9 +23,6 @@
// FIXME: Avoid using fprintf
#define FORBIDDEN_SYMBOL_EXCEPTION_fprintf

// FIXME: Avoid using vfprintf
#define FORBIDDEN_SYMBOL_EXCEPTION_vfprintf


#include "common/xmlparser.h"
#include "common/archive.h"
Expand Down Expand Up @@ -83,7 +80,7 @@ void XMLParser::close() {
_stream = 0;
}

bool XMLParser::parserError(const char *errorString, ...) {
bool XMLParser::parserError(const Common::String &errStr) {
_state = kParserError;

const int startPosition = _stream->pos();
Expand Down Expand Up @@ -134,12 +131,7 @@ bool XMLParser::parserError(const char *errorString, ...) {
fprintf(stderr, "%c", _stream->readByte());

fprintf(stderr, "\n\nParser error: ");

va_list args;
va_start(args, errorString);
vfprintf(stderr, errorString, args);
va_end(args);

fprintf(stderr, "%s", errStr.c_str());
fprintf(stderr, "\n\n");

return false;
Expand Down Expand Up @@ -181,16 +173,16 @@ bool XMLParser::parseActiveKey(bool closed) {

for (List<XMLKeyLayout::XMLKeyProperty>::const_iterator i = key->layout->properties.begin(); i != key->layout->properties.end(); ++i) {
if (i->required && !localMap.contains(i->name))
return parserError("Missing required property '%s' inside key '%s'", i->name.c_str(), key->name.c_str());
return parserError("Missing required property '" + i->name + "' inside key '" + key->name + "'");
else if (localMap.contains(i->name))
keyCount--;
}

if (keyCount > 0)
return parserError("Unhandled property inside key '%s'.", key->name.c_str());
return parserError("Unhandled property inside key '" + key->name + "'.");

} else {
return parserError("Unexpected key in the active scope ('%s').", key->name.c_str());
return parserError("Unexpected key in the active scope ('" + key->name + "').");
}

// check if any of the parents must be ignored.
Expand All @@ -205,7 +197,7 @@ bool XMLParser::parseActiveKey(bool closed) {
// when keyCallback() fails, a parserError() must be set.
// We set it manually in that case.
if (_state != kParserError)
parserError("Unhandled exception when parsing '%s' key.", key->name.c_str());
parserError("Unhandled exception when parsing '" + key->name + "' key.");

return false;
}
Expand Down Expand Up @@ -395,7 +387,7 @@ bool XMLParser::parse() {
case kParserNeedPropertyName:
if (activeClosure) {
if (!closeKey()) {
parserError("Missing data when closing key '%s'.", _activeKey.top()->name.c_str());
parserError("Missing data when closing key '" + _activeKey.top()->name + "'.");
break;
}

Expand Down
2 changes: 1 addition & 1 deletion common/xmlparser.h
Expand Up @@ -274,7 +274,7 @@ class XMLParser {
* Parser error always returns "false" so we can pass the return value
* directly and break down the parsing.
*/
bool parserError(const char *errorString, ...) GCC_PRINTF(2, 3);
bool parserError(const Common::String &errStr);

/**
* Skips spaces/whitelines etc.
Expand Down
14 changes: 7 additions & 7 deletions engines/sword25/gfx/animationresource.cpp
Expand Up @@ -38,11 +38,11 @@

namespace Sword25 {

namespace {
const int DEFAULT_FPS = 10;
const int MIN_FPS = 1;
const int MAX_FPS = 200;
}
enum {
DEFAULT_FPS = 10,
MIN_FPS = 1,
MAX_FPS = 200
};

AnimationResource::AnimationResource(const Common::String &filename) :
Resource(filename, Resource::TYPE_ANIMATION),
Expand Down Expand Up @@ -112,8 +112,8 @@ bool AnimationResource::parseBooleanKey(Common::String s, bool &result) {

bool AnimationResource::parserCallback_animation(ParserNode *node) {
if (!parseIntegerKey(node->values["fps"], 1, &_FPS) || (_FPS < MIN_FPS) || (_FPS > MAX_FPS)) {
return parserError("Illegal or missing fps attribute in <animation> tag in \"%s\". Assuming default (\"%d\").",
getFileName().c_str(), DEFAULT_FPS);
return parserError(Common::String::format("Illegal or missing fps attribute in <animation> tag in \"%s\". Assuming default (\"%d\").",
getFileName().c_str(), DEFAULT_FPS));
}

// Loop type value
Expand Down
12 changes: 6 additions & 6 deletions engines/sword25/gfx/fontresource.cpp
Expand Up @@ -115,23 +115,23 @@ bool FontResource::parserCallback_character(ParserNode *node) {
int charCode, top, left, right, bottom;

if (!parseIntegerKey(node->values["code"], 1, &charCode) || (charCode < 0) || (charCode >= 256)) {
return parserError("Illegal or missing code attribute in <character> tag in \"%s\".", getFileName().c_str());
return parserError("Illegal or missing code attribute in <character> tag in '" + getFileName() + "'.");
}

if (!parseIntegerKey(node->values["top"], 1, &top) || (top < 0)) {
return parserError("Illegal or missing top attribute in <character> tag in \"%s\".", getFileName().c_str());
return parserError("Illegal or missing top attribute in <character> tag in '" + getFileName() + "'.");
}
if (!parseIntegerKey(node->values["left"], 1, &left) || (left < 0)) {
return parserError("Illegal or missing left attribute in <character> tag in \"%s\".", getFileName().c_str());
return parserError("Illegal or missing left attribute in <character> tag in '" + getFileName() + "'.");
}
if (!parseIntegerKey(node->values["right"], 1, &right) || (right < 0)) {
return parserError("Illegal or missing right attribute in <character> tag in \"%s\".", getFileName().c_str());
return parserError("Illegal or missing right attribute in <character> tag in '" + getFileName() + "'.");
}
if (!parseIntegerKey(node->values["bottom"], 1, &bottom) || (bottom < 0)) {
return parserError("Illegal or missing bottom attribute in <character> tag in \"%s\".", getFileName().c_str());
return parserError("Illegal or missing bottom attribute in <character> tag in '" + getFileName() + "'.");
}

this->_characterRects[charCode] = Common::Rect(left, top, right, bottom);
_characterRects[charCode] = Common::Rect(left, top, right, bottom);
return true;
}

Expand Down
28 changes: 14 additions & 14 deletions gui/ThemeParser.cpp
Expand Up @@ -232,7 +232,7 @@ bool ThemeParser::parserCallback_bitmap(ParserNode *node) {
}

if (!_theme->addBitmap(node->values["filename"]))
return parserError("Error loading Bitmap file '%s'", node->values["filename"].c_str());
return parserError("Error loading Bitmap file '" + node->values["filename"] + "'");

return true;
}
Expand All @@ -252,7 +252,7 @@ bool ThemeParser::parserCallback_text(ParserNode *node) {
TextColor textColorId = parseTextColorId(node->values["text_color"]);

if (!_theme->addTextData(id, textDataId, textColorId, alignH, alignV))
return parserError("Error adding Text Data for '%s'.", id.c_str());
return parserError("Error adding Text Data for '" + id + "'.");

return true;
}
Expand All @@ -279,13 +279,13 @@ bool ThemeParser::parserCallback_color(ParserNode *node) {
Common::String name = node->values["name"];

if (_palette.contains(name))
return parserError("Color '%s' has already been defined.", name.c_str());
return parserError("Color '" + name + "' has already been defined.");

int red, green, blue;

if (parseIntegerKey(node->values["rgb"], 3, &red, &green, &blue) == false ||
red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255)
return parserError("Error parsing RGB values for palette color '%s'", name.c_str());\
return parserError("Error parsing RGB values for palette color '" + name + "'");

_palette[name].r = red;
_palette[name].g = green;
Expand Down Expand Up @@ -332,7 +332,7 @@ bool ThemeParser::parserCallback_drawstep(ParserNode *node) {
drawstep->drawingCall = getDrawingFunctionCallback(functionName);

if (drawstep->drawingCall == 0)
return parserError("%s is not a valid drawing function name", functionName.c_str());
return parserError(functionName + " is not a valid drawing function name");

if (!parseDrawStep(node, drawstep, true))
return false;
Expand Down Expand Up @@ -385,11 +385,11 @@ bool ThemeParser::parseDrawStep(ParserNode *stepNode, Graphics::DrawStep *drawst
#define __PARSER_ASSIGN_INT(struct_name, key_name, force) \
if (stepNode->values.contains(key_name)) { \
if (!parseIntegerKey(stepNode->values[key_name], 1, &x)) \
return parserError("Error parsing key value for '%s'.", key_name); \
return parserError("Error parsing key value for '" + Common::String(key_name) + "'."); \
\
drawstep->struct_name = x; \
} else if (force) { \
return parserError("Missing necessary key '%s'.", key_name); \
return parserError("Missing necessary key '" + Common::String(key_name) + "'."); \
}

/**
Expand All @@ -410,7 +410,7 @@ bool ThemeParser::parseDrawStep(ParserNode *stepNode, Graphics::DrawStep *drawst
blue = _palette[val].b; \
} else if (parseIntegerKey(val, 3, &red, &green, &blue) == false || \
red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255) \
return parserError("Error parsing color struct '%s'", val.c_str());\
return parserError("Error parsing color struct '" + val + "'");\
\
drawstep->struct_name.r = red; \
drawstep->struct_name.g = green; \
Expand Down Expand Up @@ -466,7 +466,7 @@ bool ThemeParser::parseDrawStep(ParserNode *stepNode, Graphics::DrawStep *drawst
else if (val == "right")
drawstep->extraData = Graphics::VectorRenderer::kTriangleRight;
else
return parserError("'%s' is not a valid value for triangle orientation.", val.c_str());
return parserError("'" + val + "' is not a valid value for triangle orientation.");
}
}

Expand Down Expand Up @@ -545,7 +545,7 @@ bool ThemeParser::parseDrawStep(ParserNode *stepNode, Graphics::DrawStep *drawst
else if (val == "gradient")
drawstep->fillMode = Graphics::VectorRenderer::kFillGradient;
else
return parserError("'%s' is not a valid fill mode for a shape.", stepNode->values["fill"].c_str());
return parserError("'" + stepNode->values["fill"] + "' is not a valid fill mode for a shape.");
}

#undef __PARSER_ASSIGN_INT
Expand All @@ -567,7 +567,7 @@ bool ThemeParser::parserCallback_def(ParserNode *node) {
value = _theme->getEvaluator()->getVar(node->values["value"]);

else if (!parseIntegerKey(node->values["value"], 1, &value))
return parserError("Invalid definition for '%s'.", var.c_str());
return parserError("Invalid definition for '" + var + "'.");

_theme->getEvaluator()->setVar(var, value);
return true;
Expand All @@ -585,7 +585,7 @@ bool ThemeParser::parserCallback_widget(ParserNode *node) {

var = "Globals." + node->values["name"] + ".";
if (!parseCommonLayoutProps(node, var))
return parserError("Error parsing Layout properties of '%s'.", var.c_str());
return parserError("Error parsing Layout properties of '" + var + "'.");

} else {
// FIXME: Shouldn't we distinguish the name/id and the label of a widget?
Expand All @@ -606,15 +606,15 @@ bool ThemeParser::parserCallback_widget(ParserNode *node) {
width = _theme->getEvaluator()->getVar(node->values["width"]);

else if (!parseIntegerKey(node->values["width"], 1, &width))
return parserError("Corrupted width value in key for %s", var.c_str());
return parserError("Corrupted width value in key for " + var);
}

if (node->values.contains("height")) {
if (_theme->getEvaluator()->hasVar(node->values["height"]) == true)
height = _theme->getEvaluator()->getVar(node->values["height"]);

else if (!parseIntegerKey(node->values["height"], 1, &height))
return parserError("Corrupted height value in key for %s", var.c_str());
return parserError("Corrupted height value in key for " + var);
}

Graphics::TextAlign alignH = Graphics::kTextAlignLeft;
Expand Down

0 comments on commit a6e5b97

Please sign in to comment.