LString makeRelativePath(const LString &aAbs, const LString &aBase) { fs::path abspath(aAbs.c_str()); fs::path basepath(aBase.c_str()); if (!abspath.is_complete() || !basepath.is_complete()) return aAbs; // aAbs or aBase is not absolute path if (abspath.root_path()!=basepath.root_path()) return aAbs; // root names are different; cannot make relative path fs::path::const_iterator iter1 = abspath.begin(); fs::path::const_iterator iter1_end = abspath.end(); fs::path::const_iterator iter2 = basepath.begin(); fs::path::const_iterator iter2_end = basepath.end(); for (; iter1!=iter1_end && iter2!=iter2_end ; ++iter1, ++iter2) { if (*iter1!=*iter2) break; } fs::path relpath; //if (iter1!=iter1_end && iter2!=iter2_end) { for (; iter2!=iter2_end; ++iter2) relpath /= ".."; for (; iter1!=iter1_end; ++iter1) relpath /= *iter1; //} // string() will canonicalize the path separator to "/" //return relpath.file_string(); return relpath.string(); }
void SceneXMLReader::read() { // If getPath() points to a relative path, // convert it to the absolute path name using the fs::current_path() (i.e. pwd) fs::path curpath = fs::current_path(); #if (BOOST_FILESYSTEM_VERSION==2) LString localfile = qlib::makeAbsolutePath(getPath(), curpath.file_string()); #else LString localfile = qlib::makeAbsolutePath(getPath(), curpath.string()); #endif // Enter the loading scene's context AutoStyleCtxt style_ctxt(m_pClient->getUID()); // show start msg LOG_DPRINTLN("SceneXML> Start loading: %s ...", localfile.c_str()); // // Setup streams // qlib::FileInStream fis; fis.open(localfile); qlib::LDom2InStream ois(fis); // // Construct data structure // from the XML part in the target scene file. // qlib::LDom2Tree tree; ois.read(tree); LDom2Node *pNode = tree.top(); m_pClient->setSource(localfile); m_pClient->setSourceType(getName()); // perform deserialization to the client scene (m_pClient) tree.deserialize(&m_pClient); // load data source / collect chunk-load requests procDataSrcLoad(ois, pNode); // perform chunk loading procDataChunks(ois, pNode); ois.close(); m_pClient->setUpdateFlag(); m_errmsg = pNode->getErrorMsgs(); LOG_DPRINTLN("SceneXML> File loaded: %s.", localfile.c_str()); ////////// // fire the scene-loaded event { SceneEvent ev; ev.setTarget(m_pClient->getUID()); ev.setType(SceneEvent::SCE_SCENE_ONLOADED); m_pClient->fireSceneEvent(ev); } }
LSerializable *LDOMObjInStream::readObjImpl() { LString clsname = m_data.current()->type_name; // const int ncs = m_pCurNode->children.size(); // const int nelems = LDomTree::getElemCount(*m_pCurNode); // const int nattrs = ncs-nelems; ClassRegistry *pMgr = ClassRegistry::getInstance(); LClass *pClass = pMgr->getClassObj(clsname); if (pClass==NULL) { LOG_DPRINTLN("ERROR class \"%s\" is not defined", clsname.c_str()); return NULL; } // createScrObj() creates SmartPtr object, if the class supports it. LDynamic *pNewObj = pClass->createScrObj(); if (pNewObj==NULL) { LOG_DPRINTLN("ERROR cannot instantiate class \"%s\"", clsname.c_str()); return NULL; } LSerializable *pS = dynamic_cast<LSerializable *>(pNewObj); if (pS==NULL) { LOG_DPRINTLN("ERROR cannot instantiate class \"%s\"", clsname.c_str()); return NULL; } pS->readFrom(*this); return pS; }
bool clLibrary::Load( const LString& LibName ) { #if defined( OS_WINDOWS ) FLibHandle = ::LoadLibrary( LibName.c_str() ); if ( !FLibHandle ) { LString DLL = FindLibrary( LibName ); FLibHandle = ::LoadLibrary( DLL.c_str() ); } #else const char* ErrStr; FLibHandle = dlopen( LibName.c_str(), RTLD_LAZY ); if ( ( ErrStr = dlerror() ) != NULL ) { FLibHandle = NULL; } #endif CHECK_RET( !FLibHandle, false, "Unable to load library " + LibName ); return true; }
/** register new linkage between two residues */ bool TopoDB::addLinkByName(const LString &prev_res, char prev_ch, const LString &next_res, char next_ch, const LString &link_name) { //MB_DPRINT("addLinkByName search %s\n",plusname.c_str()); ResiPatch *pPatch = patchPrefixGet('+', link_name); if (pPatch==NULL) return false; // process the case in that prev and next is inverted if (pPatch->getPrevPrefix()!=prev_ch || pPatch->getNextPrefix()!=next_ch) { pPatch = patchPrefixGet('-', link_name); if (pPatch->getPrevPrefix()!=prev_ch || pPatch->getNextPrefix()!=next_ch) return false; } Linkage link; link.prev = prev_ch; link.next = next_ch; link.patch_name = pPatch->getName(); LString key(prev_res.c_str()); key.append(':'); key.append(next_res.c_str()); //MB_DPRINT("linkage LINK register %s\n",key.c_str()); return m_linkDict.set(key, link); //return true; }
void SceneXMLReader::procDataChunks(qlib::LDom2InStream &ois, LDom2Node *pNode) { for (;;) { try { LString chunkid = ois.getNextDataChunkID(); if (chunkid.isEmpty()) return; MB_DPRINTLN("SceneXMLReader> load datachunk: %s", chunkid.c_str()); //LDataSrcContainer *pCnt = findChunkObj(chunkid); LDataSrcContainer *pCnt = ois.findChunkObj(chunkid); if (pCnt==NULL) { LOG_DPRINTLN("SceneXMLReader> data container for chunk %s not found.", chunkid.c_str()); return; } qlib::InStream *pin = ois.getNextChunkStream(); pCnt->readFromStream(*pin); ois.closeChunkStream(pin); } catch (qlib::LException &e) { pNode->appendErrMsg("SceneXML> Load object error (ignored)."); pNode->appendErrMsg("SceneXML> Reason: %s", e.getMsg().c_str()); } catch (...) { pNode->appendErrMsg("SceneXML> Load object error (ignored)."); pNode->appendErrMsg("SceneXML> Reason: unknown"); } } }
bool XmlRpcMgr::setProp(qlib::uid_t uid, const LString &propnm, const xmlrpc_c::value *pVal) { qlib::LScriptable *pObj = getObj(uid); if (pObj==NULL) { // TO DO: report error MB_DPRINTLN("SetProp error, object %d not found", uid); return false; } ReoSetProp evt; evt.m_pObj = pObj; evt.m_propname = propnm; ////////// // convert to LVariant // variant (lvar) doesn't have ownership of its content qlib::LVariant &lvar = evt.m_value; bool ok = false; LString errmsg; try { convXrval2Lvar(pVal, lvar); ok = true; } catch (const qlib::LException &e) { errmsg = LString::format("SetProp(%s) cannot converting PyObj to LVariant: %s", propnm.c_str(), e.getMsg().c_str()); MB_DPRINTLN("Err: %s", errmsg.c_str()); } catch (...) { errmsg = LString::format("SetProp(%s): Cannot converting PyObj to LVariant.", propnm.c_str()); MB_DPRINTLN("Err: %s", errmsg.c_str()); } if (!ok) { // TO DO: report error return false; } MB_DPRINTLN("XMLRPC> svrthr SetProp conv OK "); ////////// // perform setProperty m_pQue->putWait(&evt); if (!evt.m_bOK) { throw ( xmlrpc_c::fault(evt.m_errmsg.c_str(), xmlrpc_c::fault::CODE_UNSPECIFIED) ); return qlib::invalid_uid; } MB_DPRINTLN("XMLRPC> svrthr setProp() putWait OK"); // OK return true; }
void clViewport::SetViewportTitle( const LString& Title ) { #ifdef OS_WINDOWS SetWindowText( FWindowHandle, Title.c_str() ); #endif #if defined(OS_LINUX) && !defined(OS_ANDROID) XSetStandardProperties( FDeviceHandle, *FWindowHandle, Title.c_str(), Title.c_str(), None, NULL, 0, NULL ); #endif }
LString makeAbsolutePath(const LString &aRel, const LString &aBase) { MB_DPRINTLN("makeAbsPath rel=%s, base=%s", aRel.c_str(), aBase.c_str()); fs::path relpath(aRel.c_str()); fs::path basepath(aBase.c_str()); if (relpath.is_complete()) return aRel; // aRel is already in abs form // convert to the absolute representation fs::path::const_iterator iter1 = relpath.begin(); fs::path::const_iterator iter1_end = relpath.end(); int nup = 0; for (; iter1!=iter1_end; ++iter1) { if (*iter1=="..") ++nup; else break; } if (nup==0) { // There's no up-dir ('..') string --> just concat to make abs path. #if (BOOST_FILESYSTEM_VERSION==2) relpath = fs::complete(relpath, basepath); return relpath.file_string(); #else relpath = fs::absolute(relpath, basepath); return relpath.string(); #endif } for (; nup>0; --nup) { MB_ASSERT(basepath.has_parent_path()); basepath = basepath.parent_path(); } for (; iter1!=iter1_end; ++iter1) { basepath /= *iter1; } #if (BOOST_FILESYSTEM_VERSION==2) return basepath.file_string(); #else return basepath.string(); #endif }
void PrintStream::print(const LString &n) { int len = n.length(); int res = write(n.c_str(), 0, len); if (res!=len) MB_THROW(IOException, LString::format("OutStream.write() failed. (%d:%d)", res, len)); }
void clASELoader::ASE_ReadTVertexList( iIStream* FStream, clVAMender* Mender ) { guard(); #ifdef ASE_HEAVY_DEBUG Env->Logger->Log( L_DEBUG, "Reading Tvertex list..." ); #endif char Keyword[32]; while ( !FStream->Eof() ) { LString Line = FStream->ReadLineTrimLeadSpaces(); if ( LStr::ContainsSubStr( Line, "}" ) ) { break; } else if ( LStr::StartsWith( Line, ASE_MeshTVertex ) ) { int Index; float U, V, W; sscanf( Line.c_str(), "%s %d %f %f %f", Keyword, &Index, &U, &V, &W ); Mender->EmitTextureVertex( Index, LVector3( U, 1.0f - V, W ) ); } else { FATAL_MSG( "Unexpected token in " + ASE_MeshTVertexList + " : " + Line ); } } unguard(); }
void clAudioThread::InitOpenAL() { #if L_AUDIO_USE_OPENAL guard(); // Env->Logger->Log( L_LOG, "Initializing OpenAL..." ); LString DeviceName = Env->Console->GetVarValueStr( "Audio.AudioDeviceName", "" ); FDevice = alcOpenDevice( DeviceName.empty() ? NULL : DeviceName.c_str() ); FATAL( !FDevice, "Unable to open OpenAL device" ); FContext = alcCreateContext( FDevice, NULL ); FATAL( !FContext, "Unable to create OpenAL context" ); FATAL( !alcMakeContextCurrent( FContext ), "Unable to select OpenAL context" ); Env->Logger->LogP( L_LOG, "OpenAL (%s, %s, %s), extensions : %s", alGetString( AL_VERSION ), alGetString( AL_VENDOR ), alGetString( AL_RENDERER ), alGetString( AL_EXTENSIONS ) ); // Env->Logger->Log( L_LOG, "OpenAL vendor : " + LString( ) ); // Env->Logger->Log( L_LOG, "OpenAL renderer : " + LString( alGetString( AL_RENDERER ) ) ); // Env->Logger->Log( L_LOG, "OpenAL extensions: " ); /* { LString Extensions; Extensions.assign( alGetString( AL_EXTENSIONS ) ); LStr::ReplaceAll( &Extensions, ' ', '\n' ); Env->Logger->Log( L_LOG, '\n' + Extensions ); } */ unguard(); #endif }
void clResourcesManager::ReCacheC( const LString& Param ) { guard( "%s", Param.c_str() ); if ( Param.empty() ) { Env->Console->DisplayInfoTip( "Usage: RECACHE <file name>" ); return; } iResource* Resource = Env->Resources->FindResourceInGraph( FResourcesGraph, Param ); if ( !Resource ) { Env->Console->DisplayError( "Unable to find resource: " + Param ); return; } Env->Console->DisplayInfoTip( Resource->GetFileName() ); Env->Console->Display( "Recaching " + Resource->ClassName() ); Resource->ReloadResource( true ); unguard(); }
static bool is_keyword(const LString &name) { const char *str = name.c_str(); int i = 0; while ((i <= 8) && str[i]) i++; if (i > 8) return false; int start = len_start[i]; int end = len_start[i+1]; while ((start < end) && (str[0] > keywords[start][0])) start ++; if ((start >= end) || (str[0] != keywords[start][0])) return false; while ((start < end) && (str[0] == keywords[start][0])){ const char *str1 = str + 1; const char *key1 = keywords[start] + 1; while ((*key1 == *str1) && *str1) { key1++; str1++; } if (*str1 == *key1) return true; start ++; } return false; }
void clMesh::LoadAnimStates( const LString& FileName ) { guard( "%s", FileName.c_str() ); FAnimations = Env->Resources->LoadAnimation( FileName ); unguard(); }
// static LString LDataSrcContainer::selectSrcAltSrc(const LString &src, const LString &altsrc, const LString &base_path, bool &rbReadFromAltSrc) { bool bReadFromAltSrc = false; LString abs_path; // First, try to convert "src" to abs path if (isAbsolutePath(src)) abs_path = src; else if (!base_path.isEmpty()) abs_path = makeAbsolutePath(src, base_path); if (abs_path.isEmpty() || !isFileReadable(abs_path)) { // Second, try to convert "altsrc" to abs path if (altsrc.isEmpty()) { // empty alt src --> no src path info available (ERROR) LString msg = LString::format("Fatal error, cannot open file: \"%s\"", abs_path.c_str()); LOG_DPRINTLN("SceneXML> %s", msg.c_str()); MB_THROW(qlib::IOException, msg); return LString(); } if (isAbsolutePath(altsrc)) abs_path = altsrc; else if (!base_path.isEmpty()) abs_path = makeAbsolutePath(altsrc, base_path); if (!isFileReadable(abs_path)) { LString msg = LString::format("Fatal error, cannot open file: \"%s\"", abs_path.c_str()); LOG_DPRINTLN("SceneXML> %s", msg.c_str()); MB_THROW(qlib::IOException, msg); return LString(); } // read from alt_src property (abs_path==alt_src) bReadFromAltSrc = true; } rbReadFromAltSrc = bReadFromAltSrc; return abs_path; }
/// Convert from (persistent) string representation to aid int MolCoord::fromStrAID(const LString &strid) const { if (!m_reAid.match(strid)) { LOG_DPRINTLN("MolCoord> Invalid aid strid=%s (re match failed)", strid.c_str()); return -1; } // text type aid int nsc = m_reAid.getSubstrCount(); if (nsc<4) { LOG_DPRINTLN("MolCoord> Invalid aid strid=%s", strid.c_str()); return -1; } //elem.setAtomID(-1); LString sChainName = m_reAid.getSubstr(1); LString sResInd = m_reAid.getSubstr(2); ResidIndex nResInd; if (!sResInd.toInt(&nResInd.first)) { LOG_DPRINTLN("MolCoord> Invalid aid resid value=%s", sResInd.c_str()); return -1; } LString sInsCode = m_reAid.getSubstr(3); if (sInsCode.isEmpty()) nResInd.second = '\0'; else nResInd.second = sInsCode.getAt(0); LString sAtomName = m_reAid.getSubstr(4); char cAltLoc = '\0'; if (nsc>6) { LString sAltLoc = m_reAid.getSubstr(6); if (!sAltLoc.isEmpty()) cAltLoc = sAltLoc.getAt(0); } MolAtomPtr pAtom = getAtom(sChainName, nResInd, sAtomName, cAltLoc); if (pAtom.isnull()) { LOG_DPRINTLN("MolCoord> fromStrAID/ atom <%s %s %s %c> not found in %s", sChainName.c_str(), nResInd.toString().c_str(), sAtomName.c_str(), cAltLoc=='\0'?' ':cAltLoc, getName().c_str()); return -1; } return pAtom->getID(); }
LString getLeafName(const LString &aPath) { fs::path path(aPath.c_str()); #if (BOOST_FILESYSTEM_VERSION==2) return LString(path.filename()); #else return LString(path.filename().string()); #endif }
void clConsole::ExecC( const LString& Param ) { guard(); LString Parameter = Param; LStr::TrimSpaces( &Parameter ); if ( Parameter.empty() ) { DisplayInfoTip( "Usage: EXEC <File Name>" ); return; } LString FName = LStr::GetToken( Parameter, 1 ); Env->Logger->LogP( L_DEBUG, "Exec: %s", FName.c_str() ); if ( Env->FileSystem->FileExists( FName ) ) { iIStream* FCFGStream = Env->FileSystem->CreateFileReader( FName ); while ( !FCFGStream->Eof() ) { LString Cmd = FCFGStream->ReadLine(); LStr::TrimSpaces( &Cmd ); if ( !Cmd.empty() && Cmd.at( 0 ) != ';' ) { SendCommand( Cmd ); } } delete( FCFGStream ); } else { Env->Logger->LogP( L_WARNING, "Config file %s not found", FName.c_str() ); DisplayError( "Config file \"" + FName + "\" not found" ); } unguard(); }
void ExpatInStream::parse() { // check base URI LString s = getURI(); MB_DPRINTLN("ExpatInStream getURI: <%s>", s.c_str()); fs::path srcpath(s.c_str()); #if (BOOST_FILESYSTEM_VERSION==2) LString base_dir = srcpath.parent_path().directory_string(); #else LString base_dir = srcpath.parent_path().string(); #endif if (!base_dir.isEmpty()) setBaseURI(base_dir); //detail::AbstFIOImpl *pimpl = //dynamic_cast<detail::AbstFIOImpl *>(getImpl().get()); //if (pimpl!=NULL) { //MB_DPRINTLN("File: base URI %s", pimpl->getDirName().c_str()); //setBaseURI((pimpl->getDirName())+LString(MB_PATH_SEPARATOR)); //} char buf[bufsize]; bool done=false; do { int len = super_t::read(buf, 0, sizeof(buf)); done = len < sizeof(buf); if (XML_Parse(getParser(), buf, len, done) == XML_STATUS_ERROR) { LString msg = LString::format("%s at line %d\n", XML_ErrorString(XML_GetErrorCode(getParser())), getLineNo()); MB_DPRINTLN("ExpatInStream> error: %s", msg.c_str()); MB_THROW(qlib::IOException, msg); } else if (getError()) { MB_DPRINTLN("ExpatInStream> error: %s", getErrorMsg().c_str()); MB_THROW(qlib::IOException, getErrorMsg()); } } while (!done); }
BOOST_FOREACH (const LString &brush, uniqset) { LString def = pSM->getMaterial(brush, "warabi_brush"); if (!def.isEmpty()) { def = def.trim(" \r\n\t"); ps.formatln("Brush \"%s\" : {", brush.c_str()); ps.println(def); ps.println("}"); bWritten = true; } }
//static LString LDOMObjOutStream::getTypeName(const LScriptable *pScr) { LString rval; if (!pScr) return rval; if (pScr->isSmartPtr() && pScr->getSPInner()==NULL) return rval; LClass *pCls = pScr->getClassObj(); rval = pCls->getClassName(); MB_DPRINTLN("writeObject name=%s", rval.c_str()); return rval; }
qlib::LScriptable *XmlRpcMgr::getObj(qlib::uid_t uid) { ObjTable::const_iterator i = m_objtab.find(uid); if (i==m_objtab.end()) { LString msg = LString::format("getObj unknown object ID: %d", uid); throw( xmlrpc_c::fault(msg.c_str(), xmlrpc_c::fault::CODE_UNSPECIFIED) ); return NULL; } return i->second.p; }
void LDOMObjInStream::reportError() { LDomNode *pNode = m_data.current(); LString msg = LString::format("Error occurred at node type=<%s> value=<%s> blist=%d nchild=%d", pNode->type_name.c_str(), pNode->value.c_str(), (int) pNode->blist, pNode->children.size()); LOG_DPRINTLN("LDOMObjInStream> %s", msg.c_str()); }
void clLocalizer::SetLocale( const LString& LocaleName ) { guard( "%s", LocaleName.c_str() ); ClearLocalization(); FLocaleName = LocaleName; const LString FileName( FLocalePath + "/Localizer-" + LocaleName + ".txt" ); if ( Env->FileSystem->FileExists( FileName ) ) { Env->Logger->LogP( L_NOTICE, "Reading locale from %s", FileName.c_str() ); iIStream* Stream = Env->FileSystem->CreateFileReader( FileName ); while ( !Stream->Eof() ) { LString Line = Stream->ReadLine(); size_t SepPos = Line.find( "~" ); FATAL( SepPos == Line.npos, "Invalid locale translation file format: missing ~" ); LString Text( Line.substr( 0, SepPos ) ); LString Translation( Line.substr( SepPos + 1, Line.length() - SepPos - 1 ) ); FTranslations[ Text ] = Translation; } delete( Stream ); } else { Env->Logger->LogP( L_NOTICE, "Locale %s not found", FileName.c_str() ); } this->SendAsync( L_EVENT_LOCALE_CHANGED, LEventArgs(), false ); unguard(); }
void LScrVector4D::setStrValue(const LString &val) { Vector4D vec; if (!Vector4D::fromStringS(val, vec)) { LString msg = LString::format("cannot convert \"%s\" to vector", val.c_str()); MB_THROW(qlib::RuntimeException, msg); return; } for (int i=1; i<=4; ++i) ai(i) = vec.ai(i); }
SelCommand::SelCommand(const LString &psz) : m_pSelRoot(NULL) { if (psz.isEmpty()) return; bool res = compile(psz); if (!res) { // TO DO: error handling/throw exception MB_DPRINTLN("Cannot initialize SelCommand with %s", psz.c_str()); return; } m_origcmd = psz; }
LString PDBFileWriter::formatAtomName(MolAtomPtr pAtom) { LString atomnam = pAtom->getName(); char cConfID = pAtom->getConfID(); int elem = pAtom->getElement(); if (cConfID=='\0') cConfID = ' '; // invalid name case if (atomnam.length()>=4|| elem==ElemSym::XX) { return LString::format("%4s%c", atomnam.c_str(), cConfID); } LString elenam = ElemSym::symID2Str(elem); elenam = elenam.toUpperCase(); int elepos = atomnam.indexOf(elenam); if (elepos<0) { return LString::format("%4s%c", atomnam.c_str(), cConfID); } LString atommod; // if (atomnam.equals(elenam)) { // // atom name==elem name // shead += LString::format(" %2s ", elenam.c_str()); // break; // } elepos += elenam.length(); atommod = atomnam.substr(elepos); elenam = atomnam.substr(0, elepos); if (atommod.length()<=2) { return LString::format("%2s%-2s%c", elenam.c_str(), atommod.c_str(), cConfID); } return LString::format("%4s%c", atomnam.c_str(), cConfID); }
ResiPatch *TopoDB::findLinkImpl(const LString &aPrevRes, const LString &aNextRes) { // Resolve alias name ResiToppar *pTopPrev = get(aPrevRes); ResiToppar *pTopNext = get(aNextRes); if (pTopPrev==NULL || pTopNext==NULL) return NULL; const LString prev_res = pTopPrev->getName(); const LString next_res = pTopNext->getName(); // At first, find the complete matching entry LString key(prev_res.c_str()); key.append(':'); key.append(next_res.c_str()); //MB_DPRINT("linkage find %s\n",key.c_str()); if (m_linkDict.containsKey(key)) { Linkage link = m_linkDict.get(key); return patchGet(link.patch_name); } // find the (prev==wildcard) case key = LString("*:") + next_res.c_str(); if (m_linkDict.containsKey(key)) { //MB_DPRINT("linkage find %s\n",key.c_str()); Linkage link = m_linkDict.get(key); return patchGet(link.patch_name); } // find the (next==wildcard) case key = LString(prev_res.c_str()) + ":*"; if (m_linkDict.containsKey(key)) { //MB_DPRINT("linkage find %s\n",key.c_str()); Linkage link = m_linkDict.get(key); return patchGet(link.patch_name); } return NULL; }
void UndoManager::startTxn(const LString &desc) { if (isDisabled()) return; // ignore nested txn !! if (m_pPendInfo!=NULL) { m_nTxnNestLevel ++; return; } m_pPendInfo = MB_NEW UndoInfo(); m_pPendInfo->setDesc(desc); MB_DPRINTLN("===== START UNDO TXN (%s) =====", desc.c_str()); }