Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
GAIAGEO_DECLARE gaiaTextReaderPtr
gaiaTextReaderAlloc (const char *path, char field_separator,
		     char text_separator, char decimal_separator,
		     int first_line_titles, const char *encoding)
{
/* allocating the main TXT-Reader */
    int col;
    gaiaTextReaderPtr reader;
    FILE *in = fopen (path, "rb");	/* opening the input file */
    if (in == NULL)
	return NULL;

/* allocating and initializing the struct */
    reader = malloc (sizeof (gaiaTextReader));
    if (!reader)
      {
	  fclose (in);
	  return NULL;
      }
    reader->text_file = in;
    reader->field_separator = field_separator;
    reader->text_separator = text_separator;
    reader->decimal_separator = decimal_separator;
    reader->first_line_titles = first_line_titles;
    reader->toUtf8 = gaiaCreateUTF8Converter (encoding);
    if (reader->toUtf8 == (void *) 0)
      {
	  fclose (in);
	  return NULL;
      }
    reader->error = 0;
    reader->first = NULL;
    reader->last = NULL;
    reader->rows = NULL;
    reader->num_rows = 0;
    reader->line_no = 0;
    reader->max_fields = 0;
    reader->max_current_field = 0;
    reader->current_line_ready = 0;
    reader->current_buf_sz = 1024;
    reader->line_buffer = malloc (1024);
    reader->field_buffer = malloc (1024);
    if (reader->line_buffer == NULL || reader->field_buffer == NULL)
      {
	  /* insufficient memory: no input buffers */
	  gaiaTextReaderDestroy (reader);
	  return NULL;
      }
    for (col = 0; col < VRTTXT_FIELDS_MAX; col++)
      {
	  /* initializing column headers */
	  reader->columns[col].name = NULL;
	  reader->columns[col].type = VRTTXT_NULL;
      }
    return reader;
}