OSDN Git Service

Handling of string encoding in Ruby scripts is improved.
authortoshinagata1964 <toshinagata1964@a2be9bc6-48de-4e38-9406-05402d4bc13c>
Fri, 28 Mar 2014 05:26:44 +0000 (05:26 +0000)
committertoshinagata1964 <toshinagata1964@a2be9bc6-48de-4e38-9406-05402d4bc13c>
Fri, 28 Mar 2014 05:26:44 +0000 (05:26 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/molby/trunk@517 a2be9bc6-48de-4e38-9406-05402d4bc13c

Documents/etc/xtal_18.png
Documents/etc/xtal_19.png
Documents/etc/xtal_20.png
Makefile
MolLib/Ruby_bind/Molby.h
MolLib/Ruby_bind/ruby_bind.c
MolLib/Ruby_bind/ruby_dialog.c
how_to_build
xcode-build/Molby.xcodeproj/project.pbxproj

index 164ba5a..8728c2b 100644 (file)
Binary files a/Documents/etc/xtal_18.png and b/Documents/etc/xtal_18.png differ
index 58c1e26..bce5dae 100644 (file)
Binary files a/Documents/etc/xtal_19.png and b/Documents/etc/xtal_19.png differ
index 967adc7..9decfab 100644 (file)
Binary files a/Documents/etc/xtal_20.png and b/Documents/etc/xtal_20.png differ
index c03b956..fab6e71 100755 (executable)
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ ifeq ($(TARGET_PLATFORM),MAC)
  LD_EXTRA_FLAGS = -framework Accelerate -framework GLUT -L$(PWD)/../../fftw-3.3.2/osx-build/lib -lfftw3
  RUBY_DIR = $(PWD)/../../ruby-1.8.7-p160
  RUBY_CFLAGS = -isystem $(RUBY_DIR)/osx-build/include
- RUBY_LDFLAGS = -L$(RUBY_DIR)/osx-build/lib -lruby-static -lenc
+ RUBY_LDFLAGS = -L$(RUBY_DIR)/osx-build/lib -lruby-static -lenc -ltrans
  EXECUTABLE = Molby
  EXE_SUFFIX =
 endif
@@ -23,7 +23,7 @@ ifeq ($(TARGET_PLATFORM),MSW)
  LD_EXTRA_FLAGS = -L$(PWD)/../../CLAPACK-3.1.1.1-mingw/lib -L$(PWD)/../../fftw-3.3.2/msw-build/lib -llapackMinGW -lblasMinGW -lf2c_nomain -lfftw3
  RUBY_DIR = $(PWD)/../../ruby-2.0.0-p353
  RUBY_CFLAGS = -isystem $(RUBY_DIR)/msw-build/include/ruby-2.0.0 -I$(RUBY_DIR) -I$(RUBY_DIR)/msw-build/include/ruby-2.0.0/i386-mingw32
- RUBY_LDFLAGS = -L$(RUBY_DIR)/msw-build/lib -lmsvcrt-ruby200-static -lmsvcrt-ruby200 -lws2_32 -lshlwapi -limagehlp -lenc
+ RUBY_LDFLAGS = -L$(RUBY_DIR)/msw-build/lib -lmsvcrt-ruby200-static -lmsvcrt-ruby200 -lws2_32 -lshlwapi -limagehlp -lenc -ltrans
  EXECUTABLE = _Molby.exe_
  FINAL_EXECUTABLE = Molby.exe
  EXE_SUFFIX = .exe
index c10564d..fb4c440 100755 (executable)
@@ -87,6 +87,9 @@ extern VALUE ValueFromVector(const Vector *vp);
 #define FileStringValuePtr(val) Ruby_FileStringValuePtr(&val)
 extern char *Ruby_FileStringValuePtr(VALUE *valp);
 extern VALUE Ruby_NewFileStringValue(const char *fstr);
+
+#define EncodedStringValuePtr(val) Ruby_EncodedStringValuePtr(&val)
+extern char *Ruby_EncodedStringValuePtr(VALUE *valp);
        
 /*
 STUB VALUE MyAppCallback_getGlobalSettings(const char *key);
index 56b7182..e06c16a 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "version.h"       /*  for Ruby version  */
 #include "ruby/version.h"  /*  for RUBY_BIRTH_YEAR etc.  */
+#include "ruby/encoding.h" /*  for rb_str_encode() etc. */
 /*#include <node.h>     *//*  for rb_add_event_hook()  */
 
 #if defined(__WXMAC__) || defined(__CMDMAC__)
@@ -137,6 +138,13 @@ Ruby_NewFileStringValue(const char *fstr)
 #endif
 }
 
