Changeset 7661 in orxonox.OLD for trunk/src/lib/util/loading
- Timestamp:
- May 18, 2006, 12:55:34 AM (19 years ago)
- Location:
- trunk/src/lib/util/loading
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/loading/resource_manager.cc
r7460 r7661 17 17 18 18 #include "util/loading/resource_manager.h" 19 19 #include "file.h" 20 20 #include "substring.h" 21 21 #include "debug.h" … … 60 60 61 61 this->dataDir = "./"; 62 this->_cwd = "";63 62 this->tryDataDir("./data"); 64 63 } … … 87 86 bool ResourceManager::setDataDir(const std::string& dataDir) 88 87 { 89 std::string realDir = ResourceManager::homeDirCheck(dataDir); 90 if (isDir(realDir)) 91 { 92 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] != '\\') 93 94 { 94 this->dataDir = realDir;95 }96 else97 {98 this->dataDir = realDir;99 95 this->dataDir += '/'; 100 96 } … … 103 99 else 104 100 { 105 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()); 106 102 return false; 107 103 } … … 116 112 bool ResourceManager::tryDataDir(const std::string& dataDir) 117 113 { 118 std::string realDir = ResourceManager::homeDirCheck(dataDir); 119 if (isDir(realDir)) 120 { 121 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] != '\\') 122 120 { 123 this->dataDir = realDir;124 }125 else126 {127 this->dataDir = realDir;128 121 this->dataDir += '/'; 129 122 } … … 140 133 bool ResourceManager::verifyDataDir(const std::string& fileInside) 141 134 { 142 bool retVal;143 if (! isDir(this->dataDir))144 { 145 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()); 146 139 return false; 147 140 } 148 141 149 std::string testFile = this->dataDir + fileInside; 150 retVal = isFile(testFile); 151 return retVal; 142 File testFile(this->dataDir + fileInside); 143 return testFile.isFile(); 152 144 } 153 145 … … 161 153 bool ResourceManager::addImageDir(const std::string& imageDir) 162 154 { 163 std::string newDir; 164 if (imageDir[imageDir.size()-1] == '/' || imageDir[imageDir.size()-1] == '\\') 165 { 166 newDir = imageDir; 167 } 168 else 169 { 170 newDir = imageDir; 155 std::string newDir = imageDir; 156 if (imageDir[imageDir.size()-1] != '/' && imageDir[imageDir.size()-1] != '\\') 157 { 171 158 newDir += '/'; 172 159 } 173 160 // check if the param is a Directory 174 if ( isDir(newDir))161 if (File(newDir).isDirectory()) 175 162 { 176 163 // check if the Directory has been added before … … 381 368 tmpResource->param[0] = 1.0f; 382 369 383 if( ResourceManager::isFile(fullName))370 if(File(fullName).isFile()) 384 371 tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat()); 385 372 else … … 407 394 break; 408 395 case MD2: 409 if( ResourceManager::isFile(fullName))396 if(File(fullName).isFile()) 410 397 { 411 398 tmpResource->param[0] = param0; … … 425 412 tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE; 426 413 427 if( isFile(fullName))414 if(File(fullName).isFile()) 428 415 tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt()); 429 416 else … … 433 420 #ifndef NO_AUDIO 434 421 case WAV: 435 if( isFile(fullName))422 if(File(fullName).isFile()) 436 423 tmpResource->pointer = new OrxSound::SoundBuffer(fullName); 437 424 break; 438 425 case OGG: 439 if ( isFile(fullName))426 if (File(fullName).isFile()) 440 427 tmpResource->pointer = new OrxSound::OggPlayer(fullName); 441 428 break; … … 447 434 else 448 435 tmpResource->param[0] = GL_TEXTURE_2D; 449 if( isFile(fullName))436 if(File(fullName).isFile()) 450 437 { 451 438 PRINTF(4)("Image %s resides to %s\n", fileName, fullName); … … 458 445 { 459 446 std::string imgName = *imageDir + fileName; 460 if( isFile(imgName))447 if(File(imgName).isFile()) 461 448 { 462 449 PRINTF(4)("Image %s resides to %s\n", fileName, imgName); … … 472 459 #ifndef NO_SHADERS 473 460 case SHADER: 474 if( ResourceManager::isFile(fullName))461 if(File(fullName).isFile()) 475 462 { 476 463 if (param0 != MT_NULL) … … 478 465 MultiType param = param0; /// HACK 479 466 std::string secFullName = ResourceManager::getFullName(param.getCString()); 480 if ( ResourceManager::isFile(secFullName))467 if (File(secFullName).isFile()) 481 468 { 482 469 tmpResource->param[0] = secFullName; … … 750 737 751 738 /** 752 * @brief Checks if it is a Directory753 * @param directoryName the Directory to check for754 * @returns true if it is a directory/symlink false otherwise755 */756 bool ResourceManager::isDir(const std::string& directoryName)757 {758 std::string tmpDirName = directoryName;759 struct stat status;760 761 // checking for the termination of the string given. If there is a "/" at the end cut it away762 if (directoryName[directoryName.size()-1] == '/' ||763 directoryName[directoryName.size()-1] == '\\')764 {765 tmpDirName.erase(tmpDirName.size()-1);766 }767 768 if(!stat(tmpDirName.c_str(), &status))769 {770 if (status.st_mode & (S_IFDIR771 #ifndef __WIN32__772 | S_IFLNK773 #endif774 ))775 {776 return true;777 }778 else779 return false;780 }781 else782 return false;783 }784 785 /**786 * @brief Checks if the file is either a Regular file or a Symlink787 * @param fileName the File to check for788 * @returns true if it is a regular file/symlink, false otherwise789 */790 bool ResourceManager::isFile(const std::string& fileName)791 {792 if (fileName.empty())793 return false;794 std::string tmpFileName = ResourceManager::homeDirCheck(fileName);795 // actually checks the File796 struct stat status;797 if (!stat(tmpFileName.c_str(), &status))798 {799 if (status.st_mode & (S_IFREG800 #ifndef __WIN32__801 | S_IFLNK802 #endif803 ))804 {805 return true;806 }807 else808 return false;809 }810 else811 return false;812 }813 814 /**815 * @brief touches a File on the disk (thereby creating it)816 * @param fileName The file to touch817 */818 bool ResourceManager::touchFile(const std::string& fileName)819 {820 std::string tmpName = ResourceManager::homeDirCheck(fileName);821 if (tmpName.empty())822 return false;823 FILE* stream;824 if( (stream = fopen (tmpName.c_str(), "w")) == NULL)825 {826 PRINTF(1)("could not open %s fro writing\n", fileName.c_str());827 return false;828 }829 fclose(stream);830 }831 832 /**833 * @brief deletes a File from disk834 * @param fileName the File to delete835 */836 bool ResourceManager::deleteFile(const std::string& fileName)837 {838 std::string tmpName = ResourceManager::homeDirCheck(fileName);839 unlink(tmpName.c_str());840 }841 842 /**843 * @param name the Name of the file to check844 * @returns The name of the file, including the HomeDir845 */846 std::string ResourceManager::homeDirCheck(const std::string& name)847 {848 if (name.size() >= 2 && name[0] == '~' && name[1] == '/')849 {850 std::string homeDir;851 std::string newName = name.substr(1);852 #ifdef __WIN32__853 homeDir = getenv("USERPROFILE");854 #else855 homeDir = getenv("HOME");856 #endif857 return homeDir + newName;858 }859 else860 return name;861 }862 863 /**864 * @param name the relative name of the File/Directory.865 * @returns a new std::string with the name in abs-dir-format866 */867 std::string ResourceManager::getAbsDir(const std::string& name)868 {869 if (name.empty())870 return "";871 std::string retName = name;872 if (strncmp(name.c_str(), "/", 1))873 {874 if (name[0] == '.' && name[1] != '.')875 retName.erase(0);876 const std::string& absDir = ResourceManager::cwd();877 retName = absDir + retName;878 }879 return retName;880 }881 882 883 /**884 739 * @param fileName the Name of the File to check 885 740 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist … … 892 747 893 748 std::string retName = ResourceManager::getInstance()->getDataDir() +fileName; 894 if ( ResourceManager::isFile(retName) || ResourceManager::isDir(retName))749 if (File(retName).isFile() || File(retName).isDirectory()) 895 750 return retName; 896 751 else 897 752 return ""; 898 }899 900 #ifdef __unix__901 #include <unistd.h>902 #elif __WIN32__ || _MS_DOS_903 #include <dir.h>904 #else905 #include <direct.h> /* Visual C++ */906 #endif907 /**908 * @returns the Current Woring Directory909 */910 const std::string& ResourceManager::cwd()911 {912 if (ResourceManager::getInstance()->_cwd.empty())913 {914 char cwd[1024];915 char* errorCode = getcwd(cwd, 1024);916 if (errorCode == 0)917 return ResourceManager::getInstance()->_cwd;918 919 ResourceManager::getInstance()->_cwd = cwd;920 }921 return ResourceManager::getInstance()->_cwd;922 753 } 923 754 … … 936 767 std::string checkFile = ResourceManager::getInstance()->getDataDir() + fileName; 937 768 938 if ( ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile))769 if (File(checkFile).exists()) 939 770 retVal = true; 940 771 else -
trunk/src/lib/util/loading/resource_manager.h
r7225 r7661 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 static const std::string& cwd();141 135 142 136 static const char* ResourceTypeToChar(ResourceType type); … … 151 145 static ResourceManager* singletonRef; //!< singleton Reference 152 146 153 std::string _cwd; //!< The currend Working directory.154 147 std::string dataDir; //!< The Data Directory, where all relevant Data is stored. 155 148 std::vector<std::string> imageDirs; //!< A list of directories in which images are stored.
Note: See TracChangeset
for help on using the changeset viewer.