const char *CPLProjectRelativeFilename( const char *pszProjectDir, const char *pszSecondaryFilename ) { char *pszStaticResult = CPLGetStaticResult(); CPLAssert( ! (pszProjectDir >= pszStaticResult && pszProjectDir < pszStaticResult + CPL_PATH_BUF_SIZE) ); CPLAssert( ! (pszSecondaryFilename >= pszStaticResult && pszSecondaryFilename < pszStaticResult + CPL_PATH_BUF_SIZE) ); if( !CPLIsFilenameRelative( pszSecondaryFilename ) ) return pszSecondaryFilename; if( pszProjectDir == NULL || strlen(pszProjectDir) == 0 ) return pszSecondaryFilename; if (CPLStrlcpy( pszStaticResult, pszProjectDir, CPL_PATH_BUF_SIZE ) >= CPL_PATH_BUF_SIZE) goto error; if( pszProjectDir[strlen(pszProjectDir)-1] != '/' && pszProjectDir[strlen(pszProjectDir)-1] != '\\' ) { if (CPLStrlcat( pszStaticResult, SEP_STRING, CPL_PATH_BUF_SIZE ) >= CPL_PATH_BUF_SIZE) goto error; } if (CPLStrlcat( pszStaticResult, pszSecondaryFilename, CPL_PATH_BUF_SIZE ) >= CPL_PATH_BUF_SIZE) goto error; return pszStaticResult; error: return CPLStaticBufferTooSmall(pszStaticResult); }
const char *CPLFormFilename( const char * pszPath, const char * pszBasename, const char * pszExtension ) { char *pszStaticResult = CPLGetStaticResult(); const char *pszAddedPathSep = ""; const char *pszAddedExtSep = ""; CPLAssert( ! (pszPath >= pszStaticResult && pszPath < pszStaticResult + CPL_PATH_BUF_SIZE) ); CPLAssert( ! (pszBasename >= pszStaticResult && pszBasename < pszStaticResult + CPL_PATH_BUF_SIZE) ); if( pszPath == NULL ) pszPath = ""; else if( strlen(pszPath) > 0 && pszPath[strlen(pszPath)-1] != '/' && pszPath[strlen(pszPath)-1] != '\\' ) pszAddedPathSep = SEP_STRING; if( pszExtension == NULL ) pszExtension = ""; else if( pszExtension[0] != '.' && strlen(pszExtension) > 0 ) pszAddedExtSep = "."; if (CPLStrlcpy( pszStaticResult, pszPath, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE || CPLStrlcat( pszStaticResult, pszAddedPathSep, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE || CPLStrlcat( pszStaticResult, pszBasename, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE || CPLStrlcat( pszStaticResult, pszAddedExtSep, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE || CPLStrlcat( pszStaticResult, pszExtension, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE) return CPLStaticBufferTooSmall(pszStaticResult); return pszStaticResult; }
const char *CPLFormFilename( const char * pszPath, const char * pszBasename, const char * pszExtension ) { char *pszStaticResult = CPLGetStaticResult(); const char *pszAddedPathSep = ""; const char *pszAddedExtSep = ""; CPLAssert( ! (pszPath >= pszStaticResult && pszPath < pszStaticResult + CPL_PATH_BUF_SIZE) ); CPLAssert( ! (pszBasename >= pszStaticResult && pszBasename < pszStaticResult + CPL_PATH_BUF_SIZE) ); if( pszBasename[0] == '.' && pszBasename[1] == '/' ) pszBasename += 2; if( pszPath == NULL ) pszPath = ""; else if( strlen(pszPath) > 0 && pszPath[strlen(pszPath)-1] != '/' && pszPath[strlen(pszPath)-1] != '\\' ) { /* FIXME? would be better to ask the filesystems what they */ /* prefer as directory separator */ if (strncmp(pszPath, "/vsicurl/", 9) == 0) pszAddedPathSep = "/"; else if (strncmp(pszPath, "/vsizip/", 8) == 0) pszAddedPathSep = "/"; else pszAddedPathSep = SEP_STRING; } if( pszExtension == NULL ) pszExtension = ""; else if( pszExtension[0] != '.' && strlen(pszExtension) > 0 ) pszAddedExtSep = "."; if (CPLStrlcpy( pszStaticResult, pszPath, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE || CPLStrlcat( pszStaticResult, pszAddedPathSep, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE || CPLStrlcat( pszStaticResult, pszBasename, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE || CPLStrlcat( pszStaticResult, pszAddedExtSep, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE || CPLStrlcat( pszStaticResult, pszExtension, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE) return CPLStaticBufferTooSmall(pszStaticResult); return pszStaticResult; }
const char *CPLProjectRelativeFilename( const char *pszProjectDir, const char *pszSecondaryFilename ) { char *pszStaticResult = CPLGetStaticResult(); CPLAssert( ! (pszProjectDir >= pszStaticResult && pszProjectDir < pszStaticResult + CPL_PATH_BUF_SIZE) ); CPLAssert( ! (pszSecondaryFilename >= pszStaticResult && pszSecondaryFilename < pszStaticResult + CPL_PATH_BUF_SIZE) ); if( !CPLIsFilenameRelative( pszSecondaryFilename ) ) return pszSecondaryFilename; if( pszProjectDir == NULL || strlen(pszProjectDir) == 0 ) return pszSecondaryFilename; if (CPLStrlcpy( pszStaticResult, pszProjectDir, CPL_PATH_BUF_SIZE ) >= CPL_PATH_BUF_SIZE) goto error; if( pszProjectDir[strlen(pszProjectDir)-1] != '/' && pszProjectDir[strlen(pszProjectDir)-1] != '\\' ) { /* FIXME? would be better to ask the filesystems what they */ /* prefer as directory separator */ const char* pszAddedPathSep; if (strncmp(pszStaticResult, "/vsicurl/", 9) == 0) pszAddedPathSep = "/"; else pszAddedPathSep = SEP_STRING; if (CPLStrlcat( pszStaticResult, pszAddedPathSep, CPL_PATH_BUF_SIZE ) >= CPL_PATH_BUF_SIZE) goto error; } if (CPLStrlcat( pszStaticResult, pszSecondaryFilename, CPL_PATH_BUF_SIZE ) >= CPL_PATH_BUF_SIZE) goto error; return pszStaticResult; error: return CPLStaticBufferTooSmall(pszStaticResult); }
const char *CPLResetExtension( const char *pszPath, const char *pszExt ) { char *pszStaticResult = CPLGetStaticResult(); size_t i; CPLAssert( ! (pszPath >= pszStaticResult && pszPath < pszStaticResult + CPL_PATH_BUF_SIZE) ); /* -------------------------------------------------------------------- */ /* First, try and strip off any existing extension. */ /* -------------------------------------------------------------------- */ if (CPLStrlcpy( pszStaticResult, pszPath, CPL_PATH_BUF_SIZE ) >= CPL_PATH_BUF_SIZE) return CPLStaticBufferTooSmall(pszStaticResult); if (*pszStaticResult) { for( i = strlen(pszStaticResult) - 1; i > 0; i-- ) { if( pszStaticResult[i] == '.' ) { pszStaticResult[i] = '\0'; break; } if( pszStaticResult[i] == '/' || pszStaticResult[i] == '\\' || pszStaticResult[i] == ':' ) break; } } /* -------------------------------------------------------------------- */ /* Append the new extension. */ /* -------------------------------------------------------------------- */ if (CPLStrlcat( pszStaticResult, ".", CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE || CPLStrlcat( pszStaticResult, pszExt, CPL_PATH_BUF_SIZE) >= CPL_PATH_BUF_SIZE) return CPLStaticBufferTooSmall(pszStaticResult); return pszStaticResult; }
int OGRSelafinDataSource::TakeLock(CPL_UNUSED const char *pszFilename) { #ifdef notdef // Ideally, we should implement a locking mechanism for Selafin layers because different layers share the same header and file on disk. If two layers are modified at the same time, // this would most likely result in data corruption. However, locking the data source causes other problems in QGis which always tries to open the datasource twice, a first time // in Update mode, and the second time for the layer inside in Read-only mode (because the lock is taken), and layers therefore can't be changed in QGis. // For now, this procedure is deactivated and a warning message is issued when a datasource is opened in update mode. //CPLDebug("Selafin","TakeLock(%s)",pszFilename); if (pszLockName!=0) CPLFree(pszLockName); size_t nLen=strlen(pszFilename)+4; pszLockName=(char*)CPLMalloc(sizeof(char)*nLen); CPLStrlcpy(pszLockName,pszFilename,nLen-3); CPLStrlcat(pszLockName,"~~~",nLen); VSILFILE *fpLock = VSIFOpenL(pszLockName, "rb+"); // This is not thread-safe but I'm not quite sure how to open a file in exclusive mode and in a portable way if (fpLock!=NULL) { VSIFCloseL(fpLock); return 0; } fpLock=VSIFOpenL(pszLockName,"wb"); VSIFCloseL(fpLock); #endif return 1; }
void GTMWaypointLayer::WriteFeatureAttributes( OGRFeature *poFeature, float altitude ) { char psNameField[] = " "; // 10 spaces char* pszcomment = nullptr; int icon = 48; int date = 0; for (int i = 0; i < poFeatureDefn->GetFieldCount(); ++i) { OGRFieldDefn *poFieldDefn = poFeatureDefn->GetFieldDefn( i ); if( poFeature->IsFieldSetAndNotNull( i ) ) { const char* l_pszName = poFieldDefn->GetNameRef(); /* Waypoint name */ if (STARTS_WITH(l_pszName, "name")) { strncpy (psNameField, poFeature->GetFieldAsString( i ), 10); CPLStrlcat (psNameField, " ", sizeof(psNameField)); } /* Waypoint comment */ else if (STARTS_WITH(l_pszName, "comment")) { CPLFree(pszcomment); pszcomment = CPLStrdup( poFeature->GetFieldAsString( i ) ); } /* Waypoint icon */ else if (STARTS_WITH(l_pszName, "icon")) { icon = poFeature->GetFieldAsInteger( i ); // Check if it is a valid icon if (icon < 1 || icon > 220) icon = 48; } /* Waypoint date */ else if (EQUAL(l_pszName, "time")) { struct tm brokendowndate; int year, month, day, hour, min, sec, TZFlag; if (poFeature->GetFieldAsDateTime( i, &year, &month, &day, &hour, &min, &sec, &TZFlag)) { brokendowndate.tm_year = year - 1900; brokendowndate.tm_mon = month - 1; brokendowndate.tm_mday = day; brokendowndate.tm_hour = hour; brokendowndate.tm_min = min; brokendowndate.tm_sec = sec; GIntBig unixTime = CPLYMDHMSToUnixTime(&brokendowndate); if (TZFlag != 0) unixTime -= (TZFlag - 100) * 15; if (unixTime <= GTM_EPOCH || (unixTime - GTM_EPOCH) != (int)(unixTime - GTM_EPOCH)) { CPLError(CE_Warning, CPLE_AppDefined, "%04d/%02d/%02d %02d:%02d:%02d is not a valid datetime for GTM", year, month, day, hour, min, sec); } else { date = (int)(unixTime - GTM_EPOCH); } } } } } if (pszcomment == nullptr) pszcomment = CPLStrdup( "" ); const size_t commentLength = strlen(pszcomment); const size_t bufferSize = 27 + commentLength; void* pBuffer = CPLMalloc(bufferSize); void* pBufferAux = pBuffer; /* Write waypoint name to buffer */ memcpy((char*)pBufferAux, psNameField, 10); /* Write waypoint string comment size to buffer */ pBufferAux = (char*)pBuffer+10; appendUShort(pBufferAux, (unsigned short) commentLength); /* Write waypoint string comment to buffer */ memcpy((char*)pBuffer+12, pszcomment, commentLength); /* Write icon to buffer */ pBufferAux = (char*)pBuffer+12+commentLength; appendUShort(pBufferAux, (unsigned short) icon); /* Write dslp to buffer */ pBufferAux = (char*)pBufferAux + 2; appendUChar(pBufferAux, 3); /* Date */ pBufferAux = (char*)pBufferAux + 1; appendInt(pBufferAux, date); /* wrot */ pBufferAux = (char*)pBufferAux + 4; appendUShort(pBufferAux, 0); /* walt */ pBufferAux = (char*)pBufferAux + 2; appendFloat(pBufferAux, altitude); /* wlayer */ pBufferAux = (char*)pBufferAux + 4; appendUShort(pBufferAux, 0); VSIFWriteL(pBuffer, bufferSize, 1, poDS->getOutputFP()); poDS->incNumWaypoints(); CPLFree(pszcomment); CPLFree(pBuffer); }
herr_t HDF5AttrIterate( hid_t hH5ObjID, const char *AttrName, void *pDS ) { hid_t hAttrID; hid_t hAttrTypeID; hid_t hAttrNativeType; hid_t hAttrSpace; char *szData = NULL; hsize_t nSize[64]; unsigned int nAttrElmts; hsize_t nAttrSize; hsize_t i; void *buf = NULL; unsigned int nAttrDims; HDF5Dataset *poDS; char *szTemp = (char*) CPLMalloc( 8192 ); char *szValue = NULL; poDS = (HDF5Dataset *) pDS; sprintf( szTemp, "%s:%s", poDS->poH5CurrentObject->pszName, AttrName ); hAttrID = H5Aopen_name( hH5ObjID, AttrName ); hAttrTypeID = H5Aget_type( hAttrID ); hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); hAttrSpace = H5Aget_space( hAttrID ); nAttrDims = H5Sget_simple_extent_dims( hAttrSpace, nSize, NULL ); if( H5Tget_class( hAttrNativeType ) == H5T_STRING ) { nAttrSize = H5Aget_storage_size( hAttrID ); szData = (char*) CPLMalloc((size_t) (nAttrSize+1)); szValue = (char*) CPLMalloc((size_t) (nAttrSize+1)); H5Aread( hAttrID, hAttrNativeType, szData ); szData[nAttrSize]='\0'; sprintf( szValue, "%s", szData ); } else { nAttrElmts = 1; for( i=0; i < nAttrDims; i++ ) { nAttrElmts *= (int) nSize[i]; } if( nAttrElmts > 0 ){ buf = (void *) CPLMalloc( nAttrElmts* H5Tget_size( hAttrNativeType )); szData = (char*) CPLMalloc( 8192 ); szValue = (char*) CPLMalloc( MAX_METADATA_LEN ); szData[0]='\0'; szValue[0] ='\0'; H5Aread( hAttrID, hAttrNativeType, buf ); } if( H5Tequal( H5T_NATIVE_CHAR, hAttrNativeType ) ){ for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%c ", ((char *) buf)[i]); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_UCHAR, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%c", ((char *) buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_SHORT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%d ", ((short *) buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_USHORT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ud ", ((unsigned short *) buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_INT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%d ", ((int *) buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_UINT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ud ", ((unsigned int *) buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_LONG, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ld ", ((long *)buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_ULONG, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ld ", ((unsigned long *)buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_FLOAT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%f ", ((float *)buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_DOUBLE, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%g ", ((double *)buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } CPLFree( buf ); } H5Sclose(hAttrSpace); H5Tclose(hAttrNativeType); H5Tclose(hAttrTypeID); H5Aclose( hAttrID ); //printf( "%s = %s\n",szTemp, szValue ); poDS->papszMetadata = CSLSetNameValue( poDS->papszMetadata, szTemp, CPLSPrintf( "%s", szValue ) ); CPLFree( szTemp ); CPLFree( szData ); CPLFree( szValue ); return 0; }
int OGRGTMDataSource::Open(const char* pszFilename, int bUpdate) { CPLAssert( pszFilename != NULL ); /* Should not happen as the driver already returned if bUpdate == NULL */ if (bUpdate) { CPLError(CE_Failure, CPLE_NotSupported, "GTM driver does not support opening in update mode"); return FALSE; } /* -------------------------------------------------------------------- */ /* Create a GTM object and open the source file. */ /* -------------------------------------------------------------------- */ poGTMFile = new GTM(); if ( !poGTMFile->Open( pszFilename ) ) { delete poGTMFile; poGTMFile = NULL; return FALSE; } /* -------------------------------------------------------------------- */ /* Validate it by start parsing */ /* -------------------------------------------------------------------- */ if( !poGTMFile->isValid() ) { delete poGTMFile; poGTMFile = NULL; return FALSE; } pszName = CPLStrdup( pszFilename ); /* -------------------------------------------------------------------- */ /* Now, we are able to read the file header and find the position */ /* of the first waypoint and the position of the first track. */ /* -------------------------------------------------------------------- */ if ( !poGTMFile->readHeaderNumbers() ) return FALSE; /* -------------------------------------------------------------------- */ /* We can start reading the file elements */ /* We are going to translate GTM features into layers */ /* -------------------------------------------------------------------- */ char* pszBaseFileName = CPLStrdup( CPLGetBasename(pszFilename) ); /* We are going to create two layers, one for storing waypoints and another for storing tracks */ papoLayers = (OGRGTMLayer **) CPLMalloc(sizeof(void*) * 2); /* Create a spatial reference for WGS8*/ OGRSpatialReference* poSRS = new OGRSpatialReference(NULL); poSRS->SetWellKnownGeogCS( "WGS84" ); /* Waypoint layer */ size_t layerNameSize = strlen(pszBaseFileName) + sizeof("_waypoints"); char* pszLayerName = (char*) CPLMalloc(layerNameSize); /* The layer name will be "<basename>_waypoints" */ strcpy (pszLayerName, pszBaseFileName); CPLStrlcat (pszLayerName, "_waypoints", layerNameSize); /* Store the layer of waypoints */ GTMWaypointLayer* poWaypointLayer = new GTMWaypointLayer ( pszLayerName, poSRS, FALSE, this ); papoLayers[nLayers++] = poWaypointLayer; CPLFree(pszLayerName); /* Track layer */ layerNameSize = strlen(pszBaseFileName) + sizeof("_tracks"); pszLayerName = (char*) CPLMalloc(layerNameSize); /* The layer name will be "<basename>_tracks" */ strcpy (pszLayerName, pszBaseFileName); CPLStrlcat (pszLayerName, "_tracks", layerNameSize); CPLFree(pszBaseFileName); /* Store the layer of tracks */ GTMTrackLayer* poTrackLayer = new GTMTrackLayer ( pszLayerName, poSRS, FALSE, this ); papoLayers[nLayers++] = poTrackLayer; CPLFree(pszLayerName); poSRS->Release(); return TRUE; }
static herr_t HDF5AttrIterate( hid_t hH5ObjID, const char *pszAttrName, // TODO(schwehr): void * -> HDF5Dataset * void *pDS ) { char **papszTokens = nullptr; CPLString osKey; HDF5Dataset *const poDS = static_cast<HDF5Dataset *>(pDS); // Convert "/" into "_" for the path component const char *pszPath = poDS->poH5CurrentObject->pszUnderscorePath; if(pszPath != nullptr && strlen(pszPath) > 0) { papszTokens = CSLTokenizeString2(pszPath, "/", CSLT_HONOURSTRINGS); for( hsize_t i = 0; papszTokens != nullptr && papszTokens[i] != nullptr; ++i ) { if( i != 0) osKey += '_'; osKey += papszTokens[i]; } CSLDestroy(papszTokens); } // Convert whitespaces into "_" for the attribute name component papszTokens = CSLTokenizeString2( pszAttrName, " ", CSLT_STRIPLEADSPACES | CSLT_STRIPENDSPACES); for( hsize_t i = 0; papszTokens != nullptr && papszTokens[i] != nullptr; ++i ) { if(!osKey.empty()) osKey += '_'; osKey += papszTokens[i]; } CSLDestroy(papszTokens); const hid_t hAttrID = H5Aopen_name(hH5ObjID, pszAttrName); const hid_t hAttrTypeID = H5Aget_type(hAttrID); const hid_t hAttrNativeType = H5Tget_native_type(hAttrTypeID, H5T_DIR_DEFAULT); const hid_t hAttrSpace = H5Aget_space(hAttrID); if( H5Tget_class(hAttrNativeType) == H5T_VLEN ) return 0; hsize_t nSize[64] = {}; const unsigned int nAttrDims = H5Sget_simple_extent_dims(hAttrSpace, nSize, nullptr); unsigned int nAttrElmts = 1; for( hsize_t i = 0; i < nAttrDims; i++ ) { nAttrElmts *= static_cast<int>(nSize[i]); } char *szData = nullptr; hsize_t nAttrSize = 0; char *szValue = nullptr; if( H5Tget_class(hAttrNativeType) == H5T_STRING ) { if ( H5Tis_variable_str(hAttrNativeType) ) { char **papszStrings = static_cast<char **>(CPLMalloc(nAttrElmts * sizeof(char *))); // Read the values. H5Aread(hAttrID, hAttrNativeType, papszStrings); // Concatenate all values as one string separated by a space. CPLString osVal = papszStrings[0]; for( hsize_t i = 1; i < nAttrElmts; i++ ) { osVal += " "; osVal += papszStrings[i]; } szValue = static_cast<char *>(CPLMalloc(osVal.length() + 1)); strcpy(szValue, osVal.c_str()); H5Dvlen_reclaim(hAttrNativeType, hAttrSpace, H5P_DEFAULT, papszStrings); CPLFree(papszStrings); } else { nAttrSize = H5Aget_storage_size(hAttrID); szValue = static_cast<char *>(CPLMalloc((size_t)(nAttrSize + 1))); H5Aread(hAttrID, hAttrNativeType, szValue); szValue[nAttrSize] = '\0'; } } else { const size_t nDataLen = 8192; void *buf = nullptr; if( nAttrElmts > 0 ) { buf = CPLMalloc(nAttrElmts * H5Tget_size(hAttrNativeType)); szData = static_cast<char *>(CPLMalloc(nDataLen)); szValue = static_cast<char *>(CPLMalloc(MAX_METADATA_LEN)); szData[0] = '\0'; szValue[0] = '\0'; H5Aread(hAttrID, hAttrNativeType, buf); } if( H5Tequal(H5T_NATIVE_CHAR, hAttrNativeType ) || H5Tequal(H5T_NATIVE_SCHAR, hAttrNativeType) ) { for( hsize_t i = 0; i < nAttrElmts; i++ ) { snprintf(szData, nDataLen, "%c ", static_cast<char *>(buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError(CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated"); } } else if( H5Tequal(H5T_NATIVE_UCHAR, hAttrNativeType) ) { for( hsize_t i = 0; i < nAttrElmts; i++ ) { snprintf(szData, nDataLen, "%c", static_cast<char *>(buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError(CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated"); } } else if( H5Tequal(H5T_NATIVE_SHORT, hAttrNativeType) ) { for( hsize_t i = 0; i < nAttrElmts; i++ ) { snprintf(szData, nDataLen, "%d ", static_cast<short *>(buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError(CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated"); } } else if( H5Tequal(H5T_NATIVE_USHORT, hAttrNativeType) ) { for( hsize_t i = 0; i < nAttrElmts; i++ ) { snprintf(szData, nDataLen, "%ud ", static_cast<unsigned short *>(buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError(CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated"); } } else if( H5Tequal(H5T_NATIVE_INT, hAttrNativeType) ) { for( hsize_t i=0; i < nAttrElmts; i++ ) { snprintf(szData, nDataLen, "%d ", static_cast<int *>(buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError(CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated"); } } else if( H5Tequal(H5T_NATIVE_UINT, hAttrNativeType) ) { for( hsize_t i = 0; i < nAttrElmts; i++ ) { snprintf(szData, nDataLen, "%ud ", static_cast<unsigned int *>(buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError(CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated"); } } else if( H5Tequal(H5T_NATIVE_LONG, hAttrNativeType) ) { for( hsize_t i = 0; i < nAttrElmts; i++ ) { snprintf(szData, nDataLen, "%ld ", static_cast<long *>(buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError(CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated"); } } else if( H5Tequal(H5T_NATIVE_ULONG, hAttrNativeType) ) { for( hsize_t i = 0; i < nAttrElmts; i++ ) { snprintf(szData, nDataLen, "%lu ", static_cast<unsigned long *>(buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError(CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated"); } } else if( H5Tequal(H5T_NATIVE_FLOAT, hAttrNativeType) ) { for( hsize_t i = 0; i < nAttrElmts; i++ ) { CPLsnprintf(szData, nDataLen, "%.8g ", static_cast<float *>(buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError(CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated"); } } else if( H5Tequal(H5T_NATIVE_DOUBLE, hAttrNativeType) ) { for( hsize_t i = 0; i < nAttrElmts; i++ ) { CPLsnprintf(szData, nDataLen, "%.15g ", static_cast<double *>(buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError(CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated"); } } CPLFree(buf); } H5Sclose(hAttrSpace); H5Tclose(hAttrNativeType); H5Tclose(hAttrTypeID); H5Aclose(hAttrID); poDS->papszMetadata = CSLSetNameValue(poDS->papszMetadata, osKey, szValue); CPLFree(szData); CPLFree(szValue); return 0; }
static void CorrectURLs( CPLXMLNode * psRoot, const char *pszURL ) { if( psRoot == NULL || pszURL == NULL ) return; if( pszURL[0] == '\0' ) return; CPLXMLNode *psChild = psRoot->psChild; // check for xlink:href attribute while( psChild != NULL && !( ( psChild->eType == CXT_Attribute ) && ( EQUAL(psChild->pszValue, "xlink:href") )) ) psChild = psChild->psNext; if( psChild != NULL && !( strstr( psChild->psChild->pszValue, pszURL ) == psChild->psChild->pszValue && psChild->psChild->pszValue[strlen(pszURL)] == '#' ) ) { //href has a different url size_t nLen; char *pszNew; if( psChild->psChild->pszValue[0] == '#' ) { //empty URL: prepend the given URL nLen = CPLStrnlen( pszURL, 1024 ) + CPLStrnlen( psChild->psChild->pszValue, 1024 ) + 1; pszNew = (char *)CPLMalloc( nLen * sizeof(char)); CPLStrlcpy( pszNew, pszURL, nLen ); CPLStrlcat( pszNew, psChild->psChild->pszValue, nLen ); CPLSetXMLValue( psRoot, "#xlink:href", pszNew ); CPLFree( pszNew ); } else { size_t nPathLen; for( nPathLen = strlen(pszURL); nPathLen > 0 && pszURL[nPathLen - 1] != '/' && pszURL[nPathLen - 1] != '\\'; nPathLen--); if( strncmp( pszURL, psChild->psChild->pszValue, nPathLen ) != 0 ) { //different path int nURLLen = strchr( psChild->psChild->pszValue, '#' ) - psChild->psChild->pszValue; char *pszURLWithoutID = (char *)CPLMalloc( (nURLLen+1) * sizeof(char)); strncpy( pszURLWithoutID, psChild->psChild->pszValue, nURLLen ); pszURLWithoutID[nURLLen] = '\0'; if( CPLIsFilenameRelative( pszURLWithoutID ) && strstr( pszURLWithoutID, ":" ) == NULL ) { //relative URL: prepend the path of pszURL nLen = nPathLen + CPLStrnlen( psChild->psChild->pszValue, 1024 ) + 1; pszNew = (char *)CPLMalloc( nLen * sizeof(char)); size_t i; for( i = 0; i < nPathLen; i++ ) pszNew[i] = pszURL[i]; pszNew[nPathLen] = '\0'; CPLStrlcat( pszNew, psChild->psChild->pszValue, nLen ); CPLSetXMLValue( psRoot, "#xlink:href", pszNew ); CPLFree( pszNew ); } CPLFree( pszURLWithoutID ); } } } // search the child elements of psRoot for( psChild = psRoot->psChild; psChild != NULL; psChild = psChild->psNext) if( psChild->eType == CXT_Element ) CorrectURLs( psChild, pszURL ); }
herr_t HDF5AttrIterate( hid_t hH5ObjID, const char *pszAttrName, void *pDS ) { hid_t hAttrID; hid_t hAttrTypeID; hid_t hAttrNativeType; hid_t hAttrSpace; char *szData = NULL; hsize_t nSize[64]; unsigned int nAttrElmts; hsize_t nAttrSize; hsize_t i; void *buf = NULL; unsigned int nAttrDims; char **papszTokens; HDF5Dataset *poDS; CPLString osKey; char *szValue = NULL; poDS = (HDF5Dataset *) pDS; // Convert "/" into "_" for the path component const char* pszPath = poDS->poH5CurrentObject->pszUnderscorePath; if(pszPath != NULL && strlen(pszPath) > 0) { papszTokens = CSLTokenizeString2( pszPath, "/", CSLT_HONOURSTRINGS ); for( i = 0; papszTokens != NULL && papszTokens[i] != NULL; ++i ) { if( i != 0) osKey += '_'; osKey += papszTokens[i]; } CSLDestroy( papszTokens ); } // Convert whitespaces into "_" for the attribute name component papszTokens = CSLTokenizeString2( pszAttrName, " ", CSLT_STRIPLEADSPACES | CSLT_STRIPENDSPACES ); for( i = 0; papszTokens != NULL && papszTokens[i] != NULL; ++i ) { if(!osKey.empty()) osKey += '_'; osKey += papszTokens[i]; } CSLDestroy( papszTokens ); hAttrID = H5Aopen_name( hH5ObjID, pszAttrName ); hAttrTypeID = H5Aget_type( hAttrID ); hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); hAttrSpace = H5Aget_space( hAttrID ); nAttrDims = H5Sget_simple_extent_dims( hAttrSpace, nSize, NULL ); nAttrElmts = 1; for( i=0; i < nAttrDims; i++ ) { nAttrElmts *= (int) nSize[i]; } if( H5Tget_class( hAttrNativeType ) == H5T_STRING ) { if ( H5Tis_variable_str(hAttrNativeType) ) { char** papszStrings; papszStrings = (char**) CPLMalloc( nAttrElmts * sizeof(char*) ); // Read the values H5Aread( hAttrID, hAttrNativeType, papszStrings ); // Concatenate all values as one string (separated by a space) CPLString osVal = papszStrings[0]; for( i=1; i < nAttrElmts; i++ ) { osVal += " "; osVal += papszStrings[i]; } szValue = (char*) CPLMalloc(osVal.length() + 1); strcpy( szValue, osVal.c_str() ); H5Dvlen_reclaim( hAttrNativeType, hAttrSpace, H5P_DEFAULT, papszStrings ); CPLFree( papszStrings ); } else { nAttrSize = H5Aget_storage_size( hAttrID ); szValue = (char*) CPLMalloc((size_t) (nAttrSize+1)); H5Aread( hAttrID, hAttrNativeType, szValue ); szValue[nAttrSize] = '\0'; } } else { if( nAttrElmts > 0 ) { buf = (void *) CPLMalloc( nAttrElmts* H5Tget_size( hAttrNativeType )); szData = (char*) CPLMalloc( 8192 ); szValue = (char*) CPLMalloc( MAX_METADATA_LEN ); szData[0] = '\0'; szValue[0] ='\0'; H5Aread( hAttrID, hAttrNativeType, buf ); } if( H5Tequal( H5T_NATIVE_CHAR, hAttrNativeType ) || H5Tequal( H5T_NATIVE_SCHAR, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%c ", ((char *) buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_UCHAR, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%c", ((char *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_SHORT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%d ", ((short *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_USHORT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ud ", ((unsigned short *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_INT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%d ", ((int *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_UINT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ud ", ((unsigned int *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_LONG, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ld ", ((long *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_ULONG, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ld ", ((unsigned long *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_FLOAT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { CPLsprintf( szData, "%.8g ", ((float *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_DOUBLE, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { CPLsprintf( szData, "%.15g ", ((double *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } CPLFree( buf ); } H5Sclose(hAttrSpace); H5Tclose(hAttrNativeType); H5Tclose(hAttrTypeID); H5Aclose( hAttrID ); poDS->papszMetadata = CSLSetNameValue( poDS->papszMetadata, osKey, szValue); CPLFree( szData ); CPLFree( szValue ); return 0; }
const char * GDALDefaultCSVFilename( const char *pszBasename ) { /* -------------------------------------------------------------------- */ /* Do we already have this file accessed? If so, just return */ /* the existing path without any further probing. */ /* -------------------------------------------------------------------- */ CSVTable **ppsCSVTableList; ppsCSVTableList = (CSVTable **) CPLGetTLS( CTLS_CSVTABLEPTR ); if( ppsCSVTableList != NULL ) { CSVTable *psTable; int nBasenameLen = strlen(pszBasename); for( psTable = *ppsCSVTableList; psTable != NULL; psTable = psTable->psNext ) { int nFullLen = strlen(psTable->pszFilename); if( nFullLen > nBasenameLen && strcmp(psTable->pszFilename+nFullLen-nBasenameLen, pszBasename) == 0 && strchr("/\\",psTable->pszFilename[+nFullLen-nBasenameLen-1]) != NULL ) { return psTable->pszFilename; } } } /* -------------------------------------------------------------------- */ /* Otherwise we need to look harder for it. */ /* -------------------------------------------------------------------- */ DefaultCSVFileNameTLS* pTLSData = (DefaultCSVFileNameTLS *) CPLGetTLS( CTLS_CSVDEFAULTFILENAME ); if (pTLSData == NULL) { pTLSData = (DefaultCSVFileNameTLS*) CPLCalloc(1, sizeof(DefaultCSVFileNameTLS)); CPLSetTLS( CTLS_CSVDEFAULTFILENAME, pTLSData, TRUE ); } FILE *fp = NULL; const char *pszResult; pszResult = CPLFindFile( "epsg_csv", pszBasename ); if( pszResult != NULL ) return pszResult; if( !pTLSData->bCSVFinderInitialized ) { pTLSData->bCSVFinderInitialized = TRUE; if( CPLGetConfigOption("GEOTIFF_CSV",NULL) != NULL ) CPLPushFinderLocation( CPLGetConfigOption("GEOTIFF_CSV",NULL)); if( CPLGetConfigOption("GDAL_DATA",NULL) != NULL ) CPLPushFinderLocation( CPLGetConfigOption("GDAL_DATA",NULL) ); pszResult = CPLFindFile( "epsg_csv", pszBasename ); if( pszResult != NULL ) return pszResult; } if( (fp = fopen( "csv/horiz_cs.csv", "rt" )) != NULL ) { strcpy( pTLSData->szPath, "csv/" ); CPLStrlcat( pTLSData->szPath, pszBasename, sizeof(pTLSData->szPath) ); } else { #ifdef GDAL_PREFIX #ifdef MACOSX_FRAMEWORK strcpy( pTLSData->szPath, GDAL_PREFIX "/Resources/epsg_csv/" ); CPLStrlcat( pTLSData->szPath, pszBasename, sizeof(pTLSData->szPath) ); #else strcpy( pTLSData->szPath, GDAL_PREFIX "/share/epsg_csv/" ); CPLStrlcat( pTLSData->szPath, pszBasename, sizeof(pTLSData->szPath) ); #endif #else strcpy( pTLSData->szPath, "/usr/local/share/epsg_csv/" ); CPLStrlcat( pTLSData->szPath, pszBasename, sizeof(pTLSData->szPath) ); #endif if( (fp = fopen( pTLSData->szPath, "rt" )) == NULL ) CPLStrlcpy( pTLSData->szPath, pszBasename, sizeof(pTLSData->szPath) ); } if( fp != NULL ) fclose( fp ); return( pTLSData->szPath ); }