const char* HclcData::getLuaVarString(const char* luaFileName,const char* varName){ //获取堆栈 lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState(); // 加载脚本 int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if(isOpen!=0){ CCLOG("Open Lua Error: %i", isOpen); return NULL; } // 清空堆栈 lua_settop(ls, 0); // 取变量varName的值放到栈顶 lua_getglobal(ls, varName); // 栈底是否为string int statesCode = lua_isstring(ls, -1); if(statesCode!=1){ CCLOG("type Error: %i", statesCode); return NULL; } // 获取栈底字符串 const char* str = lua_tostring(ls, -1); lua_pop(ls, -1); return str; }
const char* HclcData::getLuaVarOneOfTable(const char* luaFileName,const char* varName,const char* keyName){ lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState(); int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if(isOpen!=0){ CCLOG("Open Lua Error: %i", isOpen); return NULL; } lua_getglobal(ls, varName); int statesCode = lua_istable(ls, -1); if(statesCode!=1){ CCLOG("Open Lua Error: %i", statesCode); return NULL; } lua_pushstring(ls, keyName); lua_gettable(ls, -2); const char* valueString = lua_tostring(ls, -1); lua_pop(ls, -1); return valueString; }
const char* HclcData::callLuaFunction(const char* luaFileName,const char* functionName){ lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState(); int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if(isOpen!=0){ CCLOG("Open Lua Error: %i", isOpen); return NULL; } lua_getglobal(ls, functionName); lua_pushstring(ls, "Himi"); lua_pushnumber(ls, 23); lua_pushboolean(ls, true); /* lua_call 第一个参数:函数的参数个数 第二个参数:函数返回值个数 */ lua_call(ls, 3, 1); const char* iResult = lua_tostring(ls, -1); return iResult; }
const char* HclcData::getLuaVarTable(const char* luaFileName,const char* varName){ lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState(); int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if(isOpen!=0){ CCLOG("Open Lua Error: %i", isOpen); return NULL; } lua_getglobal(ls, varName); int it = lua_gettop(ls); lua_pushnil(ls); string result=""; while(lua_next(ls, it)) { string key = lua_tostring(ls, -2); string value = lua_tostring(ls, -1); result=result+key+":"+value+"\t"; lua_pop(ls, 1); } lua_pop(ls, 1); return result.c_str(); }
bool FtpFileSystem::removeDir(const QString &dirname){ QString fullFileName = getFileFullPath(dirname); QDir d(fullFileName); if (isDeleteable(dirname)){ if (d.exists()) { return d.rmdir(fullFileName); } } return false; }
QString FtpFileSystem::getLastModified(const QString &filename) { QString fullFileName = getFileFullPath(filename); QFile f(fullFileName); if (f.exists()) { QFileInfo fi(f); return fi.lastModified().toString("yyyyMMddhhmmss"); } return NULL; }
bool FtpFileSystem::getSize(const QString &filename,qint64 *size ) { QString fullFileName = getFileFullPath(filename); QFile f(fullFileName); if (f.exists()) { *size = f.size(); return true; } return false; }
void HclcData::callCppFunction(const char* luaFileName){ lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState(); /* Lua调用的C++的函数必须是静态的 */ lua_register(ls, "cppFunction", cppFunction); lua_register(ls, "othercppFunciton", othercppFunciton); int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if(isOpen!=0){ CCLOG("Open Lua Error: %i", isOpen); return; } }
bool FtpFileSystem::deleteFile(const QString &filename){ QString fullFileName = getFileFullPath(filename); QFile f(fullFileName); QDir d(fullFileName); if (isDeleteable(filename)){ if (d.exists()) { return d.rmdir(fullFileName); } if (f.exists()) { return f.remove(); } } return false; }
//带参执行Lua方法有返回值 const char* SendLuaData::callLuaFuncParReturn(const char* luaFileName,const char* functionName,CCArray* arraypar,CCArray* arraypartype) { lua_State* ls = LuaEngine::defaultEngine()->getLuaStack()->getLuaState(); int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName)); if(isOpen!=0){ CCLOG("Open Lua Error: %i", isOpen); return NULL; } lua_getglobal(ls, functionName); int countnum = arraypar->count(); if(countnum>0) { for (int i = 0; i<arraypar->count(); i++) { CCString* typestr = (CCString*)arraypartype->objectAtIndex(i); CCString* strnr = (CCString*)arraypar->objectAtIndex(i); if(typestr->isEqual(CCString::create("string"))) { lua_pushstring(ls, strnr->getCString()); } else if(typestr->isEqual(CCString::create("int"))) { lua_pushnumber(ls, strnr->intValue()); } else if(typestr->isEqual(CCString::create("bool"))) { lua_pushboolean(ls, strnr->boolValue()); } } } /* lua_call 第一个参数:函数的参数个数 第二个参数:函数返回值个数 */ lua_call(ls, countnum, 1); const char* iResult = lua_tostring(ls, -1); return iResult; }
//c++调用lua的函数 void callLuaFunc(const char* fileName, const char* funcName, const char* msg) { lua_State* ls = CCLuaEngine::defaultEngine()->getLuaState(); CCLog("this function is in "); CCLog("%s-------%s--------%s-----------%s",fileName, funcName, msg, getFileFullPath(fileName)); lua_getglobal(ls, funcName); /* query function by name, stack: function */ if (!lua_isfunction(ls, -1)) { CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", funcName); lua_pop(ls, 1); return; } lua_pushstring(ls, msg); int error = lua_pcall(ls, 1, 0, 0); if (error) { CCLOG("[LUA ERROR] %s", lua_tostring(ls, - 1)); lua_pop(ls, 1); // clean error message return; } }
bool FtpFileSystem::isDeleteable(const QString &fileName){ QFileInfo fi(getFileFullPath(fileName)); return fi.isWritable() && mFileAccess.adelete; }
bool FtpFileSystem::mkDir(const QString &filename){ QString fullDirPath = getFileFullPath(filename); QDir dir; return dir.mkdir(fullDirPath); }
bool FtpFileSystem::isWritable(const QString &fileName){ QFileInfo fi(getFileFullPath(fileName)); return (fi.isWritable() && mFileAccess.awrite) || (!fi.exists() && mFileAccess.awrite); }
bool FtpFileSystem::isReadable(const QString &fileName){ QFileInfo fi(getFileFullPath(fileName)); return fi.isReadable() && mFileAccess.aread; }
QString FtpFileSystem::getFileWrite(const QString &filename){ if (isWritable(filename)) return getFileFullPath(filename); else return NULL; }
QString FtpFileSystem::getFileRead(const QString &filename){ if (isReadable(filename)) return getFileFullPath(filename); return NULL; }
bool FtpFileSystem::exist(const QString &fileName){ QFile f(getFileFullPath(fileName)); return f.exists(); }
bool FtpFileSystem::rename(const QString &oldName, const QString &newName){ QString oldFullPath = getFileFullPath(oldName); QString newFullPath = getFileFullPath(newName); return QFile::rename(oldFullPath, newFullPath); }
int main (int argc, const char * argv[], char* envp[]) { // usage: rstr dir2 dir3 if ( argc != 3 ) { fprintf ( stderr, "Usage: %s dir_backup dir_restore\n", argv[0] ); exit(1); } char * pwd = getenv("PWD"); DIR *backupDir; DIR *restoreDir; if ( ( backupDir = opendir(argv[1]) ) == NULL ) { free(backupDir); perror(argv[1]); exit(2); } if ( ( restoreDir = opendir(argv[2]) ) == NULL ) { // if restore dir doesnt exist, create it if(mkdir(argv[2], S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) { perror("Problem creating restore destination"); exit(5); } if ( ( restoreDir = opendir(argv[2]) ) == NULL ) { perror(argv[2]); closedir(backupDir); free(backupDir); free(restoreDir); exit(3); } } printf("The following restore points are available:\n"); // moving into backupDir will allow directory reading if(chdir(argv[1]) != 0) { perror(argv[1]); exit(8); } char** backups = getAndPrintFolders(backupDir); int numberOfBackups = getNumOfBackups(backupDir); char prompt[MAX_LEN]; sprintf(prompt,"Which restore point? (0 to cancel)\n%s", PROMPT); int lineNumber = getChoice(prompt, numberOfBackups); //User sees numbers from 1 to numberOfBackups, but we want it from 0 to numberOfBackups-1 due to array access if(lineNumber == 0) { printf("Restore program ended.\n"); freeStrArray(backups, numberOfBackups); return 0; } lineNumber = lineNumber-1; char* selectedBckpPath = getBackupFullPath(argv[1], backups[lineNumber]); char* fullBckpInfoPath = getBackupInfo(selectedBckpPath); // moving out of the backupDir if(chdir(pwd) != 0) { perror(pwd); exit(8); } DIR *selectedBackup; if ( ( selectedBackup = opendir(selectedBckpPath) ) == NULL ) { freeStrArray(backups, numberOfBackups); perror(selectedBckpPath); exit(2); } char * datestr = backupDateToReadableDate(backups[lineNumber]); printf("\n%s restore point chosen.\n", datestr); free(datestr); freeStrArray(backups, numberOfBackups); printf("This backup contains the following files:\n\n"); printBackupInfo(fullBckpInfoPath); int numberOfFiles = getNumOfLines(fullBckpInfoPath); sprintf(prompt,"\nSelect a file to restore (0 to cancel):\n%s", PROMPT); lineNumber = getChoice(prompt, numberOfFiles + 1); printf("\n"); if ( lineNumber == numberOfFiles + 1 ) { printf("Doing full restore!\n"); char* fileRestorePath; char* destFilePath; char* originFilePath; int i; for (i = 1 ; i <= getNumOfLines(fullBckpInfoPath); i++ ) { fileRestorePath = getLineAt(i, fullBckpInfoPath); char* fileName = getFileNameFromInfoLine(fileRestorePath); destFilePath = getFileFullPath(argv[2], fileName); originFilePath = getFileFullPath(argv[1], fileRestorePath); if(copyFile(originFilePath, destFilePath) == 0) printf("Restored %s successfully!\n", fileName); else printf("Error copying %s!\n", fileName); free(fileRestorePath); free(fileName); free(destFilePath); free(originFilePath); } } else if ( lineNumber > 0 && lineNumber <= numberOfFiles ) { char* fileRestorePath = getLineAt(lineNumber, fullBckpInfoPath); char* fileName = getFileNameFromInfoLine(fileRestorePath); char* destFilePath = getFileFullPath( argv[2], fileName ); char* originFilePath = getFileFullPath( argv[1], fileRestorePath ); if(copyFile(originFilePath, destFilePath) == 0) printf("Restored %s successfully!\n", fileName); else printf("Error copying %s!\n", fileName); free(fileRestorePath); free(fileName); free(destFilePath); free(originFilePath); } // clean up the strings free(selectedBckpPath); free(fullBckpInfoPath); closedir(backupDir); closedir(restoreDir); closedir(selectedBackup); printf("\nRestore program ended.\n"); return 0; }