diff --git a/lib/library.cpp b/lib/library.cpp index 550f6568f..18d54cee7 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -26,6 +26,21 @@ #include #include +static tinyxml2::XMLError LoadFile(tinyxml2::XMLDocument &doc, const std::string &path) +{ + FILE *f = fopen(path.c_str(),"rb"); + if (!f) + return tinyxml2::XML_ERROR_FILE_NOT_FOUND; + + // is file ok? if "path" is a folder then reading from it will cause ferror() to return a non-zero value + fgetc(f); + int errorcode = ferror(f); + fclose(f); + + // if file is ok, try to load it + return (errorcode == 0) ? doc.LoadFile(path.c_str()) : tinyxml2::XML_ERROR_FILE_NOT_FOUND; +} + Library::Library() : allocid(0) { } @@ -47,13 +62,13 @@ bool Library::load(const char exename[], const char path[]) // open file.. tinyxml2::XMLDocument doc; - tinyxml2::XMLError error = doc.LoadFile(path); + tinyxml2::XMLError error = LoadFile(doc,path); if (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND) { // failed to open file.. is there no extension? std::string fullfilename(path); if (Path::getFilenameExtension(fullfilename) == "") { fullfilename += ".cfg"; - error = doc.LoadFile(fullfilename.c_str()); + error = LoadFile(doc,fullfilename); } if (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND) { @@ -65,7 +80,7 @@ bool Library::load(const char exename[], const char path[]) #endif const char *sep = (!cfgfolder.empty() && cfgfolder[cfgfolder.size()-1U]=='/' ? "" : "/"); const std::string filename(cfgfolder + sep + fullfilename); - error = doc.LoadFile(filename.c_str()); + error = LoadFile(doc,filename); } }