+char *
+Ruby_EncodedStringValuePtr(VALUE *valp)
+{
+       *valp = rb_str_encode(*valp, rb_enc_from_encoding(rb_default_external_encoding()), 0, Qnil);
+       return RSTRING_PTR(*valp);
+}
+
 VALUE
 Ruby_ObjToStringObj(VALUE val)
 {
@@ -11316,8 +11324,9 @@ Molby_startup(const char *script, const char *dir)
        ruby_init();
 
        {
-               extern void Init_shift_jis(void);
+               extern void Init_shift_jis(void), Init_trans_japanese_sjis(void);
                Init_shift_jis();
+               Init_trans_japanese_sjis();
        }
        
        /*  Initialize loadpath; the specified directory, "lib" subdirectory, and "."  */
index 22e24b2..bf127cd 100644 (file)
@@ -168,7 +168,7 @@ s_RubyDialogItem_SetAttr(VALUE self, VALUE key, VALUE val)
        } else if (key == sValueSymbol) {
                /*  Value  */
                if (type == sTextFieldSymbol || type == sTextViewSymbol) {
-                       RubyDialogCallback_setStringToItem(view, (val == Qnil ? "" : StringValuePtr(val)));
+                       RubyDialogCallback_setStringToItem(view, (val == Qnil ? "" : EncodedStringValuePtr(val)));
                } else if (type == sPopUpSymbol) {
                        RubyDialogCallback_setSelectedSubItem(view, NUM2INT(rb_Integer(val)));
                } else if (type == sCheckBoxSymbol || type == sRadioSymbol || type == sToggleButtonSymbol) {
@@ -176,7 +176,7 @@ s_RubyDialogItem_SetAttr(VALUE self, VALUE key, VALUE val)
                }
        } else if (key == sTitleSymbol) {
                /*  Title  */
-               char *p = StringValuePtr(val);
+               char *p = EncodedStringValuePtr(val);
                if (type == sTextSymbol)
                        RubyDialogCallback_setStringToItem(view, p);
                else RubyDialogCallback_setTitleToItem(view, p);
@@ -203,7 +203,7 @@ s_RubyDialogItem_SetAttr(VALUE self, VALUE key, VALUE val)
                        while (RubyDialogCallback_deleteSubItem(view, 0) >= 0);
                        for (j = 0; j < len2; j++) {
                                VALUE val2 = ptr2[j];
-                               RubyDialogCallback_appendSubItem(view, StringValuePtr(val2));
+                               RubyDialogCallback_appendSubItem(view, EncodedStringValuePtr(val2));
                        }
                        RubyDialogCallback_resizeToBest(view);
                        RubyDialogCallback_setSelectedSubItem(view, 0);
@@ -303,7 +303,7 @@ s_RubyDialogItem_SetAttr(VALUE self, VALUE key, VALUE val)
                                weight = 3;
                        } else if (vali != Qnil) {
                                vali = rb_inspect(vali);
-                               rb_raise(rb_eMolbyError, "unknown font specification (%s)", StringValuePtr(vali));
+                               rb_raise(rb_eMolbyError, "unknown font specification (%s)", EncodedStringValuePtr(vali));
                        }
                }
                RubyDialogCallback_setFontForItem(view, size, family, style, weight);
@@ -334,7 +334,7 @@ s_RubyDialogItem_SetAttr(VALUE self, VALUE key, VALUE val)
                                cval = rb_ary_to_ary(RARRAY_PTR(val)[col]);
                                len = RARRAY_LEN(cval);
                                if (len >= 1) {
-                                       heading = StringValuePtr(RARRAY_PTR(cval)[0]);
+                                       heading = EncodedStringValuePtr(RARRAY_PTR(cval)[0]);
                                } else heading = "";
                                if (len >= 2) {
                                        width = NUM2INT(rb_Integer(RARRAY_PTR(cval)[1]));
@@ -557,7 +557,7 @@ s_RubyDialogItem_AppendString(VALUE self, VALUE val)
                rb_raise(rb_eStandardError, "The dialog item does not belong to any dialog (internal error?)");
        view = RubyDialogCallback_dialogItemAtIndex(dref, itag);        
        val = rb_str_to_str(val);
-       if (RubyDialogCallback_appendString(view, StringValuePtr(val)) == 0)
+       if (RubyDialogCallback_appendString(view, EncodedStringValuePtr(val)) == 0)
                rb_raise(rb_eMolbyError, "Cannot append string to the dialog item");
        return self;
 }
