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

词法分析

开发平台:

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.  * $Id: XMLFloat.cpp,v 1.11 2003/05/16 06:01:53 knoaman Exp $
  58.  * $Log: XMLFloat.cpp,v $
  59.  * Revision 1.11  2003/05/16 06:01:53  knoaman
  60.  * Partial implementation of the configurable memory manager.
  61.  *
  62.  * Revision 1.10  2003/05/16 03:11:22  knoaman
  63.  * Partial implementation of the configurable memory manager.
  64.  *
  65.  * Revision 1.9  2003/03/10 20:55:58  peiyongz
  66.  * Schema Errata E2-40 double/float
  67.  *
  68.  * Revision 1.8  2003/02/02 23:54:43  peiyongz
  69.  * getFormattedString() added to return original and converted value.
  70.  *
  71.  * Revision 1.7  2003/01/30 19:14:43  tng
  72.  * On some platforms like Solaris strtod will return -0.0.   So need to consider this scenario as well.
  73.  *
  74.  * Revision 1.6  2002/12/11 19:55:16  peiyongz
  75.  * set negZero/posZero for float.
  76.  *
  77.  * Revision 1.5  2002/12/11 00:20:02  peiyongz
  78.  * Doing businesss in value space. Converting out-of-bound value into special values.
  79.  *
  80.  * Revision 1.4  2002/11/04 15:22:05  tng
  81.  * C++ Namespace Support.
  82.  *
  83.  * Revision 1.3  2002/09/24 19:51:24  tng
  84.  * Performance: use XMLString::equals instead of XMLString::compareString
  85.  *
  86.  * Revision 1.2  2002/05/03 16:05:45  peiyongz
  87.  * Bug 7341: Missing newline at end of util and DOM source files,
  88.  * patch from Martin Kalen.
  89.  *
  90.  * Revision 1.1.1.1  2002/02/01 22:22:15  peiyongz
  91.  * sane_include
  92.  *
  93.  * Revision 1.13  2001/11/19 21:33:42  peiyongz
  94.  * Reorganization: Double/Float
  95.  *
  96.  * Revision 1.12  2001/11/19 17:27:55  peiyongz
  97.  * Boundary Values updated
  98.  *
  99.  * Revision 1.11  2001/10/26 16:37:46  peiyongz
  100.  * Add thread safe code
  101.  *
  102.  * Revision 1.9  2001/09/20 13:11:41  knoaman
  103.  * Regx  + misc. fixes
  104.  *
  105.  * Revision 1.8  2001/09/14 13:57:59  peiyongz
  106.  * exponent is a must if 'E' or 'e' is present.
  107.  *
  108.  * Revision 1.7  2001/08/23 11:54:26  tng
  109.  * Add newline at the end and various typo fixes.
  110.  *
  111.  * Revision 1.6  2001/08/21 15:10:15  peiyongz
  112.  * Bugzilla# 3017: MSVC5.0: C2202: 'compareSpecial' : not all
  113.  * control paths return a value
  114.  *
  115.  * Revision 1.5  2001/08/14 22:10:20  peiyongz
  116.  * new exception message added
  117.  *
  118.  * Revision 1.4  2001/07/31 17:38:16  peiyongz
  119.  * Fix: memory leak by static (boundry) objects
  120.  *
  121.  * Revision 1.3  2001/07/31 13:48:29  peiyongz
  122.  * fValue removed
  123.  *
  124.  * Revision 1.2  2001/07/27 20:43:53  peiyongz
  125.  * copy ctor: to check for special types.
  126.  *
  127.  * Revision 1.1  2001/07/26 20:41:37  peiyongz
  128.  * XMLFloat
  129.  *
  130.  *
  131.  */
  132. // ---------------------------------------------------------------------------
  133. //  Includes
  134. // ---------------------------------------------------------------------------
  135. #include <xercesc/util/XMLFloat.hpp>
  136. #include <xercesc/util/XMLString.hpp>
  137. #include <xercesc/util/NumberFormatException.hpp>
  138. #include <xercesc/util/Janitor.hpp>
  139. #include <string.h>
  140. #include <errno.h>
  141. #include <stdlib.h>
  142. #include <float.h>
  143. XERCES_CPP_NAMESPACE_BEGIN
  144. // ---------------------------------------------------------------------------
  145. //  ctor/dtor
  146. // ---------------------------------------------------------------------------
  147. XMLFloat::XMLFloat(const XMLCh* const strValue,
  148.                    MemoryManager* const manager)
  149. :XMLAbstractDoubleFloat(manager)
  150. {
  151.     init(strValue);
  152. }
  153. XMLFloat::~XMLFloat()
  154. {
  155. }
  156. void XMLFloat::checkBoundary(const XMLCh* const strValue)
  157. {
  158.     char *nptr = XMLString::transcode(strValue, getMemoryManager());
  159.     ArrayJanitor<char> jan1(nptr, getMemoryManager());
  160.     int   strLen = strlen(nptr);
  161.     char *endptr = 0;
  162.     errno = 0;
  163.     fValue = strtod(nptr, &endptr);
  164.     // check if all chars are valid char
  165.     if ( (endptr - nptr) != strLen)
  166.     {
  167.         ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_Inv_chars);
  168.     }
  169.     // check if overflow/underflow occurs
  170.     if (errno == ERANGE)
  171.     {
  172.         if ( fValue < 0 )
  173.         {
  174.             if (fValue > (-1)*DBL_MIN)
  175.             {
  176.                 fDataConverted = true;
  177.                 fValue = 0;
  178.             }
  179.             else
  180.             {
  181.                 fType = NegINF;
  182.                 fDataConverted = true;
  183.             }
  184.         }
  185.         else if ( fValue > 0)
  186.         {
  187.             if (fValue < DBL_MIN )
  188.             {
  189.                 fDataConverted = true;
  190.                 fValue = 0;
  191.             }
  192.             else
  193.             {
  194.                 fType = PosINF;
  195.                 fDataConverted = true;
  196.             }
  197.         }
  198.         else
  199.         {
  200.             fDataConverted = true;
  201.         }
  202.     }
  203.     else
  204.     {
  205.         /**
  206.          *  float related checking
  207.          */
  208.         if (fValue < (-1) * FLT_MAX)
  209.         {
  210.             fType = NegINF;
  211.             fDataConverted = true;
  212.         }
  213.         else if (fValue > (-1)*FLT_MIN && fValue < 0)
  214.         {
  215.             fDataConverted = true;
  216.             fValue = 0;
  217.         }
  218.         else if (fValue > 0 && fValue < FLT_MIN )
  219.         {
  220.             fDataConverted = true;
  221.             fValue = 0;
  222.         }
  223.         else if  (fValue > FLT_MAX)
  224.         {
  225.             fType = PosINF;
  226.             fDataConverted = true;
  227.         }
  228.     }
  229. }
  230. XERCES_CPP_NAMESPACE_END