1 #ifndef SH_CONFIG_LOADER_H__ 2 #define SH_CONFIG_LOADER_H__ 3 4 #include <map> 5 #include <vector> 6 #include <cassert> 7 #include <string> 8 9 namespace sh 10 { 11 class ScriptNode; 12 13 /** 14 * @brief The base class of loaders that read Ogre style script files to get configuration and settings. 15 * Heavily inspired by: http://www.ogre3d.org/tikiwiki/All-purpose+script+parser 16 * ( "Non-ogre version") 17 */ 18 class ScriptLoader 19 { 20 public: 21 static void loadAllFiles(ScriptLoader* c, const std::string& path); 22 23 ScriptLoader(const std::string& fileEnding); 24 virtual ~ScriptLoader(); 25 26 std::string mFileEnding; 27 28 // For a line like 29 // entity animals/dog 30 // { 31 // ... 32 // } 33 // The type is "entity" and the name is "animals/dog" 34 // Or if animal/dog was not there then name is "" 35 ScriptNode *getConfigScript (const std::string &name); 36 37 std::map <std::string, ScriptNode*> getAllConfigScripts (); 38 39 void parseScript(std::ifstream &stream); 40 41 std::string mCurrentFileName; 42 43 protected: 44 45 float mLoadOrder; 46 // like "*.object" 47 48 std::map <std::string, ScriptNode*> m_scriptList; 49 50 enum Token 51 { 52 TOKEN_Text, 53 TOKEN_NewLine, 54 TOKEN_OpenBrace, 55 TOKEN_CloseBrace, 56 TOKEN_EOF 57 }; 58 59 Token mToken, mLastToken; 60 std::string mTokenValue; 61 62 void _parseNodes(std::ifstream &stream, ScriptNode *parent); 63 void _nextToken(std::ifstream &stream); 64 void _skipNewLines(std::ifstream &stream); 65 66 void clearScriptList(); 67 }; 68 69 class ScriptNode 70 { 71 public: 72 ScriptNode(ScriptNode *parent, const std::string &name = "untitled"); 73 ~ScriptNode(); 74 setName(const std::string & name)75 inline void setName(const std::string &name) 76 { 77 this->mName = name; 78 } 79 getName()80 inline std::string &getName() 81 { 82 return mName; 83 } 84 setValue(const std::string & value)85 inline void setValue(const std::string &value) 86 { 87 mValue = value; 88 } 89 getValue()90 inline std::string &getValue() 91 { 92 return mValue; 93 } 94 95 ScriptNode *addChild(const std::string &name = "untitled"); 96 ScriptNode *findChild(const std::string &name); 97 getChildren()98 inline std::vector<ScriptNode*> &getChildren() 99 { 100 return mChildren; 101 } 102 getChild(unsigned int index=0)103 inline ScriptNode *getChild(unsigned int index = 0) 104 { 105 assert(index < mChildren.size()); 106 return mChildren[index]; 107 } 108 getParent()109 inline ScriptNode *getParent() 110 { 111 return mParent; 112 } 113 114 std::string mFileName; 115 116 117 private: 118 std::string mName; 119 std::string mValue; 120 std::vector<ScriptNode*> mChildren; 121 ScriptNode *mParent; 122 }; 123 124 } 125 126 #endif 127