@@ -242,6 +242,72 @@ ip_eval(VALUE self, VALUE str)
242
242
return (str_new2 (ptr -> ip -> result ));
243
243
}
244
244
245
+
246
+ static VALUE
247
+ ip_toUTF8 (VALUE self , VALUE str , VALUE encodename )
248
+ {
249
+ #ifndef TCL_UTF_MAX
250
+ return str ;
251
+ #else
252
+ Tcl_Interp * interp ;
253
+ Tcl_Encoding encoding ;
254
+ Tcl_DString dstr ;
255
+ struct tcltkip * ptr ;
256
+ char * buff1 ,* buff2 ;
257
+
258
+ Data_Get_Struct (self ,struct tcltkip , ptr );
259
+ interp = ptr -> ip ;
260
+
261
+ encoding = Tcl_GetEncoding (interp ,STR2CSTR (encodename ));
262
+ buff1 = ALLOCA_N (char ,strlen (STR2CSTR (str ))+ 1 );
263
+ strcpy (buff1 ,STR2CSTR (str ));
264
+
265
+ Tcl_DStringInit (& dstr );
266
+ Tcl_DStringFree (& dstr );
267
+ Tcl_ExternalToUtfDString (encoding ,buff1 ,strlen (buff1 ),& dstr );
268
+ buff2 = ALLOCA_N (char ,Tcl_DStringLength (& dstr )+ 1 );
269
+ strcpy (buff2 ,Tcl_DStringValue (& dstr ));
270
+
271
+ Tcl_FreeEncoding (encoding );
272
+ Tcl_DStringFree (& dstr );
273
+
274
+ return str_new2 (buff2 );
275
+ #endif
276
+ }
277
+
278
+ static VALUE
279
+ ip_fromUTF8 (VALUE self , VALUE str , VALUE encodename )
280
+ {
281
+ #ifndef TCL_UTF_MAX
282
+ return str ;
283
+ #else
284
+ Tcl_Interp * interp ;
285
+ Tcl_Encoding encoding ;
286
+ Tcl_DString dstr ;
287
+ struct tcltkip * ptr ;
288
+ char * buff1 ,* buff2 ;
289
+
290
+ Data_Get_Struct (self ,struct tcltkip , ptr );
291
+ interp = ptr -> ip ;
292
+
293
+ encoding = Tcl_GetEncoding (interp ,STR2CSTR (encodename ));
294
+ buff1 = ALLOCA_N (char ,strlen (STR2CSTR (str ))+ 1 );
295
+ strcpy (buff1 ,STR2CSTR (str ));
296
+
297
+ Tcl_DStringInit (& dstr );
298
+ Tcl_DStringFree (& dstr );
299
+ Tcl_UtfToExternalDString (encoding ,buff1 ,strlen (buff1 ),& dstr );
300
+ buff2 = ALLOCA_N (char ,Tcl_DStringLength (& dstr )+ 1 );
301
+ strcpy (buff2 ,Tcl_DStringValue (& dstr ));
302
+
303
+ Tcl_FreeEncoding (encoding );
304
+ Tcl_DStringFree (& dstr );
305
+
306
+ return str_new2 (buff2 );
307
+ #endif
308
+ }
309
+
310
+
245
311
static VALUE
246
312
ip_invoke (int argc , VALUE * argv , VALUE obj )
247
313
{
@@ -356,6 +422,8 @@ void Init_tcltklib()
356
422
357
423
rb_define_singleton_method (ip , "new" , ip_new , 0 );
358
424
rb_define_method (ip , "_eval" , ip_eval , 1 );
425
+ rb_define_method (ip , "_toUTF8" ,ip_toUTF8 ,2 );
426
+ rb_define_method (ip , "_fromUTF8" ,ip_fromUTF8 ,2 );
359
427
rb_define_method (ip , "_invoke" , ip_invoke , -1 );
360
428
rb_define_method (ip , "_return_value" , ip_retval , 0 );
361
429
rb_define_method (ip , "mainloop" , lib_mainloop , 0 );
0 commit comments