Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/npl/npl.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 113 - (show annotations) (download) (as text)
Fri Nov 3 05:48:52 2017 UTC (6 years, 5 months ago) by tamiya25
File MIME type: text/x-csrc
File size: 4446 byte(s)
vm
1 // npl.c -- 全般的な関数群
2
3 #include "npl.h"
4
5 /* npl_init() -- NPLライブラリを初期化する
6 *
7 * 説明:
8 * NPLライブラリを使用する前に必要な
9 * 準備を行う。
10 */
11 int npl_init(void)
12 {
13 if (NPL_setlocale(LC_ALL, "") == NULL)
14 return 1; // error
15
16 return 0;
17 }
18
19 // NPLライブラリの終了処理を行う
20 int npl_final(void)
21 {
22 return 0;
23 }
24
25 // ワイド文字列をマルチバイト文字列に
26 // 変換したものを返す。
27 // この関数が返すポインタは使用後に
28 // free() で解放する必要がある。
29 char* npl_convert_to_mbs(const wchar_t *s)
30 {
31 size_t bytes;
32 char *str;
33
34 if (s == NULL)
35 return NULL;
36
37 if ((bytes = NPL_wcstombs(NULL, s, 0)) == (size_t)-1)
38 return NULL;
39 if ((bytes += 1) < 1)
40 return NULL;
41 if ((str = NPL_malloc(bytes)) == NULL)
42 return NULL;
43 NPL_memset(str, 0, bytes);
44 if (NPL_wcstombs(str, s, bytes) == (size_t)-1) {
45 NPL_free(str);
46 return NULL;
47 }
48
49 return str;
50 }
51
52 // マルチバイト文字列をワイド文字列に
53 // 変換して返す。
54 // この関数が返すポインタは、
55 // 使用後に free() で解放する必要がある。
56 wchar_t* npl_convert_to_wcs(const char *s)
57 {
58 size_t len, bytes;
59 wchar_t *str;
60
61 if (s == NULL)
62 return NULL;
63
64 if ((len = NPL_mbstowcs(NULL, s, 0)) == (size_t)-1)
65 return NULL;
66 if ((len += 1) < 1)
67 return NULL;
68 if (len > (SIZE_MAX / sizeof(wchar_t)))
69 return NULL;
70 bytes = sizeof(wchar_t) * len;
71 if ((str = NPL_malloc(bytes)) == NULL)
72 return NULL;
73 NPL_memset(str, 0, bytes);
74 if (NPL_mbstowcs(str, s, len) == (size_t)-1) {
75 NPL_free(str);
76 return NULL;
77 }
78
79 return str;
80 }
81
82 // a + b を計算する。 結果は r に格納する。
83 // 結果がオーバーフローするとエラーになる。
84 int npl_add(size_t a, size_t b, size_t *r)
85 {
86 size_t n = a;
87
88 n += b;
89 if (n < b)
90 return 1; // overflow
91
92 if (r != NULL)
93 *r = n;
94
95 return 0;
96 }
97
98 // a - b を計算する。 結果は r に格納する。
99 // 結果がオーバーフローするとエラーになる。
100 int npl_sub(size_t a, size_t b, size_t *r)
101 {
102 if (a < b)
103 return 1; // overflow
104 if (r != NULL)
105 *r = a - b;
106
107 return 0;
108 }
109
110 // a * b を計算する。 結果は r に格納する。
111 // 結果がオーバーフローするとエラーになる。
112 int npl_mul(size_t a, size_t b, size_t *r)
113 {
114 size_t n;
115
116 if (a == 0 || b == 0) {
117 n = 0;
118 } else {
119 if (b > (SIZE_MAX / a))
120 return 1; // overflow
121 n = a * b;
122 }
123
124 if (r != NULL)
125 *r = n;
126
127 return 0;
128 }
129
130 void* _npl_cast(
131 void *p,
132 npl_type_t *type,
133 const char *file_name,
134 const char *func_name,
135 unsigned int line_no)
136 {
137 npl_head_t *head = p;
138 npl_type_t *t, *t2;
139 size_t i;
140
141 if (p == NULL)
142 return NULL;
143
144 // 継承関係を調べる
145 t = head->type;
146 while (t != NULL) {
147 if (t == type)
148 return p;
149 t = t->parent;
150 }
151
152 // オブジェクトの場合、
153 // インターフェースも調べる。
154 if (head->type->ctype == NPL_CT_OBJECT) {
155 t = head->type;
156 while (t != NULL) {
157 if (t->ifaces != NULL) {
158 i = 0;
159 while (t->ifaces[i] != NULL) {
160 t2 = t->ifaces[i];
161 while (t2 != NULL) {
162 if (t2 == type)
163 return p;
164 t2 = t2->parent;
165 }
166 i++;
167 }
168 }
169 t = t->parent;
170 }
171 }
172
173 // プログラミングが間違っていることを通知し強制終了
174 NPL_fprintf(NPL_stderr, "[NPL_TYPE_ERROR]: %s - %u: %s\n",
175 file_name, line_no, func_name);
176 NPL_abort();
177 return NULL;
178 }
179
180 // ライブラリ・モジュールに存在する型情報
181 static npl_type_t *_types[] = {
182 &_npl_unset_type,
183 &_npl_bool_type,
184 &_npl_byte_type,
185 &_npl_size_type,
186 &_npl_int_type,
187 &_npl_uint_type,
188 &_npl_long_type,
189 &_npl_ulong_type,
190 &_npl_float_type,
191 &_npl_wchar_type,
192 &_npl_mbchar_type,
193 &_npl_ws_type,
194 &_npl_mbs_type,
195 &_npl_type_type,
196 &_npl_struct_type,
197 &_npl_args_type,
198 &_npl_state_event_type,
199 &_npl_cmd_event_type,
200 &_npl_iface_type,
201 &_npl_uniface_type,
202 &_npl_stream_type,
203 &_npl_iptr_type,
204 &_npl_istate_type,
205 &_npl_object_type,
206 &_npl_chunk_type,
207 &_npl_def_type,
208 &_npl_value_type,
209 &_npl_str_type,
210 &_npl_mbstr_type,
211 &_npl_var_type,
212 &_npl_ptr_type,
213 &_npl_tuple_type,
214 &_npl_buffer_type,
215 &_npl_list_type,
216 &_npl_dict_type,
217 &_npl_func_type,
218 &_npl_listener_type,
219 &_npl_vstack_type,
220 &_npl_state_type,
221 &_npl_file_type,
222 &_npl_cmd_type,
223 NULL,
224 };
225
226 // NPLライブラリ・モジュールの設定
227 npl_mod_t _npl_module = {
228 .types = _types,
229 };
230
231 npl_mod_t *npl_module = &_npl_module;
232

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26