1 /*
2 * Copyright 2006-2008 The FLWOR Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "stdafx.h"
17
18 #include <stdexcept>
19
20 #include "common/common.h"
21 #include "util/string_util.h"
22 #include "zorbatypes/numconversions.h"
23
24 namespace zorba {
25
26 ///////////////////////////////////////////////////////////////////////////////
27
28 #define RANGE_ERROR(N,TYPE) \
29 std::range_error( BUILD_STRING( '"', (N), "\": number can not be represented as an " TYPE ) )
30
to_xs_int(xs_double const & d)31 xs_int to_xs_int( xs_double const &d ) {
32 zstring const temp( d.toIntegerString() );
33 return ztd::aton<xs_int>( temp.c_str() );
34 }
35
to_xs_int(xs_integer const & i)36 xs_int to_xs_int( xs_integer const &i ) {
37 #ifdef ZORBA_WITH_BIG_INTEGER
38 zstring const temp( i.toString() );
39 return ztd::aton<xs_int>( temp.c_str() );
40 #else
41 if ( i.is_xs_int() )
42 return static_cast<xs_int>( i.value_ );
43 throw RANGE_ERROR( i, "xs:int" );
44 #endif /* ZORBA_WITH_BIG_INTEGER */
45 }
46
to_xs_long(xs_decimal const & d)47 xs_long to_xs_long( xs_decimal const &d ) {
48 if ( d.is_xs_long() ) {
49 zstring const temp( d.toString() );
50 return ztd::aton<xs_long>( temp.c_str() );
51 }
52 throw RANGE_ERROR( d, "xs:long" );
53 }
54
to_xs_long(xs_integer const & i)55 xs_long to_xs_long( xs_integer const &i ) {
56 #ifdef ZORBA_WITH_BIG_INTEGER
57 zstring const temp( i.toString() );
58 return ztd::aton<xs_long>( temp.c_str() );
59 #else
60 if ( i.is_xs_long() )
61 return static_cast<xs_long>( i.value_ );
62 throw RANGE_ERROR( i, "xs:long" );
63 #endif /* ZORBA_WITH_BIG_INTEGER */
64 }
65
66 #ifndef ZORBA_WITH_BIG_INTEGER
to_xs_long(xs_nonNegativeInteger const & i)67 xs_long to_xs_long( xs_nonNegativeInteger const &i ) {
68 zstring const temp( i.toString() );
69 return ztd::aton<xs_long>( temp.c_str() );
70 }
71 #endif /* ZORBA_WITH_BIG_INTEGER */
72
to_xs_unsignedInt(xs_integer const & i)73 xs_unsignedInt to_xs_unsignedInt( xs_integer const &i ) {
74 #ifdef ZORBA_WITH_BIG_INTEGER
75 zstring const temp( i.toString() );
76 return ztd::aton<xs_unsignedInt>( temp.c_str() );
77 #else
78 if ( i.is_xs_unsignedInt() )
79 return static_cast<xs_unsignedInt>( i.value_ );
80 throw RANGE_ERROR( i, "xs:unsignedInt" );
81 #endif /* ZORBA_WITH_BIG_INTEGER */
82 }
83
to_xs_unsignedLong(xs_integer const & i)84 xs_unsignedLong to_xs_unsignedLong( xs_integer const &i ) {
85 #ifdef ZORBA_WITH_BIG_INTEGER
86 zstring const temp( i.toString() );
87 return ztd::aton<xs_unsignedLong>( temp.c_str() );
88 #else
89 if ( i.is_xs_unsignedLong() )
90 return static_cast<xs_unsignedLong>( i.value_ );
91 throw RANGE_ERROR( i, "xs:unsignedLong" );
92 #endif /* ZORBA_WITH_BIG_INTEGER */
93 }
94
95 ///////////////////////////////////////////////////////////////////////////////
96
97 } // namespace zorba
98 /* vim:set et sw=2 ts=2: */
99