------------------------------------------------------------------------------
| 152 | |
| 153 | //------------------------------------------------------------------------------ |
| 154 | bool ofxLua::doScript(const std::string& script, bool changeDir) { |
| 155 | |
| 156 | if(!isValid()) { |
| 157 | ofLogError("ofxLua") << "Cannot do script, lua state not inited!"; |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | std::string fullpath = ofFilePath::getAbsolutePath(ofToDataPath(script)); |
| 162 | std::string file = ofFilePath::getFileName(fullpath); |
| 163 | std::string folder = ofFilePath::getEnclosingDirectory(fullpath); |
| 164 | |
| 165 | // trim trailing slash |
| 166 | if(folder.size() > 0 && folder.at(folder.size()-1) == '/') { |
| 167 | folder.erase(folder.end()-1); |
| 168 | } |
| 169 | |
| 170 | ofLogVerbose("ofxLua") << "Doing script: \"" << file << "\" path: \"" << folder << "\""; |
| 171 | if(changeDir) { |
| 172 | ofLogVerbose("ofxLua") << "Changing to script dir \"" << folder << "\""; |
| 173 | if(CHDIR(folder.c_str()) < 0) { |
| 174 | switch(errno) { |
| 175 | case ENOENT: |
| 176 | ofLogError("ofxLua") << "Script dir \"" << folder << "\" does not exist"; |
| 177 | break; |
| 178 | case EACCES: |
| 179 | ofLogError("ofxLua") << "Could not access script dir \"" << folder << "\""; |
| 180 | break; |
| 181 | default: |
| 182 | ofLogError("ofxLua") << "Could not change to script dir \"" << folder << "\", error " << errno; |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // load the script |
| 189 | int ret = luaL_loadfile(L, fullpath.c_str()); |
| 190 | if(ret != 0) { |
| 191 | switch(ret) { |
| 192 | case LUA_ERRFILE: { |
| 193 | std::string msg = (std::string)"Script \""+file+"\" not found or unreadable"; |
| 194 | errorOccurred(msg); |
| 195 | break; |
| 196 | } |
| 197 | case LUA_ERRSYNTAX: { |
| 198 | std::string msg = (std::string) lua_tostring(L, LUA_STACK_TOP); |
| 199 | errorOccurred(msg); |
| 200 | break; |
| 201 | } |
| 202 | case LUA_ERRMEM: { |
| 203 | std::string msg = "Memory error for script \""+file+"\""; |
| 204 | errorOccurred(msg); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | // run the script |
no outgoing calls
no test coverage detected