XMLBigInteger.cpp
上传用户:zhuqijet
上传日期:2013-06-25
资源大小:10074k
文件大小:12k
源码类别:

词法分析

开发平台:

Visual C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2001 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) 2001, 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.  * $Log: XMLBigInteger.cpp,v $
  58.  * Revision 1.6  2003/05/22 02:10:52  knoaman
  59.  * Default the memory manager.
  60.  *
  61.  * Revision 1.5  2003/05/16 06:01:53  knoaman
  62.  * Partial implementation of the configurable memory manager.
  63.  *
  64.  * Revision 1.4  2003/05/15 19:07:46  knoaman
  65.  * Partial implementation of the configurable memory manager.
  66.  *
  67.  * Revision 1.3  2002/11/04 15:22:05  tng
  68.  * C++ Namespace Support.
  69.  *
  70.  * Revision 1.2  2002/08/13 22:11:23  peiyongz
  71.  * Fix to Bug#9442
  72.  *
  73.  * Revision 1.1.1.1  2002/02/01 22:22:14  peiyongz
  74.  * sane_include
  75.  *
  76.  * Revision 1.7  2001/08/23 11:54:26  tng
  77.  * Add newline at the end and various typo fixes.
  78.  *
  79.  * Revision 1.6  2001/07/24 13:58:11  peiyongz
  80.  * XMLDouble and related supporting methods from XMLBigInteger/XMLBigDecimal
  81.  *
  82.  * Revision 1.5  2001/05/31 17:50:26  knoaman
  83.  * small modification to XMLBigInteger::toString.
  84.  *
  85.  * Revision 1.4  2001/05/18 20:17:57  tng
  86.  * Schema: More exception messages in XMLBigDecimal/XMLBigInteger/DecimalDatatypeValidator.  By Pei Yong Zhang.
  87.  *
  88.  * Revision 1.3  2001/05/18 13:23:01  tng
  89.  * Schema: Exception messages in DatatypeValidator.  By Pei Yong Zhang.
  90.  *
  91.  * Revision 1.2  2001/05/11 13:26:31  tng
  92.  * Copyright update.
  93.  *
  94.  * Revision 1.1  2001/05/10 20:51:23  tng
  95.  * Schema: Add DecimalDatatypeValidator and XMLBigDecimal, XMLBigInteger.  By Pei Yong Zhang.
  96.  *
  97.  */
  98. // ---------------------------------------------------------------------------
  99. //  Includes
  100. // ---------------------------------------------------------------------------
  101. #include <xercesc/util/XMLBigInteger.hpp>
  102. #include <xercesc/util/XMLString.hpp>
  103. #include <xercesc/util/NumberFormatException.hpp>
  104. #include <xercesc/util/PlatformUtils.hpp>
  105. #include <xercesc/util/TransService.hpp>
  106. #include <xercesc/util/XMLUniDefs.hpp>
  107. #include <xercesc/util/Janitor.hpp>
  108. XERCES_CPP_NAMESPACE_BEGIN
  109. /***
  110.    *
  111.    *  Leading and trailing whitespaces are allowed, and trimmed
  112.    *
  113.    *  Only one and either of (+,-) after the leading whitespace, before
  114.    *  any other characters are allowed, and trimmed
  115.    *
  116.    *  Leading zero, after leading whitespace, (+|-), before any other
  117.    *  characters are allowed, and trimmed
  118.    *
  119.    *  '.' NOT allowed
  120.    *  return status: void
  121.    *  ret_buf: w/o leading and/or trailing whitespace
  122.    *           w/o '+' and '-'
  123.    *           w/o leading zero
  124.    *
  125.    *  see XMLString::parseInt();
  126.    *      XMLString::textToBin();
  127.    *
  128.    *  "    +000203456"            "203456"
  129.    *  "    -000203456"            "203456"
  130.    *
  131. ***/
  132. void XMLBigInteger::parseBigInteger(const XMLCh* const toConvert
  133.                                   , XMLCh* const retBuffer
  134.                                   , int&   signValue)
  135. {
  136.     // If no string, then its a failure
  137.     if ((!toConvert) || (!*toConvert))
  138.         ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_emptyString);
  139.     //
  140.     // Note: in Java's BigInteger, it seems any leading and/or trailing
  141.     // whitespaces are not allowed. If this is the case, we may
  142.     // need to skip the trimming below.
  143.     //
  144.     // Scan past any whitespace. If we hit the end, then return failure
  145.     const XMLCh* startPtr = toConvert;
  146.     while (XMLPlatformUtils::fgTransService->isSpace(*startPtr))
  147.         startPtr++;
  148.     if (!*startPtr)
  149.         ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_WSString);
  150.     // Start at the end and work back through any whitespace
  151.     const XMLCh* endPtr = toConvert + XMLString::stringLen(toConvert);
  152.     while (XMLPlatformUtils::fgTransService->isSpace(*(endPtr - 1)))
  153.         endPtr--;
  154.     //
  155.     //  Work through what remains and convert each char to a digit.
  156.     //  anything other than '
  157.     //
  158.     XMLCh* retPtr = retBuffer;
  159.     signValue = 1;
  160.     //
  161.     // '+' or '-' is allowed only at the first position
  162.     //
  163.     if (*startPtr == chDash)
  164.     {
  165.         signValue = -1;
  166.         startPtr++;
  167.     }
  168.     else if (*startPtr == chPlus)
  169.     {
  170.         // skip the '+'
  171.         startPtr++;
  172.     }
  173.     // Scan past any leading zero.
  174.     while (*startPtr == chDigit_0)
  175.         startPtr++;
  176.     if (!*startPtr)
  177.     {
  178.         signValue = 0;
  179.         // containning zero, only zero, nothing but zero
  180.         // it is a zero, indeed
  181.         return;
  182.     }
  183.     while (startPtr < endPtr)
  184.     {
  185.         // If not valid decimal digit, then an error
  186.         if ((*startPtr < chDigit_0) || (*startPtr > chDigit_9))
  187.             ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_Inv_chars);
  188.         // copy over
  189.         *retPtr = *startPtr;
  190.         retPtr++;
  191.         startPtr++;
  192.     }
  193.     *retPtr = 0;   //terminated
  194.     return;
  195. }
  196. /**
  197.  * Translates a string containing an optional minus sign followed by a
  198.  * sequence of one or more digits into a BigInteger.
  199.  * Any extraneous characters (including whitespace),
  200.  * inclusive, will result in a NumberFormatException.
  201.  */
  202. XMLBigInteger::XMLBigInteger(const XMLCh* const strValue,
  203.                              MemoryManager* const manager)
  204. : fSign(0)
  205. , fMagnitude(0)
  206. , fRawData(0)
  207. , fMemoryManager(manager)
  208. {
  209.     if (!strValue)
  210.         ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_emptyString);
  211.     XMLCh* ret_value = (XMLCh*) fMemoryManager->allocate
  212.     (
  213.        (XMLString::stringLen(strValue) + 1) * sizeof(XMLCh)
  214.     );//new XMLCh[XMLString::stringLen(strValue)+1];
  215.     ArrayJanitor<XMLCh> janName(ret_value, fMemoryManager);
  216.     parseBigInteger(strValue, ret_value, fSign);
  217.     if (fSign == 0)
  218.         fMagnitude = XMLString::replicate(XMLUni::fgZeroLenString, fMemoryManager);
  219.     else
  220.         fMagnitude = XMLString::replicate(ret_value, fMemoryManager);
  221. fRawData = XMLString::replicate(strValue, fMemoryManager);
  222. }
  223. XMLBigInteger::~XMLBigInteger()
  224. {
  225.     fMemoryManager->deallocate(fMagnitude);//delete[] fMagnitude;
  226. if (fRawData)
  227. fMemoryManager->deallocate(fRawData);//delete[] fRawData;
  228. }
  229. XMLBigInteger::XMLBigInteger(const XMLBigInteger& toCopy)
  230. : fSign(toCopy.fSign)
  231. , fMagnitude(0)
  232. , fRawData(0)
  233. , fMemoryManager(toCopy.fMemoryManager)
  234. {
  235.     fMagnitude = XMLString::replicate(toCopy.fMagnitude, fMemoryManager);
  236. fRawData = XMLString::replicate(toCopy.fRawData, fMemoryManager);
  237. }
  238. /**
  239.  * Returns -1, 0 or 1 as lValue is less than, equal to, or greater
  240.  * than rValue.
  241. */
  242. int  XMLBigInteger::compareValues(const XMLBigInteger* const lValue
  243.                                 , const XMLBigInteger* const rValue)
  244. {
  245.     if ((!lValue) || (!rValue) )
  246.         ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_null_ptr);
  247.     int lSign = lValue->getSign();
  248.     int rSign = rValue->getSign();
  249.     //
  250.     // different sign
  251.     //
  252.     if (lSign != rSign)
  253.         return(lSign > rSign ? 1 : -1);
  254.     //
  255.     // same sign
  256.     //
  257.     if (lSign == 0)    // optimization
  258.         return 0;
  259.     int lStrLen = XMLString::stringLen(lValue->fMagnitude);
  260.     int rStrLen = XMLString::stringLen(rValue->fMagnitude);
  261.     //
  262.     // different length
  263.     //
  264.     if (lStrLen > rStrLen)
  265.         return ( lSign > 0 ? 1 : -1 );
  266.     else if (lStrLen < rStrLen)
  267.         return ( lSign > 0 ? -1 : 1 );
  268.     //
  269.     // same length
  270.     // XMLString::compareString() return > 0, 0 and <0
  271.     // we need to convert it to 1, 0, and -1
  272.     //
  273.     int retVal = XMLString::compareString(lValue->fMagnitude, rValue->fMagnitude);
  274.     if ( retVal > 0 )
  275.     {
  276.         return ( lSign > 0 ? 1 : -1 );
  277.     }
  278.     else if ( retVal < 0 )
  279.     {
  280.         return ( lSign > 0 ? -1 : 1 );
  281.     }
  282.     else
  283.         return 0;
  284. }
  285. /**
  286.  * Shift the fMagnitude to the left
  287.  */
  288. void XMLBigInteger::multiply(const unsigned int byteToShift)
  289. {
  290.     if (byteToShift <= 0)
  291.         return;
  292.     int strLen = XMLString::stringLen(fMagnitude);
  293.     XMLCh* tmp = (XMLCh*) fMemoryManager->allocate
  294.     (
  295.         (strLen + byteToShift + 1) * sizeof(XMLCh)
  296.     );//new XMLCh[strLen+byteToShift+1];
  297.     XMLString::moveChars(tmp, fMagnitude, strLen);
  298.     unsigned int i = 0;
  299.     for ( ; i < byteToShift; i++)
  300.         tmp[strLen+i] = chDigit_0;
  301.     tmp[strLen+i] = chNull;
  302.     fMemoryManager->deallocate(fMagnitude);//delete[] fMagnitude;
  303.     fMagnitude = tmp;
  304. }
  305. /**
  306.  * Shift the fMagnitude to the right
  307.  * by doing this, we lose precision.
  308.  */
  309. void XMLBigInteger::divide(const unsigned int byteToShift)
  310. {
  311.     if (byteToShift <= 0)
  312.         return;
  313.     int strLen = XMLString::stringLen(fMagnitude);
  314.     XMLCh* tmp = (XMLCh*) fMemoryManager->allocate
  315.     (
  316.         (strLen - byteToShift + 1) * sizeof(XMLCh)
  317.     );//new XMLCh[strLen-byteToShift+1];
  318.     XMLString::moveChars(tmp, fMagnitude, strLen-byteToShift);
  319.     tmp[strLen-byteToShift] = chNull;
  320.     fMemoryManager->deallocate(fMagnitude);//delete[] fMagnitude;
  321.     fMagnitude = tmp;
  322. }
  323. //
  324. //
  325. //
  326. int XMLBigInteger::intValue() const
  327. {
  328.     unsigned int retVal;
  329.     XMLString::textToBin(fMagnitude, retVal);
  330.     return retVal * getSign();
  331. }
  332. XERCES_CPP_NAMESPACE_END