utf8proc

view ruby/utf8proc_native.c @ 10:00d2bcbdc945

Version 1.1.4

- replaced C++ style comments for compatibility reasons
- added typecasts to suppress compiler warnings
- removed redundant source files for ruby-gemfile generation
- Changed copyright notice for Public Software Group e. V.
- Minor changes in the README file
author jbe
date Wed Aug 19 12:00:00 2009 +0200 (2009-08-19)
parents fcfd8c836c64
children d79da2302625
line source
1 /*
2 * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
24 /*
25 * File name: ruby/utf8proc_native.c
26 *
27 * Description:
28 * Native part of the ruby wrapper for libutf8proc.
29 */
32 #include "../utf8proc.c"
33 #include "ruby.h"
35 typedef struct utf8proc_ruby_mapenv_struct {
36 int32_t *buffer;
37 } utf8proc_ruby_mapenv_t;
39 void utf8proc_ruby_mapenv_free(utf8proc_ruby_mapenv_t *env) {
40 free(env->buffer);
41 free(env);
42 }
44 VALUE utf8proc_ruby_module;
45 VALUE utf8proc_ruby_options;
46 VALUE utf8proc_ruby_eUnicodeError;
47 VALUE utf8proc_ruby_eInvalidUtf8Error;
48 VALUE utf8proc_ruby_eCodeNotAssignedError;
50 VALUE utf8proc_ruby_map_error(ssize_t result) {
51 VALUE excpt_class;
52 switch (result) {
53 case UTF8PROC_ERROR_NOMEM:
54 excpt_class = rb_eNoMemError; break;
55 case UTF8PROC_ERROR_OVERFLOW:
56 case UTF8PROC_ERROR_INVALIDOPTS:
57 excpt_class = rb_eArgError; break;
58 case UTF8PROC_ERROR_INVALIDUTF8:
59 excpt_class = utf8proc_ruby_eInvalidUtf8Error; break;
60 case UTF8PROC_ERROR_NOTASSIGNED:
61 excpt_class = utf8proc_ruby_eCodeNotAssignedError; break;
62 default:
63 excpt_class = rb_eRuntimeError;
64 }
65 rb_raise(excpt_class, "%s", utf8proc_errmsg(result));
66 return Qnil;
67 }
69 VALUE utf8proc_ruby_map(VALUE self, VALUE str_param, VALUE options_param) {
70 VALUE str;
71 int options;
72 VALUE env_obj;
73 utf8proc_ruby_mapenv_t *env;
74 ssize_t result;
75 VALUE retval;
76 str = StringValue(str_param);
77 options = NUM2INT(options_param) & ~UTF8PROC_NULLTERM;
78 env_obj = Data_Make_Struct(rb_cObject, utf8proc_ruby_mapenv_t, NULL,
79 utf8proc_ruby_mapenv_free, env);
80 result = utf8proc_decompose(RSTRING(str)->ptr, RSTRING(str)->len,
81 NULL, 0, options);
82 if (result < 0) {
83 utf8proc_ruby_map_error(result);
84 return Qnil; /* needed to prevent problems with optimization */
85 }
86 env->buffer = ALLOC_N(int32_t, result+1);
87 result = utf8proc_decompose(RSTRING(str)->ptr, RSTRING(str)->len,
88 env->buffer, result, options);
89 if (result < 0) {
90 free(env->buffer);
91 env->buffer = 0;
92 utf8proc_ruby_map_error(result);
93 return Qnil; /* needed to prevent problems with optimization */
94 }
95 result = utf8proc_reencode(env->buffer, result, options);
96 if (result < 0) {
97 free(env->buffer);
98 env->buffer = 0;
99 utf8proc_ruby_map_error(result);
100 return Qnil; /* needed to prevent problems with optimization */
101 }
102 retval = rb_str_new((char *)env->buffer, result);
103 free(env->buffer);
104 env->buffer = 0;
105 return retval;
106 }
108 static VALUE utf8proc_ruby_char(VALUE self, VALUE code_param) {
109 char buffer[4];
110 ssize_t result;
111 int uc;
112 uc = NUM2INT(code_param);
113 if (!utf8proc_codepoint_valid(uc))
114 rb_raise(rb_eArgError, "Invalid Unicode code point");
115 result = utf8proc_encode_char(uc, buffer);
116 return rb_str_new(buffer, result);
117 }
119 #define register_utf8proc_option(sym, field) \
120 rb_hash_aset(utf8proc_ruby_options, ID2SYM(rb_intern(sym)), INT2FIX(field))
122 void Init_utf8proc_native() {
123 utf8proc_ruby_module = rb_define_module("Utf8Proc");
124 rb_define_module_function(utf8proc_ruby_module, "utf8map",
125 utf8proc_ruby_map, 2);
126 rb_define_module_function(utf8proc_ruby_module, "utf8char",
127 utf8proc_ruby_char, 1);
128 utf8proc_ruby_eUnicodeError = rb_define_class_under(utf8proc_ruby_module,
129 "UnicodeError", rb_eStandardError);
130 utf8proc_ruby_eInvalidUtf8Error = rb_define_class_under(
131 utf8proc_ruby_module, "InvalidUtf8Error", utf8proc_ruby_eUnicodeError);
132 utf8proc_ruby_eCodeNotAssignedError = rb_define_class_under(
133 utf8proc_ruby_module, "CodeNotAssignedError",
134 utf8proc_ruby_eUnicodeError);
135 utf8proc_ruby_options = rb_hash_new();
136 register_utf8proc_option("stable", UTF8PROC_STABLE);
137 register_utf8proc_option("compat", UTF8PROC_COMPAT);
138 register_utf8proc_option("compose", UTF8PROC_COMPOSE);
139 register_utf8proc_option("decompose", UTF8PROC_DECOMPOSE);
140 register_utf8proc_option("ignore", UTF8PROC_IGNORE);
141 register_utf8proc_option("rejectna", UTF8PROC_REJECTNA);
142 register_utf8proc_option("nlf2ls", UTF8PROC_NLF2LS);
143 register_utf8proc_option("nlf2ps", UTF8PROC_NLF2PS);
144 register_utf8proc_option("nlf2lf", UTF8PROC_NLF2LF);
145 register_utf8proc_option("stripcc", UTF8PROC_STRIPCC);
146 register_utf8proc_option("casefold", UTF8PROC_CASEFOLD);
147 register_utf8proc_option("charbound", UTF8PROC_CHARBOUND);
148 register_utf8proc_option("lump", UTF8PROC_LUMP);
149 register_utf8proc_option("stripmark", UTF8PROC_STRIPMARK);
150 OBJ_FREEZE(utf8proc_ruby_options);
151 rb_define_const(utf8proc_ruby_module, "Options", utf8proc_ruby_options);
152 }

Impressum / About Us