Changeset 7221 in orxonox.OLD for trunk/src/lib/util/loading
- Timestamp:
- Mar 15, 2006, 3:10:45 PM (19 years ago)
- Location:
- trunk/src/lib/util/loading
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/loading/factory.cc
r7193 r7221 23 23 //SHELL_COMMAND(create, Factory, fabricate); 24 24 25 26 /* -------------------------------------------------- 27 * Factory28 * --------------------------------------------------25 /** 26 * @brief constructor 27 * 28 * set everything to zero and define factoryName 29 29 */ 30 31 /** 32 * constructor 33 34 set everything to zero and define factoryName 35 */ 36 Factory::Factory (const char* factoryName, ClassID classID) 30 Factory::Factory (const std::string& factoryName, ClassID classID) 31 : className(factoryName), classID(classID) 37 32 { 38 33 this->setClassID(CL_FACTORY, "Factory"); 39 34 this->setName(factoryName); 40 41 this->classID = classID;42 this->className = factoryName;43 35 44 36 if( Factory::factoryList == NULL) … … 48 40 } 49 41 50 /** a reference to the First Factory */42 /** @brief a reference to the First Factory */ 51 43 std::list<Factory*>* Factory::factoryList = NULL; 52 44 53 45 /** 54 * destructor 55 * 56 * clear the Q 46 * @brief destructor 57 47 */ 58 48 Factory::~Factory () … … 64 54 65 55 /** 66 * deletes all the Factories. (cleanup)56 * @brief deletes all the Factories. (cleanup) 67 57 */ 68 58 void Factory::deleteFactories() … … 90 80 91 81 /** 92 * Compares the Factories Name against a given ClassName82 * @brief Compares the Factories Name against a given ClassName 93 83 * @param className the Name of the Class to Query 94 84 * @returns true on match, false otherwise. … … 96 86 bool Factory::operator==(const char* className) const 97 87 { 98 return(className != NULL && !strcmp(className, this->className)); 88 return(className != NULL && this->className == className); 89 } 90 91 /** 92 * @brief Compares the Factories Name against a given ClassName 93 * @param className the Name of the Class to Query 94 * @returns true on match, false otherwise. 95 */ 96 bool Factory::operator==(const std::string& className) const 97 { 98 return(this->className == className); 99 99 } 100 100 101 101 102 102 /** 103 * Creates a new Object of type root->Value() (name)103 * @brief Creates a new Object of type root->Value() (name) 104 104 * @param root the XML-Root to match for the newly created Object 105 105 * @returns a new Object of Type root->Value() on match, NULL otherwise … … 133 133 * @returns a new Object of Type className on match, NULL otherwise 134 134 */ 135 BaseObject* Factory::fabricate(const char*className)135 BaseObject* Factory::fabricate(const std::string& className) 136 136 { 137 if ( className == NULL ||Factory::factoryList == NULL)137 if (Factory::factoryList == NULL) 138 138 return NULL; 139 139 … … 178 178 else 179 179 { 180 PRINTF(2)("Could not Fabricate an Object of ClassID ' %h'\n", classID);180 PRINTF(2)("Could not Fabricate an Object of ClassID '0x%h'\n", classID); 181 181 return NULL; 182 182 } -
trunk/src/lib/util/loading/factory.h
r7167 r7221 27 27 #include "parser/tinyxml/tinyxml.h" 28 28 #include "base_object.h" 29 #include "debug.h"30 29 #include <vector> 31 30 #include <list> … … 34 33 * Creates a factory to a Loadable Class. 35 34 * this should be used at the beginning of all the Classes that should be loadable (in the cc-file) 36 */35 */ 37 36 #define CREATE_FACTORY(CLASS_NAME, CLASS_ID) \ 38 37 tFactory<CLASS_NAME>* global_##CLASS_NAME##_Factory = new tFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID) … … 49 48 static void deleteFactories(); 50 49 51 static BaseObject* fabricate(const char*className);50 static BaseObject* fabricate(const std::string& className); 52 51 static BaseObject* fabricate(ClassID classID); 53 52 static BaseObject* fabricate(const TiXmlElement* root = NULL); … … 56 55 bool operator==(ClassID classID) const; 57 56 bool operator==(const char* className) const; 57 bool operator==(const std::string& className) const; 58 58 59 59 protected: 60 Factory (const char*factoryName, ClassID classID);60 Factory (const std::string& factoryName, ClassID classID); 61 61 virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0; 62 62 63 63 protected: 64 ClassIDclassID; //!< The Class-Identifyer of the Factory.65 const char*className; //!< The name of the Class.64 const ClassID classID; //!< The Class-Identifyer of the Factory. 65 const std::string className; //!< The name of the Class. 66 66 static std::list<Factory*>* factoryList; //!< List of Registered Factories 67 67 }; 68 68 69 69 /** 70 * a factory that is able to load any kind of Object70 * @brief a factory that is able to load any kind of Object 71 71 * (this is a Functor) 72 72 */ … … 75 75 public: 76 76 /** 77 * creates a new type Factory to enable the loading of T77 * @brief creates a new type Factory to enable the loading of T 78 78 * @param factoryName the Name of the Factory to load. 79 79 * @param classID the ID of the Class to be created. … … 85 85 private: 86 86 /** 87 * fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)87 * @brief fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*) 88 88 * @param root the TiXmlElement T should load parameters from. 89 89 * @return the newly fabricated T. -
trunk/src/lib/util/loading/game_loader.cc
r7193 r7221 86 86 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns 87 87 */ 88 ErrorMessage GameLoader::loadCampaign(const char*fileName)88 ErrorMessage GameLoader::loadCampaign(const std::string& fileName) 89 89 { 90 90 ErrorMessage errorCode; 91 char*campaignName = ResourceManager::getFullName(fileName);92 if ( campaignName)91 std::string campaignName = ResourceManager::getFullName(fileName); 92 if (!campaignName.empty()) 93 93 { 94 94 this->currentCampaign = this->fileToCampaign(campaignName); 95 delete[] campaignName;96 95 } 97 96 } … … 105 104 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns 106 105 */ 107 ErrorMessage GameLoader::loadNetworkCampaign(const char*fileName)106 ErrorMessage GameLoader::loadNetworkCampaign(const std::string& fileName) 108 107 { 109 108 ErrorMessage errorCode; 110 char*campaignName = ResourceManager::getFullName(fileName);111 if ( campaignName)109 std::string campaignName = ResourceManager::getFullName(fileName); 110 if (!campaignName.empty()) 112 111 { 113 112 this->currentCampaign = this->fileToCampaign(campaignName); 114 delete[] campaignName;115 113 } 116 114 } … … 220 218 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns 221 219 */ 222 Campaign* GameLoader::fileToCampaign(const char*fileName)220 Campaign* GameLoader::fileToCampaign(const std::string& fileName) 223 221 { 224 222 /* do not entirely load the campaign. just the current world … … 227 225 */ 228 226 229 if( fileName == NULL)227 if( fileName.empty()) 230 228 { 231 229 PRINTF(2)("No filename specified for loading"); … … 233 231 } 234 232 235 TiXmlDocument * XMLDoc = new TiXmlDocument(fileName);233 TiXmlDocument XMLDoc(fileName); 236 234 // load the campaign document 237 if( !XMLDoc ->LoadFile())235 if( !XMLDoc.LoadFile(fileName)) 238 236 { 239 237 // report an error 240 PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName, XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); 241 delete XMLDoc; 238 PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName.c_str(), XMLDoc.ErrorDesc(), XMLDoc.ErrorRow(), XMLDoc.ErrorCol()); 242 239 return NULL; 243 240 } 244 241 245 242 // check basic validity 246 TiXmlElement* root = XMLDoc ->RootElement();243 TiXmlElement* root = XMLDoc.RootElement(); 247 244 assert( root != NULL); 248 245 … … 251 248 // report an error 252 249 PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n"); 253 delete XMLDoc;254 250 return NULL; 255 251 } 256 252 257 253 // construct campaign 258 Campaign* c = new Campaign( root); 259 260 // free the XML data 261 delete XMLDoc; 262 263 return c; 254 return new Campaign( root); 264 255 } 265 256 -
trunk/src/lib/util/loading/game_loader.h
r6981 r7221 44 44 static GameLoader* getInstance() { if(singletonRef == NULL) singletonRef = new GameLoader(); return singletonRef; } 45 45 46 ErrorMessage loadCampaign(const char*name);46 ErrorMessage loadCampaign(const std::string& name); 47 47 ErrorMessage loadDebugCampaign(Uint32 campaignID); 48 ErrorMessage loadNetworkCampaign(const char*fileName);48 ErrorMessage loadNetworkCampaign(const std::string& fileName); 49 49 50 50 ErrorMessage init(); … … 62 62 GameLoader (); 63 63 64 Campaign* fileToCampaign(const char*name);64 Campaign* fileToCampaign(const std::string& name); 65 65 66 66 -
trunk/src/lib/util/loading/load_param.cc
r7201 r7221 28 28 * @param executor the Executor, that executes the loading procedure. 29 29 */ 30 CLoadParam::CLoadParam(const TiXmlElement* root, const char*paramName, BaseObject* object, const Executor& executor, bool inLoadCycle)31 { 32 this->paramName = paramName; 30 CLoadParam::CLoadParam(const TiXmlElement* root, const std::string& paramName, BaseObject* object, const Executor& executor, bool inLoadCycle) 31 : paramName(paramName), object(object) 32 { 33 33 this->object = object; 34 34 this->inLoadCycle = inLoadCycle; … … 37 37 if (likely(!inLoadCycle)) 38 38 this->loadElem = grabParameterElement(root, paramName); 39 //this->loadString = grabParameter(root, paramName); 40 else if (!strcmp(root->Value(), paramName)) 39 else if (paramName == root->Value()) 41 40 this->loadElem = (TiXmlElement*)root->FirstChild(); 42 41 else … … 46 45 this->executor = executor.clone(); 47 46 48 if (this->executor)49 this->executor->setName(paramName);47 //if (this->executor) 48 // this->executor->setName(paramName); 50 49 } 51 50 … … 60 59 if (likely(this->executor != NULL)) 61 60 { 61 std::string loadString = ""; 62 62 if (this->loadElem != NULL && this->loadElem->ToText()) 63 this->loadString = this->loadElem->Value(); 64 else 65 this->loadString = NULL; 63 loadString = this->loadElem->Value(); 66 64 if (likely(this->object != NULL) && 67 ( this->loadString != NULL||65 ( !loadString.empty() || 68 66 ((this->executor->getType() & Executor_NoLoadString) == Executor_NoLoadString))) 69 67 { 70 PRINTF(4)("Loading '%s' with Parameters '%s' onto: '%s'(%s)\n", this->paramName, this->loadString, this->object->getName(), this->object->getClassName());71 this->executor->execute(this->object, this->loadString);68 PRINTF(4)("Loading value '%s' with Parameters '%s' onto: %s::%s\n", this->paramName.c_str(), loadString.c_str(), this->object->getClassName(), this->object->getName()); 69 this->executor->execute(this->object, loadString); 72 70 } 73 71 delete this->executor; … … 99 97 * @returns a pointer to itself. 100 98 */ 101 CLoadParam& CLoadParam::describe(const char*descriptionText)102 { 103 if (LoadClassDescription::parametersDescription && this->paramDesc && !this->paramDesc->getDescription())99 CLoadParam& CLoadParam::describe(const std::string& descriptionText) 100 { 101 if (LoadClassDescription::parametersDescription && this->paramDesc && this->paramDesc->getDescription().empty()) 104 102 { 105 103 this->paramDesc->setDescription(descriptionText); … … 230 228 * @returns the Value of the parameter if found, NULL otherwise 231 229 */ 232 const char* grabParameter(const TiXmlElement* root, const char*parameterName)230 std::string grabParameter(const TiXmlElement* root, const std::string& parameterName) 233 231 { 234 232 const TiXmlElement* element; 235 233 const TiXmlNode* node; 236 234 237 if (root == NULL || parameterName == NULL) 238 return NULL; 239 assert( parameterName != NULL); 235 if (root == NULL) 236 return ""; 240 237 241 238 element = root->FirstChildElement( parameterName); 242 if( element == NULL) return NULL;239 if( element == NULL) return ""; 243 240 244 241 node = element->FirstChild(); … … 248 245 node = node->NextSibling(); 249 246 } 250 return NULL;247 return ""; 251 248 } 252 249 … … 256 253 * @returns the Element of the parameter if found, NULL otherwise 257 254 */ 258 const TiXmlElement* grabParameterElement(const TiXmlElement* root, const char*parameterName)255 const TiXmlElement* grabParameterElement(const TiXmlElement* root, const std::string& parameterName) 259 256 { 260 257 const TiXmlElement* element; 261 258 const TiXmlNode* node; 262 259 263 if (root == NULL || parameterName == NULL)260 if (root == NULL) 264 261 return NULL; 265 assert( parameterName != NULL);266 262 267 263 element = root->FirstChildElement( parameterName); -
trunk/src/lib/util/loading/load_param.h
r7198 r7221 87 87 { 88 88 public: 89 CLoadParam(const TiXmlElement* root, const char*paramName, BaseObject* object, const Executor& executor, bool inLoadCycle = false);89 CLoadParam(const TiXmlElement* root, const std::string& paramName, BaseObject* object, const Executor& executor, bool inLoadCycle = false); 90 90 virtual ~CLoadParam(); 91 91 92 CLoadParam& describe(const char*descriptionText);92 CLoadParam& describe(const std::string& descriptionText); 93 93 CLoadParam& defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL, 94 94 const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL, 95 95 const MultiType& value4 = MT_NULL); 96 CLoadParam& attribute(const char*attributeName, const Executor& executor);96 CLoadParam& attribute(const std::string& attributeName, const Executor& executor); 97 97 98 98 … … 101 101 Executor* executor; 102 102 BaseObject* object; 103 const char*paramName;103 const std::string paramName; 104 104 105 105 LoadClassDescription* classDesc; //!< The LoadClassDescription of this CLoadParameter 106 106 LoadParamDescription* paramDesc; //!< The LoadParameterDescription of this LoadParameter 107 107 const TiXmlElement* loadElem; //!< The Element to load. 108 const char* loadString; //!< The string loaded by this LoadParam109 108 const void* pointerToParam; //!< A Pointer to a Parameter. 110 109 … … 114 113 // helper function 115 114 116 const char* grabParameter(const TiXmlElement* root, const char*parameterName);117 const TiXmlElement* grabParameterElement(const TiXmlElement* root, const char*parameterName);115 std::string grabParameter(const TiXmlElement* root, const std::string& parameterName); 116 const TiXmlElement* grabParameterElement(const TiXmlElement* root, const std::string& parameterName); 118 117 119 118 #endif /* _LOAD_PARAM_H */ -
trunk/src/lib/util/loading/load_param_description.cc
r7130 r7221 22 22 * @param paramName the name of the parameter to load 23 23 */ 24 LoadParamDescription::LoadParamDescription(const char*paramName)24 LoadParamDescription::LoadParamDescription(const std::string& paramName) 25 25 { 26 26 this->types = NULL; 27 this->description = NULL;28 27 this->defaultValues = NULL; 29 this->paramName = new char[strlen(paramName)+1]; 30 strcpy(this->paramName, paramName); 28 this->paramName = paramName; 31 29 } 32 30 … … 46 44 delete[] this->types; 47 45 delete[] this->defaultValues; 48 delete[] this->paramName;49 delete[] this->description;50 46 } 51 47 … … 53 49 * @param descriptionText The text to set as a description for this Parameter 54 50 */ 55 void LoadParamDescription::setDescription(const char* descriptionText) 56 { 57 this->description = new char[strlen(descriptionText)+1]; 58 strcpy(this->description, descriptionText); 51 void LoadParamDescription::setDescription(const std::string& descriptionText) 52 { 53 this->description = descriptionText; 59 54 } 60 55 … … 64 59 void LoadParamDescription::print() const 65 60 { 66 PRINT(3)(" <%s>", this->paramName );61 PRINT(3)(" <%s>", this->paramName.c_str()); 67 62 for (int i = 0; i < this->paramCount; i++) 68 63 { … … 101 96 // } 102 97 } 103 PRINT(3)("</%s>", this->paramName );104 if ( this->description)105 PRINT(3)(" -- %s", this->description );98 PRINT(3)("</%s>", this->paramName.c_str()); 99 if (!this->description.empty()) 100 PRINT(3)(" -- %s", this->description.c_str()); 106 101 // default values 107 102 if (this->paramCount > 0) … … 139 134 * @param className the name of the class to be loadable 140 135 */ 141 LoadClassDescription::LoadClassDescription(const char* className) 142 { 143 this->className = new char[strlen(className)+1]; 144 strcpy(this->className, className); 136 LoadClassDescription::LoadClassDescription(const std::string& className) 137 { 138 this->className = className; 145 139 146 140 if (LoadClassDescription::classList == NULL) … … 161 155 this->paramList.pop_front(); 162 156 } 163 164 delete[] this->className;165 157 } 166 158 … … 187 179 Otherwise it returns a new classDescription 188 180 */ 189 LoadClassDescription* LoadClassDescription::addClass(const char*className)181 LoadClassDescription* LoadClassDescription::addClass(const std::string& className) 190 182 { 191 183 if (LoadClassDescription::classList != NULL) … … 194 186 while (it != LoadClassDescription::classList->end()) 195 187 { 196 if ( !strcmp((*it)->className, className))188 if ((*it)->className == className) 197 189 { 198 190 return (*it); … … 205 197 206 198 /** 207 * does the same as addClass(const char*className), but with params199 * does the same as addClass(const std::string& className), but with params 208 200 * @param paramName the name of the parameter to add. 209 201 */ 210 LoadParamDescription* LoadClassDescription::addParam(const char*paramName)202 LoadParamDescription* LoadClassDescription::addParam(const std::string& paramName) 211 203 { 212 204 std::list<LoadParamDescription*>::iterator it = this->paramList.begin(); 213 205 while (it != this->paramList.end()) 214 206 { 215 if ( !strcmp((*it)->paramName, paramName))207 if ((*it)->paramName == paramName) 216 208 { 217 209 return NULL; … … 231 223 * @todo implement it 232 224 */ 233 void LoadClassDescription::printAll(const char*fileName)225 void LoadClassDescription::printAll(const std::string& fileName) 234 226 { 235 227 PRINT(3)("===============================================================\n"); … … 240 232 while (classDesc != LoadClassDescription::classList->end()) 241 233 { 242 PRINT(3)("<%s>\n", (*classDesc)->className );234 PRINT(3)("<%s>\n", (*classDesc)->className.c_str()); 243 235 std::list<LoadParamDescription*>::iterator param = (*classDesc)->paramList.begin(); 244 236 while (param != (*classDesc)->paramList.end()) … … 247 239 param++; 248 240 } 249 PRINT(3)("</%s>\n\n", (*classDesc)->className );241 PRINT(3)("</%s>\n\n", (*classDesc)->className.c_str()); 250 242 classDesc++; 251 243 } … … 262 254 * !! The strings MUST NOT be deleted !! 263 255 */ 264 std::list< const char*> LoadClassDescription::searchClassWithShort(const char*classNameBegin)256 std::list<std::string> LoadClassDescription::searchClassWithShort(const std::string& classNameBegin) 265 257 { 266 258 /// FIXME 267 259 // NOT USED 268 260 /* unsigned int searchLength = strlen(classNameBegin); 269 std::list<const char*> retVal;261 std::list<const std::string&> retVal; 270 262 271 263 tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator(); -
trunk/src/lib/util/loading/load_param_description.h
r7130 r7221 36 36 friend class LoadParam; 37 37 friend class LoadClassDescription; 38 39 LoadParamDescription(const char*paramName);38 public: 39 LoadParamDescription(const std::string& paramName); 40 40 ~LoadParamDescription(); 41 41 42 void setDescription(const char*descriptionText);42 void setDescription(const std::string& descriptionText); 43 43 /** @returns the descriptionString */ 44 const char*getDescription() { return this->description; };44 const std::string& getDescription() { return this->description; }; 45 45 46 46 void print() const; 47 47 48 49 char*paramName; //!< The name of the parameter.48 private: 49 std::string paramName; //!< The name of the parameter. 50 50 int paramCount; //!< The count of parameters. 51 51 int* types; //!< What kind of parameters does this function take ?? 52 char*description; //!< A longer description about this function.52 std::string description; //!< A longer description about this function. 53 53 char** defaultValues; //!< The 'Default Values'. @TODO MAKE THIS A MULTITYPE 54 54 }; … … 58 58 { 59 59 friend class CLoadParam; 60 61 LoadClassDescription(const char*className);60 public: 61 LoadClassDescription(const std::string& className); 62 62 ~LoadClassDescription(); 63 63 64 static LoadClassDescription* addClass(const char*className);65 LoadParamDescription* addParam(const char*paramName);64 static LoadClassDescription* addClass(const std::string& className); 65 LoadParamDescription* addParam(const std::string& paramName); 66 66 67 67 static void deleteAllDescriptions(); 68 68 69 static void printAll(const char* fileName = NULL);70 static std::list< const char*> searchClassWithShort(const char*classNameBegin);71 // static const LoadParamDescription* getClass(const char*className);69 static void printAll(const std::string& fileName = ""); 70 static std::list<std::string> searchClassWithShort(const std::string& classNameBegin); 71 // static const LoadParamDescription* getClass(const std::string& className); 72 72 73 73 private: 74 74 static bool parametersDescription; //!< if parameter-description should be enabled. 75 75 static std::list<LoadClassDescription*>* classList; //!< a list, that stores all the loadable classes. (after one instance has been loaded) 76 char*className; //!< name of the class76 std::string className; //!< name of the class 77 77 78 78 std::list<LoadParamDescription*> paramList; //!< List of parameters this class knows. -
trunk/src/lib/util/loading/resource_manager.cc
r7199 r7221 59 59 this->setName("ResourceManager"); 60 60 61 this->dataDir = new char[3];62 strcpy(this->dataDir, "./");61 this->dataDir = "./"; 62 this->_cwd = ""; 63 63 this->tryDataDir("./data"); 64 65 this->_cwd = NULL;66 64 } 67 65 … … 80 78 PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size()); 81 79 82 // deleting the Directorie Lists83 while (!this->imageDirs.empty())84 {85 delete[] this->imageDirs.back();86 this->imageDirs.pop_back();87 }88 89 delete[] this->dataDir;90 if (this->_cwd)91 delete[] this->_cwd;92 80 ResourceManager::singletonRef = NULL; 93 81 } … … 97 85 * @param dataDir the DataDirectory. 98 86 */ 99 bool ResourceManager::setDataDir(const char*dataDir)100 { 101 char*realDir = ResourceManager::homeDirCheck(dataDir);87 bool ResourceManager::setDataDir(const std::string& dataDir) 88 { 89 std::string realDir = ResourceManager::homeDirCheck(dataDir); 102 90 if (isDir(realDir)) 103 91 { 104 delete[] this->dataDir; 105 if (dataDir[strlen(dataDir)-1] == '/' || dataDir[strlen(dataDir)-1] == '\\') 92 if (dataDir[dataDir.size()-1] == '/' || dataDir[dataDir.size()-1] == '\\') 106 93 { 107 this->dataDir = new char[strlen(realDir)+1]; 108 strcpy(this->dataDir, realDir); 94 this->dataDir = realDir; 109 95 } 110 96 else 111 97 { 112 this->dataDir = new char[strlen(realDir)+2]; 113 strcpy(this->dataDir, realDir); 114 this->dataDir[strlen(realDir)] = '/'; 115 this->dataDir[strlen(realDir)+1] = '\0'; 98 this->dataDir = realDir; 99 this->dataDir += '/'; 116 100 } 117 delete[] realDir;118 101 return true; 119 102 } 120 103 else 121 104 { 122 PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", realDir, this->dataDir); 123 delete[] realDir; 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()); 124 106 return false; 125 107 } … … 132 114 * this is essentially the same as setDataDir, but it ommits the error-message 133 115 */ 134 bool ResourceManager::tryDataDir(const char*dataDir)135 { 136 char*realDir = ResourceManager::homeDirCheck(dataDir);116 bool ResourceManager::tryDataDir(const std::string& dataDir) 117 { 118 std::string realDir = ResourceManager::homeDirCheck(dataDir); 137 119 if (isDir(realDir)) 138 120 { 139 delete[] this->dataDir; 140 if (dataDir[strlen(dataDir)-1] == '/' || dataDir[strlen(dataDir)-1] == '\\') 121 if (dataDir[dataDir.size()-1] == '/' || dataDir[dataDir.size()-1] == '\\') 141 122 { 142 this->dataDir = new char[strlen(realDir)+1]; 143 strcpy(this->dataDir, realDir); 123 this->dataDir = realDir; 144 124 } 145 125 else 146 126 { 147 this->dataDir = new char[strlen(realDir)+2]; 148 strcpy(this->dataDir, realDir); 149 this->dataDir[strlen(realDir)] = '/'; 150 this->dataDir[strlen(realDir)+1] = '\0'; 127 this->dataDir = realDir; 128 this->dataDir += '/'; 151 129 } 152 delete[] realDir;153 130 return true; 154 131 } 155 delete[] realDir;156 132 return false; 157 133 } … … 162 138 * @param fileInside is iniside of the given directory. 163 139 */ 164 bool ResourceManager::verifyDataDir(const char*fileInside)140 bool ResourceManager::verifyDataDir(const std::string& fileInside) 165 141 { 166 142 bool retVal; 167 143 if (!isDir(this->dataDir)) 168 144 { 169 PRINTF(1)("%s is not a directory\n", this->dataDir); 170 return false; 171 } 172 173 char* testFile = new char[strlen(this->dataDir)+strlen(fileInside)+1]; 174 sprintf(testFile, "%s%s", this->dataDir, fileInside); 145 PRINTF(1)("%s is not a directory\n", this->dataDir.c_str()); 146 return false; 147 } 148 149 std::string testFile = this->dataDir + fileInside; 175 150 retVal = isFile(testFile); 176 delete[] testFile;177 151 return retVal; 178 152 } … … 185 159 false otherwise 186 160 */ 187 bool ResourceManager::addImageDir(const char* imageDir) 188 { 189 if (imageDir == NULL) 190 return false; 191 192 char* newDir; 193 if (imageDir[strlen(imageDir)-1] == '/' || imageDir[strlen(imageDir)-1] == '\\') 194 { 195 newDir = new char[strlen(imageDir)+1]; 196 strcpy(newDir, imageDir); 197 } 198 else 199 { 200 newDir = new char[strlen(imageDir)+2]; 201 strcpy(newDir, imageDir); 202 newDir[strlen(imageDir)] = '/'; 203 newDir[strlen(imageDir)+1] = '\0'; 161 bool ResourceManager::addImageDir(const std::string& imageDir) 162 { 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; 171 newDir += '/'; 204 172 } 205 173 // check if the param is a Directory … … 207 175 { 208 176 // check if the Directory has been added before 209 std::vector< char*>::const_iterator imageDir;177 std::vector<std::string>::const_iterator imageDir; 210 178 for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) 211 179 { 212 if ( !strcmp(*imageDir, newDir))180 if (*imageDir == newDir) 213 181 { 214 PRINTF(3)("Path %s already loaded\n", newDir); 215 delete[] newDir; 182 PRINTF(3)("Path %s already loaded\n", newDir.c_str()); 216 183 return true; 217 184 } … … 223 190 else 224 191 { 225 PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir); 226 delete[] newDir; 192 PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir.c_str()); 227 193 return false; 228 194 } … … 239 205 * @returns a pointer to a desired Resource. 240 206 */ 241 BaseObject* ResourceManager::load(const char*fileName, ResourcePriority prio,207 BaseObject* ResourceManager::load(const std::string& fileName, ResourcePriority prio, 242 208 const MultiType& param0, const MultiType& param1, const MultiType& param2) 243 209 { 244 if (fileName == NULL)245 return NULL;246 210 ResourceType tmpType; 247 211 #ifndef NO_MODEL 248 212 #define __IF_OK 249 if (!strncasecmp(fileName +(strlen(fileName)-4), ".obj", 4))213 if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".obj", 4)) 250 214 tmpType = OBJ; 251 else if (!strncmp(fileName +(strlen(fileName)-4), ".md2", 4))215 else if (!strncmp(fileName.c_str()+(fileName.size()-4), ".md2", 4)) 252 216 tmpType = MD2; 253 else if (!strcasecmp(fileName , "cube") ||254 !strcasecmp(fileName, "sphere") ||255 !strcasecmp(fileName, "plane") ||256 !strcasecmp(fileName, "cylinder") ||257 !strcasecmp(fileName, "cone"))217 else if (!strcasecmp(fileName.c_str(), "cube") || 218 !strcasecmp(fileName.c_str(), "sphere") || 219 !strcasecmp(fileName.c_str(), "plane") || 220 !strcasecmp(fileName.c_str(), "cylinder") || 221 !strcasecmp(fileName.c_str(), "cone")) 258 222 tmpType = PRIM; 259 223 #endif /* NO_MODEL */ … … 263 227 #endif 264 228 #define __IF_OK 265 if (!strncasecmp(fileName +(strlen(fileName)-4), ".wav", 4))229 if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".wav", 4)) 266 230 tmpType = WAV; 267 else if (!strncasecmp(fileName +(strlen(fileName)-4), ".mp3", 4))231 else if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".mp3", 4)) 268 232 tmpType = MP3; 269 else if (!strncasecmp(fileName +(strlen(fileName)-4), ".ogg", 4))233 else if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".ogg", 4)) 270 234 tmpType = OGG; 271 235 #endif /* NO_AUDIO */ … … 275 239 #endif 276 240 #define __IF_OK 277 if (!strncasecmp(fileName +(strlen(fileName)-4), ".ttf", 4))241 if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".ttf", 4)) 278 242 tmpType = TTF; 279 243 #endif /* NO_TEXT */ … … 283 247 #endif 284 248 #define __IF_OK 285 if (!strncasecmp(fileName +(strlen(fileName)-5), ".vert", 5))249 if (!strncasecmp(fileName.c_str()+(fileName.size()-5), ".vert", 5)) 286 250 tmpType = SHADER; 287 251 #endif /* NO_SHADERS */ … … 308 272 * during the initialisation instead of at Runtime. 309 273 */ 310 bool ResourceManager::cache(const char*fileName, ResourceType type, ResourcePriority prio,274 bool ResourceManager::cache(const std::string& fileName, ResourceType type, ResourcePriority prio, 311 275 const MultiType& param0, const MultiType& param1, const MultiType& param2) 312 276 { 313 assert(fileName != NULL);314 315 277 // searching if the resource was loaded before. 316 278 Resource* tmpResource; … … 359 321 * @returns a pointer to a desired Resource. 360 322 */ 361 BaseObject* ResourceManager::load(const char*fileName, ResourceType type, ResourcePriority prio,323 BaseObject* ResourceManager::load(const std::string& fileName, ResourceType type, ResourcePriority prio, 362 324 const MultiType& param0, const MultiType& param1, const MultiType& param2) 363 325 { 364 assert(fileName != NULL);365 326 366 327 // searching if the resource was loaded before. … … 397 358 * @returns a pointer to a desired Resource. 398 359 */ 399 Resource* ResourceManager::loadResource(const char*fileName, ResourceType type, ResourcePriority prio,360 Resource* ResourceManager::loadResource(const std::string& fileName, ResourceType type, ResourcePriority prio, 400 361 const MultiType& param0, const MultiType& param1, const MultiType& param2) 401 362 { … … 406 367 tmpResource->prio = prio; 407 368 tmpResource->pointer = NULL; 408 tmpResource->name = new char[strlen(fileName)+1]; 409 strcpy(tmpResource->name, fileName); 369 tmpResource->name = fileName; 410 370 411 371 // creating the full name. (directoryName + FileName) 412 char*fullName = ResourceManager::getFullName(fileName);372 std::string fullName = ResourceManager::getFullName(fileName); 413 373 // Checking for the type of resource \see ResourceType 414 374 switch(type) … … 425 385 else 426 386 { 427 PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName , dataDir);387 PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName.c_str(), dataDir.c_str()); 428 388 tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat()); 429 389 } … … 435 395 tmpResource->param[0] = 1.0f; 436 396 437 if ( !strcmp(tmpResource->name, "cube"))397 if (tmpResource->name == "cube") 438 398 tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat()); 439 else if ( !strcmp(tmpResource->name, "sphere"))399 else if (tmpResource->name == "sphere") 440 400 tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat()); 441 else if ( !strcmp(tmpResource->name, "plane"))401 else if (tmpResource->name == "plane") 442 402 tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat()); 443 else if ( !strcmp(tmpResource->name, "cylinder"))403 else if (tmpResource->name == "cylinder") 444 404 tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat()); 445 else if ( !strcmp(tmpResource->name, "cone"))405 else if (tmpResource->name == "cone") 446 406 tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat()); 447 407 break; … … 468 428 tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt()); 469 429 else 470 PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName , this->dataDir);430 PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName.c_str(), this->dataDir.c_str()); 471 431 break; 472 432 #endif /* NO_TEXT */ … … 494 454 else 495 455 { 496 std::vector< char*>::iterator imageDir;456 std::vector<std::string>::iterator imageDir; 497 457 for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) 498 458 { 499 char* imgName = new char[strlen(*imageDir)+strlen(fileName)+1]; 500 sprintf(imgName, "%s%s", *imageDir, fileName); 459 std::string imgName = *imageDir + fileName; 501 460 if(isFile(imgName)) 502 461 { 503 462 PRINTF(4)("Image %s resides to %s\n", fileName, imgName); 504 463 tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt()); 505 delete[] imgName;506 464 break; 507 465 } 508 delete[] imgName;509 466 } 510 467 } 511 468 if(!tmpResource) 512 PRINTF(2)("!!Image %s not Found!!\n", fileName );469 PRINTF(2)("!!Image %s not Found!!\n", fileName.c_str()); 513 470 break; 514 471 #endif /* NO_TEXTURES */ … … 520 477 { 521 478 MultiType param = param0; /// HACK 522 char*secFullName = ResourceManager::getFullName(param.getCString());479 std::string secFullName = ResourceManager::getFullName(param.getCString()); 523 480 if (ResourceManager::isFile(secFullName)) 524 481 { … … 526 483 tmpResource->pointer = new Shader(fullName, secFullName); 527 484 } 528 delete[] secFullName;529 485 } 530 486 else 531 487 { 532 488 tmpResource->param[0] = param0; 533 tmpResource->pointer = new Shader(fullName, NULL);489 tmpResource->pointer = new Shader(fullName, ""); 534 490 } 535 491 } … … 538 494 default: 539 495 tmpResource->pointer = NULL; 540 PRINTF(1)("No type found for %s.\n !!This should not happen unless the Type is not supported yet. JUST DO IT!!\n", tmpResource->name );496 PRINTF(1)("No type found for %s.\n !!This should not happen unless the Type is not supported yet. JUST DO IT!!\n", tmpResource->name.c_str()); 541 497 break; 542 498 } 543 499 if (tmpResource->pointer != NULL) 544 500 this->resourceList.push_back(tmpResource); 545 delete[] fullName;546 547 501 548 502 if (tmpResource->pointer != NULL) … … 550 504 else 551 505 { 552 PRINTF(2)("Resource %s could not be loaded\n", fileName); 553 delete[] tmpResource->name; 506 PRINTF(2)("Resource %s could not be loaded\n", fileName.c_str()); 554 507 delete tmpResource; 555 508 return NULL; … … 597 550 delete resource->pointer; 598 551 // deleting the List Entry: 599 PRINTF(4)("Resource %s safely removed.\n", resource->name); 600 delete[] resource->name; 552 PRINTF(4)("Resource %s safely removed.\n", resource->name.c_str()); 601 553 std::vector<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource); 602 554 this->resourceList.erase(resourceIT); … … 604 556 } 605 557 else 606 PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name , resource->count);607 } 608 else 609 PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name );558 PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name.c_str(), resource->count); 559 } 560 else 561 PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name.c_str()); 610 562 return true; 611 563 } … … 633 585 if (round == 3) 634 586 PRINTF(2)("unable to unload %s because there are still %d references to it\n", 635 this->resourceList[index]->name , this->resourceList[index]->count);587 this->resourceList[index]->name.c_str(), this->resourceList[index]->count); 636 588 removeCount++; 637 589 } … … 653 605 * @returns a Pointer to the Resource if found, NULL otherwise. 654 606 */ 655 Resource* ResourceManager::locateResourceByInfo(const char*fileName, ResourceType type,607 Resource* ResourceManager::locateResourceByInfo(const std::string& fileName, ResourceType type, 656 608 const MultiType& param0, const MultiType& param1, const MultiType& param2) const 657 609 { … … 659 611 for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++) 660 612 { 661 if ((*resource)->type == type && !strcmp(fileName, (*resource)->name))613 if ((*resource)->type == type && fileName == (*resource)->name) 662 614 { 663 615 bool match = false; … … 743 695 } 744 696 745 char*ResourceManager::toResourcableString(unsigned int i)746 { 747 int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));748 len += strlen(this->resourceList[i]->name);697 std::string ResourceManager::toResourcableString(unsigned int i) 698 { 699 /* int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type)); 700 len += this->resourceList[i]->name.size(); 749 701 if (this->resourceList[i]->param[0].getCString()) len += strlen(this->resourceList[i]->param[0].getCString()) +1; 750 702 if (this->resourceList[i]->param[1].getCString()) len += strlen(this->resourceList[i]->param[1].getCString()) +1; 751 703 if (this->resourceList[i]->param[2].getCString()) len += strlen(this->resourceList[i]->param[2].getCString()) +1; 752 704 len += 10; 753 char*tmp = new char[len];705 std::string tmp = new char[len]; 754 706 tmp[0] = '\0'; 755 strcat( 707 strcat(tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type)); 756 708 strcat(tmp,","); 757 709 strcat (tmp, this->resourceList[i]->name); … … 771 723 strcat( tmp, this->resourceList[i]->param[2].getCString()); 772 724 } 773 return tmp; 725 return tmp;*/ 774 726 } 775 727 … … 778 730 * @param resourceableString the String to cache the resource from. 779 731 */ 780 bool ResourceManager::fromResourceableString(const char*resourceableString)781 { 782 SubString splits(resourceableString, ',');732 bool ResourceManager::fromResourceableString(const std::string& resourceableString) 733 { 734 /* SubString splits(resourceableString, ','); 783 735 splits.debug(); 784 736 if (splits.getCount() == 2) … … 793 745 else if (splits.getCount() == 5) 794 746 return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), 795 RP_LEVEL, splits[2], splits[3], splits[4]); 747 RP_LEVEL, splits[2], splits[3], splits[4]);*/ 796 748 } 797 749 … … 802 754 * @returns true if it is a directory/symlink false otherwise 803 755 */ 804 bool ResourceManager::isDir(const char* directoryName) 805 { 806 if (directoryName == NULL) 807 return false; 808 809 char* tmpDirName = NULL; 756 bool ResourceManager::isDir(const std::string& directoryName) 757 { 758 std::string tmpDirName = directoryName; 810 759 struct stat status; 811 760 812 761 // checking for the termination of the string given. If there is a "/" at the end cut it away 813 if (directoryName[strlen(directoryName)-1] == '/' || 814 directoryName[strlen(directoryName)-1] == '\\') 815 { 816 tmpDirName = new char[strlen(directoryName)]; 817 strncpy(tmpDirName, directoryName, strlen(directoryName)-1); 818 tmpDirName[strlen(directoryName)-1] = '\0'; 819 } 820 else 821 { 822 tmpDirName = new char[strlen(directoryName)+1]; 823 strcpy(tmpDirName, directoryName); 824 } 825 826 if(!stat(tmpDirName, &status)) 762 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)) 827 769 { 828 770 if (status.st_mode & (S_IFDIR … … 832 774 )) 833 775 { 834 delete[] tmpDirName;835 776 return true; 836 777 } 837 778 else 838 {839 delete[] tmpDirName;840 779 return false; 841 } 842 } 843 else 844 { 845 delete[] tmpDirName; 846 return false; 847 } 780 } 781 else 782 return false; 848 783 } 849 784 … … 853 788 * @returns true if it is a regular file/symlink, false otherwise 854 789 */ 855 bool ResourceManager::isFile(const char*fileName)856 { 857 if (fileName == NULL)858 return false; 859 char*tmpFileName = ResourceManager::homeDirCheck(fileName);790 bool ResourceManager::isFile(const std::string& fileName) 791 { 792 if (fileName.empty()) 793 return false; 794 std::string tmpFileName = ResourceManager::homeDirCheck(fileName); 860 795 // actually checks the File 861 796 struct stat status; 862 if (!stat(tmpFileName , &status))797 if (!stat(tmpFileName.c_str(), &status)) 863 798 { 864 799 if (status.st_mode & (S_IFREG … … 868 803 )) 869 804 { 870 delete[] tmpFileName;871 805 return true; 872 806 } 873 807 else 874 {875 delete[] tmpFileName;876 808 return false; 877 } 878 } 879 else 880 { 881 delete[] tmpFileName; 882 return false; 883 } 809 } 810 else 811 return false; 884 812 } 885 813 … … 888 816 * @param fileName The file to touch 889 817 */ 890 bool ResourceManager::touchFile(const char*fileName)891 { 892 char*tmpName = ResourceManager::homeDirCheck(fileName);893 if (tmpName == NULL)818 bool ResourceManager::touchFile(const std::string& fileName) 819 { 820 std::string tmpName = ResourceManager::homeDirCheck(fileName); 821 if (tmpName.empty()) 894 822 return false; 895 823 FILE* stream; 896 if( (stream = fopen (tmpName, "w")) == NULL) 897 { 898 PRINTF(1)("could not open %s fro writing\n", fileName); 899 delete[] tmpName; 824 if( (stream = fopen (tmpName.c_str(), "w")) == NULL) 825 { 826 PRINTF(1)("could not open %s fro writing\n", fileName.c_str()); 900 827 return false; 901 828 } 902 829 fclose(stream); 903 904 delete[] tmpName;905 830 } 906 831 … … 909 834 * @param fileName the File to delete 910 835 */ 911 bool ResourceManager::deleteFile(const char* fileName) 912 { 913 if (fileName == NULL) 914 return false; 915 char* tmpName = ResourceManager::homeDirCheck(fileName); 916 unlink(tmpName); 917 delete[] tmpName; 836 bool ResourceManager::deleteFile(const std::string& fileName) 837 { 838 std::string tmpName = ResourceManager::homeDirCheck(fileName); 839 unlink(tmpName.c_str()); 918 840 } 919 841 … … 921 843 * @param name the Name of the file to check 922 844 * @returns The name of the file, including the HomeDir 923 * IMPORTANT: this has to be deleted from the outside 924 */ 925 char* ResourceManager::homeDirCheck(const char* name) 926 { 927 if (name == NULL) 928 return NULL; 929 char* retName; 930 if (!strncmp(name, "~/", 2)) 931 { 932 char tmpFileName[500]; 845 */ 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); 933 852 #ifdef __WIN32__ 934 strcpy(tmpFileName, getenv("USERPROFILE"));853 homeDir = getenv("USERPROFILE"); 935 854 #else 936 strcpy(tmpFileName, getenv("HOME")); 937 #endif 938 retName = new char[strlen(tmpFileName)+strlen(name)]; 939 sprintf(retName, "%s%s", tmpFileName, name+1); 940 } 941 else 942 { 943 retName = new char[strlen(name)+1]; 944 strcpy(retName, name); 945 } 946 return retName; 855 homeDir = getenv("HOME"); 856 #endif 857 return homeDir + newName; 858 } 859 else 860 return name; 947 861 } 948 862 949 863 /** 950 864 * @param name the relative name of the File/Directory. 951 * @returns a new char* with the name in abs-dir-format 952 */ 953 char* ResourceManager::getAbsDir(const char* name) 954 { 955 if (name == NULL) 956 return NULL; 957 char* retName; 958 if (strncmp(name, "/", 1)) 959 { 960 if (*name == '.' && *(name+1) != '.') 961 name++; 962 const char* absDir = ResourceManager::cwd(); 963 retName = new char[strlen(absDir)+strlen(name)+1]; 964 sprintf(retName, "%s%s", absDir, name); 965 } 966 else 967 { 968 retName = new char[strlen(name)+1]; 969 strcpy(retName, name); 865 * @returns a new std::string with the name in abs-dir-format 866 */ 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; 970 878 } 971 879 return retName; … … 978 886 * !!IMPORTANT: this has to be deleted from the outside!! 979 887 */ 980 char* ResourceManager::getFullName(const char* fileName) 981 { 982 if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL) 983 return NULL; 984 985 char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir()) 986 + strlen(fileName) + 1]; 987 sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); 888 std::string ResourceManager::getFullName(const std::string& fileName) 889 { 890 if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty()) 891 return ""; 892 893 std::string retName = ResourceManager::getInstance()->getDataDir() +fileName; 988 894 if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName)) 989 895 return retName; 990 896 else 991 { 992 delete[] retName; 993 return NULL; 994 } 897 return ""; 995 898 } 996 899 … … 1005 908 * @returns the Current Woring Directory 1006 909 */ 1007 const char*ResourceManager::cwd()1008 { 1009 if (ResourceManager::getInstance()->_cwd == NULL)910 const std::string& ResourceManager::cwd() 911 { 912 if (ResourceManager::getInstance()->_cwd.empty()) 1010 913 { 1011 914 char cwd[1024]; 1012 915 char* errorCode = getcwd(cwd, 1024); 1013 916 if (errorCode == 0) 1014 return NULL; 1015 1016 ResourceManager::getInstance()->_cwd = new char[strlen(cwd)+1]; 1017 strcpy(ResourceManager::getInstance()->_cwd, cwd); 917 return ResourceManager::getInstance()->_cwd; 918 919 ResourceManager::getInstance()->_cwd = cwd; 1018 920 } 1019 921 return ResourceManager::getInstance()->_cwd; … … 1026 928 * @returns true if the file exists, false otherwise 1027 929 */ 1028 bool ResourceManager::isInDataDir(const char*fileName)1029 { 1030 if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL)930 bool ResourceManager::isInDataDir(const std::string& fileName) 931 { 932 if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty()) 1031 933 return false; 1032 934 1033 935 bool retVal = false; 1034 char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir()) 1035 + strlen(fileName) + 1]; 1036 sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); 936 std::string checkFile = ResourceManager::getInstance()->getDataDir() + fileName; 1037 937 1038 938 if (ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile)) … … 1040 940 else 1041 941 retVal = false; 1042 delete[] checkFile;1043 942 return retVal; 1044 943 } … … 1055 954 // if it is not initialized 1056 955 PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef); 1057 PRINT(0)(" Data-Directory is: %s\n", this->dataDir );956 PRINT(0)(" Data-Directory is: %s\n", this->dataDir.c_str()); 1058 957 PRINT(0)(" List of Image-Directories: "); 1059 std::vector< char*>::const_iterator imageDir;958 std::vector<std::string>::const_iterator imageDir; 1060 959 for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) 1061 PRINT(0)("%s ", (*imageDir) );960 PRINT(0)("%s ", (*imageDir).c_str()); 1062 961 PRINT(0)("\n"); 1063 962 … … 1068 967 { 1069 968 PRINT(0)("-----------------------------------------\n"); 1070 PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name , (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));969 PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name.c_str(), (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type)); 1071 970 1072 971 PRINT(0)("gets deleted at "); -
trunk/src/lib/util/loading/resource_manager.h
r7196 r7221 71 71 unsigned int count; //!< How many times this Resource has been loaded. 72 72 73 char*name; //!< Name of the Resource.73 std::string name; //!< Name of the Resource. 74 74 ResourceType type; //!< ResourceType of this Resource. 75 75 ResourcePriority prio; //!< The Priority of this resource. (This will only be increased) … … 96 96 inline static ResourceManager* getInstance() { if (!singletonRef) singletonRef = new ResourceManager(); return singletonRef; }; 97 97 98 bool setDataDir(const char*dataDir);98 bool setDataDir(const std::string& dataDir); 99 99 /** @returns the Name of the data directory */ 100 inline const char*getDataDir() const { return this->dataDir; };100 inline const std::string& getDataDir() const { return this->dataDir; }; 101 101 102 102 103 bool tryDataDir(const char*dataDir);104 bool verifyDataDir(const char*fileInside);105 bool addImageDir(const char*imageDir);103 bool tryDataDir(const std::string& dataDir); 104 bool verifyDataDir(const std::string& fileInside); 105 bool addImageDir(const std::string& imageDir); 106 106 107 bool cache(const char*fileName, ResourceType type, ResourcePriority prio = RP_NO,107 bool cache(const std::string& fileName, ResourceType type, ResourcePriority prio = RP_NO, 108 108 const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); 109 109 BaseObject* copy(BaseObject* resourcePointer); 110 110 111 BaseObject* load(const char*fileName, ResourcePriority prio = RP_NO,111 BaseObject* load(const std::string& fileName, ResourcePriority prio = RP_NO, 112 112 const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); 113 BaseObject* load(const char*fileName, ResourceType type, ResourcePriority prio = RP_NO,113 BaseObject* load(const std::string& fileName, ResourceType type, ResourcePriority prio = RP_NO, 114 114 const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); 115 115 bool unload(BaseObject* pointer, ResourcePriority prio = RP_NO); … … 117 117 bool unloadAllByPriority(ResourcePriority prio); 118 118 119 Resource* locateResourceByInfo(const char*fileName, ResourceType type,119 Resource* locateResourceByInfo(const std::string& fileName, ResourceType type, 120 120 const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()) const; 121 121 Resource* locateResourceByPointer(const void* pointer) const; 122 122 123 char*toResourcableString(unsigned int i);124 bool fromResourceableString(const char*resourceableString);123 std::string toResourcableString(unsigned int i); 124 bool fromResourceableString(const std::string& resourceableString); 125 125 /** @returns the Count of Resources the ResourceManager handles */ 126 126 unsigned int resourceCount() const { return this->resourceList.size(); } … … 130 130 131 131 // utility functions for handling files in and around the data-directory 132 static bool isDir(const char*directory);133 static bool isFile(const char*fileName);134 static bool touchFile(const char*fileName);135 static bool deleteFile(const char*fileName);136 static char* homeDirCheck(const char*fileName);137 static char* getFullName(const char*fileName);138 static bool isInDataDir(const char*fileName);139 static char* getAbsDir(const char*fileName);140 static const char*cwd();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 static std::string getFullName(const std::string& fileName); 138 static bool isInDataDir(const std::string& fileName); 139 static std::string getAbsDir(const std::string& fileName); 140 static const std::string& cwd(); 141 141 142 142 static const char* ResourceTypeToChar(ResourceType type); … … 145 145 private: 146 146 ResourceManager(); 147 Resource* loadResource(const char*fileName, ResourceType type, ResourcePriority prio,147 Resource* loadResource(const std::string& fileName, ResourceType type, ResourcePriority prio, 148 148 const MultiType& param0, const MultiType& param1, const MultiType& param2); 149 149 150 150 private: 151 static ResourceManager* singletonRef; //!< singleton Reference151 static ResourceManager* singletonRef; //!< singleton Reference 152 152 153 char*_cwd; //!< The currend Working directory.154 char*dataDir; //!< The Data Directory, where all relevant Data is stored.155 std::vector< char*>imageDirs; //!< A list of directories in which images are stored.153 std::string _cwd; //!< The currend Working directory. 154 std::string dataDir; //!< The Data Directory, where all relevant Data is stored. 155 std::vector<std::string> imageDirs; //!< A list of directories in which images are stored. 156 156 157 std::vector<Resource*> resourceList; //!< The List of Resources, that has already been loaded.157 std::vector<Resource*> resourceList; //!< The List of Resources, that has already been loaded. 158 158 159 static const char* resourceNames[RESOURCE_TYPE_SIZE];159 static const char* resourceNames[RESOURCE_TYPE_SIZE]; 160 160 }; 161 161
Note: See TracChangeset
for help on using the changeset viewer.