# JoyWapi for Ruby TEXCELL
# 2018.03 Ver.1.05   2012.06 Ver.1.0
require 'fiddle/import'

module JOYWAPI
   extend Fiddle::Importer
   dlload "JoyWaApi"
   typealias("BOOL", "int")
   extern "int ConnectNet()",:cdecl
   extern "void DisconnectNet()",:cdecl
   extern "BOOL JWGetTagIDS2(long,void*,int,long,void*,int,long,long,long)",:cdecl
   extern "long JWRead(int,void*,long,void*)",:cdecl
   extern "long JWWrite(int,void*,long,long,long,void*,void*)",:cdecl
   extern "long JWWrite_LS(int,void*,int,int,int,void*,void*)",:cdecl
   extern "long JWRead_LS(int,void*,int,void*,void*,void*,int)",:cdecl
end

class JWCom

   CP_ACP = 0
   CP_UTF8 = 65001

   def initialize(ccode)

      @imcode = -1
      sa = ccode.to_s.upcase
      if sa == "SHIFT_JIS" || sa == "WINDOWS-31J"
         @imcode = CP_ACP
      elsif sa == "UTF-8"
         @imcode = CP_UTF8
      end
      @iuserid = 0
      @spassword = nil
   end

   def connect

      ir = JOYWAPI.ConnectNet()
      if ir == 0
         disconnect
      end
      return ir
   end

   def disconnect

      JOYWAPI.DisconnectNet()
   end

   def gettagid(arstag)

      ik = arstag.length
      arsg = arstag.each.map{|ax|
         if @imcode == CP_UTF8
            ax.encode("windows-31j", :undef => :replace, :replace => '?')
         else
            ax
         end
      }
      imxl = 0
      arsg.each{|ax|
         if imxl < ax.bytesize
            imxl = ax.bytesize
         end
      }
      imxl += 1
      stag = "\0" * imxl * ik
      ip = 0
      arsg.each{|ax|
         it = 0
         ax.bytes{|bc|
            stag[ip + it] = bc.chr
            it += 1
         }
         ip += imxl
      }
      iid = "\0" * 4 * ik
      bi = JOYWAPI.JWGetTagIDS2(ik,stag,imxl,0,iid,4,0,-1,-1)
      if bi == 1
         aritag = iid.unpack("i*")
      else
         aritag = nil
      end
      return aritag
   end

   def gettagid1(sjwtag)

      if @imcode == CP_UTF8
         sjwtag = sjwtag.encode("windows-31j", :undef => :replace, :replace => '?')
      end
      id = nil
      il = sjwtag.bytesize
      stag = "\0" * il
      it = 0
      sjwtag.bytes{|bc|
         stag[it] = bc.chr
         it += 1
      }
      iid = "\0" * 4
      bi = JOYWAPI.JWGetTagIDS2(1,stag,il,0,iid,4,0,-1,-1)
      if bi == 1
         id = iid.unpack("i")[0]
      end
      return id
   end

   def tagread(aritag)

      ik = aritag.length
      tcdata = ""
      aritag.each{|id|
         tcdata += [id].pack("i") + "\0" * 28
      }
      ir = JOYWAPI.JWRead(@iuserid,@spassword,ik,tcdata)
      ip = 0
      arval = Array.new
      ik.times.map{
         case tcdata[ip + 24,1]
            when "\x04"
               arval << tcdata[ip + 8,1].unpack("c")[0]
            when "\x05"
               sz = tcdata[ip + 8,16].unpack("Z*")[0]
               sz = sz.force_encoding("Windows-31j")
               if @imcode == CP_UTF8
                  sz = sz.encode("utf-8", :undef => :replace, :replace => '?')
               end
               arval << sz
            else
               arval << tcdata[ip + 8,8].unpack("d")[0]
         end
         ip += 32
      }
      return arval
   end

   def tagreadv1(itag)

      aritag = [itag]
      ar = tagread(aritag)
      return ar[0]
   end

   def tagwrite(artag,arvalue)

      ik1 = artag.length
      ik2 = arvalue.length
      if ik1 != ik2
         return -1
      end
      arid = ""; ardt = ""; ib = 0
      artag.each{|id|
         arid += [id].pack("i")
      }
      arvalue.each{|dt|
         sc = dt.class.to_s
         case sc
            when "Integer", "Fixnum", "Float"
               ardt += [dt].pack("d")
               ib += 8
            when "String"
               if @imcode == CP_UTF8
                  dt = dt.encode("windows-31j", :undef => :replace, :replace => '?')
               end
               ardt += [dt].pack("a16")
               ib += 16
            else
               return -2
         end
      }
      ir = JOYWAPI.JWWrite(@iuserid,@spassword,0,ik1,ib,arid,ardt)
      return ir
   end

   def tagwritev1(itag, value)

      artag = [itag]; arval = [value]
      ir = tagwrite(artag, arval)
      return ir
   end

   def tagwrite_LS(artag, arsvalue)

      ik1 = artag.length
      ik2 = arsvalue.length
      if ik1 != ik2
         return -1
      end
      arid = ""; ardt = ""
      artag.each{|id|
         arid += [id].pack("i")
      }
      arsvalue.each{|dt|
         if @imcode == CP_UTF8
            dt = dt.encode("windows-31j", :undef => :replace, :replace => '?')
         end
         il = dt.bytesize
         il += 1
         ardt += [il].pack("s")
         ardt += [dt].pack("a" + il.to_s)
      }
      bl = ardt.bytesize
      ir = JOYWAPI.JWWrite_LS(@iuserid,@spassword,0,ik1,bl,arid,ardt)
      return ir
   end

   def tagwritev1_LS(itag, value)

      artag = [itag]; arval = [value]
      ir = tagwrite_LS(artag, arval)
      return ir
   end

   attr_accessor :iuserid
   attr_accessor :spassword

end
