1 |
|
---|
2 | /*
|
---|
3 | *@@sourcefile encodings.c:
|
---|
4 | * character encoding translations.
|
---|
5 | *
|
---|
6 | *@@header "encodings\base.h"
|
---|
7 | *@@added V0.9.9 (2001-02-14) [umoeller]
|
---|
8 | */
|
---|
9 |
|
---|
10 | /*
|
---|
11 | * Copyright (C) 2001 Ulrich Mller.
|
---|
12 | * This file is part of the "XWorkplace helpers" source package.
|
---|
13 | * This is free software; you can redistribute it and/or modify
|
---|
14 | * it under the terms of the GNU General Public License as published
|
---|
15 | * by the Free Software Foundation, in version 2 as it comes in the
|
---|
16 | * "COPYING" file of the XWorkplace main distribution.
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #define OS2EMX_PLAIN_CHAR
|
---|
24 | // this is needed for "os2emx.h"; if this is defined,
|
---|
25 | // emx will define PSZ as _signed_ char, otherwise
|
---|
26 | // as unsigned char
|
---|
27 |
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <string.h>
|
---|
30 |
|
---|
31 | #include "setup.h" // code generation and debugging options
|
---|
32 |
|
---|
33 | #include "encodings\base.h" // includes all other encodings
|
---|
34 |
|
---|
35 | #pragma hdrstop
|
---|
36 |
|
---|
37 | typedef struct _ENCODINGTABLE
|
---|
38 | {
|
---|
39 | unsigned short cEntries;
|
---|
40 | unsigned short ausEntries[1]; // variable size
|
---|
41 | } ENCODINGTABLE, *PENCODINGTABLE;
|
---|
42 |
|
---|
43 |
|
---|
44 | /*
|
---|
45 | *@@ encRegisterEncoding:
|
---|
46 | * registers a new proprietary encoding with the engine.
|
---|
47 | *
|
---|
48 | * Before you can translate encodings with this engine,
|
---|
49 | * you have to register them. This makes sure that the
|
---|
50 | * big encoding tables will only be linked to the executable
|
---|
51 | * code if they are explicitly referenced. As a result, you
|
---|
52 | * have to #include "encodings\base.h" and pass a pointer to
|
---|
53 | * one of the global tables in the header files to this
|
---|
54 | * function.
|
---|
55 | *
|
---|
56 | * This returns an encoding handle that can then be used
|
---|
57 | * with the other encoding functions.
|
---|
58 | *
|
---|
59 | * Example:
|
---|
60 | *
|
---|
61 | + #include "encodings\base.h"
|
---|
62 | + #include "encodings\alltables.h" // or a specific table only
|
---|
63 | +
|
---|
64 | + int rc = encRegisterEncoding(&G_iso8859_1,
|
---|
65 | + sizeof(G_iso8859_1) / sizeof(G_iso8859_1[0]),
|
---|
66 | + enc_iso8859_1); // ID to register with
|
---|
67 | */
|
---|
68 |
|
---|
69 | long encRegisterEncoding(PXWPENCODINGMAP pEncodingMap,
|
---|
70 | unsigned long cArrayEntries, // count of array items
|
---|
71 | XWPENCODINGID EncodingID)
|
---|
72 | {
|
---|
73 | long lrc = 0;
|
---|
74 |
|
---|
75 | unsigned short usHighest = 0;
|
---|
76 | unsigned long ul;
|
---|
77 |
|
---|
78 | // step 1:
|
---|
79 | // run through the table and calculate the highest
|
---|
80 | // character entry used
|
---|
81 | for (ul = 0;
|
---|
82 | ul < cArrayEntries;
|
---|
83 | ul++)
|
---|
84 | {
|
---|
85 | unsigned short usFrom = pEncodingMap[ul].usFrom;
|
---|
86 | if (usFrom > usHighest)
|
---|
87 | usHighest = usFrom;
|
---|
88 | }
|
---|
89 |
|
---|
90 | // step 2: allocate encoding table
|
---|
91 | if (usHighest)
|
---|
92 | {
|
---|
93 | // allocate memory as needed
|
---|
94 | unsigned long cb = sizeof(ENCODINGTABLE)
|
---|
95 | + ( (usHighest - 1)
|
---|
96 | * sizeof(unsigned short)
|
---|
97 | );
|
---|
98 |
|
---|
99 | PENCODINGTABLE pTableNew = (PENCODINGTABLE)malloc(cb);
|
---|
100 | if (pTableNew)
|
---|
101 | {
|
---|
102 | memset(pTableNew, -1, cb);
|
---|
103 | pTableNew->cEntries = usHighest; // array size
|
---|
104 |
|
---|
105 | // step 3: fill encoding table
|
---|
106 | // this only has the Unicode target USHORTs;
|
---|
107 | // the source is simply the offset. So to
|
---|
108 | // get Unicode for character 123 in the specific encoding,
|
---|
109 | // do pTableNew->ausEntries[123].
|
---|
110 | // If you get 0xFFFF, the encoding is undefined.
|
---|
111 |
|
---|
112 | for (ul = 0;
|
---|
113 | ul < cArrayEntries;
|
---|
114 | ul++)
|
---|
115 | {
|
---|
116 | PXWPENCODINGMAP pEntry = &pEncodingMap[ul];
|
---|
117 | pTableNew->ausEntries[pEntry->usFrom] = pEntry->usUni;
|
---|
118 | }
|
---|
119 |
|
---|
120 | lrc = (long)pTableNew;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | return (lrc);
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|