/vb6.air/modStrings.bas

http://fsch.googlecode.com/ · Basic · 220 lines · 191 code · 10 blank · 19 comment · 0 complexity · 7bb32a13e4675e0ff4ee7ba750a3f37c MD5 · raw file

  1. Attribute VB_Name = "modString"
  2. Option Explicit
  3. Public Enum KnownCodePage
  4. CP_UNKNOWN = -1
  5. CP_ACP = 0
  6. CP_OEMCP = 1
  7. CP_MACCP = 2
  8. CP_THREAD_ACP = 3
  9. CP_SYMBOL = 42
  10. ' ARABIC
  11. CP_AWIN = 101 ' Bidi Windows codepage
  12. CP_709 = 102 ' MS-DOS Arabic Support CP 709
  13. CP_720 = 103 ' MS-DOS Arabic Support CP 720
  14. CP_A708 = 104 ' ASMO 708
  15. CP_A449 = 105 ' ASMO 449+
  16. CP_TARB = 106 ' MS Transparent Arabic
  17. CP_NAE = 107 ' Nafitha Enhanced Arabic Char Set
  18. CP_V4 = 108 ' Nafitha v 4.0
  19. CP_MA2 = 109 ' Mussaed Al Arabi (MA/2) CP 786
  20. CP_I864 = 110 ' IBM Arabic Supplement CP 864
  21. CP_A437 = 111 ' Ansi 437 codepage
  22. CP_AMAC = 112 ' Macintosh Code Page
  23. ' HEBREW
  24. CP_HWIN = 201 ' Bidi Windows codepage
  25. CP_862I = 202 ' IBM Hebrew Supplement CP 862
  26. CP_7BIT = 203 ' IBM Hebrew Supplement CP 862 Folded
  27. CP_ISO = 204 ' ISO Hebrew 8859-8 Character Set
  28. CP_H437 = 205 ' Ansi 437 codepage
  29. CP_HMAC = 206 ' Macintosh Code Page
  30. ' CODE PAGES
  31. CP_OEM_437 = 437
  32. CP_ARABICDOS = 708
  33. CP_DOS720 = 720
  34. CP_DOS737 = 737
  35. CP_DOS775 = 775
  36. CP_IBM850 = 850
  37. CP_IBM852 = 852
  38. CP_DOS861 = 861
  39. CP_DOS862 = 862
  40. CP_IBM866 = 866
  41. CP_DOS869 = 869
  42. CP_THAI = 874
  43. CP_EBCDIC = 875
  44. CP_JAPAN = 932
  45. CP_CHINA = 936
  46. CP_KOREA = 949
  47. CP_TAIWAN = 950
  48. ' UNICODE
  49. CP_UNICODELITTLE = 1200
  50. CP_UNICODEBIG = 1201
  51. ' CODE PAGES
  52. CP_EASTEUROPE = 1250
  53. CP_RUSSIAN = 1251
  54. CP_WESTEUROPE = 1252
  55. CP_GREEK = 1253
  56. CP_TURKISH = 1254
  57. CP_HEBREW = 1255
  58. CP_ARABIC = 1256
  59. CP_BALTIC = 1257
  60. CP_VIETNAMESE = 1258
  61. ' KOREAN
  62. CP_JOHAB = 1361
  63. ' MAC
  64. CP_MAC_ROMAN = 10000
  65. CP_MAC_JAPAN = 10001
  66. CP_MAC_ARABIC = 10004
  67. CP_MAC_GREEK = 10006
  68. CP_MAC_CYRILLIC = 10007
  69. CP_MAC_LATIN2 = 10029
  70. CP_MAC_TURKISH = 10081
  71. ' CODE PAGES
  72. CP_CHINESECNS = 20000
  73. CP_CHINESEETEN = 20002
  74. CP_IA5WEST = 20105
  75. CP_IA5GERMAN = 20106
  76. CP_IA5SWEDISH = 20107
  77. CP_IA5NORWEGIAN = 20108
  78. CP_ASCII = 20127
  79. CP_RUSSIANKOI8R = 20866
  80. CP_RUSSIANKOI8U = 21866
  81. CP_ISOLATIN1 = 28591
  82. CP_ISOEASTEUROPE = 28592
  83. CP_ISOTURKISH = 28593
  84. CP_ISOBALTIC = 28594
  85. CP_ISORUSSIAN = 28595
  86. CP_ISOARABIC = 28596
  87. CP_ISOGREEK = 28597
  88. CP_ISOHEBREW = 28598
  89. CP_ISOTURKISH2 = 28599
  90. CP_ISOLATIN9 = 28605
  91. CP_HEBREWLOG = 38598
  92. CP_USER = 50000
  93. CP_AUTOALL = 50001
  94. CP_JAPANNHK = 50220
  95. CP_JAPANESC = 50221
  96. CP_JAPANISO = 50222
  97. CP_KOREAISO = 50225
  98. CP_TAIWANISO = 50227
  99. CP_CHINAISO = 50229
  100. CP_AUTOJAPAN = 50932
  101. CP_AUTOCHINA = 50936
  102. CP_AUTOKOREA = 50949
  103. CP_AUTOTAIWAN = 50950
  104. CP_AUTORUSSIAN = 51251
  105. CP_AUTOGREEK = 51253
  106. CP_AUTOARABIC = 51256
  107. CP_JAPANEUC = 51932
  108. CP_CHINAEUC = 51936
  109. CP_KOREAEUC = 51949
  110. CP_TAIWANEUC = 51950
  111. CP_CHINAHZ = 52936
  112. CP_GB18030 = 54936
  113. ' UNICODE
  114. CP_UTF7 = 65000
  115. CP_UTF8 = 65001
  116. End Enum
  117. ' Flags
  118. Public Const MB_PRECOMPOSED = &H1
  119. Public Const MB_COMPOSITE = &H2
  120. Public Const MB_USEGLYPHCHARS = &H4
  121. Public Const MB_ERR_INVALID_CHARS = &H8
  122. Public Const WC_DEFAULTCHECK = &H100 ' check for default char
  123. Public Const WC_COMPOSITECHECK = &H200 ' convert composite to precomposed
  124. Public Const WC_DISCARDNS = &H10 ' discard non-spacing chars
  125. Public Const WC_SEPCHARS = &H20 ' generate separate chars
  126. Public Const WC_DEFAULTCHAR = &H40 ' replace with default char
  127. Public Declare Function GetACP Lib "kernel32" () As Long
  128. Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, _
  129. ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
  130. ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
  131. Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, _
  132. ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, _
  133. ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, _
  134. lpUsedDefaultChar As Long) As Long
  135. Public Declare Function CharToOem Lib "user32" Alias "CharToOemA" (ByVal lpszSrc As String, ByVal lpszDst As String) As Long
  136. Public Declare Function OemToChar Lib "user32" Alias "OemToCharA" (ByVal lpszSrc As String, ByVal lpszDst As String) As Long
  137. Public Function ANSItoUTF16(ByRef Text() As Byte, Optional ByVal cPage As KnownCodePage = CP_UNKNOWN, _
  138. Optional lFlags As Long) As Byte()
  139. Static tmpArr() As Byte, textStr As String
  140. Dim tmpLen As Long, textLen As Long, A As Long
  141. If (Not Text) = True Then Exit Function
  142. ' set code page to a valid one
  143. If cPage = CP_UNKNOWN Then cPage = GetACP
  144. If cPage = CP_ACP Or cPage = CP_WESTEUROPE Then
  145. textLen = UBound(Text)
  146. tmpLen = textLen + textLen + 1
  147. If (Not tmpArr) = True Then ReDim Preserve tmpArr(tmpLen)
  148. If UBound(tmpArr) <> tmpLen Then ReDim Preserve tmpArr(tmpLen)
  149. For A = 0 To UBound(Text)
  150. tmpArr(A + A) = Text(A)
  151. Next A
  152. Else
  153. textStr = CStr(Text) & "|"
  154. textLen = LenB(textStr)
  155. tmpLen = textLen + textLen
  156. ReDim Preserve tmpArr(tmpLen + 1)
  157. ' get the new string to tmpArr
  158. tmpLen = MultiByteToWideChar(CLng(cPage), lFlags, ByVal StrPtr(textStr), -1, _
  159. ByVal VarPtr(tmpArr(0)), tmpLen)
  160. If tmpLen = 0 Then Exit Function
  161. tmpLen = tmpLen + tmpLen - 5
  162. 'If tmpArr(tmpLen - 1) = 0 And tmpArr(tmpLen) = 0 Then tmpLen = tmpLen - 2
  163. If UBound(tmpArr) <> tmpLen Then ReDim Preserve tmpArr(tmpLen)
  164. End If
  165. ' return the result
  166. ANSItoUTF16 = tmpArr
  167. End Function
  168. Public Function UTF16toANSI(ByRef Text() As Byte, Optional ByVal cPage As KnownCodePage = CP_UNKNOWN, _
  169. Optional lFlags As Long) As Byte()
  170. Static tmpArr() As Byte
  171. Dim tmpLen As Long, textLen As Long, A As Long
  172. If (Not Text) = True Then Exit Function
  173. ' set code page to a valid one
  174. If cPage = CP_UNKNOWN Then cPage = GetACP
  175. If cPage = CP_ACP Or cPage = CP_WESTEUROPE Then
  176. textLen = UBound(Text)
  177. tmpLen = (textLen + 1) \ 2 - 1
  178. If (Not tmpArr) = True Then ReDim Preserve tmpArr(tmpLen)
  179. If UBound(tmpArr) <> tmpLen Then ReDim Preserve tmpArr(tmpLen)
  180. For A = 0 To tmpLen
  181. tmpArr(A) = Text(A + A)
  182. Next A
  183. Else
  184. textLen = (UBound(Text) + 1) \ 2
  185. ' at maximum ANSI can be four bytes per character in new Chinese encoding GB18030–2000
  186. tmpLen = textLen + textLen + textLen + textLen + 1
  187. ReDim Preserve tmpArr(tmpLen - 1)
  188. ' get the new string to tmpArr
  189. tmpLen = WideCharToMultiByte(CLng(cPage), lFlags, ByVal VarPtr(Text(0)), textLen, ByVal VarPtr(tmpArr(0)), _
  190. tmpLen, ByVal 0&, ByVal 0&)
  191. If tmpLen = 0 Then Exit Function
  192. ' a hopeless try to correct a weird error?
  193. ReDim Preserve tmpArr(tmpLen - 1)
  194. End If
  195. ' return the result
  196. UTF16toANSI = tmpArr
  197. End Function
  198. Public Function OemToCharS(sOutput As String)
  199. Dim outputstr As String
  200. outputstr = Space$(Len(sOutput))
  201. OemToChar sOutput, outputstr
  202. OemToCharS = outputstr
  203. End Function
  204. Public Function ToOEM(sourcestring As String)
  205. Dim deststring As String ' ?????????? ??????
  206. Dim code As Long
  207. deststring = Space$(Len(sourcestring)) '???????? ???????????????? ??????
  208. code = CharToOem(sourcestring, deststring)
  209. ToOEM = deststring
  210. End Function