diff --git a/projects/mtg/include/utils.h b/projects/mtg/include/utils.h index b862750ec..a1f1d3471 100644 --- a/projects/mtg/include/utils.h +++ b/projects/mtg/include/utils.h @@ -30,20 +30,59 @@ #include #include +// enable this define to collect statistics on how many times an ifstream is created for a given file. +//#define TRACK_FILE_USAGE_STATS + +namespace wagic +{ + +#ifdef TRACK_FILE_USAGE_STATS + class ifstream : public std::ifstream + { + public: + explicit ifstream(const char *inFilename, ios_base::openmode inMode = ios_base::in) : + std::ifstream(inFilename, inMode) + { + sFileMap[std::string(inFilename)] += 1; + } + + static void Dump() + { + DebugTrace("-------------------"); + DebugTrace("File Usage Statistics" << std::endl); + std::map::const_iterator iter = sFileMap.begin(); + for (; iter != sFileMap.end(); ++iter) + { + DebugTrace(iter->first << " -- " << iter->second); + } + + DebugTrace("End File Usage Statistics"); + DebugTrace("-------------------"); + } + + private: + static std::map sFileMap; + }; + +#else +typedef std::ifstream ifstream; +#endif + +} //namespace wagic using std::string; //string manipulation methods -string& trim(string &str); -string& ltrim(string &str); -string& rtrim(string &str); +string& trim(string& str); +string& ltrim(string& str); +string& rtrim(string& str); -std::string join(vector &v, string delim = " "); +std::string join(vector& v, string delim = " "); -std::vector &split(const std::string &s, char delim, std::vector &elems); -std::vector split(const std::string &s, char delim); //splits a string with "delim" and returns a vector of strings. -std::string wordWrap(std::string s, float width, int fontId); +std::vector& split(const std::string& s, char delim, std::vector& elems); +std::vector split(const std::string& s, char delim); //splits a string with "delim" and returns a vector of strings. +std::string wordWrap(const std::string& s, float width, int fontId); int loadRandValues(string s); int filesize(const char * filename); diff --git a/projects/mtg/src/AIPlayer.cpp b/projects/mtg/src/AIPlayer.cpp index fbe2bcb9c..e80684925 100644 --- a/projects/mtg/src/AIPlayer.cpp +++ b/projects/mtg/src/AIPlayer.cpp @@ -1117,7 +1117,7 @@ AIPlayer * AIPlayerFactory::createAIPlayer(MTGAllCards * collection, Player * op found = 0; char buffer[512]; sprintf(buffer, JGE_GET_RES("ai/baka/deck%i.txt").c_str(), nbdecks + 1); - std::ifstream file(buffer); + wagic::ifstream file(buffer); if (file) { found = 1; diff --git a/projects/mtg/src/AIStats.cpp b/projects/mtg/src/AIStats.cpp index 27ded1d03..2e15eea0b 100644 --- a/projects/mtg/src/AIStats.cpp +++ b/projects/mtg/src/AIStats.cpp @@ -143,7 +143,7 @@ AIStat * AIStats::find(MTGCard * source) void AIStats::load(char * filename) { - std::ifstream file(filename); + wagic::ifstream file(filename); std::string s; if (file) diff --git a/projects/mtg/src/Credits.cpp b/projects/mtg/src/Credits.cpp index 408dd3530..2ee93cb8f 100644 --- a/projects/mtg/src/Credits.cpp +++ b/projects/mtg/src/Credits.cpp @@ -448,7 +448,7 @@ int Credits::IsMoreAIDecksUnlocked(DeckStats * stats) { found = 0; char buffer[512]; sprintf(buffer, JGE_GET_RES("ai/baka/deck%i.txt").c_str(), nbdecks + 1); - std::ifstream file(buffer); + wagic::ifstream file(buffer); if (file) { found = 1; diff --git a/projects/mtg/src/DeckStats.cpp b/projects/mtg/src/DeckStats.cpp index 5e4bb9687..58e03605a 100644 --- a/projects/mtg/src/DeckStats.cpp +++ b/projects/mtg/src/DeckStats.cpp @@ -111,7 +111,7 @@ void DeckStats::load(Player * player) void DeckStats::load(const char * filename) { cleanStats(); - std::ifstream file(filename); + wagic::ifstream file(filename); std::string s; if (file) diff --git a/projects/mtg/src/GameApp.cpp b/projects/mtg/src/GameApp.cpp index a2c20b47c..acc657235 100644 --- a/projects/mtg/src/GameApp.cpp +++ b/projects/mtg/src/GameApp.cpp @@ -90,7 +90,7 @@ void GameApp::Create() LOG("starting Game"); //Find the Res folder - std::ifstream mfile("Res.txt"); + wagic::ifstream mfile("Res.txt"); string resPath; if (mfile) { @@ -122,14 +122,14 @@ void GameApp::Create() LOG("Checking for music files"); //Test for Music files presence string filepath = JGE_GET_RES(WResourceManager::Instance()->musicFile("Track0.mp3")); - std::ifstream file(filepath.c_str()); + wagic::ifstream file(filepath.c_str()); if (file) file.close(); else HasMusic = 0; filepath = JGE_GET_RES(WResourceManager::Instance()->musicFile("Track1.mp3")); - std::ifstream file2(filepath.c_str()); + wagic::ifstream file2(filepath.c_str()); if (file2) file2.close(); else @@ -281,6 +281,10 @@ void GameApp::Destroy() options.theGame = NULL; LOG("==Destroying GameApp Successful=="); +#ifdef TRACK_FILE_USAGE_STATS + wagic::ifstream::Dump(); +#endif + } void GameApp::Update() diff --git a/projects/mtg/src/GameOptions.cpp b/projects/mtg/src/GameOptions.cpp index c1c6dd256..9029b2ae1 100644 --- a/projects/mtg/src/GameOptions.cpp +++ b/projects/mtg/src/GameOptions.cpp @@ -316,7 +316,7 @@ GameOptions::GameOptions(string filename) int GameOptions::load() { - std::ifstream file(mFilename.c_str()); + wagic::ifstream file(mFilename.c_str()); std::string s; if (file) diff --git a/projects/mtg/src/GameStateDuel.cpp b/projects/mtg/src/GameStateDuel.cpp index 30caadd04..c82327d6a 100644 --- a/projects/mtg/src/GameStateDuel.cpp +++ b/projects/mtg/src/GameStateDuel.cpp @@ -266,7 +266,7 @@ void GameStateDuel::End() bool GameStateDuel::MusicExist(string FileName) { string filepath = JGE_GET_RES(WResourceManager::Instance()->musicFile(FileName)); - std::ifstream file(filepath.c_str()); + wagic::ifstream file(filepath.c_str()); if (file) { file.close(); diff --git a/projects/mtg/src/GameStateMenu.cpp b/projects/mtg/src/GameStateMenu.cpp index 3b5dbca38..4fa1d0e33 100644 --- a/projects/mtg/src/GameStateMenu.cpp +++ b/projects/mtg/src/GameStateMenu.cpp @@ -286,7 +286,7 @@ int GameStateMenu::nextDirectory(const char * root, const char * file) while (!found && (mDit = readdir(mDip))) { sprintf(mCurrentSetFileName, "%s/%s/%s", root, mDit->d_name, file); - std::ifstream file(mCurrentSetFileName); + wagic::ifstream file(mCurrentSetFileName); if (file) { sprintf(mCurrentSetName, "%s", mDit->d_name); @@ -313,7 +313,7 @@ string GameStateMenu::loadRandomWallpaper() return wallpaper; vector wallpapers; - std::ifstream file(JGE_GET_RES("graphics/wallpapers.txt").c_str()); + wagic::ifstream file(JGE_GET_RES("graphics/wallpapers.txt").c_str()); if (!file) return wallpaper; @@ -371,7 +371,7 @@ void GameStateMenu::loadLangMenu() { string filename = JGE_GET_RES("lang/"); filename += mDit->d_name; - std::ifstream file(filename.c_str()); + wagic::ifstream file(filename.c_str()); string s; string lang; if (file) @@ -407,7 +407,7 @@ void GameStateMenu::listPrimitives() { string filename = JGE_GET_RES("sets/primitives/"); filename += mDit->d_name; - std::ifstream file(filename.c_str()); + wagic::ifstream file(filename.c_str()); if (!file) continue; file.close(); @@ -500,7 +500,7 @@ void GameStateMenu::Update(float dt) mSplash = NULL; //check for deleted collection / first-timer - std::ifstream file(options.profileFile(PLAYER_COLLECTION).c_str()); + wagic::ifstream file(options.profileFile(PLAYER_COLLECTION).c_str()); if (file) { file.close(); diff --git a/projects/mtg/src/GameStateStory.cpp b/projects/mtg/src/GameStateStory.cpp index 374997721..bc4863191 100644 --- a/projects/mtg/src/GameStateStory.cpp +++ b/projects/mtg/src/GameStateStory.cpp @@ -31,7 +31,7 @@ void GameStateStory::loadStoriesMenu(const char * root) { char buffer[4096]; sprintf(buffer, "%s%s/story.xml", root, mDit->d_name); - std::ifstream file(buffer); + wagic::ifstream file(buffer); if (file) { string fname = mDit->d_name; diff --git a/projects/mtg/src/MTGDeck.cpp b/projects/mtg/src/MTGDeck.cpp index 952de33d4..c1c3cc1dd 100644 --- a/projects/mtg/src/MTGDeck.cpp +++ b/projects/mtg/src/MTGDeck.cpp @@ -301,7 +301,7 @@ int MTGAllCards::load(const char * config_file, const char * set_name, int autol const int set_id = set_name ? setlist.Add(set_name) : MTGSets::INTERNAL_SET; MTGSetInfo *si = setlist.getInfo(set_id); - std::ifstream setFile(config_file, ios::in | ios::ate); + wagic::ifstream setFile(config_file, ios::in | ios::ate); if (!setFile) return total_cards; streampos fileSize = setFile.tellg(); @@ -620,7 +620,7 @@ MTGDeck::MTGDeck(const char * config_file, MTGAllCards * _allcards, int meta_onl size_t slash = filename.find_last_of("/"); size_t dot = filename.find("."); meta_name = filename.substr(slash + 1, dot - slash - 1); - std::ifstream file(config_file); + wagic::ifstream file(config_file); std::string s; if (file) diff --git a/projects/mtg/src/OptionItem.cpp b/projects/mtg/src/OptionItem.cpp index 51254abbd..3f7b863ac 100644 --- a/projects/mtg/src/OptionItem.cpp +++ b/projects/mtg/src/OptionItem.cpp @@ -168,7 +168,7 @@ void OptionProfile::populate() PlayerData * pdata = NEW PlayerData(app->collection); int unlocked = 0, sets = setlist.size(); - std::ifstream file(options.profileFile(PLAYER_SETTINGS).c_str()); + wagic::ifstream file(options.profileFile(PLAYER_SETTINGS).c_str()); std::string s; if (file) { @@ -333,7 +333,7 @@ void OptionLanguage::Reload() { string filename = JGE_GET_RES("lang/"); filename += mDit->d_name; - std::ifstream file(filename.c_str()); + wagic::ifstream file(filename.c_str()); string s; string lang; if (file) @@ -412,7 +412,7 @@ void OptionDirectory::Reload() while ((mDit = readdir(mDip))) { sprintf(buf, "%s/%s/%s", root.c_str(), mDit->d_name, type.c_str()); - std::ifstream file(buf); + wagic::ifstream file(buf); if (!file) continue; file.close(); @@ -439,7 +439,7 @@ OptionDirectory::OptionDirectory(string root, int id, string displayValue, strin while ((mDit = readdir(mDip))) { sprintf(buf, "%s/%s/%s", root.c_str(), mDit->d_name, type.c_str()); - std::ifstream file(buf); + wagic::ifstream file(buf); if (!file) continue; file.close(); @@ -498,7 +498,7 @@ void OptionTheme::Render() sprintf(buf, "%s", JGE_GET_RES("graphics/themeinfo.txt").c_str()); else sprintf(buf, "%s%s", JGE_GET_RES("themes/%s/themeinfo.txt").c_str(), selections[value].c_str()); - std::ifstream file(buf); + wagic::ifstream file(buf); if (file) { string temp; diff --git a/projects/mtg/src/PlayerData.cpp b/projects/mtg/src/PlayerData.cpp index 393a45311..3733132a3 100644 --- a/projects/mtg/src/PlayerData.cpp +++ b/projects/mtg/src/PlayerData.cpp @@ -25,7 +25,7 @@ void PlayerData::init() //CREDITS credits = 3000; //Default value - std::ifstream file(options.profileFile(PLAYER_SAVEFILE).c_str()); + wagic::ifstream file(options.profileFile(PLAYER_SAVEFILE).c_str()); std::string s; if (file) { diff --git a/projects/mtg/src/PriceList.cpp b/projects/mtg/src/PriceList.cpp index 66223b837..eab8011c8 100644 --- a/projects/mtg/src/PriceList.cpp +++ b/projects/mtg/src/PriceList.cpp @@ -8,7 +8,7 @@ PriceList::PriceList(const char * _filename, MTGAllCards * _collection) : collection(_collection) { filename = _filename; - std::ifstream file(_filename); + wagic::ifstream file(_filename); std::string cardid; std::string price; if (file) diff --git a/projects/mtg/src/Rules.cpp b/projects/mtg/src/Rules.cpp index 0b084d3d0..f442f818d 100644 --- a/projects/mtg/src/Rules.cpp +++ b/projects/mtg/src/Rules.cpp @@ -487,7 +487,7 @@ int Rules::load(string _filename) { sprintf(filename, JGE_GET_RES("rules/%s").c_str(), _filename.c_str()); } - std::ifstream file(filename); + wagic::ifstream file(filename); std::string s; int state = PARSE_UNDEFINED; diff --git a/projects/mtg/src/Tasks.cpp b/projects/mtg/src/Tasks.cpp index bdf7850e8..a4cdb82d2 100644 --- a/projects/mtg/src/Tasks.cpp +++ b/projects/mtg/src/Tasks.cpp @@ -352,7 +352,7 @@ int TaskList::load(string _fileName) return -1; } - std::ifstream file(fileName.c_str()); + wagic::ifstream file(fileName.c_str()); std::string s; if (file) diff --git a/projects/mtg/src/TestSuiteAI.cpp b/projects/mtg/src/TestSuiteAI.cpp index 422ce140c..7a4517b9f 100644 --- a/projects/mtg/src/TestSuiteAI.cpp +++ b/projects/mtg/src/TestSuiteAI.cpp @@ -509,7 +509,7 @@ TestSuite::TestSuite(const char * filename, MTGAllCards* _collection) { collection = _collection; timerLimit = 0; - std::ifstream file(filename); + wagic::ifstream file(filename); std::string s; nbfiles = 0; currentfile = 0; @@ -617,7 +617,7 @@ int TestSuite::load(const char * _filename) gameType = GAME_TYPE_CLASSIC; char filename[4096]; sprintf(filename, JGE_GET_RES("/test/%s").c_str(), _filename); - std::ifstream file(filename); + wagic::ifstream file(filename); std::string s; loadRandValues(""); diff --git a/projects/mtg/src/Translate.cpp b/projects/mtg/src/Translate.cpp index 3503a9707..04fe7ea96 100644 --- a/projects/mtg/src/Translate.cpp +++ b/projects/mtg/src/Translate.cpp @@ -65,7 +65,7 @@ Translator::Translator() void Translator::load(string filename, map * dictionary) { - std::ifstream file(filename.c_str()); + wagic::ifstream file(filename.c_str()); if (file) { @@ -89,7 +89,7 @@ void Translator::load(string filename, map * dictionary) #if defined DEBUG_TRANSLATE if (!checkMisses) return; - std::ifstream file2(JGE_GET_RES("lang/dontcare.txt").c_str()); + wagic::ifstream file2(JGE_GET_RES("lang/dontcare.txt").c_str()); if(file2) { @@ -123,7 +123,7 @@ void Translator::initDecks() string decks_dict = JGE_GET_RES("lang/") + lang + "_decks.txt"; // Load file - std::ifstream file(decks_dict.c_str()); + wagic::ifstream file(decks_dict.c_str()); if (file) { string s; diff --git a/projects/mtg/src/WResourceManager.cpp b/projects/mtg/src/WResourceManager.cpp index 496c22c90..9ae4fd7bf 100644 --- a/projects/mtg/src/WResourceManager.cpp +++ b/projects/mtg/src/WResourceManager.cpp @@ -843,13 +843,13 @@ int WResourceManager::dirOK(const string& dirname) int WResourceManager::fileOK(const string& filename, bool relative) { - std::ifstream * fp = NULL; + wagic::ifstream * fp = NULL; if (relative) { - fp = NEW std::ifstream(JGE_GET_RES(filename).c_str()); + fp = NEW wagic::ifstream(JGE_GET_RES(filename).c_str()); } else - fp = NEW std::ifstream(filename.c_str()); + fp = NEW wagic::ifstream(filename.c_str()); int result = 0; if (fp) diff --git a/projects/mtg/src/utils.cpp b/projects/mtg/src/utils.cpp index ee3f9c4d7..93ef23b02 100644 --- a/projects/mtg/src/utils.cpp +++ b/projects/mtg/src/utils.cpp @@ -6,6 +6,13 @@ #include "WResourceManager.h" #include "WFont.h" +namespace wagic +{ +#ifdef TRACK_FILE_USAGE_STATS + std::map ifstream::sFileMap; +#endif +} + using std::vector; int randValuesCursor = -1; @@ -70,14 +77,14 @@ int filesize(const char * filename) int fileExists(const char * filename) { - std::ifstream fichier(filename); + wagic::ifstream fichier(filename); if (fichier) { fichier.close(); return 1; } - std::ifstream fichier2(JGE_GET_RES(filename).c_str()); + wagic::ifstream fichier2(JGE_GET_RES(filename).c_str()); if (fichier2) { fichier2.close(); @@ -190,26 +197,26 @@ u32 ramAvailable(void) return size; } -string& trim(string &str) +string& trim(string& str) { str = ltrim(str); str = rtrim(str); return str; } -string& ltrim(string &str) +string& ltrim(string& str) { str.erase(0, str.find_first_not_of(" \t")); return str; } -string& rtrim(string &str) +string& rtrim(string& str) { str.resize(str.find_last_not_of(" \t") + 1); return str; } -std::vector &split(const std::string &s, char delim, std::vector &elems) +std::vector& split(const std::string& s, char delim, std::vector& elems) { std::stringstream ss(s); std::string item; @@ -220,7 +227,7 @@ std::vector &split(const std::string &s, char delim, std::vector &v, string delim) +std::string join(vector& v, string delim) { std::string retVal; for ( vector::iterator it = v.begin(); it != v.end(); ++it ) @@ -232,7 +239,7 @@ std::string join( vector &v, string delim) return retVal; } -std::vector split(const std::string &s, char delim) +std::vector split(const std::string& s, char delim) { std::vector elems; return split(s, delim, elems); @@ -241,7 +248,7 @@ std::vector split(const std::string &s, char delim) // This is a customized word wrap based on pixel width. It tries it's best // to wrap strings using spaces as delimiters. // Not sure how this translates into non-english fonts. -std::string wordWrap(std::string sentence, float width, int fontId) +std::string wordWrap(const std::string& sentence, float width, int fontId) { WFont * mFont = WResourceManager::Instance()->GetWFont(fontId); float lineWidth = mFont->GetStringWidth( sentence.c_str() );