📄 scriptserver.cpp
字号:
Eval(s.str());}boolScriptServer::ParseVarName(const string& varName, string& nameSpace, string& name){ stringstream ss(varName); string current; vector<string> tokens; // segment varName while(! ss.eof()) { getline(ss, current,'.'); if (current.size()) { tokens.push_back(current); } } if (tokens.size() != 2) { return false; } nameSpace = tokens[0]; name = tokens[1]; return ( (nameSpace.size() >= 1) && (nameSpace[0] >= 'A') && (nameSpace[0] <= 'Z') && (name.size() >= 1) && (name[0] >= 'A') && (name[0] <= 'Z') );}boolScriptServer::ExistsVariable(const string &varName){ return (! GetVariable(varName).IsNil());}GCValueScriptServer::GetVariable(const string &varName){ string nameSpace; string name; if (! ParseVarName(varName,nameSpace,name)) { return GCValue(); } GCValue v; if (nameSpace != "") { // get namespace class GCValue ns = rb_const_get(rb_cObject, rb_intern(nameSpace.c_str())); if (! ns.IsNil()) { // get member variable of namespace object ID var = rb_intern(name.c_str()); int error; RbArguments arg(ns.Get(), var, 0, 0); v = rb_protect( RbFuncallWrap, reinterpret_cast<VALUE>(&arg), &error ); if (error) { GetLog()->Debug() << "(ScriptServer) Ruby ERROR: " << RbGetError() << "\n"; v = Qnil; } } } else { v = rb_const_get(rb_cObject, rb_intern(name.c_str())); } return v;}boolScriptServer::GetVariable(const string &varName, int &value){ return GetVariable(varName).GetInt(value);}boolScriptServer::GetVariable(const std::string &varName, float &value){ return GetVariable(varName).GetFloat(value);}boolScriptServer::GetVariable(const string &varName, bool &value){ return GetVariable(varName).GetBool(value);}boolScriptServer::GetVariable(const string &varName, string &value){ return GetVariable(varName).GetString(value);}boost::shared_ptr<CoreContext>ScriptServer::GetContext() const{ return gMyPrivateContext;}boolScriptServer::ConstructInternal(){ if (! Leaf::ConstructInternal()) { return false; } gMyPrivateContext = GetCore()->CreateContext(); return true;}voidScriptServer::SetInitRelPathPrefix(const std::string &relPathPrefix){ mRelPathPrefix = relPathPrefix;}ScriptServer::ERunScriptErrorTypeScriptServer::RunInitScriptInternal(const string &sourceDir, const string &name, bool copy, const string& destDir){ // run the init script in the sourceDir string sourcePath = sourceDir + salt::RFile::Sep() + name; GetLog()->Normal() << "(ScriptServer) Running " << sourcePath << "... " << endl; shared_ptr<salt::StdFile> file(new(salt::StdFile)); if (! file->Open(sourcePath.c_str())) { GetLog()->Error() << "(ScriptServer) Script not found " << sourcePath << endl; return eNotFound; } else if (! Run(file)) { GetLog()->Error() << "(ScriptServer) Error in script " << sourcePath << endl; return eError; } else { GetLog()->Normal() << "(ScriptServer) Script ended OK " << sourcePath << endl; } // copy it to the destDir if (! copy) { return eOK; } string destPath = destDir + salt::RFile::Sep() + name; GetLog()->Normal() << "Copying " << sourcePath << " to " << destPath << endl; stringstream s; s << "cp " << sourcePath << " " << destPath; system(s.str().c_str()); return eOK;}boolScriptServer::GetDotDirName(string& dotDir){ if (mDotName == "") { GetLog()->Warning() << "(ScriptServer) WARNING: Dot directory name unset.\n"; return false; } const char* envName =#ifdef WIN32 "USERPROFILE";#else "HOME";#endif char* home = getenv(envName); if (!home) { GetLog()->Warning() << "(ScriptServer) WARNING: $HOME is unset.\n"; return false; } dotDir = string(home) + salt::RFile::Sep() + mDotName; return true;}boolScriptServer::CreateDotDir(const string& dotDir){ char cwd[PATH_MAX+1];#if WIN32 if (GetCurrentDirectory(PATH_MAX, cwd) == 0)#else if (getcwd(cwd,sizeof(cwd)) == NULL)#endif { GetLog()->Error() << "(ScriptServer) ERROR: Cannot get current directory\n"; return false; }#if WIN32 if (! SetCurrentDirectory(dotDir.c_str()))#else if (chdir(dotDir.c_str()) == 0)#endif { // dot dir exists; change back to original directory chdir(cwd); return true; } // dot dir is not existent, try to create it#if WIN32 if (! CreateDirectory(dotDir.c_str(), 0))#else if (mkdir(dotDir.c_str(),0777) != 0)#endif { GetLog()->Error() << "(ScriptServer) ERROR: Cannot create directory '" << dotDir << "'\n"; return false; } GetLog()->Normal() << "(ScriptServer) Created Directory '" << dotDir << "'\n"; return true;}boolScriptServer::RunInitScript(const string &fileName, const string &relPath, EInitScriptType type){ string dotDir; bool validDotDir = (type == IS_USERLOCAL) && GetDotDirName(dotDir) && CreateDotDir(dotDir); // get the (OS specific) path to the script directory string pkgdatadir = salt::RFile::BundlePath();#if __APPLE__ pkgdatadir += "Contents/Resources/";#endif ERunScriptErrorType result = eNotFound; if (validDotDir) { result = RunInitScriptInternal(dotDir, fileName, false); } if (result == eOK) { GetLog()->Debug() << "(ScriptServer) : Ran init script '" << dotDir << salt::RFile::Sep() << fileName << "'\n"; return true; } if (result == eNotFound) { GetLog()->Debug() << "(ScriptServer) : Did not find init script '" << dotDir << salt::RFile::Sep() << fileName << "'\n"; } else if (result == eError) { GetLog()->Error() << "(ScriptServer) ERROR: Found error in init script '" << dotDir << salt::RFile::Sep() << fileName << "'\n"; return false; } // result = RunInitScriptInternal(pkgdatadir, fileName, validDotDir, dotDir); if (result == eOK) { GetLog()->Debug() << "(ScriptServer) : Ran init script '" << pkgdatadir << salt::RFile::Sep() << fileName << "'\n"; return true; } if (result == eNotFound) { GetLog()->Debug() << "(ScriptServer) : Did not find init script '" << pkgdatadir << salt::RFile::Sep() << fileName << "'\n"; } else if (result == eError) { GetLog()->Error() << "(ScriptServer) ERROR: Found error in init script '" << pkgdatadir << salt::RFile::Sep() << fileName << "'\n"; } result = RunInitScriptInternal(mRelPathPrefix+relPath, fileName, validDotDir, dotDir); if (result == eNotFound) { GetLog()->Error() << "(ScriptServer) ERROR: Cannot locate init script '" << fileName << "'\n"; } else if (result == eError) { GetLog()->Error() << "(ScriptServer) ERROR: Found error in init script '" << mRelPathPrefix+relPath << salt::RFile::Sep() << fileName << "'\n"; } return (result == eOK);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -