PostGIS  2.3.7dev-r@@SVN_REVISION@@
int colmap_read ( const char *  fname,
colmap map,
char *  ebuf,
size_t  ebuflen 
)

Read the content of filename into a symbol map.

The content of the file is lines of two names separated by white space and no trailing or leading space:

COLUMNNAME DBFFIELD1 AVERYLONGCOLUMNNAME DBFFIELD2

etc.

It is the reponsibility of the caller to reclaim the allocated space as follows:

free(map->colmap_pgfieldnames[]) to free the column names free(map->colmap_dbffieldnames[]) to free the dbf field names

TODO: provide a clean_colmap()

Parameters
filenamename of the mapping file
mapcontainer of colmap where the malloc'd symbol map will be stored.
errbufbuffer to write error messages to
errbuflenlength of buffer to write error messages to
Returns
1 on success, 0 on error (and errbuf would be filled)

Definition at line 211 of file shpcommon.c.

References _, colmap_t::dbffieldnames, malloc(), colmap_t::pgfieldnames, and colmap_t::size.

Referenced by ShpDumperOpenTable(), and ShpLoaderOpenShape().

212 {
213  FILE *fptr;
214  char linebuffer[1024];
215  char *tmpstr;
216  int curmapsize, fieldnamesize;
217 
218  /* Read column map file and load the colmap_dbffieldnames
219  * and colmap_pgfieldnames arrays */
220  fptr = fopen(filename, "r");
221  if (!fptr)
222  {
223  /* Return an error */
224  snprintf(errbuf, errbuflen, _("ERROR: Unable to open column map file %s"),
225  filename);
226  return 0;
227  }
228 
229  /* First count how many columns we have... */
230  while (fgets(linebuffer, 1024, fptr) != NULL) ++map->size;
231 
232  /* Now we know the final size, allocate the arrays and load the data */
233  fseek(fptr, 0, SEEK_SET);
234  map->pgfieldnames = (char **)malloc(sizeof(char *) * map->size);
235  map->dbffieldnames = (char **)malloc(sizeof(char *) * map->size);
236 
237  /* Read in a line at a time... */
238  curmapsize = 0;
239  while (fgets(linebuffer, 1024, fptr) != NULL)
240  {
241  /* Split into two separate strings: pgfieldname and dbffieldname */
242  /* First locate end of first column (pgfieldname) */
243  fieldnamesize = strcspn(linebuffer, "\t\n ");
244  tmpstr = linebuffer;
245 
246  /* Allocate memory and copy the string ensuring it is terminated */
247  map->pgfieldnames[curmapsize] = malloc(fieldnamesize + 1);
248  strncpy(map->pgfieldnames[curmapsize], tmpstr, fieldnamesize);
249  map->pgfieldnames[curmapsize][fieldnamesize] = '\0';
250 
251  /* Now swallow up any whitespace */
252  tmpstr = linebuffer + fieldnamesize;
253  tmpstr += strspn(tmpstr, "\t\n ");
254 
255  /* Finally locate end of second column (dbffieldname) */
256  fieldnamesize = strcspn(tmpstr, "\t\n ");
257 
258  /* Allocate memory and copy the string ensuring it is terminated */
259  map->dbffieldnames[curmapsize] = malloc(fieldnamesize + 1);
260  strncpy(map->dbffieldnames[curmapsize], tmpstr, fieldnamesize);
261  map->dbffieldnames[curmapsize][fieldnamesize] = '\0';
262 
263  /* Error out if the dbffieldname is > 10 chars */
264  if (strlen(map->dbffieldnames[curmapsize]) > 10)
265  {
266  snprintf(errbuf, errbuflen, _("ERROR: column map file specifies a DBF field name \"%s\" which is longer than 10 characters"), map->dbffieldnames[curmapsize]);
267  return 0;
268  }
269 
270  ++curmapsize;
271  }
272 
273  fclose(fptr);
274 
275  /* Done; return success */
276  return 1;
277 }
int size
Definition: shpcommon.h:61
#define _(String)
Definition: shpcommon.h:24
char ** pgfieldnames
Definition: shpcommon.h:55
char ** dbffieldnames
Definition: shpcommon.h:58
void * malloc(YYSIZE_T)

Here is the call graph for this function:

Here is the caller graph for this function: