static void FindChangePattern( char *cdata,char **substs, char **keys, CPLString &ret) { char **papszTokens=CSLTokenizeString2(cdata," \t\n\r", CSLT_STRIPLEADSPACES|CSLT_STRIPENDSPACES); ret.clear(); int matchcount=CSLCount(substs); int keycount=CSLCount(keys); if (keycount<matchcount) { CSLDestroy(papszTokens); return; } // A valid string has only the keys in the substs list and none other for (int j=0;j<CSLCount(papszTokens);j++) { ret=papszTokens[j]; // The target string bool matches=true; for (int k=0;k<keycount && keys != NULL;k++) { const char *key=keys[k]; int sub_number=CSLPartialFindString(substs,key); if (sub_number!=-1) { // It is a listed match // But is the match for the key position? char *found_key=NULL; const char *found_value=CPLParseNameValue(substs[sub_number],&found_key); if (found_key!=NULL && EQUAL(found_key,key)) { // Should exits in the request if (std::string::npos==ret.find(key)) { matches=false; CPLFree(found_key); break; } // Execute the substitution on the "ret" string URLSearchAndReplace(&ret,key,"%s",found_value); } if (found_key!=NULL) CPLFree(found_key); } else { // Key not in the subst list, should not match if (std::string::npos!=ret.find(key)) { matches=false; break; } } } // Key loop if (matches) { CSLDestroy(papszTokens); return; // We got the string ready, all keys accounted for and substs applied } } ret.clear(); CSLDestroy(papszTokens); }
bool GH5_FetchAttribute( hid_t loc_id, const char *pszAttrName, CPLString &osResult, bool bReportError ) { bool retVal = false; hid_t hAttr = H5Aopen_name( loc_id, pszAttrName ); osResult.clear(); if( hAttr < 0 ) { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attempt to read attribute %s failed, not found.", pszAttrName ); return false; } hid_t hAttrTypeID = H5Aget_type( hAttr ); hid_t hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); if( H5Tget_class( hAttrNativeType ) == H5T_STRING ) { int nAttrSize = H5Tget_size( hAttrTypeID ); char *pachBuffer = (char *) CPLCalloc(nAttrSize+1,1); H5Aread( hAttr, hAttrNativeType, pachBuffer ); osResult = pachBuffer; CPLFree( pachBuffer ); retVal = true; } else { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attribute %s of unsupported type for conversion to string.", pszAttrName ); retVal = false; } H5Tclose( hAttrNativeType ); H5Tclose( hAttrTypeID ); H5Aclose( hAttr ); return retVal; }
void GMLASXPathMatcher::SetDocumentMapURIToPrefix( const std::map<CPLString,CPLString>& oMapURIToPrefix ) { m_aosReferenceXPaths.clear(); // Split each reference XPath into its components for(size_t i = 0; i < m_aosReferenceXPathsUncompiled.size(); ++i ) { const CPLString& osXPath( m_aosReferenceXPathsUncompiled[i] ); std::vector<XPathComponent> oVector; size_t iPos = 0; bool bDirectChild = false; if( osXPath.size() >= 2 && osXPath[0] == '/' && osXPath[1] == '/' ) { iPos += 2; } else if( osXPath.size() >= 1 && osXPath[0] == '/' ) { iPos += 1; bDirectChild = true; } while( iPos < osXPath.size() ) { size_t iPosNextSlash = osXPath.find('/', iPos); if( iPos == iPosNextSlash ) { bDirectChild = false; iPos ++; continue; } CPLString osCurNode; if( iPosNextSlash == std::string::npos ) osCurNode.assign(osXPath, iPos, std::string::npos); else osCurNode.assign(osXPath, iPos, iPosNextSlash - iPos); // Translate the configuration prefix to the equivalent in // this current schema size_t iPosColumn = osCurNode.find(':'); if( iPosColumn != std::string::npos ) { bool bIsAttr = ( osCurNode[0] == '@' ); CPLString osPrefix; CPLString osLocalname; osPrefix.assign(osCurNode, bIsAttr ? 1 : 0, iPosColumn - (bIsAttr ? 1 : 0)); osLocalname.assign(osCurNode, iPosColumn+1, std::string::npos); std::map<CPLString, CPLString>::const_iterator oIter = m_oMapPrefixToURIReferenceXPaths.find(osPrefix); if( oIter != m_oMapPrefixToURIReferenceXPaths.end() ) { const CPLString& osURI( oIter->second ); oIter = oMapURIToPrefix.find( osURI ); if( oIter == oMapURIToPrefix.end() ) break; osPrefix.assign(oIter->second); } osCurNode.clear(); if( bIsAttr ) osCurNode.append(1, '@'); osCurNode.append(osPrefix); osCurNode.append(1, ':'); osCurNode.append(osLocalname); } XPathComponent comp; comp.m_osValue = osCurNode; comp.m_bDirectChild = bDirectChild; oVector.push_back(comp); if( iPosNextSlash == std::string::npos ) iPos = osXPath.size(); else iPos = iPosNextSlash + 1; bDirectChild = true; } if ( iPos < osXPath.size() ) oVector.clear(); m_aosReferenceXPaths.push_back(oVector); } }
static CPLString GetProj4Filename(const char* pszFilename) { CPLString osFilename; /* or fixed path: /name, ./name or ../name */ if ( !CPLIsFilenameRelative(pszFilename) || *pszFilename == '.' ) { return pszFilename; } #if defined(PROJ_STATIC) && PROJ_VERSION >= 5 PJ_GRID_INFO info = proj_grid_info(pszFilename); if( info.filename[0] ) { osFilename = info.filename; } #elif defined(PROJ_STATIC) && PJ_VERSION > 493 osFilename.resize(2048); projCtx ctx = pj_ctx_alloc(); if( pj_find_file(ctx, pszFilename, &osFilename[0], osFilename.size()) ) { osFilename.resize( strlen(osFilename) ); } else { osFilename.clear(); } pj_ctx_free(ctx); #else // Transpose some of the proj.4 pj_open_lib() logic... /* check if ~/name */ char* pszSysname; if (*pszFilename == '~' && (pszFilename[1] == '/' || pszFilename[1] == '\\') ) { if ((pszSysname = getenv("HOME")) != nullptr) { osFilename = CPLFormFilename(pszSysname, pszFilename + 1, nullptr); } return osFilename; } /* or is environment PROJ_LIB defined */ else if ((pszSysname = getenv("PROJ_LIB")) != nullptr) { osFilename = CPLFormFilename(pszSysname, pszFilename, nullptr); VSIStatBufL sStat; if( VSIStatL(osFilename, &sStat) == 0 ) return osFilename; osFilename.clear(); } #if defined(PROJ_STATIC) && PJ_VERSION >= 490 // Super messy. proj.4 up to 4.9.3 had no public API to return the full // path to a resource file, so we rely on the fact that it emits a log // message with it... // Basically this is needed in the case where the file is in the // resource installation directory of proj.4, which we have no way to // know otherwise. CPLString osMsg; projCtx ctx = pj_ctx_alloc(); pj_ctx_set_app_data(ctx, &osMsg); pj_ctx_set_debug(ctx, PJ_LOG_DEBUG_MAJOR); pj_ctx_set_logger(ctx, my_proj4_logger); PAFile f = pj_open_lib(ctx, pszFilename, "rb"); if( f ) { pj_ctx_fclose(ctx, f); size_t nPos = osMsg.find("fopen("); if( nPos != std::string::npos ) { osFilename = osMsg.substr(nPos + strlen("fopen(")); nPos = osFilename.find(")"); if( nPos != std::string::npos ) osFilename = osFilename.substr(0, nPos); } } pj_ctx_free(ctx); #endif #endif return osFilename; }