PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ATF2/control-software/epics-3.14.8/extensions/src/ChannelArchiver/ThirdParty/xerces-c-src2_4_0/src/xercesc/util/Transcoders/Iconv/IconvTransService.cpp

http://atf2flightsim.googlecode.com/
C++ | 619 lines | 422 code | 96 blank | 101 comment | 74 complexity | d065f3df767d9b90f46946018128cd6d MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.0, IPL-1.0, BSD-3-Clause
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999-2000 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution,
  20. * if any, must include the following acknowledgment:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgment may appear in the software itself,
  24. * if and wherever such third-party acknowledgments normally appear.
  25. *
  26. * 4. The names "Xerces" and "Apache Software Foundation" must
  27. * not be used to endorse or promote products derived from this
  28. * software without prior written permission. For written
  29. * permission, please contact apache\@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache",
  32. * nor may "Apache" appear in their name, without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation, and was
  51. * originally based on software copyright (c) 1999, International
  52. * Business Machines, Inc., http://www.ibm.com . For more information
  53. * on the Apache Software Foundation, please see
  54. * <http://www.apache.org/>.
  55. */
  56. /*
  57. * $Id: IconvTransService.cpp,v 1.1.1.1 2009/03/14 06:42:36 whitegr Exp $
  58. */
  59. // ---------------------------------------------------------------------------
  60. // Includes
  61. // ---------------------------------------------------------------------------
  62. #include "IconvTransService.hpp"
  63. #include <xercesc/util/XMLUniDefs.hpp>
  64. #include <xercesc/util/XMLUni.hpp>
  65. #include <xercesc/framework/MemoryManager.hpp>
  66. #include <wchar.h>
  67. #if defined (XML_GCC) || defined (XML_PTX) || defined (XML_IBMVAOS2)
  68. #if defined(XML_BEOS)
  69. wint_t towlower(wint_t wc) {
  70. return ((wc>'A')&&(wc<'Z') ? wc+'a'-'A' : wc);
  71. }
  72. wint_t towupper(wint_t wc) {
  73. return ((wc>'a')&&(wc<'z') ? wc-'a'+'A' : wc);
  74. }
  75. wint_t iswspace(wint_t wc) {
  76. return (wc==(wint_t)' ');
  77. }
  78. #elif !defined(XML_OPENSERVER)
  79. #include <wctype.h>
  80. #endif
  81. #endif
  82. #include <string.h>
  83. #include <stdlib.h>
  84. #include <stdio.h>
  85. XERCES_CPP_NAMESPACE_BEGIN
  86. // ---------------------------------------------------------------------------
  87. // Local, const data
  88. // ---------------------------------------------------------------------------
  89. static const int gTempBuffArraySize = 1024;
  90. static const XMLCh gMyServiceId[] =
  91. {
  92. chLatin_I, chLatin_C, chLatin_o, chLatin_n, chLatin_v, chNull
  93. };
  94. // ---------------------------------------------------------------------------
  95. // Local methods
  96. // ---------------------------------------------------------------------------
  97. static unsigned int getWideCharLength(const XMLCh* const src)
  98. {
  99. if (!src)
  100. return 0;
  101. unsigned int len = 0;
  102. const XMLCh* pTmp = src;
  103. while (*pTmp++)
  104. len++;
  105. return len;
  106. }
  107. // ---------------------------------------------------------------------------
  108. // IconvTransService: Constructors and Destructor
  109. // ---------------------------------------------------------------------------
  110. IconvTransService::IconvTransService()
  111. {
  112. }
  113. IconvTransService::~IconvTransService()
  114. {
  115. }
  116. // ---------------------------------------------------------------------------
  117. // IconvTransService: The virtual transcoding service API
  118. // ---------------------------------------------------------------------------
  119. int IconvTransService::compareIString( const XMLCh* const comp1
  120. , const XMLCh* const comp2)
  121. {
  122. const XMLCh* cptr1 = comp1;
  123. const XMLCh* cptr2 = comp2;
  124. while ( (*cptr1 != 0) && (*cptr2 != 0) )
  125. {
  126. wint_t wch1 = towupper(*cptr1);
  127. wint_t wch2 = towupper(*cptr2);
  128. if (wch1 != wch2)
  129. break;
  130. cptr1++;
  131. cptr2++;
  132. }
  133. return (int) ( towupper(*cptr1) - towupper(*cptr2) );
  134. }
  135. int IconvTransService::compareNIString( const XMLCh* const comp1
  136. , const XMLCh* const comp2
  137. , const unsigned int maxChars)
  138. {
  139. unsigned int n = 0;
  140. const XMLCh* cptr1 = comp1;
  141. const XMLCh* cptr2 = comp2;
  142. while (true && maxChars)
  143. {
  144. wint_t wch1 = towupper(*cptr1);
  145. wint_t wch2 = towupper(*cptr2);
  146. if (wch1 != wch2)
  147. return (int) (wch1 - wch2);
  148. // If either ended, then both ended, so equal
  149. if (!*cptr1 || !*cptr2)
  150. break;
  151. cptr1++;
  152. cptr2++;
  153. // Bump the count of chars done. If it equals the count then we
  154. // are equal for the requested count, so break out and return
  155. // equal.
  156. n++;
  157. if (n == maxChars)
  158. break;
  159. }
  160. return 0;
  161. }
  162. const XMLCh* IconvTransService::getId() const
  163. {
  164. return gMyServiceId;
  165. }
  166. bool IconvTransService::isSpace(const XMLCh toCheck) const
  167. {
  168. return (iswspace(toCheck) != 0);
  169. }
  170. XMLLCPTranscoder* IconvTransService::makeNewLCPTranscoder()
  171. {
  172. // Just allocate a new transcoder of our type
  173. return new IconvLCPTranscoder;
  174. }
  175. bool IconvTransService::supportsSrcOfs() const
  176. {
  177. return true;
  178. }
  179. // ---------------------------------------------------------------------------
  180. // IconvTransService: The protected virtual transcoding service API
  181. // ---------------------------------------------------------------------------
  182. XMLTranscoder*
  183. IconvTransService::makeNewXMLTranscoder(const XMLCh* const encodingName
  184. , XMLTransService::Codes& resValue
  185. , const unsigned int
  186. , MemoryManager* const)
  187. {
  188. //
  189. // NOTE: We don't use the block size here
  190. //
  191. // This is a minimalist transcoding service, that only supports a local
  192. // default transcoder. All named encodings return zero as a failure,
  193. // which means that only the intrinsic encodings supported by the parser
  194. // itself will work for XML data.
  195. //
  196. resValue = XMLTransService::UnsupportedEncoding;
  197. return 0;
  198. }
  199. void IconvTransService::upperCase(XMLCh* const toUpperCase) const
  200. {
  201. XMLCh* outPtr = toUpperCase;
  202. while (*outPtr)
  203. {
  204. *outPtr = towupper(*outPtr);
  205. outPtr++;
  206. }
  207. }
  208. void IconvTransService::lowerCase(XMLCh* const toLowerCase) const
  209. {
  210. XMLCh* outPtr = toLowerCase;
  211. while (*outPtr)
  212. {
  213. *outPtr = towlower(*outPtr);
  214. outPtr++;
  215. }
  216. }
  217. // ---------------------------------------------------------------------------
  218. // IconvLCPTranscoder: The virtual transcoder API
  219. // ---------------------------------------------------------------------------
  220. unsigned int IconvLCPTranscoder::calcRequiredSize(const char* const srcText)
  221. {
  222. if (!srcText)
  223. return 0;
  224. unsigned charLen = ::mblen(srcText, MB_CUR_MAX);
  225. if (charLen == -1)
  226. return 0;
  227. else if (charLen != 0)
  228. charLen = strlen(srcText)/charLen;
  229. if (charLen == -1)
  230. return 0;
  231. return charLen;
  232. }
  233. unsigned int IconvLCPTranscoder::calcRequiredSize(const XMLCh* const srcText)
  234. {
  235. if (!srcText)
  236. return 0;
  237. unsigned int wLent = getWideCharLength(srcText);
  238. wchar_t tmpWideCharArr[gTempBuffArraySize];
  239. wchar_t* allocatedArray = 0;
  240. wchar_t* wideCharBuf = 0;
  241. if (wLent >= gTempBuffArraySize)
  242. wideCharBuf = allocatedArray = (wchar_t*)
  243. XMLPlatformUtils::fgMemoryManager->allocate
  244. (
  245. (wLent + 1) * sizeof(wchar_t)
  246. );//new wchar_t[wLent + 1];
  247. else
  248. wideCharBuf = tmpWideCharArr;
  249. for (unsigned int i = 0; i < wLent; i++)
  250. {
  251. wideCharBuf[i] = srcText[i];
  252. }
  253. wideCharBuf[wLent] = 0x00;
  254. const unsigned int retVal = ::wcstombs(NULL, wideCharBuf, 0);
  255. XMLPlatformUtils::fgMemoryManager->deallocate(allocatedArray);//delete [] allocatedArray;
  256. if (retVal == ~0)
  257. return 0;
  258. return retVal;
  259. }
  260. char* IconvLCPTranscoder::transcode(const XMLCh* const toTranscode)
  261. {
  262. if (!toTranscode)
  263. return 0;
  264. char* retVal = 0;
  265. if (*toTranscode)
  266. {
  267. unsigned int wLent = getWideCharLength(toTranscode);
  268. wchar_t tmpWideCharArr[gTempBuffArraySize];
  269. wchar_t* allocatedArray = 0;
  270. wchar_t* wideCharBuf = 0;
  271. if (wLent >= gTempBuffArraySize)
  272. wideCharBuf = allocatedArray = new wchar_t[wLent + 1];
  273. else
  274. wideCharBuf = tmpWideCharArr;
  275. for (unsigned int i = 0; i < wLent; i++)
  276. {
  277. wideCharBuf[i] = toTranscode[i];
  278. }
  279. wideCharBuf[wLent] = 0x00;
  280. // Calc the needed size.
  281. const size_t neededLen = ::wcstombs(NULL, wideCharBuf, 0);
  282. if (neededLen == -1)
  283. {
  284. delete [] allocatedArray;
  285. retVal = new char[1];
  286. retVal[0] = 0;
  287. return retVal;
  288. }
  289. retVal = new char[neededLen + 1];
  290. ::wcstombs(retVal, wideCharBuf, neededLen);
  291. retVal[neededLen] = 0;
  292. delete [] allocatedArray;
  293. }
  294. else
  295. {
  296. retVal = new char[1];
  297. retVal[0] = 0;
  298. }
  299. return retVal;
  300. }
  301. char* IconvLCPTranscoder::transcode(const XMLCh* const toTranscode,
  302. MemoryManager* const manager)
  303. {
  304. if (!toTranscode)
  305. return 0;
  306. char* retVal = 0;
  307. if (*toTranscode)
  308. {
  309. unsigned int wLent = getWideCharLength(toTranscode);
  310. wchar_t tmpWideCharArr[gTempBuffArraySize];
  311. wchar_t* allocatedArray = 0;
  312. wchar_t* wideCharBuf = 0;
  313. if (wLent >= gTempBuffArraySize)
  314. wideCharBuf = allocatedArray = (wchar_t*) manager->allocate
  315. (
  316. (wLent + 1) * sizeof(wchar_t)
  317. );//new wchar_t[wLent + 1];
  318. else
  319. wideCharBuf = tmpWideCharArr;
  320. for (unsigned int i = 0; i < wLent; i++)
  321. {
  322. wideCharBuf[i] = toTranscode[i];
  323. }
  324. wideCharBuf[wLent] = 0x00;
  325. // Calc the needed size.
  326. const size_t neededLen = ::wcstombs(NULL, wideCharBuf, 0);
  327. if (neededLen == -1)
  328. {
  329. manager->deallocate(allocatedArray);//delete [] allocatedArray;
  330. retVal = (char*) manager->allocate(sizeof(char)); //new char[1];
  331. retVal[0] = 0;
  332. return retVal;
  333. }
  334. retVal = (char*) manager->allocate((neededLen + 1) * sizeof(char));//new char[neededLen + 1];
  335. ::wcstombs(retVal, wideCharBuf, neededLen);
  336. retVal[neededLen] = 0;
  337. manager->deallocate(allocatedArray);//delete [] allocatedArray;
  338. }
  339. else
  340. {
  341. retVal = (char*) manager->allocate(sizeof(char));//new char[1];
  342. retVal[0] = 0;
  343. }
  344. return retVal;
  345. }
  346. bool IconvLCPTranscoder::transcode( const XMLCh* const toTranscode
  347. , char* const toFill
  348. , const unsigned int maxBytes)
  349. {
  350. // Watch for a couple of pyscho corner cases
  351. if (!toTranscode || !maxBytes)
  352. {
  353. toFill[0] = 0;
  354. return true;
  355. }
  356. if (!*toTranscode)
  357. {
  358. toFill[0] = 0;
  359. return true;
  360. }
  361. unsigned int wLent = getWideCharLength(toTranscode);
  362. wchar_t tmpWideCharArr[gTempBuffArraySize];
  363. wchar_t* allocatedArray = 0;
  364. wchar_t* wideCharBuf = 0;
  365. if (wLent > maxBytes) {
  366. wLent = maxBytes;
  367. }
  368. if (maxBytes >= gTempBuffArraySize) {
  369. wideCharBuf = allocatedArray = (wchar_t*)
  370. XMLPlatformUtils::fgMemoryManager->allocate
  371. (
  372. (maxBytes + 1) * sizeof(wchar_t)
  373. );//new wchar_t[maxBytes + 1];
  374. }
  375. else
  376. wideCharBuf = tmpWideCharArr;
  377. for (unsigned int i = 0; i < wLent; i++)
  378. {
  379. wideCharBuf[i] = toTranscode[i];
  380. }
  381. wideCharBuf[wLent] = 0x00;
  382. // Ok, go ahead and try the transcoding. If it fails, then ...
  383. size_t mblen = ::wcstombs(toFill, wideCharBuf, maxBytes);
  384. if (mblen == -1)
  385. {
  386. XMLPlatformUtils::fgMemoryManager->deallocate(allocatedArray);//delete [] allocatedArray;
  387. return false;
  388. }
  389. // Cap it off just in case
  390. toFill[mblen] = 0;
  391. XMLPlatformUtils::fgMemoryManager->deallocate(allocatedArray);//delete [] allocatedArray;
  392. return true;
  393. }
  394. XMLCh* IconvLCPTranscoder::transcode(const char* const toTranscode)
  395. {
  396. if (!toTranscode)
  397. return 0;
  398. XMLCh* retVal = 0;
  399. if (*toTranscode)
  400. {
  401. const unsigned int len = calcRequiredSize(toTranscode);
  402. if (len == 0)
  403. {
  404. retVal = new XMLCh[1];
  405. retVal[0] = 0;
  406. return retVal;
  407. }
  408. wchar_t tmpWideCharArr[gTempBuffArraySize];
  409. wchar_t* allocatedArray = 0;
  410. wchar_t* wideCharBuf = 0;
  411. if (len >= gTempBuffArraySize)
  412. wideCharBuf = allocatedArray = new wchar_t[len + 1];
  413. else
  414. wideCharBuf = tmpWideCharArr;
  415. ::mbstowcs(wideCharBuf, toTranscode, len);
  416. retVal = new XMLCh[len + 1];
  417. for (unsigned int i = 0; i < len; i++)
  418. {
  419. retVal[i] = (XMLCh) wideCharBuf[i];
  420. }
  421. retVal[len] = 0x00;
  422. delete [] allocatedArray;
  423. }
  424. else
  425. {
  426. retVal = new XMLCh[1];
  427. retVal[0] = 0;
  428. }
  429. return retVal;
  430. }
  431. XMLCh* IconvLCPTranscoder::transcode(const char* const toTranscode,
  432. MemoryManager* const manager)
  433. {
  434. if (!toTranscode)
  435. return 0;
  436. XMLCh* retVal = 0;
  437. if (*toTranscode)
  438. {
  439. const unsigned int len = calcRequiredSize(toTranscode);
  440. if (len == 0)
  441. {
  442. retVal = (XMLCh*) manager->allocate(sizeof(XMLCh)); //new XMLCh[1];
  443. retVal[0] = 0;
  444. return retVal;
  445. }
  446. wchar_t tmpWideCharArr[gTempBuffArraySize];
  447. wchar_t* allocatedArray = 0;
  448. wchar_t* wideCharBuf = 0;
  449. if (len >= gTempBuffArraySize)
  450. wideCharBuf = allocatedArray = (wchar_t*) manager->allocate
  451. (
  452. (len + 1) * sizeof(wchar_t)
  453. );//new wchar_t[len + 1];
  454. else
  455. wideCharBuf = tmpWideCharArr;
  456. ::mbstowcs(wideCharBuf, toTranscode, len);
  457. retVal = (XMLCh*) manager->allocate((len + 1) *sizeof(XMLCh));//new XMLCh[len + 1];
  458. for (unsigned int i = 0; i < len; i++)
  459. {
  460. retVal[i] = (XMLCh) wideCharBuf[i];
  461. }
  462. retVal[len] = 0x00;
  463. manager->deallocate(allocatedArray);//delete [] allocatedArray;
  464. }
  465. else
  466. {
  467. retVal = (XMLCh*) manager->allocate(sizeof(XMLCh));//new XMLCh[1];
  468. retVal[0] = 0;
  469. }
  470. return retVal;
  471. }
  472. bool IconvLCPTranscoder::transcode( const char* const toTranscode
  473. , XMLCh* const toFill
  474. , const unsigned int maxChars)
  475. {
  476. // Check for a couple of psycho corner cases
  477. if (!toTranscode || !maxChars)
  478. {
  479. toFill[0] = 0;
  480. return true;
  481. }
  482. if (!*toTranscode)
  483. {
  484. toFill[0] = 0;
  485. return true;
  486. }
  487. unsigned int len = calcRequiredSize(toTranscode);
  488. wchar_t tmpWideCharArr[gTempBuffArraySize];
  489. wchar_t* allocatedArray = 0;
  490. wchar_t* wideCharBuf = 0;
  491. if (len > maxChars) {
  492. len = maxChars;
  493. }
  494. if (maxChars >= gTempBuffArraySize)
  495. wideCharBuf = allocatedArray = (wchar_t*) XMLPlatformUtils::fgMemoryManager->allocate
  496. (
  497. (maxChars + 1) * sizeof(wchar_t)
  498. );//new wchar_t[maxChars + 1];
  499. else
  500. wideCharBuf = tmpWideCharArr;
  501. if (::mbstowcs(wideCharBuf, toTranscode, maxChars) == -1)
  502. {
  503. XMLPlatformUtils::fgMemoryManager->deallocate(allocatedArray);//delete [] allocatedArray;
  504. return false;
  505. }
  506. for (unsigned int i = 0; i < len; i++)
  507. {
  508. toFill[i] = (XMLCh) wideCharBuf[i];
  509. }
  510. toFill[len] = 0x00;
  511. XMLPlatformUtils::fgMemoryManager->deallocate(allocatedArray);//delete [] allocatedArray;
  512. return true;
  513. }
  514. // ---------------------------------------------------------------------------
  515. // IconvLCPTranscoder: Constructors and Destructor
  516. // ---------------------------------------------------------------------------
  517. IconvLCPTranscoder::IconvLCPTranscoder()
  518. {
  519. }
  520. IconvLCPTranscoder::~IconvLCPTranscoder()
  521. {
  522. }
  523. XERCES_CPP_NAMESPACE_END