OSDN Git Service

* Support for Japanese-MetaText output by libiconv
[playumidi/playumidi.git] / src / play_event.c
index 6cef64b..4eab74f 100644 (file)
@@ -38,6 +38,7 @@ MIDIMIDIMIDIMIDIMIDIMIDIMIDIMIDIMIDIMIDIMIDIMIDIMIDIMIDIMIDIMIDIMIDI*/
 #include<fcntl.h>
 #include<assert.h>
 #include<pthread.h>
+#include<errno.h>
 #include<sys/stat.h>
 #include<sys/types.h>
 #include<sys/uio.h>
@@ -50,6 +51,10 @@ int                                  gi_outport;
 int                                    gi_prefix_channel;
 Byte                           gb_status                       = PLAYSTATUS_INITIALIZE;
 pthread_mutex_t                gt_mutex_status;
+#ifdef SUPPORT_JAPANESE
+char                           *gpstr_buffer;
+size_t                         gsz_buffer;
+#endif
 
 
 /* ===================================================================*/
@@ -86,22 +91,67 @@ int PlayEvent_StopVoiceOut(
 }
                
 
+#ifdef SUPPORT_JAPANESE
+/* ===================================================================*/
+int    PlayEvent_ConvLanguage_toTerminal(
+               char    *pstr_dest, 
+               size_t  sz_dest,
+               char    *pstr_src, 
+               size_t  sz_src )        {
+
+       int             i_errcount      = 0;
+       size_t          sz_result;
+
+       while(( 0 < sz_src ) && ( ICONV_PERMIT_ERRCOUNT > i_errcount ))         {
+               sz_result       = iconv( gt_iconv,
+                               (const char **)&pstr_src, &sz_src,
+                               &pstr_dest, &sz_dest );
+               if( -1 == sz_result )   {
+                       if( 0 < sz_src )        {
+                               *pstr_dest++    = *pstr_src++;
+                               sz_src--;
+                               sz_dest--;
+                       }
+                       i_errcount++;
+               }
+       }
+
+       *pstr_dest      = '\0';
+
+       return i_errcount;
+}
+#endif
+
 /* ===================================================================*/
 int    Play_OutputText_fromMetaEvent(
                Byte    *pb_str,
                int             i_len,
                char    *pstr_title )           {
 
-       Byte    b_save;
+       Byte            b_save;
+       char            *pstr_dest;
+       int                     i_err;
 
        if( IS_MSGQUIET )       { return 0x00; }
 
+       i_err   = 0;
        b_save  = *(pb_str + i_len);
        *(pb_str + i_len)       = '\0';
+#ifdef SUPPORT_JAPANESE
+       i_err   = PlayEvent_ConvLanguage_toTerminal( gpstr_buffer,
+                                       (size_t)(gsz_buffer - 1), (char *)pb_str, (size_t)i_len );
+       if( ICONV_PERMIT_ERRCOUNT <= i_err )
+               { fprintf( stderr, "Error!!: iconv function error (%d)\n", errno ); }
+       pstr_dest       = gpstr_buffer;
+#else
+       pstr_dest       = (char *)pb_str;
+#endif
+
        if( -1 == gi_prefix_channel )
-               { printf("  [%-10s]: %s\n", pstr_title, pb_str ); }
+               { printf("  [%-10s]: %s\n", pstr_title, pstr_dest ); }
        else
-               { printf("  [%-10s]: (Channel:%01d) %s\n", pstr_title, gi_prefix_channel, pb_str ); }
+               { printf("  [%-10s]: (Channel:%01d) %s\n", pstr_title, gi_prefix_channel, pstr_dest ); }
+
        *(pb_str + i_len)       = b_save;
 
        return 0x00;
@@ -304,6 +354,16 @@ EXTERN_FUNC_PLAY_EVENT
        pb_vardata      = t_midi.pb_vardata;
        assert( NULL != pb_vardata );
 
+#ifdef SUPPORT_JAPANESE
+       gsz_buffer      = ((t_midi.dw_max_metasize * 3) / 2) + 1;
+       gpstr_buffer = (char *)malloc( gsz_buffer );
+       if( NULL == gpstr_buffer )      {
+               fprintf( stderr,
+                       "Error!!: Can't Alloc LangCode Convert Buffer Memory (%d)\n", errno );
+               return;
+       } 
+#endif
+
        guw_division    = t_midi.uw_division;
 
        gdw_tempo       = DEFAULT_TEMPO;                /* Default tempo = 120 */
@@ -372,6 +432,10 @@ EXTERN_FUNC_PLAY_EVENT
                p_mevent++;
        }
 
+#ifdef SUPPORT_JAPANESE
+       if( NULL != gpstr_buffer )      { free( gpstr_buffer ); }
+#endif
+
        return;
 }