PostGIS  2.2.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 131 of file shpcommon.c.

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

Referenced by ShpDumperOpenTable(), and ShpLoaderOpenShape().

132 {
133  FILE *fptr;
134  char linebuffer[1024];
135  char *tmpstr;
136  int curmapsize, fieldnamesize;
137 
138  /* Read column map file and load the colmap_dbffieldnames
139  * and colmap_pgfieldnames arrays */
140  fptr = fopen(filename, "r");
141  if (!fptr)
142  {
143  /* Return an error */
144  snprintf(errbuf, errbuflen, _("ERROR: Unable to open column map file %s"),
145  filename);
146  return 0;
147  }
148 
149  /* First count how many columns we have... */
150  while (fgets(linebuffer, 1024, fptr) != NULL) ++map->size;
151 
152  /* Now we know the final size, allocate the arrays and load the data */
153  fseek(fptr, 0, SEEK_SET);
154  map->pgfieldnames = (char **)malloc(sizeof(char *) * map->size);
155  map->dbffieldnames = (char **)malloc(sizeof(char *) * map->size);
156 
157  /* Read in a line at a time... */
158  curmapsize = 0;
159  while (fgets(linebuffer, 1024, fptr) != NULL)
160  {
161  /* Split into two separate strings: pgfieldname and dbffieldname */
162  /* First locate end of first column (pgfieldname) */
163  fieldnamesize = strcspn(linebuffer, "\t\n ");
164  tmpstr = linebuffer;
165 
166  /* Allocate memory and copy the string ensuring it is terminated */
167  map->pgfieldnames[curmapsize] = malloc(fieldnamesize + 1);
168  strncpy(map->pgfieldnames[curmapsize], tmpstr, fieldnamesize);
169  map->pgfieldnames[curmapsize][fieldnamesize] = '\0';
170 
171  /* Now swallow up any whitespace */
172  tmpstr = linebuffer + fieldnamesize;
173  tmpstr += strspn(tmpstr, "\t\n ");
174 
175  /* Finally locate end of second column (dbffieldname) */
176  fieldnamesize = strcspn(tmpstr, "\t\n ");
177 
178  /* Allocate memory and copy the string ensuring it is terminated */
179  map->dbffieldnames[curmapsize] = malloc(fieldnamesize + 1);
180  strncpy(map->dbffieldnames[curmapsize], tmpstr, fieldnamesize);
181  map->dbffieldnames[curmapsize][fieldnamesize] = '\0';
182 
183  /* Error out if the dbffieldname is > 10 chars */
184  if (strlen(map->dbffieldnames[curmapsize]) > 10)
185  {
186  snprintf(errbuf, errbuflen, _("ERROR: column map file specifies a DBF field name \"%s\" which is longer than 10 characters"), map->dbffieldnames[curmapsize]);
187  return 0;
188  }
189 
190  ++curmapsize;
191  }
192 
193  fclose(fptr);
194 
195  /* Done; return success */
196  return 1;
197 }
int size
Definition: shpcommon.h:140
#define _(String)
Definition: shpcommon.h:24
char ** pgfieldnames
Definition: shpcommon.h:134
char ** dbffieldnames
Definition: shpcommon.h:137
void * malloc(YYSIZE_T)

Here is the call graph for this function:

Here is the caller graph for this function: