CFx SDK Documentation  2020SP3
OdCharConverter.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 
25 #ifndef __ODCHARCONVERTER_H__
26 #define __ODCHARCONVERTER_H__
27 
28 #include "OdaCommon.h"
29 #include "OdCharMapper.h"
30 #include "OdArray.h"
31 
36 namespace OdCharConverter
37 {
38  template <class CharType> inline bool isDigit(CharType ch)
39  {
40  return (ch >= '0' && ch <= '9');
41  }
42 
43  template <class CharType> inline bool isHexDigit(CharType ch)
44  {
45  return (ch >= '0' && ch <= '9') ||
46  (ch >= 'a' && ch <= 'f') ||
47  (ch >= 'A' && ch <= 'F');
48  }
49 
50  template <class CharType> inline bool checkDigits(const CharType* ptr, int numDigits, bool hex = true)
51  {
52  for (int i = 0; i < numDigits; i++)
53  {
54  if ( !(hex ? isHexDigit(ptr[i]) : isDigit(ptr[i])) )
55  {
56  return false;
57  }
58  }
59  return true;
60  }
61 
62  template <class CharType> inline char isCIForMIF(const CharType* ptr)
63  {
64  if (*ptr == '\\')
65  {
66  CharType ch2 = *++ptr;
67  if (*ptr++ == '+')
68  {
69  if ((ch2 == 'U' || ch2 == 'u') && checkDigits(ptr, 4))
70  return 'u';
71 
72  if ((ch2 == 'M' || ch2 == 'm') && (*ptr >= '1' && *ptr <= '5') && checkDigits(++ptr, 4))
73  return 'm';
74  }
75  }
76  return '\0';
77  }
78 
79  template <class CharType> inline bool isCIF(const CharType* ptr)
80  {
81  if ( !ptr )
82  return false;
83  return (ptr[0] == '\\' && ( ptr[1] == 'U' || ptr[1] == 'u' ) && ptr[2] == '+' &&
84  checkDigits(&ptr[3], 4) );
85  }
86 
87  template <class CharType> inline bool isMIF(const CharType* ptr)
88  {
89  if ( !ptr )
90  return false;
91  return (ptr[0] == '\\' && ( ptr[1] == 'M' || ptr[1] == 'm' ) && ptr[2] == '+' &&
92  checkDigits(&ptr[4], 4) && ptr[3] >= '1' && ptr[3] <= '5');
93  }
94 
95  template <class CharType> inline unsigned int hexValue(const CharType c)
96  {
97  if (c >= '0' && c <= '9') return(c - '0');
98  if (c >= 'A' && c <= 'F') return(c - 'A' + 10);
99  if (c >= 'a' && c <= 'f') return(c - 'a' + 10);
100  return(0);
101  }
102 
103  inline int getMIFIndex(OdCodePageId id )
104  {
105  switch(id)
106  {
107  case CP_ANSI_932:
108  case CP_DOS932: return '1';
109  case CP_ANSI_950:
110  case CP_BIG5: return '2';
111  case CP_ANSI_949:
112  case CP_KSC5601: return '3';
113  case CP_ANSI_1361:
114  case CP_JOHAB: return '4';
115  case CP_ANSI_936:
116  case CP_GB2312: return '5';
117  default: return '0';
118  }
119  }
120 
121  template <class CharType> inline int getMIFString(OdChar val, OdCodePageId cp, CharType* dstBuf, int size)
122  {
123  if ( size < 8 )
124  return 0;
125  dstBuf[0] = '\\';
126  dstBuf[1] = 'M';
127  dstBuf[2] = '+';
128  dstBuf[3] = (CharType)getMIFIndex(cp);
129  for( int i = 0; i < 4; i++ )
130  {
131  CharType byteVal = (CharType)(val & 0x0f);
132  dstBuf[7 - i] = CharType((byteVal <= 9) ? ('0' + byteVal) : ('A' - 10 + byteVal));
133  val >>= 4;
134  }
135  return 8;
136  }
137 
138  template <class CharType> inline int getCIFString(OdUInt16 val, CharType* dstBuf, int size)
139  {
140  if ( size < 7 )
141  return 0;
142  dstBuf[0] = '\\';
143  dstBuf[1] = 'U';
144  dstBuf[2] = '+';
145  for( int i = 0; i < 4; i++ )
146  {
147  CharType byteVal = (CharType)(val & 0x0f);
148  dstBuf[6 - i] = CharType((byteVal <= 9) ? ('0' + byteVal) : ('A' - 10 + byteVal));
149  val >>= 4;
150  }
151  return 7;
152  }
153 
154  template <class CharType> inline OdCodePageId getMIFCodepage(CharType cp)
155  {
156  switch(cp)
157  {
158  case '1': return CP_ANSI_932;
159  case '2': return CP_ANSI_950;
160  case '3': return CP_ANSI_949;
161  case '4': return CP_ANSI_1361;
162  case '5': return CP_ANSI_936;
163  default: return CP_UNDEFINED;
164  }
165  }
166 
167  template <class CharType> inline bool parseMIFString(CharType* srcBuf, OdChar& val, OdCodePageId& cp)
168  {
169  if ( !isMIF<CharType>(srcBuf) )
170  return false;
171 
172  cp = getMIFCodepage(srcBuf[3]);
173  val = OdChar(
174  (hexValue(srcBuf[4]) << 12)
175  + (hexValue(srcBuf[5]) << 8)
176  + (hexValue(srcBuf[6]) << 4)
177  + hexValue(srcBuf[7]));
178  return true;
179  }
180 
181  template <class CharType> inline bool parseCIFString(CharType* srcBuf, OdChar& val)
182  {
183  if ( !isCIF<CharType>(srcBuf) )
184  return false;
185 
186  val = OdChar(
187  (hexValue(srcBuf[3]) << 12)
188  + (hexValue(srcBuf[4]) << 8)
189  + (hexValue(srcBuf[5]) << 4)
190  + hexValue(srcBuf[6]));
191  return true;
192  }
193 
194  inline bool isMBCBCodepage(OdCodePageId id)
195  {
196  return id == CP_ANSI_932 ||
197  id == CP_ANSI_936 ||
198  id == CP_ANSI_949 ||
199  id == CP_ANSI_950 ||
200  id == CP_ANSI_1361 ||
201  id == CP_BIG5 ||
202  id == CP_JOHAB ||
203  id == CP_GB2312 ||
204  id == CP_KSC5601 ||
205  id == CP_DOS932;
206  }
207 
208  inline bool isMIFCodepage(OdCodePageId id)
209  {
210  return isMBCBCodepage(id);
211  }
212 
213 
215  {
216  switch (cp)
217  {
218  case CP_DOS932: return CP_ANSI_932;
219  case CP_BIG5: return CP_ANSI_950;
220  case CP_KSC5601: return CP_ANSI_949;
221  case CP_GB2312: return CP_ANSI_936;
222  case CP_JOHAB: return CP_ANSI_1361;
223  default:break;
224  }
225  return cp;
226  }
227 }
228 
229 #endif // __ODCHARCONVERTER_H__
CP_UNDEFINED
@ CP_UNDEFINED
Definition: OdCodePage.h:32
CP_BIG5
@ CP_BIG5
Definition: OdCodePage.h:56
CP_DOS932
@ CP_DOS932
Definition: OdCodePage.h:54
OdCharConverter::hexValue
unsigned int hexValue(const CharType c)
Definition: OdCharConverter.h:95
CP_ANSI_950
@ CP_ANSI_950
Definition: OdCodePage.h:73
OdCharConverter::isMIF
bool isMIF(const CharType *ptr)
Definition: OdCharConverter.h:87
OdCharConverter::getMIFIndex
int getMIFIndex(OdCodePageId id)
Definition: OdCharConverter.h:103
OdCharMapper.h
OdCharConverter::checkTheSameCP
OdCodePageId checkTheSameCP(OdCodePageId cp)
Definition: OdCharConverter.h:214
OdCharConverter::getCIFString
int getCIFString(OdUInt16 val, CharType *dstBuf, int size)
Definition: OdCharConverter.h:138
OdCharConverter::getMIFString
int getMIFString(OdChar val, OdCodePageId cp, CharType *dstBuf, int size)
Definition: OdCharConverter.h:121
OdCodePageId
OdCodePageId
Definition: OdCodePage.h:31
OdUInt16
unsigned short OdUInt16
Definition: OdPlatformSettings.h:760
CP_JOHAB
@ CP_JOHAB
Definition: OdCodePage.h:58
OdCharConverter::isHexDigit
bool isHexDigit(CharType ch)
Definition: OdCharConverter.h:43
size
GLsizeiptr size
Definition: gles2_ext.h:182
CP_ANSI_949
@ CP_ANSI_949
Definition: OdCodePage.h:72
CP_ANSI_932
@ CP_ANSI_932
Definition: OdCodePage.h:70
OdArray.h
CP_KSC5601
@ CP_KSC5601
Definition: OdCodePage.h:57
OdCharConverter::isMIFCodepage
bool isMIFCodepage(OdCodePageId id)
Definition: OdCharConverter.h:208
OdCharConverter::getMIFCodepage
OdCodePageId getMIFCodepage(CharType cp)
Definition: OdCharConverter.h:154
CP_ANSI_936
@ CP_ANSI_936
Definition: OdCodePage.h:71
OdCharConverter::isCIF
bool isCIF(const CharType *ptr)
Definition: OdCharConverter.h:79
OdChar
wchar_t OdChar
Definition: OdPlatformSettings.h:745
OdCharConverter::isDigit
bool isDigit(CharType ch)
Definition: OdCharConverter.h:38
OdaCommon.h
OdCharConverter::checkDigits
bool checkDigits(const CharType *ptr, int numDigits, bool hex=true)
Definition: OdCharConverter.h:50
CP_GB2312
@ CP_GB2312
Definition: OdCodePage.h:63
CP_ANSI_1361
@ CP_ANSI_1361
Definition: OdCodePage.h:74
OdCharConverter::isCIForMIF
char isCIForMIF(const CharType *ptr)
Definition: OdCharConverter.h:62
OdCharConverter
Definition: OdCharConverter.h:37
OdCharConverter::parseCIFString
bool parseCIFString(CharType *srcBuf, OdChar &val)
Definition: OdCharConverter.h:181
OdCharConverter::parseMIFString
bool parseMIFString(CharType *srcBuf, OdChar &val, OdCodePageId &cp)
Definition: OdCharConverter.h:167
OdCharConverter::isMBCBCodepage
bool isMBCBCodepage(OdCodePageId id)
Definition: OdCharConverter.h:194