From f1ef4efb60e341a2a8ec72560071656b9d8b927d Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Sat, 15 Nov 2008 22:14:21 +0100 Subject: Encoding revision --- common/strutils.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ common/strutils.h | 1 + common/treepathutils.c | 13 +++++++---- common/treepathutils.h | 7 +++--- 4 files changed, 75 insertions(+), 7 deletions(-) (limited to 'common') diff --git a/common/strutils.c b/common/strutils.c index 47494de..ffd95c3 100644 --- a/common/strutils.c +++ b/common/strutils.c @@ -193,3 +193,64 @@ char* resolve_relative(const char *source, const char *point_to) return canon; } + + + +// Originally stolen from wine-0.9.19, utf8.c +// Copyright 2000 Alexandre Julliard +char* +wide_to_utf8 (const wchar_t *src) +{ +#define CONV_BUFF_MAX 32768 + int len; + char *buf, *dst, *ret; + + buf = (char *) malloc (CONV_BUFF_MAX); + memset (&buf[0], 0, CONV_BUFF_MAX); + dst = buf; + + if (src) + for (len = CONV_BUFF_MAX; *src; src++) + { + wchar_t ch = *src; + + if (ch < 0x80) /* 0x00-0x7f: 1 byte */ + { + if (!len--) { + log ("wide_to_utf8: error converting input string, overflow.\n"); + break; /* overflow */ + } + *dst++ = ch; + continue; + } + + if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */ + { + if ((len -= 2) < 0) { + log ("wide_to_utf8: error converting input string, overflow.\n"); + break; /* overflow */ + } + dst[1] = 0x80 | (ch & 0x3f); + ch >>= 6; + dst[0] = 0xc0 | ch; + dst += 2; + continue; + } + + /* 0x800-0xffff: 3 bytes */ + if ((len -= 3) < 0) { + log ("wide_to_utf8: error converting input string, overflow.\n"); + break; /* overflow */ + } + dst[2] = 0x80 | (ch & 0x3f); + ch >>= 6; + dst[1] = 0x80 | (ch & 0x3f); + ch >>= 6; + dst[0] = 0xe0 | ch; + dst += 3; + } + + ret = g_strdup (buf); + free (buf); + return ret; +} diff --git a/common/strutils.h b/common/strutils.h index 872365c..84f6013 100644 --- a/common/strutils.h +++ b/common/strutils.h @@ -46,5 +46,6 @@ char* extract_file_path(const char *APath); char* resolve_relative(const char *source, const char *point_to); char* canonicalize_filename(const char *filename); +char* wide_to_utf8(const wchar_t *src); #endif /* __STRUTILS_H__ */ diff --git a/common/treepathutils.c b/common/treepathutils.c index d8a5f10..d3ace5a 100644 --- a/common/treepathutils.c +++ b/common/treepathutils.c @@ -42,6 +42,7 @@ struct PathTree* filelist_tree_new() tree->data = NULL; tree->index = 0; tree->node = strdup("/"); + tree->original_pathstr = NULL; // create placeholder data tree->data = (struct TVFSItem*)malloc(sizeof(struct TVFSItem)); @@ -83,6 +84,8 @@ void filelist_tree_free(struct PathTree *tree) } if (tree->node) free(tree->node); + if (tree->original_pathstr) + free(tree->original_pathstr); free(tree); } } @@ -182,7 +185,7 @@ unsigned long int filelist_find_index_by_path(struct PathTree *tree, const char else return 0; } -void filelist_tree_add_item_recurr(struct PathTree *tree, const char *path, struct TVFSItem *item, unsigned long index) +void filelist_tree_add_item_recurr(struct PathTree *tree, const char *path, const char *original_pathstr, struct TVFSItem *item, unsigned long index) { char *pos = strstr(path, "/"); char *first_part; @@ -208,6 +211,7 @@ void filelist_tree_add_item_recurr(struct PathTree *tree, const char *path, stru t->data = item; t->index = index; t->node = strdup(path); + if (original_pathstr) t->original_pathstr = strdup(original_pathstr); if (t->data) t->data->FName = strdup(path); if (t->data) t->data->FDisplayName = strdup(path); // create new list of subitems and add new item @@ -240,6 +244,7 @@ void filelist_tree_add_item_recurr(struct PathTree *tree, const char *path, stru node->items = g_ptr_array_new(); node->index = 0; node->node = strdup(first_part); + node->original_pathstr = NULL; // create placeholder data node->data = (struct TVFSItem*)malloc(sizeof(struct TVFSItem)); @@ -258,7 +263,7 @@ void filelist_tree_add_item_recurr(struct PathTree *tree, const char *path, stru } // and recurse one level deeper - filelist_tree_add_item_recurr(node, last_part, item, index); + filelist_tree_add_item_recurr(node, last_part, original_pathstr, item, index); } free(first_part); @@ -266,7 +271,7 @@ void filelist_tree_add_item_recurr(struct PathTree *tree, const char *path, stru } -gboolean filelist_tree_add_item(struct PathTree *tree, const char *path, struct TVFSItem *item, unsigned long index) +gboolean filelist_tree_add_item(struct PathTree *tree, const char *path, const char *original_pathstr, struct TVFSItem *item, unsigned long index) { if (! tree) { fprintf(stderr, "filelist_tree_add_item: tree == NULL !\n"); @@ -306,7 +311,7 @@ gboolean filelist_tree_add_item(struct PathTree *tree, const char *path, struct if (found->data) found->data->FDisplayName = strdup(found->node); } else // create new item recursively - filelist_tree_add_item_recurr(tree, pp, item, index); + filelist_tree_add_item_recurr(tree, pp, original_pathstr, item, index); free(p); free(pp); diff --git a/common/treepathutils.h b/common/treepathutils.h index 13f3057..a902a33 100644 --- a/common/treepathutils.h +++ b/common/treepathutils.h @@ -33,6 +33,7 @@ struct PathTree { struct TVFSItem *data; unsigned long index; char *node; + char *original_pathstr; }; @@ -40,11 +41,11 @@ struct PathTree* filelist_tree_new(); void filelist_tree_free(struct PathTree *tree); void filelist_tree_print(struct PathTree *tree); -/* Symlink resolving: strongly discouraged to use at the present state of art. - * We would have to implement full symlink system, do loop checking etc. */ +/* Symlink resolving: strongly discouraged to use at the present state of art. + * We would have to implement full symlink system, do loop checking etc. */ void filelist_tree_resolve_symlinks(struct PathTree *tree); -gboolean filelist_tree_add_item(struct PathTree *tree, const char *path, struct TVFSItem *item, unsigned long index); +gboolean filelist_tree_add_item(struct PathTree *tree, const char *path, const char *original_pathstr, struct TVFSItem *item, unsigned long index); struct PathTree* filelist_tree_find_node_by_path(struct PathTree *tree, const char *path); unsigned long int filelist_find_index_by_path(struct PathTree *tree, const char *path); struct PathTree* filelist_tree_get_item_by_index(struct PathTree *tree, unsigned long index); -- cgit v1.2.3