@@ -630,14 +630,14 @@ s_RubyDialog_Initialize(int argc, VALUE *argv, VALUE self)
        RubyDialogCallback_setRubyObject(dref, (RubyValue)self);
        
        if (!NIL_P(val1)) {
-               char *p = StringValuePtr(val1);
+               char *p = EncodedStringValuePtr(val1);
                RubyDialogCallback_setWindowTitle(dref, p);
        }
        if (val2 != Qnil)
-               title1 = StringValuePtr(val2);
+               title1 = EncodedStringValuePtr(val2);
        else title1 = NULL;
        if (val3 != Qnil)
-               title2 = StringValuePtr(val3);
+               title2 = EncodedStringValuePtr(val3);
        else title2 = NULL;
 
        //  Array of item informations
@@ -700,7 +700,7 @@ s_RubyDialog_ItemIndexForTag(VALUE self, VALUE tag)
        if (i == -1)
                rb_raise(rb_eStandardError, "item number (%d) out of range", i);
        else if (i == -2)
-               rb_raise(rb_eStandardError, "Dialog has no item with tag %s", StringValuePtr(tag));
+               rb_raise(rb_eStandardError, "Dialog has no item with tag %s", EncodedStringValuePtr(tag));
        return i;
 }
 
@@ -1150,7 +1150,7 @@ s_RubyDialog_Item(int argc, VALUE *argv, VALUE self)
 
        val = rb_hash_aref(hash, sTitleSymbol);
        if (!NIL_P(val)) {
-               title = StringValuePtr(val);
+               title = EncodedStringValuePtr(val);
        } else {
                title = "";
        }
@@ -1400,7 +1400,7 @@ s_RubyDialog_CallActionProc(VALUE self, VALUE aval, int argc, VALUE *argv)
                return rb_funcall2(aval, rb_intern("call"), argc, argv);
        else {
                VALUE insval = rb_inspect(aval);
-               rb_raise(rb_eTypeError, "Cannot call action method '%s'", StringValuePtr(insval));
+               rb_raise(rb_eTypeError, "Cannot call action method '%s'", EncodedStringValuePtr(insval));
        }
        return Qnil;  /*  Not reached  */
 }
@@ -1587,7 +1587,7 @@ s_RubyDialog_Listen(VALUE self, VALUE oval, VALUE sval, VALUE pval)
        if (sval == Qnil)
                sptr = NULL;
        else
-               sptr = StringValuePtr(sval);
+               sptr = EncodedStringValuePtr(sval);
        if (rb_obj_is_kind_of(oval, rb_cMolecule)) {
                Molecule *mol = MoleculeFromValue(oval);
                i = RubyDialogCallback_Listen(s_RubyDialog_GetController(self), mol, "Molecule", sptr, (RubyValue)oval, (RubyValue)pval);
@@ -1632,7 +1632,7 @@ s_RubyDialog_SavePanel(int argc, VALUE *argv, VALUE klass)
        rb_scan_args(argc, argv, "04", &mval, &dval, &fval, &wval);
        if (mval == Qnil)
                mp = NULL;
-       else mp = StringValuePtr(mval);
+       else mp = EncodedStringValuePtr(mval);
        if (dval == Qnil)
                dp = NULL;
        else dp = FileStringValuePtr(dval);
@@ -1670,7 +1670,7 @@ s_RubyDialog_OpenPanel(int argc, VALUE *argv, VALUE klass)
        rb_scan_args(argc, argv, "05", &mval, &dval, &wval, &fval, &mulval);
        if (mval == Qnil)
                mp = NULL;
-       else mp = StringValuePtr(mval);
+       else mp = EncodedStringValuePtr(mval);
        if (dval == Qnil)
                dp = NULL;
        else dp = FileStringValuePtr(dval);
@@ -1736,7 +1736,7 @@ s_RubyDialog_doTableAction(VALUE val)
                args[2] = INT2NUM((int)vp[4]);
                retval = s_RubyDialog_CallActionProc(self, pval, 3, args);
                retval = rb_str_to_str(retval);
-               vp[5] = strdup(StringValuePtr(retval));
+               vp[5] = strdup(EncodedStringValuePtr(retval));
                return retval;
        } else if (sym == sOnSetValueSymbol) {
                args[1] = INT2NUM((int)vp[3]);
@@ -1808,7 +1808,7 @@ s_RubyDialog_doTableAction(VALUE val)
                        *((char ***)vp[5]) = titles;
                        for (i = 0; i < n; i++) {
                                VALUE tval = RARRAY_PTR(retval)[i];
-                               titles[i] = strdup(StringValuePtr(tval));
+                               titles[i] = strdup(EncodedStringValuePtr(tval));
                        }
                }
                return retval;
