]> git.buserror.net Git - polintos/scott/priv.git/blob - lib/c++/stlport/num_put.cpp
Add STLport 5.1.4
[polintos/scott/priv.git] / lib / c++ / stlport / num_put.cpp
1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18
19 #include "stlport_prefix.h"
20
21 #include <locale>
22 #include <ostream>
23
24 _STLP_BEGIN_NAMESPACE
25
26 // Note that grouping[0] is the number of digits in the *rightmost* group.
27 // We assume, without checking, that *last is null and that there is enough
28 // space in the buffer to extend the number past [first, last).
29 template <class Char>
30 static ptrdiff_t
31 __insert_grouping_aux(Char* first, Char* last, const string& grouping,
32                       Char separator, Char Plus, Char Minus,
33                       int basechars) {
34   typedef string::size_type str_size;
35
36   if (first == last)
37     return 0;
38
39   int sign = 0;
40
41   if (*first == Plus || *first == Minus) {
42     sign = 1;
43     ++first;
44   }
45
46   first += basechars;
47   str_size n = 0;               // Index of the current group.
48   Char* cur_group = last;       // Points immediately beyond the rightmost
49                                 // digit of the current group.
50   int groupsize = 0;            // Size of the current group.
51
52   for (;;) {
53     groupsize = n < grouping.size() ? grouping[n] : groupsize;
54     ++n;
55
56     if (groupsize <= 0 || groupsize >= cur_group - first)
57       break;
58
59     // Insert a separator character just before position cur_group - groupsize
60     cur_group -= groupsize;
61     ++last;
62     copy_backward(cur_group, last, last + 1);
63     *cur_group = separator;
64   }
65
66   return (last - first) + sign + basechars;
67 }
68
69 //Dynamic output buffer version.
70 template <class Char, class Str>
71 static void
72 __insert_grouping_aux( /* __basic_iostring<Char> */ Str& iostr, size_t __group_pos,
73                       const string& grouping,
74                       Char separator, Char Plus, Char Minus,
75                       int basechars) {
76   typedef string::size_type str_size;
77
78   if (iostr.size() < __group_pos)
79     return;
80
81   size_t __first_pos = 0;
82   Char __first = *iostr.begin();
83
84   if (__first == Plus || __first == Minus) {
85     ++__first_pos;
86   }
87
88   __first_pos += basechars;
89   str_size n = 0;                                                   // Index of the current group.
90   typename basic_string<Char>::iterator cur_group(iostr.begin() + __group_pos);  // Points immediately beyond the rightmost
91                                                                     // digit of the current group.
92   unsigned int groupsize = 0;                                       // Size of the current group.
93
94   for (;;) {
95     groupsize = n < grouping.size() ? grouping[n] : groupsize;
96     ++n;
97
98     if (groupsize <= 0 || groupsize >= ((cur_group - iostr.begin()) + __first_pos))
99       break;
100
101     // Insert a separator character just before position cur_group - groupsize
102     cur_group -= groupsize;
103     cur_group = iostr.insert(cur_group, separator);
104   }
105 }
106
107 //----------------------------------------------------------------------
108 // num_put
109
110 _STLP_MOVE_TO_PRIV_NAMESPACE
111
112 _STLP_DECLSPEC const char* _STLP_CALL __hex_char_table_lo()
113 { return "0123456789abcdefx"; }
114
115 _STLP_DECLSPEC const char* _STLP_CALL __hex_char_table_hi()
116 { return "0123456789ABCDEFX"; }
117
118 char* _STLP_CALL
119 __write_integer(char* buf, ios_base::fmtflags flags, long x) {
120   char tmp[64];
121   char* bufend = tmp+64;
122   char* beg = __write_integer_backward(bufend, flags, x);
123   return copy(beg, bufend, buf);
124 }
125
126 ///-------------------------------------
127
128 ptrdiff_t _STLP_CALL
129 __insert_grouping(char * first, char * last, const string& grouping,
130                   char separator, char Plus, char Minus, int basechars) {
131   return __insert_grouping_aux(first, last, grouping,
132                                separator, Plus, Minus, basechars);
133 }
134
135 void _STLP_CALL
136 __insert_grouping(__iostring &str, size_t group_pos, const string& grouping,
137                   char separator, char Plus, char Minus, int basechars) {
138   __insert_grouping_aux(str, group_pos, grouping, separator, Plus, Minus, basechars);
139 }
140
141 #if !defined (_STLP_NO_WCHAR_T)
142 ptrdiff_t _STLP_CALL
143 __insert_grouping(wchar_t* first, wchar_t* last, const string& grouping,
144                   wchar_t separator, wchar_t Plus, wchar_t Minus,
145                   int basechars) {
146   return __insert_grouping_aux(first, last, grouping, separator,
147                                Plus, Minus, basechars);
148 }
149
150 void _STLP_CALL
151 __insert_grouping(__iowstring &str, size_t group_pos, const string& grouping,
152                   wchar_t separator, wchar_t Plus, wchar_t Minus,
153                   int basechars) {
154   __insert_grouping_aux(str, group_pos, grouping, separator, Plus, Minus, basechars);
155 }
156 #endif
157
158 _STLP_MOVE_TO_STD_NAMESPACE
159
160 //----------------------------------------------------------------------
161 // Force instantiation of num_put<>
162 #if !defined(_STLP_NO_FORCE_INSTANTIATE)
163 template class _STLP_CLASS_DECLSPEC ostreambuf_iterator<char, char_traits<char> >;
164 // template class num_put<char, char*>;
165 template class num_put<char, ostreambuf_iterator<char, char_traits<char> > >;
166 # ifndef _STLP_NO_WCHAR_T
167 template class ostreambuf_iterator<wchar_t, char_traits<wchar_t> >;
168 template class num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
169 // template class num_put<wchar_t, wchar_t*>;
170 # endif /* INSTANTIATE_WIDE_STREAMS */
171 #endif
172
173 _STLP_END_NAMESPACE
174
175 // Local Variables:
176 // mode:C++
177 // End: