Ticket #467: shp2pgsql_iconv_error_policy.patch
| File shp2pgsql_iconv_error_policy.patch, 4.7 KB (added by agnat, 2 years ago) |
|---|
-
loader/shp2pgsql-core.c
40 40 * Internal functions 41 41 */ 42 42 43 char *utf8(const char *fromcode, char *inputbuf);43 char *utf8(const SHPLOADERCONFIG *config, char *inputbuf); 44 44 void vasbappend(stringbuffer_t *sb, char *fmt, ... ); 45 45 char *escape_copy_string(char *str); 46 46 char *escape_insert_string(char *str); … … 77 77 78 78 /* Return allocated string containing UTF8 string converted from encoding fromcode */ 79 79 char * 80 utf8(const char *fromcode, char *inputbuf)80 utf8(const SHPLOADERCONFIG *config, char *inputbuf) 81 81 { 82 82 iconv_t cd; 83 83 char *outputptr; … … 87 87 88 88 inbytesleft = strlen(inputbuf); 89 89 90 cd = iconv_open( "UTF-8", fromcode);90 cd = iconv_open(config->target_encoding, config->encoding); 91 91 if ( cd == ((iconv_t)(-1)) ) 92 92 return NULL; 93 93 … … 856 856 config->createindex = 0; 857 857 config->readshape = 1; 858 858 config->encoding = strdup(ENCODING_DEFAULT); 859 config->target_encoding = strdup(ICONV_POLICY_ERROR); 859 860 config->null_policy = POLICY_NULL_INSERT; 860 861 config->sr_id = -1; 861 862 config->hwgeom = 0; … … 1148 1149 if (state->config->encoding) 1149 1150 { 1150 1151 /* If we are converting from another encoding to UTF8, convert the field name to UTF8 */ 1151 utf8str = utf8(state->config ->encoding, name);1152 utf8str = utf8(state->config, name); 1152 1153 if (!utf8str) 1153 1154 { 1154 1155 snprintf(state->message, SHPLOADERMSGLEN, "Unable to convert field name \"%s\" to UTF-8: iconv reports \"%s\"", name, strerror(errno)); … … 1575 1576 if (state->config->encoding) 1576 1577 { 1577 1578 /* If we are converting from another encoding to UTF8, convert the field value to UTF8 */ 1578 utf8str = utf8(state->config ->encoding, val);1579 utf8str = utf8(state->config, val); 1579 1580 if (!utf8str) 1580 1581 { 1581 1582 snprintf(state->message, SHPLOADERMSGLEN, "Unable to convert field value \"%s\" to UTF-8: iconv reports \"%s\"", val, strerror(errno)); -
loader/shp2pgsql-core.h
40 40 41 41 42 42 /* 43 * Error policies for iconv 44 */ 45 46 #define ICONV_POLICY_ERROR "UTF-8" 47 #define ICONV_POLICY_IGNORE "UTF-8//IGNORE" 48 #define ICONV_POLICY_TRANSLIT "UTF-8//TRANSLIT" 49 50 /* 43 51 * Error message handling 44 52 */ 45 53 … … 71 79 #define ENCODING_DEFAULT "WINDOWS-1252" 72 80 73 81 /* 74 * Structure to hold the loader configuration options 82 * Structure to hold the loader configuration options 75 83 */ 76 84 77 85 typedef struct shp_loader_config … … 86 94 char *schema; 87 95 88 96 /* geometry column name to use */ 89 char *geom; 97 char *geom; 90 98 91 99 /* the shape file (without the .shp extension) */ 92 100 char *shp_file; … … 96 104 97 105 /* 0 = MULTIPOLYGON/MULTILINESTRING, 1 = force to POLYGON/LINESTRING */ 98 106 int simple_geometries; 99 107 100 108 /* 0 = geometry, 1 = geography */ 101 109 int geography; 102 110 … … 115 123 /* iconv encoding name */ 116 124 char *encoding; 117 125 126 /* always UTF-8 but with different iconv error policies */ 127 char *target_encoding; 128 118 129 /* how to handle nulls */ 119 130 int null_policy; 120 131 … … 128 139 129 140 130 141 /* 131 * Structure to holder the current loader state 142 * Structure to holder the current loader state 132 143 */ 133 144 134 145 typedef struct shp_loader_state … … 138 149 139 150 /* Shapefile handle */ 140 151 SHPHandle hSHPHandle; 141 152 142 153 /* Shapefile type */ 143 154 int shpfiletype; 144 155 -
loader/shp2pgsql-cli.c
41 41 printf(" -S Generate simple geometries instead of MULTI geometries.\n"); 42 42 printf(" -W <encoding> Specify the character encoding of Shape's\n"); 43 43 printf(" attribute column. (default : \"WINDOWS-1252\")\n"); 44 printf(" -C (e|i|t) Character encoding error policy.\n"); 45 printf(" (e: error, i: ignore, t: transliterate, default: error)\n"); 44 46 printf(" -N <policy> NULL geometries handling policy (insert*,skip,abort)\n"); 45 47 printf(" -n Only import DBF file.\n"); 46 48 printf(" -? Display this help screen.\n"); … … 132 134 config->encoding = optarg; 133 135 break; 134 136 137 case 'C': 138 switch (optarg[0]) 139 { 140 case 'e': 141 config->target_encoding = ICONV_POLICY_ERROR; 142 break; 143 144 case 'i': 145 config->target_encoding = ICONV_POLICY_IGNORE; 146 break; 147 148 case 't': 149 config->target_encoding = ICONV_POLICY_TRANSLIT; 150 break; 151 152 default: 153 fprintf(stderr, "Unsupported character encoding error policy.\nValid options: error, ignore, transliterate\n"); 154 exit(1); 155 } 156 break; 135 157 case 'N': 136 158 switch (optarg[0]) 137 159 {
