PostGIS  2.1.10dev-r@@SVN_REVISION@@
static int read_column_map ( SHPDUMPERSTATE state)
static

Read the content of filename into a symbol map stored at state->column_map_filename.

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(state->column_map_pgfieldnames[]) to free the column names free(state->column_map_dbffieldnames[]) to free the dbf field names

Parameters
state: container of state->column_map where the malloc'd symbol map will be stored.

Definition at line 1168 of file pgsql2shp-core.c.

References _, shp_dumper_state::column_map_dbffieldnames, shp_dumper_config::column_map_filename, shp_dumper_state::column_map_pgfieldnames, shp_dumper_state::column_map_size, shp_dumper_state::config, shp_dumper_state::message, SHPDUMPERERR, SHPDUMPERMSGLEN, and SHPDUMPEROK.

Referenced by ShpDumperOpenTable().

1169 {
1170  FILE *fptr;
1171  char linebuffer[1024];
1172  char *tmpstr, *tmpptr;
1173  int curmapsize, fieldnamesize;
1174 
1175  /* Read column map file and load the column_map_dbffieldnames and column_map_pgfieldnames
1176  arrays */
1177  fptr = fopen(state->config->column_map_filename, "r");
1178  if (!fptr)
1179  {
1180  /* Return an error */
1181  snprintf(state->message, SHPDUMPERMSGLEN, _("ERROR: Unable to open column map file %s"), state->config->column_map_filename);
1182  return SHPDUMPERERR;
1183  }
1184 
1185  /* First count how many columns we have... */
1186  while (fgets(linebuffer, 1024, fptr) != NULL)
1187  state->column_map_size++;
1188 
1189  /* Now we know the final size, allocate the arrays and load the data */
1190  fseek(fptr, 0, SEEK_SET);
1191  state->column_map_pgfieldnames = (char **)malloc(sizeof(char *) * state->column_map_size);
1192  state->column_map_dbffieldnames = (char **)malloc(sizeof(char *) * state->column_map_size);
1193 
1194  /* Read in a line at a time... */
1195  curmapsize = 0;
1196  while (fgets(linebuffer, 1024, fptr) != NULL)
1197  {
1198  /* Split into two separate strings - pgfieldname followed by dbffieldname */
1199 
1200  /* First locate end of first column (pgfieldname) */
1201  for (tmpptr = tmpstr = linebuffer; *tmpptr != '\t' && *tmpptr != '\n' && *tmpptr != ' ' && *tmpptr != '\0'; tmpptr++);
1202  fieldnamesize = tmpptr - tmpstr;
1203 
1204  /* Allocate memory and copy the string ensuring it is terminated */
1205  state->column_map_pgfieldnames[curmapsize] = malloc(fieldnamesize + 1);
1206  strncpy(state->column_map_pgfieldnames[curmapsize], tmpstr, fieldnamesize);
1207  state->column_map_pgfieldnames[curmapsize][fieldnamesize] = '\0';
1208 
1209  /* Now swallow up any whitespace */
1210  for (tmpstr = tmpptr; *tmpptr == '\t' || *tmpptr == '\n' || *tmpptr == ' '; tmpptr++) {
1211  ; /* Do nothing */
1212  }
1213 
1214  /* Finally locate end of second column (dbffieldname) */
1215  for (tmpstr = tmpptr; *tmpptr != '\t' && *tmpptr != '\n' && *tmpptr != ' ' && *tmpptr != '\0'; tmpptr++);
1216  fieldnamesize = tmpptr - tmpstr;
1217 
1218  /* Allocate memory and copy the string ensuring it is terminated */
1219  state->column_map_dbffieldnames[curmapsize] = malloc(fieldnamesize + 1);
1220  strncpy(state->column_map_dbffieldnames[curmapsize], tmpstr, fieldnamesize);
1221  state->column_map_dbffieldnames[curmapsize][fieldnamesize] = '\0';
1222 
1223  /* Error out if the dbffieldname is > 10 chars */
1224  if (strlen(state->column_map_dbffieldnames[curmapsize]) > 10)
1225  {
1226  snprintf(state->message, SHPDUMPERMSGLEN, _("ERROR: column map file specifies a DBF field name \"%s\" which is longer than 10 characters"), state->column_map_dbffieldnames[curmapsize]);
1227  return SHPDUMPERERR;
1228  }
1229 
1230  curmapsize++;
1231  }
1232 
1233  fclose(fptr);
1234 
1235  /* Done; return success */
1236  return SHPDUMPEROK;
1237 }
char * column_map_filename
#define _(String)
Definition: shpcommon.h:23
SHPDUMPERCONFIG * config
char ** column_map_pgfieldnames
#define SHPDUMPERERR
char ** column_map_dbffieldnames
#define SHPDUMPERMSGLEN
#define SHPDUMPEROK
char message[SHPDUMPERMSGLEN]

Here is the caller graph for this function: