PostGIS  3.4.0dev-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 217 of file shpcommon.c.

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

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

Referenced by ShpDumperOpenTable(), and ShpLoaderOpenShape().

Here is the call graph for this function:
Here is the caller graph for this function: