aboutsummaryrefslogtreecommitdiff
path: root/odb/oracle/traits.cxx
blob: c842755144c729fd80eb394e09d365d36170eb4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// file      : odb/oracle/traits.cxx
// author    : Constantin Michael <constantin@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <cmath>
#include <deque>

#include <odb/oracle/traits.hxx>

namespace odb
{
  namespace oracle
  {
    long long
    out (unsigned char const* n)
    {
      // The first byte specifies the length of the number representation,
      // including the exponent byte and significant digits but not the length
      // itself.
      //
      int len (n[0]);

      if (len == 1)
        return 0;

      long long v (0);
      if (n[1] & 0x80)
      {
        // The exponent of a positive number is calculated as n[1] - 193.
        // The base-100 exponent of the least significant digit can be
        // calculated as exp - sig - 1, where sig is the number of significant
        // digits. sig may be calculated as len - 2.
        //
        int exp (n[1] - 191 - len);

        for (unsigned char const* m (n + len), *e (n + 1); m > e; --m, ++exp)
          v += (*m - 1) * pow (100, exp);
      }
      else
      {
        // The exponent of a negative number is calculated as ~n[1] - 193.
        // The base-100 exponent of the least significant digit can be
        // calculated as exp - sig - 1, where sig is the number of significant
        // digits. sig may be calculated as len - 2.
        //
        int exp (~n[1] - 190 - len);

        for (unsigned char const* m (n + len - 1), *e (n + 1); m > e;
             --m, ++exp)
          v -= (101 - *m) * pow (100, exp);
      }

      return v;
    }

    void
    in (unsigned char b[22], long long n)
    {
      if (n == 0)
      {
        b[0] = 1;
        b[1] = 128;

        return;
      }

      // @@ Is there some sort of reserve() implementation for deque?
      //
      deque<unsigned char> s;

      bool sig (false);
      size_t c (0);
      unsigned char exp (0);

      if (n < 0)
      {
        // Termination marker for negative numbers.
        //
        s.push_front (102);

        while (n != 0)
        {
          int v (static_cast<int> (n % 100));
          sig = sig ? true : v != 0;

          if (sig)
            s.push_front (static_cast<unsigned char> (101 + v));

          n /= 100;
          ++c;
        }

        // The exponent is one less than the number of base 100 digits. It is
        // inverted for negative values.
        //
        exp = static_cast<unsigned char> (~(c + 192));
      }
      else
      {
        while (n != 0)
        {
          int v (static_cast<int> (n % 100));
          sig = sig ? true : v != 0;

          if (sig)
            s.push_front (static_cast<unsigned char> (v + 1));

          n /= 100;
          ++c;
        }

        // Exponent is one less than the number of base 100 digits.
        //
        exp = static_cast<unsigned char> (c + 192);
      }

      // Set the length.
      //
      b[0] = static_cast<unsigned char> (s.size () + 1);

      // Set the exponent.
      //
      b[1] = exp;

      // Set the significant digits (big-endian byte order) and the terminator,
      // if any.
      //
      c = 2;
      while (!s.empty ())
      {
        b[i] = s.front ();
        s.pop_front ();
        ++c;
      }
    }
  }
}