Skip to content
Browse files

FileParser yields error messages instead of all the other classes.

  • Loading branch information...
1 parent 8d6b068 commit e0bb78b4b3c9611d42d5ba973d2ad822baed1bc1 @stefanbeller stefanbeller committed Mar 31, 2013
View
4 src/AnimationSet.cpp
@@ -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
2 src/Avatar.cpp
@@ -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
2 src/CombatText.cpp
@@ -54,7 +54,7 @@ CombatText::CombatText() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/combat_text.txt!\n");
+ }
}
void CombatText::setCam(Point location) {
View
2 src/EnemyGroupManager.cpp
@@ -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
7 src/FileParser.cpp
@@ -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
11 src/FileParser.h
@@ -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
4 src/FontEngine.cpp
@@ -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
8 src/GameStateConfig.cpp
@@ -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
10 src/GameStateLoad.cpp
@@ -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
2 src/GameStateNew.cpp
@@ -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
1 src/GameStatePlay.cpp
@@ -382,7 +382,6 @@ void GameStatePlay::loadTitles() {
}
infile.close();
}
- else fprintf(stderr, "Unable to open engine/titles.txt!\n");
}
void GameStatePlay::checkTitle() {
View
2 src/GameSwitcher.cpp
@@ -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
4 src/InputState.cpp
@@ -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
10 src/ItemManager.cpp
@@ -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
4 src/LootManager.cpp
@@ -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
4 src/MapRenderer.cpp
@@ -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
2 src/MenuActionBar.cpp
@@ -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;
View
2 src/MenuActiveEffects.cpp
@@ -59,7 +59,7 @@ MenuActiveEffects::MenuActiveEffects(SDL_Surface *_icons) {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/activeeffects.txt!\n");
+ }
loadGraphics();
}
View
2 src/MenuCharacter.cpp
@@ -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
2 src/MenuEnemy.cpp
@@ -55,7 +55,7 @@ MenuEnemy::MenuEnemy() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/enemy.txt!\n");
+ }
loadGraphics();
enemy = NULL;
View
2 src/MenuInventory.cpp
@@ -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
2 src/MenuLog.cpp
@@ -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
2 src/MenuManager.cpp
@@ -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
2 src/MenuMiniMap.cpp
@@ -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
2 src/MenuNPCActions.cpp
@@ -118,7 +118,7 @@ MenuNPCActions::MenuNPCActions()
}
}
infile.close();
- } else fprintf(stderr, "Unable to open menus/npc.txt!\n");
+ }
}
void MenuNPCActions::update() {
View
2 src/MenuPowers.cpp
@@ -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
2 src/MenuStash.cpp
@@ -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
2 src/MenuStatBar.cpp
@@ -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
2 src/MenuTalker.cpp
@@ -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
2 src/MenuVendor.cpp
@@ -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;
}
View
2 src/NPC.cpp
@@ -176,7 +176,7 @@ void NPC::load(const string& npc_id, int hero_level) {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open npcs/%s.txt!\n", npc_id.c_str());
+ }
loadGraphics(filename_portrait);
}
View
2 src/NPCManager.cpp
@@ -52,7 +52,7 @@ NPCManager::NPCManager(MapRenderer *_map, LootManager *_loot, ItemManager *_item
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/tooltips.txt!\n");
+ }
}
void NPCManager::addRenders(std::vector<Renderable> &r) {
View
4 src/PowerManager.cpp
@@ -82,10 +82,8 @@ void PowerManager::loadAll() {
*/
void PowerManager::loadPowers(const std::string& filename) {
FileParser infile;
- if (!infile.open(filename)) {
- fprintf(stderr, "Unable to open %s!\n", filename.c_str());
+ if (!infile.open(filename))
return;
- }
int input_id = 0;
bool skippingEntry = false;
View
4 src/QuestLog.cpp
@@ -102,10 +102,8 @@ void QuestLog::loadIndex(const std::string& filename) {
*/
void QuestLog::load(const std::string& filename) {
FileParser infile;
- if (!infile.open(mods->locate("quests/" + filename))) {
- fprintf(stderr, "Unable to open quests/%s!\n", filename.c_str());
+ if (!infile.open(mods->locate("quests/" + filename)))
return;
- }
while (infile.next()) {
if (infile.new_section) {
View
32 src/Settings.cpp
@@ -325,7 +325,7 @@ static ConfigEntry * getConfigEntry(const std::string & name) {
void loadTilesetSettings() {
FileParser infile;
// load tileset settings from engine config
- if (infile.open(mods->locate("engine/tileset_config.txt").c_str())) {
+ if (infile.open(mods->locate("engine/tileset_config.txt"), "Unable to open engine/tileset_config.txt! Defaulting to 64x32 isometric tiles.\n")) {
while (infile.next()) {
if (infile.key == "units_per_tile") {
UNITS_PER_TILE = toInt(infile.val);
@@ -344,7 +344,7 @@ void loadTilesetSettings() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/tileset_config.txt! Defaulting to 64x32 isometric tiles.\n");
+ }
// Init automatically calculated parameters
TILE_SHIFT = log2(UNITS_PER_TILE);
@@ -364,7 +364,7 @@ void loadMiscSettings() {
FileParser infile;
// load miscellaneous settings from engine config
// misc.txt
- if (infile.open(mods->locate("engine/misc.txt").c_str())) {
+ if (infile.open(mods->locate("engine/misc.txt"))) {
while (infile.next()) {
if (infile.key == "save_hpmp") {
if (toInt(infile.val) == 1)
@@ -388,9 +388,9 @@ void loadMiscSettings() {
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/misc.txt!\n");
+ }
// resolutions.txt
- if (infile.open(mods->locate("engine/resolutions.txt").c_str())) {
+ if (infile.open(mods->locate("engine/resolutions.txt"))) {
while (infile.next()) {
if (infile.key == "menu_frame_width")
FRAME_W = toInt(infile.val);
@@ -409,9 +409,9 @@ void loadMiscSettings() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/resolutions.txt!\n");
+ }
// gameplay.txt
- if (infile.open(mods->locate("engine/gameplay.txt").c_str())) {
+ if (infile.open(mods->locate("engine/gameplay.txt"))) {
while (infile.next()) {
if (infile.key == "enable_playgame") {
if (toInt(infile.val) == 1)
@@ -421,9 +421,9 @@ void loadMiscSettings() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/gameplay.txt!\n");
+ }
// combat.txt
- if (infile.open(mods->locate("engine/combat.txt").c_str())) {
+ if (infile.open(mods->locate("engine/combat.txt"))) {
while (infile.next()) {
if (infile.key == "max_absorb_percent") {
MAX_ABSORB = toInt(infile.val);
@@ -436,9 +436,9 @@ void loadMiscSettings() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/combat.txt!\n");
+ }
// elements.txt
- if (infile.open(mods->locate("engine/elements.txt").c_str())) {
+ if (infile.open(mods->locate("engine/elements.txt"))) {
Element e;
ELEMENTS.clear();
while (infile.next()) {
@@ -451,9 +451,9 @@ void loadMiscSettings() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/elements.txt!\n");
+ }
// classes.txt
- if (infile.open(mods->locate("engine/classes.txt").c_str())) {
+ if (infile.open(mods->locate("engine/classes.txt"))) {
HeroClass c;
HERO_CLASSES.clear();
while (infile.next()) {
@@ -492,7 +492,7 @@ void loadMiscSettings() {
}
}
infile.close();
- } else fprintf(stderr, "Unable to open engine/classes.txt!\n");
+ }
// Make a default hero class if none were found
if (HERO_CLASSES.empty()) {
@@ -513,8 +513,8 @@ bool loadSettings() {
// try read from file
FileParser infile;
- if (!infile.open(PATH_CONF + FILE_SETTINGS)) {
- if (!infile.open(mods->locate("engine/default_settings.txt").c_str())) {
+ if (!infile.open(PATH_CONF + FILE_SETTINGS, "")) {
+ if (!infile.open(mods->locate("engine/default_settings.txt"), "")) {
saveSettings();
return true;
} else saveSettings();
View
13 src/StatBlock.cpp
@@ -217,10 +217,8 @@ bool sortLoot(const EnemyLoot &a, const EnemyLoot &b) {
*/
void StatBlock::load(const string& filename) {
FileParser infile;
- if (!infile.open(mods->locate(filename))) {
- fprintf(stderr, "Unable to open %s!\n", filename.c_str());
+ if (!infile.open(mods->locate(filename)))
return;
- }
int num = 0;
string loot_token;
@@ -579,10 +577,8 @@ bool StatBlock::canUsePower(const Power &power, unsigned powerid) const {
void StatBlock::loadHeroStats() {
// Redefine numbers from config file if present
FileParser infile;
- if (!infile.open(mods->locate("engine/stats.txt"))) {
- fprintf(stderr, "Unable to open engine/stats.txt!\n");
+ if (!infile.open(mods->locate("engine/stats.txt")))
return;
- }
while (infile.next()) {
int value = toInt(infile.val);
@@ -672,10 +668,9 @@ void StatBlock::loadHeroStats() {
statsLoaded = true;
// Load the XP table as well
- if (!infile.open(mods->locate("engine/xp_table.txt"))) {
- fprintf(stderr, "Unable to open engine/xp_table.txt!\n");
+ if (!infile.open(mods->locate("engine/xp_table.txt")))
return;
- }
+
while(infile.next()) {
xp_table[toInt(infile.key) - 1] = toInt(infile.val);
}
View
2 src/TileSet.cpp
@@ -128,7 +128,7 @@ void TileSet::load(const std::string& filename) {
}
infile.close();
loadGraphics(img);
- } else fprintf(stderr, "Unable to open tilesetdefs/%s!\n", filename.c_str());
+ }
current_map = filename;
}
View
4 src/WidgetTooltip.cpp
@@ -34,7 +34,7 @@ WidgetTooltip::WidgetTooltip() {
FileParser infile;
// load tooltip settings from engine config file
- if (infile.open(mods->locate("engine/tooltips.txt").c_str())) {
+ if (infile.open(mods->locate("engine/tooltips.txt"))) {
while (infile.next()) {
if (infile.key == "tooltip_offset")
offset = toInt(infile.val);
@@ -45,8 +45,6 @@ WidgetTooltip::WidgetTooltip() {
}
infile.close();
}
- else fprintf(stderr, "Unable to open engine/tooltips.txt!\n");
-
}
/**

0 comments on commit e0bb78b

Please sign in to comment.
Something went wrong with that request. Please try again.