]> mj.ucw.cz Git - libucw.git/commitdiff
Conversion between charsets with allocation on the stack
authorPavel Charvat <pchar@camellia.ms.mff.cuni.cz>
Sat, 4 Mar 2006 18:10:05 +0000 (19:10 +0100)
committerPavel Charvat <pchar@camellia.ms.mff.cuni.cz>
Sat, 4 Mar 2006 18:10:05 +0000 (19:10 +0100)
or a given memory pool. I will propably add some optimalizations...

charset/Makefile
charset/charconv.h
charset/mp-charconv.c [new file with mode: 0644]
charset/mp-charconv.h [new file with mode: 0644]
charset/stk-charconv.c [new file with mode: 0644]
charset/stk-charconv.h [new file with mode: 0644]

index 64a8119e4baae0fb623bf74a88c9b3e677be8ffc..dc8f46810554bc6923d4e875473f04f463b46728 100644 (file)
@@ -2,8 +2,8 @@
 
 DIRS+=charset
 
-LIBCHARSET_MODS=toupper tolower tocat toligatures unaccent charconv setnames fb-charconv
-LIBCHARSET_INCLUDES=charconv.h unicat.h fb-charconv.h
+LIBCHARSET_MODS=toupper tolower tocat toligatures unaccent charconv setnames fb-charconv stk-charconv mp-charconv
+LIBCHARSET_INCLUDES=charconv.h unicat.h fb-charconv.h stk-charconv.h mp-charconv.h
 
 $(o)/charset/libcharset.a: $(addsuffix .o,$(addprefix $(o)/charset/,$(LIBCHARSET_MODS)))
 $(o)/charset/libcharset.so: $(addsuffix .oo,$(addprefix $(o)/charset/,$(LIBCHARSET_MODS)))
index d3482438b3484bfa3eb59ec024fefa572b34e53d..d14493c16b8c241f27aa90280e15865cc2dd8024 100644 (file)
@@ -7,6 +7,9 @@
  *     of the GNU Lesser General Public License.
  */
 
+#ifndef _CHARCONV_H
+#define _CHARCONV_H
+
 struct conv_context {
 
   /* Parameters supplied by the caller */
@@ -77,3 +80,5 @@ unsigned int conv_x_count(void);
 
 int find_charset_by_name(char *);
 char *charset_name(int);
+
+#endif
diff --git a/charset/mp-charconv.c b/charset/mp-charconv.c
new file mode 100644 (file)
index 0000000..731d591
--- /dev/null
@@ -0,0 +1,17 @@
+/*
+ *     Sherlock Library -- Character Conversion with Allocation on a Memory Pool 
+ *
+ *     (c) 2006 Pavel Charvat <pchar@ucw.cz>
+ */
+
+#include "lib/lib.h"
+#include "lib/mempool.h"
+#include "charset/mp-charconv.h"
+#include "charset/stk-charconv.h"
+#include <string.h>
+
+byte *
+mp_conv(struct mempool *mp, byte *s, uns in_cs, uns out_cs)
+{
+  return mp_strdup(mp, stk_conv(s, in_cs, out_cs));
+}
diff --git a/charset/mp-charconv.h b/charset/mp-charconv.h
new file mode 100644 (file)
index 0000000..6519535
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ *     Sherlock Library -- Character Conversion with Allocation on a Memory Pool 
+ *
+ *     (c) 2006 Pavel Charvat <pchar@ucw.cz>
+ */
+
+#ifndef _MP_CHARCONV_H
+#define _MP_CHARCONV_H
+
+#include "lib/mempool.h"
+#include "charset/charconv.h"
+
+byte *
+mp_conv(struct mempool *mp, byte *s, uns cs_in, uns cs_out);
+
+static inline byte *
+mp_conv_to_utf8(struct mempool *mp, byte *s, uns cs_in) 
+{ return mp_conv(mp, s, cs_in, CONV_CHARSET_UTF8); }
+
+static inline byte *
+mp_conv_from_utf8(struct mempool *mp, byte *s, uns cs_out)
+{ return mp_conv(mp, s, CONV_CHARSET_UTF8, cs_out); }
+
+#endif
diff --git a/charset/stk-charconv.c b/charset/stk-charconv.c
new file mode 100644 (file)
index 0000000..6550ea3
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ *     Sherlock Library -- Character Conversion with Allocation on the Stack 
+ *
+ *     (c) 2006 Pavel Charvat <pchar@ucw.cz>
+ */
+
+#include "lib/lib.h"
+#include "charset/stk-charconv.h"
+#include <string.h>
+
+uns
+stk_conv_internal(struct conv_context *c, byte *s, uns in_cs, uns out_cs)
+{
+  /* We do not allocate anything for identical charsets. */
+  if (in_cs == out_cs)
+    {
+      c->dest_start = s;
+      return 0;
+    }  
+  
+  uns l = strlen(s);
+  
+  conv_init(c);
+  conv_set_charset(c, in_cs, out_cs);
+  c->source = s;
+  c->source_end = s + l + 1;
+
+  /* Resulting string can be longer after the conversion.
+   * The following constatnt must be at least 4 for conversion to UTF-8
+   * and at least the maximum length of the strings in string_table for other charsets. */
+  return 4 * l + 1;
+}
diff --git a/charset/stk-charconv.h b/charset/stk-charconv.h
new file mode 100644 (file)
index 0000000..c0d8568
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ *     Sherlock Library -- Character Conversion with Allocation on the Stack 
+ *
+ *     (c) 2006 Pavel Charvat <pchar@ucw.cz>
+ */
+
+#ifndef _STK_CHARCONV_H
+#define _STK_CHARCONV_H
+
+#include "charset/charconv.h"
+#include <alloca.h>
+
+#define stk_conv(s, cs_in, cs_out) \
+    ({ struct conv_context _c; uns _l=stk_conv_internal(&_c, (s), (cs_in), (cs_out)); \
+    if (_l) { _c.dest=_c.dest_start=alloca(_l); _c.dest_end=_c.dest+_l; conv_run(&_c); } \
+    _c.dest_start; })
+
+#define stk_conv_to_utf8(s, cs_in) stk_conv(s, cs_in, CONV_CHARSET_UTF8)
+#define stk_conv_from_utf8(s, cs_out) stk_conv(s, CONV_CHARSET_UTF8, cs_out)
+    
+uns stk_conv_internal(struct conv_context *, byte *, uns, uns);
+
+#endif