File: unios2.cpp

package info (click to toggle)
unrar-nonfree 1%3A4.1.4-1%2Bdeb7u1
  • links: PTS
  • area: non-free
  • in suites: wheezy
  • size: 1,120 kB
  • sloc: cpp: 21,159; makefile: 32; sh: 10
file content (128 lines) | stat: -rw-r--r-- 3,997 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// (c) 2001,2004 by Max Alekseyev
// ver. 2.1

#include <stddef.h>

#define INCL_DOSMODULEMGR
#include <os2.h>

typedef void* UconvObject;
typedef unsigned short UniChar;

int uni_init(int codepage);

int uni_done();

int uni_toucs(           /* translate to Unicode                   */
	      char*,     /* I - input string                       */
	      size_t,    /* I - length of input string (chars)     */
	      UniChar*,  /* O - output Unicode string              */
	      size_t* ); /* O - length of output string (UniChars) */

int uni_fromucs(            /* translate from Unicode                */
		UniChar*,   /* I - input Unicode string              */
		size_t,     /* I - length of input string (UniChars) */
		char*,      /* O - output string                     */
		size_t* );  /* O - length of output string (chars)   */

/* IMPLEMENTATION */

static int (*uniMapCpToUcsCp) (
	  unsigned long, /* I  - Codepage to convert         */
	  UniChar*,      /* O  - Output buffer               */
	  size_t );      /* I  - UniChars in output buffer   */

static int (*uniCreateUconvObject) (
	  UniChar*,      /* I  - Unicode name of uconv table */
	  UconvObject* );/* O  - Uconv object handle         */

static int (*uniFreeUconvObject) (
          UconvObject ); /* I  - Uconv object handle         */

static int (*uniUconvToUcs) (
	  UconvObject,   /* I  - Uconv object handle         */
	  void**,        /* IO - Input buffer                */
	  size_t*,       /* IO - Input buffer size (bytes)   */
	  UniChar**,     /* IO - Output buffer size          */
	  size_t*,       /* IO - Output size (chars)         */
	  size_t* );     /* IO - Substitution count          */

static int (*uniUconvFromUcs) (
	  UconvObject,   /* I  - Uconv object handle         */
	  UniChar**,     /* IO - Input buffer                */
	  size_t*,       /* IO - Input buffer size (bytes)   */
	  void**,        /* IO - Output buffer size          */
	  size_t*,       /* IO - Output size (chars)         */
	  size_t* );     /* IO - Substitution count          */

static int uni_ready = 0;
static HMODULE uni_UCONV;
static UconvObject uni_obj;

int uni_init(int codepage) {
    UniChar unistr[256];

    uni_ready = 0;

    if(!&DosLoadModule) {
	/* DOS enviroment detected */
	return -1;
    }

    if( DosLoadModule(0,0,(PCSZ)"UCONV",&uni_UCONV) ) {
	/* no Unicode API found (obsolete OS/2 version) */
	return -2;
    }

    if( !DosQueryProcAddr(uni_UCONV,0,(PCSZ)"UniMapCpToUcsCp",     (PPFN)&uniMapCpToUcsCp     ) &&
        !DosQueryProcAddr(uni_UCONV,0,(PCSZ)"UniUconvToUcs",       (PPFN)&uniUconvToUcs       ) &&
        !DosQueryProcAddr(uni_UCONV,0,(PCSZ)"UniUconvFromUcs",     (PPFN)&uniUconvFromUcs     ) &&
        !DosQueryProcAddr(uni_UCONV,0,(PCSZ)"UniCreateUconvObject",(PPFN)&uniCreateUconvObject) &&
        !DosQueryProcAddr(uni_UCONV,0,(PCSZ)"UniFreeUconvObject",  (PPFN)&uniFreeUconvObject  )
      ) {
	unistr[0] = 0;
	if( (!codepage || !uniMapCpToUcsCp(codepage, unistr, 256)) && !uniCreateUconvObject(unistr,&uni_obj) ) {
	    uni_ready = 1;
	    return 0;
	}
    }
    DosFreeModule(uni_UCONV);
    return -2;
}

int uni_toucs(char* src, size_t srclen, UniChar* dst, size_t* dstlen) {
    size_t srcbytes, srcsize, dstsize, subsc=0;

    if(!uni_ready) return -1;

    dstsize = srcbytes = srclen * sizeof(UniChar);

    if( uniUconvToUcs(uni_obj,(void**)&src,&srclen,&dst,&dstsize,&subsc) ) {
        return -1;
    }
    *dstlen = srcbytes - dstsize;
    return 0;
}

int uni_fromucs(UniChar* src, size_t srclen, char* dst, size_t* dstlen) {
    size_t srcbytes, srcsize, dstsize, subsc=0;

    if(!uni_ready) return -1;

    dstsize = srcbytes = *dstlen;

    if( uniUconvFromUcs(uni_obj,&src,&srclen,(void**)&dst,&dstsize,&subsc) ) {
        return -1;
    }
    *dstlen = srcbytes - dstsize;
    return 0;
}

int uni_done() {
    if( uni_ready ) {
      uniFreeUconvObject(uni_obj);
      DosFreeModule(uni_UCONV);
      uni_ready = 0;
    }
    return 0;
}