PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ colmap_read()

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 213 of file shpcommon.c.

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

Referenced by ShpDumperOpenTable(), and ShpLoaderOpenShape().

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