00001 /********************************************************************** 00002 * $Id: cpl_csv_incode_c-source.html 385 2001-03-05 04:58:33Z warmerda $ 00003 * 00004 * Name: cpl_struct.c 00005 * Project: libgeotiff 00006 * Purpose: Provide "CSV API" interface to in-code EPSG CSV tables. 00007 * Author: Derrick J Brashear <shadow@dementia.org> 00008 * 00009 ****************************************************************************** 00010 * Copyright (c) 1999, Frank Warmerdam 00011 * 00012 * Permission is hereby granted, free of charge, to any person obtaining a 00013 * copy of this software and associated documentation files (the "Software"), 00014 * to deal in the Software without restriction, including without limitation 00015 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00016 * and/or sell copies of the Software, and to permit persons to whom the 00017 * Software is furnished to do so, subject to the following conditions: 00018 * 00019 * The above copyright notice and this permission notice shall be included 00020 * in all copies or substantial portions of the Software. 00021 * 00022 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00023 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00024 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00025 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00026 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00027 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00028 * DEALINGS IN THE SOFTWARE. 00029 ****************************************************************************** 00030 * 00031 * $Log$ 00031 * Revision 1.2 2001/03/05 04:58:33 warmerda 00031 * updated 00031 * 00032 * Revision 1.1 1999/12/10 18:00:58 warmerda 00033 * New 00034 * 00035 */ 00036 00037 #include <stdio.h> 00038 #include "cpl_csv.h" 00039 00040 #include "defs.h" 00041 00042 extern const datafile_rows_t *compd_cs_rows[]; 00043 extern const datafile_rows_t *ellips_alias_rows[]; 00044 extern const datafile_rows_t *ellipsoid_rows[]; 00045 extern const datafile_rows_t *gdatum_alias_rows[]; 00046 extern const datafile_rows_t *geod_datum_rows[]; 00047 extern const datafile_rows_t *geod_trf_rows[]; 00048 extern const datafile_rows_t *horiz_cs_rows[]; 00049 extern const datafile_rows_t *p_meridian_rows[]; 00050 extern const datafile_rows_t *trf_nonpolynomial_rows[]; 00051 extern const datafile_rows_t *trf_path_rows[]; 00052 extern const datafile_rows_t *uom_an_alias_rows[]; 00053 extern const datafile_rows_t *uom_angle_rows[]; 00054 extern const datafile_rows_t *uom_le_alias_rows[]; 00055 extern const datafile_rows_t *uom_length_rows[]; 00056 extern const datafile_rows_t *uom_scale_rows[]; 00057 extern const datafile_rows_t *uom_sc_alias_rows[]; 00058 extern const datafile_rows_t *vert_cs_rows[]; 00059 extern const datafile_rows_t *vert_datum_rows[]; 00060 extern const datafile_rows_t *vert_offset_rows[]; 00061 00062 00063 /* Pointers to data */ 00064 static const datafile_t files[] = { 00065 { "compd_cs", compd_cs_rows }, 00066 { "ellips_alias", ellips_alias_rows }, 00067 { "ellipsoid", ellipsoid_rows }, 00068 { "gdatum_alias", gdatum_alias_rows }, 00069 { "geod_datum", geod_datum_rows }, 00070 { "geod_trf", geod_trf_rows }, 00071 { "horiz_cs", horiz_cs_rows }, 00072 { "p_meridian", p_meridian_rows }, 00073 { "trf_nonpolynomial", trf_nonpolynomial_rows }, 00074 { "trf_path", trf_path_rows }, 00075 { "uom_an_alias", uom_an_alias_rows }, 00076 { "uom_angle", uom_angle_rows }, 00077 { "uom_le_alias", uom_le_alias_rows }, 00078 { "uom_length", uom_length_rows }, 00079 { "uom_sc_alias", uom_sc_alias_rows }, 00080 { "uom_scale", uom_scale_rows }, 00081 { "vert_cs", vert_cs_rows }, 00082 { "vert_datum", vert_datum_rows }, 00083 { "vert_offset", vert_offset_rows }, 00084 { NULL, NULL } 00085 }; 00086 00087 #if 0 00088 main() 00089 { 00090 datafile_t *csvfile; 00091 char **papszRecord; 00092 int i, row, col; 00093 00094 papszRecord = CSVScanFileByName( "horiz_cs.csv", "HORIZCS_CODE", 00095 "2200", CC_Integer ); 00096 00097 printf ("%s %s\n", papszRecord[0], papszRecord[1]); 00098 csvfile = &files[0]; 00099 while (csvfile->name != NULL) { 00100 printf("file name is %s\n", csvfile->name); 00101 for (i = 0; csvfile->cols[i]; i++) { 00102 printf("column name is %s\n", csvfile->cols[i]); 00103 row = 1; col = 0; 00104 if (csvfile->rows[row] && csvfile->rows[row][col]) 00105 printf("value %d,%d is %s\n", row, col, csvfile->rows[row][col]); 00106 } 00107 csvfile++; 00108 } 00109 } 00110 #endif 00111 00112 const char * CSVFilename( const char *pszBasename ) 00113 { 00114 return pszBasename; 00115 } 00116 00117 static int CSVCompare( const char * pszFieldValue, const char * pszTarget, 00118 CSVCompareCriteria eCriteria ) 00119 { 00120 if( eCriteria == CC_ExactString ) 00121 { 00122 return( strcmp( pszFieldValue, pszTarget ) == 0 ); 00123 } 00124 else if( eCriteria == CC_ApproxString ) 00125 { 00126 return( strcasecmp( pszFieldValue, pszTarget ) ); 00127 } 00128 else if( eCriteria == CC_Integer ) 00129 { 00130 return( atoi(pszFieldValue) == atoi(pszTarget) ); 00131 } 00132 return FALSE; 00133 } 00134 00135 static int __CSVGetFileId( const char * pszFilename) 00136 { 00137 datafile_t *csvfile; 00138 int i = 0; 00139 00140 csvfile = (datafile_t *)&files[i]; 00141 for (i = 0; csvfile->name; i++, csvfile = (datafile_t *)&files[i]) 00142 { 00143 if (!strncmp(csvfile->name,pszFilename,(strlen(pszFilename)-4))) 00144 return i; 00145 } 00146 00147 return -1; 00148 } 00149 00150 char **CSVScanFileByName( const char * pszFilename, 00151 const char * pszKeyFieldName, 00152 const char * pszValue, 00153 CSVCompareCriteria eCriteria ) 00154 { 00155 datafile_t *csvfile; 00156 int row, col; 00157 00158 col = CSVGetFileFieldId(pszFilename, pszKeyFieldName); 00159 00160 csvfile = (datafile_t *)&files[__CSVGetFileId(pszFilename)]; 00161 for (row = 1; ((csvfile->rows[row] != 0) && (csvfile->rows[row][col] != 0)); 00162 row++) { 00163 if (CSVCompare(csvfile->rows[row][col], pszValue, eCriteria)) 00164 return ((char **)csvfile->rows[row]); 00165 } 00166 00167 return NULL; 00168 } 00169 00170 int CSVGetFileFieldId( const char * pszFilename, const char * pszFieldName) 00171 { 00172 datafile_t *csvfile; 00173 int i; 00174 00175 csvfile = (datafile_t *)&files[__CSVGetFileId(pszFilename)]; 00176 if (!strncmp(csvfile->name,pszFilename,(strlen(pszFilename)-4))) 00177 { 00178 for (i = 0; csvfile->rows[0][i]; i++) 00179 if (!strcasecmp(pszFieldName, csvfile->rows[0][i])) 00180 return i; 00181 } 00182 00183 return -1; 00184 } 00185 00186 const char *CSVGetField( const char * pszFilename, 00187 const char * pszKeyFieldName, 00188 const char * pszKeyFieldValue, 00189 CSVCompareCriteria eCriteria, 00190 const char * pszTargetField ) 00191 { 00192 char **papszRecord; 00193 00194 papszRecord = CSVScanFileByName( pszFilename, pszKeyFieldName, 00195 pszKeyFieldValue, CC_Integer ); 00196 00197 return (papszRecord[CSVGetFileFieldId(pszFilename, pszTargetField)]); 00198 } 00199 00200 /* Dummy function */ 00201 void SetCSVFilenameHook( const char *(*pfnNewHook)( const char * ) ) 00202 00203 { 00204 }