/** * Convert a utf8 string to utf16 * @param str the source utf8 string * @param len the length og str to process * @return a utf16 string the caller must free */ UChar *utf8toutf16Len( char *str, int s_len ) { int u_len = measure_from_encoding( str, s_len, "UTF-8" ); UChar *u_str = calloc(u_len+1,sizeof(UChar)); if ( u_str != NULL ) { int res = convert_from_encoding( str, s_len, u_str, u_len+1, "UTF-8" ); if ( res/sizeof(UChar) != u_len ) return NULL; } return u_str; }
void test_encoding( int *passed, int *failed ) { size_t srclen; char *charset="utf-8"; char *src = read_file( "tests/can_1316_01.txt", &srclen ); if ( srclen > 0 ) { size_t dstlen = measure_from_encoding( src, srclen, "utf-8" ); if ( dstlen > 0 ) { UChar *dst = (UChar*)calloc( dstlen+1, sizeof(UChar) ); if ( dst != NULL ) { int res = convert_from_encoding( src, srclen, dst, dstlen+1, charset ); if ( res ) { (*passed)++; size_t dst2len = measure_to_encoding( dst, dstlen, "utf-8" ); char *dst2 = calloc( dst2len+1, sizeof(char) ); if ( dst2 != NULL ) { int res = convert_to_encoding( dst, dstlen, dst2, dst2len+1, "utf-8" ); if ( res ) (*passed)++; else (*failed)++; if ( !data_is_same(dst2,dst2len,src,srclen) ) (*failed)++; else (*passed)++; free( dst2 ); } } else { printf("conversion failed\n"); (*failed)++; } free( dst ); } } free( src ); } }
/** * Read a directory, saving the file information in entries * @param folder the folder where the files are * @param passed VAR param number of passed tests * @param failed VAR param number of failed tests * @return number of files found or 0 on failure */ static int read_dir( char *folder, int *passed, int *failed, plugin_log *log ) { int n_files = 0; DIR *dir; struct dirent *ent; if ((dir = opendir(folder)) != NULL) { while ((ent = readdir(dir)) != NULL) { int flen; int old_passed = *passed; if ( strcmp(ent->d_name,".")!=0&&strcmp(ent->d_name,"..")!=0 ) { char *path = create_path(folder,ent->d_name); //printf("building tree for %s\n",ent->d_name); char *txt = read_file( path, &flen ); if ( txt == NULL ) break; else { int tlen = strlen(txt); long mem2,mem1 = get_mem_usage(); int64_t time2,time1 = epoch_time(); int ulen = measure_from_encoding( txt, flen, "utf-8" ); if ( ulen > 0 ) { UChar *dst = calloc( ulen+1, sizeof(UChar) ); if ( dst != NULL ) { int res = convert_from_encoding( txt, flen, dst, ulen+1, "utf-8" ); if ( res ) { suffixtree *tree = suffixtree_create( dst, ulen, log ); if ( tree != NULL ) { mem2 = get_mem_usage(); time2 = epoch_time(); entry *e = calloc( 1, sizeof(entry) ); if ( e != NULL ) { e->file = strdup(ent->d_name); e->space = mem2-mem1; e->time = time2-time1; e->size = flen; append_entry( e ); (*passed)++; n_files++; } else { n_files = 0; dispose_entries(); fprintf(stderr, "test: failed to allocate entry\n"); break; } suffixtree_dispose( tree ); } } free(dst); } } free( txt ); } if ( *passed == old_passed ) { (*failed)++; fprintf(stderr,"suffixtree: failed to create tree %s\n",path); } if ( path != NULL ) free( path ); } } closedir( dir ); } else fprintf(stderr,"test: failed to open directory %s\n",folder); return n_files; }