Permalink
Browse files

FileParser yields error messages instead of all the other classes.

  • Loading branch information...
stefanbeller committed Mar 31, 2013
1 parent 8d6b068 commit e0bb78b4b3c9611d42d5ba973d2ad822baed1bc1
View
@@ -66,10 +66,8 @@ void AnimationSet::load() {
FileParser parser;
const string filename = mods->locate(name);
- if (!parser.open(filename.c_str())) {
- cout << "Error loading animation definition file: " << name << endl;
+ if (!parser.open(filename, "Error loading animation definition: " + name))
return;
- }
string _name = "";
int position = 0;
View
@@ -171,7 +171,7 @@ void Avatar::loadLayerDefinitions() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/hero_options.txt!\n");
+ }
// There are the positions of the items relative to layer_reference_order
// so if layer_reference_order=main,body,head,off
View
@@ -54,7 +54,7 @@ CombatText::CombatText() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/combat_text.txt!\n");
+ }
}
void CombatText::setCam(Point location) {
@@ -77,7 +77,7 @@ void EnemyGroupManager::parseEnemyFileAndStore(const string& filename) {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open enemies/%s!\n", filename.c_str());
+ }
}
Enemy_Level EnemyGroupManager::getRandomEnemy(const std::string& category, int minlevel, int maxlevel) const {
View
@@ -33,10 +33,13 @@ FileParser::FileParser()
, val("")
{}
-bool FileParser::open(const string& filename) {
+bool FileParser::open(const string& filename, const string &errormessage) {
this->filename = filename;
infile.open(filename.c_str(), ios::in);
- return infile.is_open();
+ bool ret = infile.is_open();
+ if (!ret && !errormessage.empty())
+ fprintf(stderr, "%s: %s\n", errormessage.c_str(), filename.c_str());
+ return ret;
}
void FileParser::close() {
View
@@ -39,7 +39,16 @@ class FileParser {
FileParser();
~FileParser();
- bool open(const std::string& filename);
+ /**
+ * @brief open
+ * @param filename The file to be opened
+ * @param errormessage
+ * Optional parameter, will be printed to stderr together with the filename
+ * if an error occurs. If errormessage is empty, there will be no output to
+ * stderr in any case.
+ * @return true if file could be opened successfully for reading.
+ */
+ bool open(const std::string& filename, const std::string &errormessage = "Could not open text file");
void close();
bool next();
std::string nextValue(); // next value inside one line.
View
@@ -77,7 +77,7 @@ FontEngine::FontEngine()
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/font_settings.txt!\n");
+ }
// set the font colors
// RGB values, the last value is 'unused'. For info,
@@ -92,7 +92,7 @@ FontEngine::FontEngine()
color_map[infile.key] = color;
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/font_colors.txt!\n");
+ }
// Attempt to set the default active font
setFont("font_regular");
View
@@ -595,7 +595,7 @@ void GameStateConfig::readConfig () {
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/config.txt!\n");
+ }
// Load the MenuConfirm positions and alignments from menus/menus.txt
if (infile.open(mods->locate("menus/menus.txt"))) {
@@ -622,7 +622,7 @@ void GameStateConfig::readConfig () {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/menus.txt!\n");
+ }
defaults_confirm->window_area = menuConfirm_area;
defaults_confirm->alignment = menuConfirm_align;
@@ -1163,7 +1163,7 @@ bool GameStateConfig::getLanguagesList()
i += 1;
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/languages.txt!\n");
+ }
return true;
}
@@ -1177,7 +1177,7 @@ int GameStateConfig::getLanguagesNumber()
languages_num += 1;
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/languages.txt!\n");
+ }
return languages_num;
}
View
@@ -113,7 +113,7 @@ GameStateLoad::GameStateLoad() : GameState() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/gameload.txt!\n");
+ }
// Load the MenuConfirm positions and alignments from menus/menus.txt
if (infile.open(mods->locate("menus/menus.txt"))) {
@@ -140,7 +140,7 @@ GameStateLoad::GameStateLoad() : GameState() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/menus.txt!\n");
+ }
confirm->align();
confirm->update();
@@ -164,7 +164,7 @@ GameStateLoad::GameStateLoad() : GameState() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/hero_options.txt!\n");
+ }
if (!found_layer) fprintf(stderr, "Warning: Could not find layers for direction 6\n");
button_action->pos.x += (VIEW_W - FRAME_W)/2;
@@ -225,7 +225,7 @@ void GameStateLoad::readGameSlots() {
string GameStateLoad::getMapName(const string& map_filename) {
FileParser infile;
- if (!infile.open(mods->locate("maps/" + map_filename))) return "";
+ if (!infile.open(mods->locate("maps/" + map_filename), "")) return "";
string map_name = "";
while (map_name == "" && infile.next()) {
@@ -251,7 +251,7 @@ void GameStateLoad::readGameSlot(int slot) {
filename << GAME_PREFIX << "_";
filename << "save" << (slot+1) << ".txt";
- if (!infile.open(filename.str())) return;
+ if (!infile.open(filename.str(), "")) return;
while (infile.next()) {
View
@@ -121,7 +121,7 @@ GameStateNew::GameStateNew() : GameState() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/gamenew.txt!\n");
+ }
button_prev->pos.x += (VIEW_W - FRAME_W)/2;
button_prev->pos.y += (VIEW_H - FRAME_H)/2;
View
@@ -382,7 +382,6 @@ void GameStatePlay::loadTitles() {
}
infile.close();
}
- else fprintf(stderr, "Unable to open engine/titles.txt!\n");
}
void GameStatePlay::checkTitle() {
View
@@ -118,7 +118,7 @@ void GameSwitcher::loadFPS() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/fps.txt!\n");
+ }
// this is a dummy string used to approximate the fps position when aligned to the right
font->setFont("font_regular");
View
@@ -126,8 +126,8 @@ void InputState::loadKeyBindings() {
FileParser infile;
- if (!infile.open(PATH_CONF + FILE_KEYBINDINGS)) {
- if (!infile.open(mods->locate("engine/default_keybindings.txt").c_str())) {
+ if (!infile.open(PATH_CONF + FILE_KEYBINDINGS, "")) {
+ if (!infile.open(mods->locate("engine/default_keybindings.txt"), "")) {
saveKeyBindings();
return;
} else saveKeyBindings();
View
@@ -136,10 +136,8 @@ void ItemManager::loadAll() {
*/
void ItemManager::load(const string& filename) {
FileParser infile;
- if (!infile.open(filename)) {
- fprintf(stderr, "Unable to open %s!\n", filename.c_str());
+ if (!infile.open(filename))
return;
- }
int id = 0;
bool id_line = false;
@@ -293,7 +291,7 @@ void ItemManager::loadTypes(const string& filename) {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open %s!\n", filename.c_str());
+ }
}
string ItemManager::getItemType(std::string _type) {
@@ -307,10 +305,8 @@ string ItemManager::getItemType(std::string _type) {
void ItemManager::loadSets(const string& filename) {
FileParser infile;
- if (!infile.open(filename)) {
- fprintf(stderr, "Unable to open %s!\n", filename.c_str());
+ if (!infile.open(filename))
return;
- }
int id = 0;
bool id_line;
View
@@ -57,7 +57,7 @@ LootManager::LootManager(ItemManager *_items, MapRenderer *_map, StatBlock *_her
FileParser infile;
// load loot animation settings from engine config file
- if (infile.open(mods->locate("engine/loot.txt").c_str())) {
+ if (infile.open(mods->locate("engine/loot.txt"))) {
while (infile.next()) {
infile.val = infile.val + ',';
@@ -96,7 +96,7 @@ LootManager::LootManager(ItemManager *_items, MapRenderer *_map, StatBlock *_her
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/loot.txt!\n");
+ }
// reset current map loot
loot.clear();
View
@@ -135,10 +135,8 @@ int MapRenderer::load(string filename) {
show_tooltip = false;
- if (!infile.open(mods->locate("maps/" + filename))) {
- cerr << "Unable to open maps/" << filename << endl;
+ if (!infile.open(mods->locate("maps/" + filename)))
return 0;
- }
while (infile.next()) {
if (infile.new_section) {
View
@@ -168,7 +168,7 @@ void MenuActionBar::update() {
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/actionbar.txt!\n");
+ }
// screen areas occupied by the three main sections
numberArea.y = mouseArea.y = menuArea.y = window_area.y;
@@ -59,7 +59,7 @@ MenuActiveEffects::MenuActiveEffects(SDL_Surface *_icons) {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/activeeffects.txt!\n");
+ }
loadGraphics();
}
View
@@ -199,7 +199,7 @@ MenuCharacter::MenuCharacter(StatBlock *_stats) {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/character.txt!\n");
+ }
// stat list
statList = new WidgetListBox(STATLIST_COUNT-1+stats->vulnerable.size(), statlist_rows, mods->locate("images/menus/buttons/listbox_char.png"));
View
@@ -55,7 +55,7 @@ MenuEnemy::MenuEnemy() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/enemy.txt!\n");
+ }
loadGraphics();
enemy = NULL;
View
@@ -93,7 +93,7 @@ MenuInventory::MenuInventory(ItemManager *_items, StatBlock *_stats, PowerManage
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/inventory.txt!\n");
+ }
MAX_EQUIPPED = equipped_area.size();
MAX_CARRIED = carried_cols * carried_rows;
View
@@ -61,7 +61,7 @@ MenuLog::MenuLog() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/log.txt!\n");
+ }
// Store the amount of displayed log messages on each log, and the maximum.
for (int i=0; i<LOG_TYPE_COUNT; i++) {
View
@@ -182,7 +182,7 @@ MenuManager::MenuManager(PowerManager *_powers, StatBlock *_stats, CampaignManag
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/menus.txt!\n");
+ }
// Some menus need to be updated to apply their new dimensions
act->update();
View
@@ -55,7 +55,7 @@ MenuMiniMap::MenuMiniMap() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/minimap.txt!\n");
+ }
// label for map name
label = new WidgetLabel();
View
@@ -118,7 +118,7 @@ MenuNPCActions::MenuNPCActions()
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/npc.txt!\n");
+ }
}
void MenuNPCActions::update() {
View
@@ -142,7 +142,7 @@ MenuPowers::MenuPowers(StatBlock *_stats, PowerManager *_powers, SDL_Surface *_i
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/powers.txt!\n");
+ }
loadGraphics();
View
@@ -64,7 +64,7 @@ MenuStash::MenuStash(ItemManager *_items, StatBlock *_stats)
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/stash.txt!\n");
+ }
STASH_SLOTS = slots_cols * slots_rows;
}
View
@@ -72,7 +72,7 @@ MenuStatBar::MenuStatBar(std::string type) {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/%s.txt!\n", type.c_str());
+ }
loadGraphics(type);
View
@@ -102,7 +102,7 @@ MenuTalker::MenuTalker(MenuManager *_menu, CampaignManager *_camp) {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/talker.txt!\n");
+ }
color_normal = font->getColor("menu_normal");
}
View
@@ -74,7 +74,7 @@ MenuVendor::MenuVendor(ItemManager *_items, StatBlock *_stats)
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/vendor.txt!\n");
+ }
VENDOR_SLOTS = slots_cols * slots_rows;
}
Oops, something went wrong.

0 comments on commit e0bb78b

Please sign in to comment.