// from ogr_srs_esri.cpp static std::map<GByte,GByte> InitReplaceTable(const char *pszReplaceFilename, const char *pszReplaceFieldFrom, const char *pszReplaceFieldTo) { std::map<GByte,GByte> mReplaceTable; /* -------------------------------------------------------------------- */ /* Try to open the file. */ /* -------------------------------------------------------------------- */ FILE * fp = VSIFOpen( pszReplaceFilename, "rb" ); if( fp == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to load csv file." ); return mReplaceTable; } /* -------------------------------------------------------------------- */ /* Figure out what fields we are interested in. */ /* -------------------------------------------------------------------- */ char **papszFieldNames = CSVReadParseLine( fp ); int nReplaceFieldFrom = CSLFindString( papszFieldNames, pszReplaceFieldFrom ); int nReplaceFieldTo = CSLFindString( papszFieldNames, pszReplaceFieldTo ); CSLDestroy( papszFieldNames ); if( nReplaceFieldFrom == -1 || nReplaceFieldTo == -1 ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to find info in csv file." ); return mReplaceTable; } /* -------------------------------------------------------------------- */ /* Read each line, adding a detail line for each. */ /* -------------------------------------------------------------------- */ char **papszFields; for( papszFields = CSVReadParseLine( fp ); papszFields != NULL; papszFields = CSVReadParseLine( fp ) ) { if ( strcmp (papszFields[nReplaceFieldFrom],"") != 0 ) { printf("read %s - %s - %d - %d\n",papszFields[nReplaceFieldFrom],papszFields[nReplaceFieldTo], atoi(papszFields[nReplaceFieldFrom]), atoi(papszFields[nReplaceFieldTo])); mReplaceTable[atoi(papszFields[nReplaceFieldFrom])] = atoi(papszFields[nReplaceFieldTo]) ; } } CSLDestroy( papszFields ); VSIFClose( fp ); return mReplaceTable; }
CSVTable *CSVAccess( const char * pszFilename ) { CSVTable *psTable; FILE *fp; /* -------------------------------------------------------------------- */ /* Is the table already in the list. */ /* -------------------------------------------------------------------- */ for( psTable = psCSVTableList; psTable != NULL; psTable = psTable->psNext ) { if( EQUAL(psTable->pszFilename,pszFilename) ) { /* * Eventually we should consider promoting to the front of * the list to accelerate frequently accessed tables. */ return( psTable ); } } /* -------------------------------------------------------------------- */ /* If not, try to open it. */ /* -------------------------------------------------------------------- */ fp = VSIFOpen( pszFilename, "rb" ); if( fp == NULL ) return NULL; /* -------------------------------------------------------------------- */ /* Create an information structure about this table, and add to */ /* the front of the list. */ /* -------------------------------------------------------------------- */ psTable = (CSVTable *) CPLCalloc(sizeof(CSVTable),1); psTable->fp = fp; psTable->pszFilename = CPLStrdup( pszFilename ); psTable->psNext = psCSVTableList; psCSVTableList = psTable; /* -------------------------------------------------------------------- */ /* Read the table header record containing the field names. */ /* -------------------------------------------------------------------- */ psTable->papszFieldNames = CSVReadParseLine( fp ); return( psTable ); }
int CSVGetFieldId( FILE * fp, const char * pszFieldName ) { char **papszFields; int i; CPLAssert( fp != NULL && pszFieldName != NULL ); VSIRewind( fp ); papszFields = CSVReadParseLine( fp ); for( i = 0; papszFields != NULL && papszFields[i] != NULL; i++ ) { if( EQUAL(papszFields[i],pszFieldName) ) { CSLDestroy( papszFields ); return i; } } CSLDestroy( papszFields ); return -1; }
static CSVTable *CSVAccess( const char * pszFilename ) { CSVTable *psTable; FILE *fp; /* -------------------------------------------------------------------- */ /* Fetch the table, and allocate the thread-local pointer to it */ /* if there isn't already one. */ /* -------------------------------------------------------------------- */ CSVTable **ppsCSVTableList; ppsCSVTableList = (CSVTable **) CPLGetTLS( CTLS_CSVTABLEPTR ); if( ppsCSVTableList == NULL ) { ppsCSVTableList = (CSVTable **) CPLCalloc(1,sizeof(CSVTable*)); CPLSetTLSWithFreeFunc( CTLS_CSVTABLEPTR, ppsCSVTableList, CSVFreeTLS ); } /* -------------------------------------------------------------------- */ /* Is the table already in the list. */ /* -------------------------------------------------------------------- */ for( psTable = *ppsCSVTableList; psTable != NULL; psTable = psTable->psNext ) { if( EQUAL(psTable->pszFilename,pszFilename) ) { /* * Eventually we should consider promoting to the front of * the list to accelerate frequently accessed tables. */ return( psTable ); } } /* -------------------------------------------------------------------- */ /* If not, try to open it. */ /* -------------------------------------------------------------------- */ fp = VSIFOpen( pszFilename, "rb" ); if( fp == NULL ) return NULL; /* -------------------------------------------------------------------- */ /* Create an information structure about this table, and add to */ /* the front of the list. */ /* -------------------------------------------------------------------- */ psTable = (CSVTable *) CPLCalloc(sizeof(CSVTable),1); psTable->fp = fp; psTable->pszFilename = CPLStrdup( pszFilename ); psTable->bNonUniqueKey = FALSE; /* as far as we know now */ psTable->psNext = *ppsCSVTableList; *ppsCSVTableList = psTable; /* -------------------------------------------------------------------- */ /* Read the table header record containing the field names. */ /* -------------------------------------------------------------------- */ psTable->papszFieldNames = CSVReadParseLine( fp ); return( psTable ); }
static void CSV2HTML( const char * pszFilename, int nColumns, int * panColumns, char ** papszOptions, int bSingletons ) { FILE *fp; char **papszFields, **papszFieldNames; int iCol, nColCount; /* -------------------------------------------------------------------- */ /* Open the source file. */ /* -------------------------------------------------------------------- */ fp = VSIFOpen( pszFilename, "rt" ); if( fp == NULL ) { perror( "fopen" ); return; } /* -------------------------------------------------------------------- */ /* Read and emit the title line specially. */ /* -------------------------------------------------------------------- */ papszFieldNames = CSVReadParseLine( fp ); nColCount = CSLCount( papszFieldNames ); if( nColumns == 0 ) { nColumns = nColCount; panColumns = (int *) CPLMalloc(sizeof(int) * nColCount); for( iCol = 0; iCol < nColCount; iCol++ ) panColumns[iCol] = iCol; } printf( "<table border>\n" ); if( !bSingletons ) { for( iCol = 0; iCol < nColumns; iCol++ ) { if( panColumns[iCol] < nColCount ) { printf( "<th>%s\n", papszFieldNames[panColumns[iCol]] ); } } printf( "<tr>\n" ); } /* -------------------------------------------------------------------- */ /* Read and emit normal records. */ /* -------------------------------------------------------------------- */ while( (papszFields = CSVReadParseLine( fp )) != NULL ) { int bDisplay=TRUE, i; nColCount = CSLCount( papszFields ); for( i = 0; papszOptions != NULL && papszOptions[i] != NULL; i++ ) { if( EQUALN(papszOptions[i],"CODE=",5) ) { if( atoi(papszOptions[i]+5) != atoi(papszFields[0]) ) bDisplay = FALSE; } else if( EQUALN(papszOptions[i],"CODE<",5) ) { if( atoi(papszOptions[i]+5) <= atoi(papszFields[0]) ) bDisplay = FALSE; } else if( EQUALN(papszOptions[i],"CODE>",5) ) { if( atoi(papszOptions[i]+5) >= atoi(papszFields[0]) ) bDisplay = FALSE; } else if( EQUALN(papszOptions[i],"NAMEKEY=",8) ) { if( strstr(papszFields[1],papszOptions[i]+8) == NULL ) bDisplay = FALSE; } } if( bDisplay ) { for( iCol = 0; iCol < nColumns; iCol++ ) { const char *pszSubTable = NULL; const char *pszFieldName; if( panColumns[iCol] < 0 || panColumns[iCol] >= nColCount ) continue; pszFieldName = papszFieldNames[panColumns[iCol]]; if( bSingletons ) { printf( "<td>%s\n", pszFieldName ); } if( EQUAL(pszFieldName,"PRIME_MERIDIAN_CODE") ) pszSubTable = "p_meridian"; else if( EQUAL(pszFieldName,"GEOD_DATUM_CODE") ) pszSubTable = "geod_datum"; else if( EQUAL(pszFieldName,"UOM_LENGTH_CODE") ) pszSubTable = "uom_length"; else if( EQUAL(pszFieldName,"UOM_ANGLE_CODE") ) pszSubTable = "uom_angle"; else if( EQUAL(pszFieldName,"SOURCE_GEOGCS_CODE") ) pszSubTable = "horiz_cs"; else if( EQUAL(pszFieldName,"PROJECTION_TRF_CODE") ) pszSubTable = "trf_nonpolynomial"; else if( EQUAL(pszFieldName,"ELLIPSOID_CODE") ) pszSubTable = "ellipsoid"; else if( EQUAL(pszFieldName,"COORD_TRF_METHOD_CODE") ) pszSubTable = "trf_method"; if( pszSubTable != NULL ) printf( "<td><a href=" "\"/cgi-bin/csv2html/TABLE=%s/CODE=%s/\">" "%s</a>\n", pszSubTable,papszFields[panColumns[iCol]], papszFields[panColumns[iCol]] ); else printf( "<td>%s\n", papszFields[panColumns[iCol]] ); if( bSingletons ) printf( "<tr>\n" ); } if( !bSingletons ) printf( "<tr>\n" ); } CSLDestroy( papszFields ); } printf( "</table>\n" ); CSLDestroy( papszFieldNames ); VSIFClose( fp ); }