Changeset 7616 in orxonox.OLD for branches/qt_gui/src/lib/util/loading
- Timestamp:
- May 15, 2006, 2:41:51 PM (19 years ago)
- Location:
- branches/qt_gui/src/lib/util/loading
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/qt_gui/src/lib/util/loading/resource_manager.cc
r7611 r7616 86 86 bool ResourceManager::setDataDir(const std::string& dataDir) 87 87 { 88 std::string realDir = ResourceManager::homeDirCheck(dataDir); 89 if (isDir(realDir)) 90 { 91 if (dataDir[dataDir.size()-1] == '/' || dataDir[dataDir.size()-1] == '\\') 88 File dataDirectory(dataDir); 89 if (dataDirectory.isDirectory()) 90 { 91 this->dataDir = dataDirectory.name(); 92 93 if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\') 92 94 { 93 this->dataDir = realDir;94 }95 else96 {97 this->dataDir = realDir;98 95 this->dataDir += '/'; 99 96 } … … 102 99 else 103 100 { 104 PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", realDir.c_str(), this->dataDir.c_str());101 PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", dataDir.c_str(), this->dataDir.c_str()); 105 102 return false; 106 103 } … … 115 112 bool ResourceManager::tryDataDir(const std::string& dataDir) 116 113 { 117 std::string realDir = ResourceManager::homeDirCheck(dataDir); 118 if (isDir(realDir)) 119 { 120 if (dataDir[dataDir.size()-1] == '/' || dataDir[dataDir.size()-1] == '\\') 114 File dataDirectory(dataDir); 115 if (dataDirectory.isDirectory()) 116 { 117 this->dataDir = dataDirectory.name(); 118 119 if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\') 121 120 { 122 this->dataDir = realDir;123 }124 else125 {126 this->dataDir = realDir;127 121 this->dataDir += '/'; 128 122 } … … 139 133 bool ResourceManager::verifyDataDir(const std::string& fileInside) 140 134 { 141 bool retVal;142 if (! isDir(this->dataDir))143 { 144 PRINTF(1)(" %sis not a directory\n", this->dataDir.c_str());135 File dataDirectory(this->dataDir); 136 if (!dataDirectory.isDirectory()) 137 { 138 PRINTF(1)("'%s' is not a directory\n", this->dataDir.c_str()); 145 139 return false; 146 140 } 147 141 148 std::string testFile = this->dataDir + fileInside; 149 retVal = isFile(testFile); 150 return retVal; 142 File testFile(this->dataDir + fileInside); 143 return testFile.isFile(); 151 144 } 152 145 … … 160 153 bool ResourceManager::addImageDir(const std::string& imageDir) 161 154 { 162 std::string newDir; 163 if (imageDir[imageDir.size()-1] == '/' || imageDir[imageDir.size()-1] == '\\') 164 { 165 newDir = imageDir; 166 } 167 else 168 { 169 newDir = imageDir; 155 std::string newDir = imageDir; 156 if (imageDir[imageDir.size()-1] != '/' && imageDir[imageDir.size()-1] != '\\') 157 { 170 158 newDir += '/'; 171 159 } 172 160 // check if the param is a Directory 173 if ( isDir(newDir))161 if (File(newDir).isDirectory()) 174 162 { 175 163 // check if the Directory has been added before … … 380 368 tmpResource->param[0] = 1.0f; 381 369 382 if( ResourceManager::isFile(fullName))370 if(File(fullName).isFile()) 383 371 tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat()); 384 372 else … … 406 394 break; 407 395 case MD2: 408 if( ResourceManager::isFile(fullName))396 if(File(fullName).isFile()) 409 397 { 410 398 tmpResource->param[0] = param0; … … 424 412 tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE; 425 413 426 if( isFile(fullName))414 if(File(fullName).isFile()) 427 415 tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt()); 428 416 else … … 432 420 #ifndef NO_AUDIO 433 421 case WAV: 434 if( isFile(fullName))422 if(File(fullName).isFile()) 435 423 tmpResource->pointer = new OrxSound::SoundBuffer(fullName); 436 424 break; 437 425 case OGG: 438 if ( isFile(fullName))426 if (File(fullName).isFile()) 439 427 tmpResource->pointer = new OrxSound::OggPlayer(fullName); 440 428 break; … … 446 434 else 447 435 tmpResource->param[0] = GL_TEXTURE_2D; 448 if( isFile(fullName))436 if(File(fullName).isFile()) 449 437 { 450 438 PRINTF(4)("Image %s resides to %s\n", fileName, fullName); … … 457 445 { 458 446 std::string imgName = *imageDir + fileName; 459 if( isFile(imgName))447 if(File(imgName).isFile()) 460 448 { 461 449 PRINTF(4)("Image %s resides to %s\n", fileName, imgName); … … 471 459 #ifndef NO_SHADERS 472 460 case SHADER: 473 if( ResourceManager::isFile(fullName))461 if(File(fullName).isFile()) 474 462 { 475 463 if (param0 != MT_NULL) … … 477 465 MultiType param = param0; /// HACK 478 466 std::string secFullName = ResourceManager::getFullName(param.getCString()); 479 if ( ResourceManager::isFile(secFullName))467 if (File(secFullName).isFile()) 480 468 { 481 469 tmpResource->param[0] = secFullName; … … 749 737 750 738 /** 751 * @brief Checks if it is a Directory752 * @param directoryName the Directory to check for753 * @returns true if it is a directory/symlink false otherwise754 */755 bool ResourceManager::isDir(const std::string& directoryName)756 {757 std::string tmpDirName = directoryName;758 struct stat status;759 760 // checking for the termination of the string given. If there is a "/" at the end cut it away761 if (directoryName[directoryName.size()-1] == '/' ||762 directoryName[directoryName.size()-1] == '\\')763 {764 tmpDirName.erase(tmpDirName.size()-1);765 }766 767 if(!stat(tmpDirName.c_str(), &status))768 {769 if (status.st_mode & (S_IFDIR770 #ifndef __WIN32__771 | S_IFLNK772 #endif773 ))774 {775 return true;776 }777 else778 return false;779 }780 else781 return false;782 }783 784 /**785 * @brief Checks if the file is either a Regular file or a Symlink786 * @param fileName the File to check for787 * @returns true if it is a regular file/symlink, false otherwise788 */789 bool ResourceManager::isFile(const std::string& fileName)790 {791 if (fileName.empty())792 return false;793 std::string tmpFileName = ResourceManager::homeDirCheck(fileName);794 // actually checks the File795 struct stat status;796 if (!stat(tmpFileName.c_str(), &status))797 {798 if (status.st_mode & (S_IFREG799 #ifndef __WIN32__800 | S_IFLNK801 #endif802 ))803 {804 return true;805 }806 else807 return false;808 }809 else810 return false;811 }812 813 /**814 * @brief touches a File on the disk (thereby creating it)815 * @param fileName The file to touch816 */817 bool ResourceManager::touchFile(const std::string& fileName)818 {819 std::string tmpName = ResourceManager::homeDirCheck(fileName);820 if (tmpName.empty())821 return false;822 FILE* stream;823 if( (stream = fopen (tmpName.c_str(), "w")) == NULL)824 {825 PRINTF(1)("could not open %s fro writing\n", fileName.c_str());826 return false;827 }828 fclose(stream);829 }830 831 /**832 * @brief deletes a File from disk833 * @param fileName the File to delete834 */835 bool ResourceManager::deleteFile(const std::string& fileName)836 {837 std::string tmpName = ResourceManager::homeDirCheck(fileName);838 unlink(tmpName.c_str());839 }840 841 /**842 * @param name the Name of the file to check843 * @returns The name of the file, including the HomeDir844 */845 std::string ResourceManager::homeDirCheck(const std::string& name)846 {847 if (name.size() >= 2 && name[0] == '~' && name[1] == '/')848 {849 std::string homeDir;850 std::string newName = name.substr(1);851 #ifdef __WIN32__852 homeDir = getenv("USERPROFILE");853 #else854 homeDir = getenv("HOME");855 #endif856 return homeDir + newName;857 }858 else859 return name;860 }861 862 /**863 * @param name the relative name of the File/Directory.864 * @returns a new std::string with the name in abs-dir-format865 */866 std::string ResourceManager::getAbsDir(const std::string& name)867 {868 if (name.empty())869 return "";870 std::string retName = name;871 if (strncmp(name.c_str(), "/", 1))872 {873 if (name[0] == '.' && name[1] != '.')874 retName.erase(0);875 const std::string& absDir = File::cwd();876 retName = absDir + retName;877 }878 return retName;879 }880 881 882 /**883 739 * @param fileName the Name of the File to check 884 740 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist … … 891 747 892 748 std::string retName = ResourceManager::getInstance()->getDataDir() +fileName; 893 if ( ResourceManager::isFile(retName) || ResourceManager::isDir(retName))749 if (File(retName).isFile() || File(retName).isDirectory()) 894 750 return retName; 895 751 else … … 911 767 std::string checkFile = ResourceManager::getInstance()->getDataDir() + fileName; 912 768 913 if ( ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile))769 if (File(checkFile).exists()) 914 770 retVal = true; 915 771 else -
branches/qt_gui/src/lib/util/loading/resource_manager.h
r7611 r7616 21 21 22 22 #include "base_object.h" 23 #include "file.h" 24 23 25 #include "multi_type.h" 24 25 26 #include <vector> 26 27 … … 130 131 131 132 // utility functions for handling files in and around the data-directory 132 static bool isDir(const std::string& directory);133 static bool isFile(const std::string& fileName);134 static bool touchFile(const std::string& fileName);135 static bool deleteFile(const std::string& fileName);136 static std::string homeDirCheck(const std::string& fileName);137 133 static std::string getFullName(const std::string& fileName); 138 134 static bool isInDataDir(const std::string& fileName); 139 static std::string getAbsDir(const std::string& fileName);140 135 141 136 static const char* ResourceTypeToChar(ResourceType type);
Note: See TracChangeset
for help on using the changeset viewer.