@@ -2389,7 +2389,7 @@ static VALUE
 s_RubyDialog_DrawText(VALUE self, VALUE sval, VALUE xval, VALUE yval)
 {
        RDDeviceContext *dc = s_RubyDialog_GetDeviceContext(self);
-       const char *s = StringValuePtr(sval);
+       const char *s = EncodedStringValuePtr(sval);
        float x = NUM2DBL(rb_Float(xval));
        float y = NUM2DBL(rb_Float(yval));
        RubyDialogCallback_drawText(dc, s, x, y);
index ca82ef8..0d11eb1 100644 (file)
@@ -42,9 +42,9 @@ $ make
 
 Get ruby-2.0.0-p353.tar.gz, unpack it, and move the resulting directory ruby-2.0.0-p353 to $MOLBY/../.
 $ cd $MOLBY/../ruby-2.0.0-p353
-$ CFLAGS='-isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -O2' ./configure --prefix=$PWD/osx-build --with-arch=i386,ppc --disable-shared --with-ext=date --disable-install-rdoc
+$ CFLAGS='-isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -O2' ./configure --prefix=$PWD/osx-build --with-arch=i386,ppc --disable-shared --with-ext=date --disable-install-rdoc --with-static-linked-ext
 $ make && make install
-$ cd enc; ar -r libenc.a *.o; cp libenc.a ../osx-build/lib  #  We need shift_jis.o etc. for encoding support
+$ cp enc/libenc.a enc/libtrans.a osx-build/lib  #  We need this for encoding support
 
 The osx-build directory contains lib (which contains libruby-static.a) and include (which contains include files). (There are also bin and share directories, but these are not used for building Molby.)
 $ make clean  #  Remove configure files and intermediates; the osx-build directory remains intact
@@ -119,7 +119,7 @@ If you use gcc 4.6 or later, comment out the following line in cygwin/GNUmakefil
 
 $ ./configure --prefix=$PWD/msw-build --disable-shared --with-ext=date --disable-install-rdoc --with-static-linked-ext
 $ make && make install
-$ cp enc/libenc.a msw-build/lib  #  We need this for encoding support
+$ cp enc/libenc.a enc/libtrans.a msw-build/lib  #  We need this for encoding support
 
 The msw-build directory contains lib (which contains libmsvcrt-ruby200-static.a and libmsvcrt-rubuy200.a) and include (which contains include files). (There are also bin and share directories, but these are not used for building Molby.)
 $ make clean  #  Remove configure files and intermediates; the msw-build directory remains intact
index 8cf725f..0d3c30f 100644 (file)
                                        "-lfftw3",
                                        "-lruby-static",
                                        "-lenc",
+                                       "-ltrans",
                                );
                                PRODUCT_NAME = Molby;
                                SDKROOT = macosx10.5;
                                        "-lfftw3",
                                        "-lruby-static",
                                        "-lenc",
+                                       "-ltrans",
                                );
                                PRODUCT_NAME = Molby;
                                SDKROOT = macosx10.5;
                                        "-lfftw3",
                                        "-lruby-static",
                                        "-lenc",
+                                       "-ltrans",
                                );
                                PREBINDING = NO;
                                PRODUCT_NAME = Molby_command;
                                        "-lfftw3",
                                        "-lruby-static",
                                        "-lenc",
+                                       "-ltrans",
                                );
                                PREBINDING = NO;
                                PRODUCT_NAME = Molby_command;