Changeset 956 for code/branches/script/src/orxonox/script/Script.cc
- Timestamp:
- Mar 30, 2008, 4:03:19 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/script/src/orxonox/script/Script.cc
r954 r956 27 27 28 28 #include "Script.h" 29 30 #include <fstream> 31 29 32 extern "C" { 30 33 #include "lualib.h" … … 37 40 Script::Script() 38 41 { 42 fileLines_ = std::list<std::string>(); 43 39 44 state_ = lua_open(); 40 #if L ua_VERSION == 5145 #if LUA_VERSION_NUM == 501 41 46 luaL_openlibs(state_); 42 47 #else … … 55 60 } 56 61 62 /** 63 @brief Loads the specified file line by line 64 @param filename The filename of the file 65 */ 66 void Script::loadFile(std::string filename) 67 { 68 std::ifstream file; 69 file.open(filename.c_str(), std::fstream::in); 70 71 if (!file.is_open()) 72 { 73 // some error msg 74 } 75 76 char line[1024]; 77 78 while (file.good() && !file.eof()) 79 { 80 file.getline(line, 1024); 81 this->fileLines_.push_back(line); 82 } 83 84 // The last line is useless 85 this->fileLines_.pop_back(); 86 87 file.close(); 88 } 89 90 /** 91 @brief Parses the level file to correct Lua code 92 */ 93 void Script::xmlToLua() 94 { 95 // We will iterate through all the lines and replace things. 96 std::list<std::string>::iterator it; 97 for(it = this->fileLines_.begin(); it != this->fileLines_.end(); ++it) 98 { 99 int pos = (*it).find("<?lua"); 100 while (pos < (int)(*it).length()) 101 { 102 // We found a lua tag 103 std::string front = (*it).substr(0,pos); 104 std::string back = (*it).substr(pos + 5); 105 (*it) = front + "]])" + back; 106 pos = (*it).find("<?lua"); 107 } 108 109 pos = (*it).find("?>"); 110 while (pos < (int)(*it).length()) 111 { 112 // We found a lua tag 113 std::string front = (*it).substr(0,pos); 114 std::string back = (*it).substr(pos + 2); 115 (*it) = front + "print([[" + back; 116 pos = (*it).find("?>"); 117 } 118 } 119 this->fileLines_.push_front("print([["); 120 this->fileLines_.push_back("]])"); 121 } 122 57 123 }
Note: See TracChangeset
for help on using the changeset viewer.