int main (int argc, char *argv[]) { #ifndef OMIT_ICONV /* only if ICONV is supported */ void * converter; char *test_str1; int err; asprintf(&test_str1, "Hello World"); gaiaConvertCharset(&test_str1, "ASCII", "UTF-8"); if (strcmp(test_str1, "Hello World") != 0) { fprintf(stderr, "bad ASCII to UTF-8 conversion: %s\n", test_str1); free(test_str1); return -1; } free(test_str1); #if 0 /* TODO: this will cause a buffer overflow */ asprintf(&test_str1, "Hello World"); gaiaConvertCharset(&test_str1, "ASCII", "UTF-16LE"); if (memcmp(test_str1, "H\0e\0l\0l\0o\0 \0W\0o\0r\0l\0d\0\0\0", 24) != 0) { fprintf(stderr, "bad ASCII to UTF-16LE conversion\n"); free(test_str1); return -2; } free(test_str1); #endif converter = gaiaCreateUTF8Converter ("CP1252"); if (! converter) { fprintf(stderr, "null UTF8 converter\n"); return -3; } test_str1 = gaiaConvertToUTF8(converter, "Hello world", strlen("Hello world"), &err); if (memcmp("Hello world", test_str1, strlen("Hello world") + 1) != 0) { fprintf(stderr, "bad conversion to UTF8: %s\n", test_str1); free(test_str1); return -4; } free(test_str1); gaiaFreeUTF8Converter (converter); converter = NULL; /* test null converter */ gaiaFreeUTF8Converter (converter); test_str1 = gaiaConvertToUTF8(converter, "Hello world", strlen("Hello world"), &err); if ((test_str1 != NULL) || (err != 1)) { fprintf(stderr, "unexpected null converter result: %s, %i\n", test_str1, err); return -5; } /* there is no sane way to test this automatically */ printf("Local codeset: %s\n", gaiaGetLocaleCharset() ); #endif /* end ICONV conditional */ return 0; }
static int vrttxt_set_column_title (gaiaTextReaderPtr txt, int col_no, const char *name) { /* setting a Column header name */ int err; int ind; char *utf8text; char *str = (char *) name; int len = strlen (str); if (len <= 0) return 0; if (str[0] == txt->text_separator && str[len - 1] == txt->text_separator) { /* cleaning the enclosing quotes */ str[len - 1] = '\0'; str = (char *) (name + 1); len -= 2; if (len <= 0) return 0; } utf8text = gaiaConvertToUTF8 (txt->toUtf8, str, len, &err); if (err) { if (utf8text) free (utf8text); return 0; } else str = utf8text; len = strlen (str); for (ind = 0; ind < len; ind++) { /* masking spaces and so on within the column name */ switch (str[ind]) { case ' ': case '\t': case '-': case '+': case '*': case '/': case '(': case ')': case '[': case ']': case '{': case '}': str[ind] = '_'; break; } } if (txt->columns[col_no].name) free (txt->columns[col_no].name); txt->columns[col_no].name = malloc (len + 1); if (txt->columns[col_no].name == NULL) return 0; strcpy (txt->columns[col_no].name, utf8text); free (utf8text); return 1; }
GAIAGEO_DECLARE int gaiaTextReaderFetchField (gaiaTextReaderPtr txt, int field_idx, int *type, const char **value) { /* fetching a field value */ char *utf8text = NULL; int err; int len; char *str; if (txt->current_line_ready == 0) { *type = VRTTXT_NULL; *value = NULL; return 0; } if (field_idx < 0 || field_idx >= txt->max_fields) { *type = VRTTXT_NULL; *value = NULL; return 0; } if (field_idx < 0 || field_idx >= txt->max_current_field) { *type = VRTTXT_NULL; *value = NULL; return 0; } *type = txt->columns[field_idx].type; if (txt->field_lens[field_idx] == 0) *(txt->field_buffer) = '\0'; memcpy (txt->field_buffer, txt->line_buffer + txt->field_offsets[field_idx], txt->field_lens[field_idx]); *(txt->field_buffer + txt->field_lens[field_idx]) = '\0'; *value = txt->field_buffer; /* sandro 2012-02-01: fixing CR handling for last column [windows] */ if (*(txt->field_buffer) == '\r' && txt->field_lens[field_idx] == 1 && (field_idx + 1) == txt->max_fields) *(txt->field_buffer) = '\0'; if (*(txt->field_buffer) == '\0') *type = VRTTXT_NULL; else if (*type == VRTTXT_TEXT) { /* converting to UTF-8 */ str = (char *) *value; len = strlen (str); if (str[len - 1] == '\r') { /* skipping trailing CR, if any */ str[len - 1] = '\0'; len--; } if (str[0] == txt->text_separator && str[len - 1] == txt->text_separator) { /* cleaning the enclosing quotes */ str[len - 1] = '\0'; str = (char *) (*value + 1); len -= 2; if (len <= 0) { *type = VRTTXT_NULL; *value = NULL; return 1; } } utf8text = gaiaConvertToUTF8 (txt->toUtf8, str, len, &err); if (err) { /* memory cleanup: Kashif Rasul 14 Jan 2010 */ if (utf8text) free (utf8text); *type = VRTTXT_NULL; *value = NULL; return 0; } *value = utf8text; } return 1; }