unios2.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // (c) 2001,2004 by Max Alekseyev
  2. // ver. 2.1
  3. #include <stddef.h>
  4. #define INCL_DOSMODULEMGR
  5. #include <os2.h>
  6. typedef void* UconvObject;
  7. typedef unsigned short UniChar;
  8. int uni_init(int codepage);
  9. int uni_done();
  10. int uni_toucs( /* translate to Unicode */
  11. char*, /* I - input string */
  12. size_t, /* I - length of input string (chars) */
  13. UniChar*, /* O - output Unicode string */
  14. size_t* ); /* O - length of output string (UniChars) */
  15. int uni_fromucs( /* translate from Unicode */
  16. UniChar*, /* I - input Unicode string */
  17. size_t, /* I - length of input string (UniChars) */
  18. char*, /* O - output string */
  19. size_t* ); /* O - length of output string (chars) */
  20. /* IMPLEMENTATION */
  21. static int (*uniMapCpToUcsCp) (
  22. unsigned long, /* I - Codepage to convert */
  23. UniChar*, /* O - Output buffer */
  24. size_t ); /* I - UniChars in output buffer */
  25. static int (*uniCreateUconvObject) (
  26. UniChar*, /* I - Unicode name of uconv table */
  27. UconvObject* );/* O - Uconv object handle */
  28. static int (*uniFreeUconvObject) (
  29. UconvObject ); /* I - Uconv object handle */
  30. static int (*uniUconvToUcs) (
  31. UconvObject, /* I - Uconv object handle */
  32. void**, /* IO - Input buffer */
  33. size_t*, /* IO - Input buffer size (bytes) */
  34. UniChar**, /* IO - Output buffer size */
  35. size_t*, /* IO - Output size (chars) */
  36. size_t* ); /* IO - Substitution count */
  37. static int (*uniUconvFromUcs) (
  38. UconvObject, /* I - Uconv object handle */
  39. UniChar**, /* IO - Input buffer */
  40. size_t*, /* IO - Input buffer size (bytes) */
  41. void**, /* IO - Output buffer size */
  42. size_t*, /* IO - Output size (chars) */
  43. size_t* ); /* IO - Substitution count */
  44. static int uni_ready = 0;
  45. static HMODULE uni_UCONV;
  46. static UconvObject uni_obj;
  47. int uni_init(int codepage) {
  48. UniChar unistr[256];
  49. uni_ready = 0;
  50. if(!&DosLoadModule) {
  51. /* DOS enviroment detected */
  52. return -1;
  53. }
  54. if( DosLoadModule(0,0,(PCSZ)"UCONV",&uni_UCONV) ) {
  55. /* no Unicode API found (obsolete OS/2 version) */
  56. return -2;
  57. }
  58. if( !DosQueryProcAddr(uni_UCONV,0,(PCSZ)"UniMapCpToUcsCp", (PPFN)&uniMapCpToUcsCp ) &&
  59. !DosQueryProcAddr(uni_UCONV,0,(PCSZ)"UniUconvToUcs", (PPFN)&uniUconvToUcs ) &&
  60. !DosQueryProcAddr(uni_UCONV,0,(PCSZ)"UniUconvFromUcs", (PPFN)&uniUconvFromUcs ) &&
  61. !DosQueryProcAddr(uni_UCONV,0,(PCSZ)"UniCreateUconvObject",(PPFN)&uniCreateUconvObject) &&
  62. !DosQueryProcAddr(uni_UCONV,0,(PCSZ)"UniFreeUconvObject", (PPFN)&uniFreeUconvObject )
  63. ) {
  64. unistr[0] = 0;
  65. if( (!codepage || !uniMapCpToUcsCp(codepage, unistr, 256)) && !uniCreateUconvObject(unistr,&uni_obj) ) {
  66. uni_ready = 1;
  67. return 0;
  68. }
  69. }
  70. DosFreeModule(uni_UCONV);
  71. return -2;
  72. }
  73. int uni_toucs(char* src, size_t srclen, UniChar* dst, size_t* dstlen) {
  74. size_t srcbytes, srcsize, dstsize, subsc=0;
  75. if(!uni_ready) return -1;
  76. dstsize = srcbytes = srclen * sizeof(UniChar);
  77. if( uniUconvToUcs(uni_obj,(void**)&src,&srclen,&dst,&dstsize,&subsc) ) {
  78. return -1;
  79. }
  80. *dstlen = srcbytes - dstsize;
  81. return 0;
  82. }
  83. int uni_fromucs(UniChar* src, size_t srclen, char* dst, size_t* dstlen) {
  84. size_t srcbytes, srcsize, dstsize, subsc=0;
  85. if(!uni_ready) return -1;
  86. dstsize = srcbytes = *dstlen;
  87. if( uniUconvFromUcs(uni_obj,&src,&srclen,(void**)&dst,&dstsize,&subsc) ) {
  88. return -1;
  89. }
  90. *dstlen = srcbytes - dstsize;
  91. return 0;
  92. }
  93. int uni_done() {
  94. if( uni_ready ) {
  95. uniFreeUconvObject(uni_obj);
  96. DosFreeModule(uni_UCONV);
  97. uni_ready = 0;
  98. }
  99. return 0;
  100. }