os/ossrv/genericopenlibs/cppstdlib/stl/src/num_put.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     3
 *
sl@0
     4
 * Copyright (c) 1999
sl@0
     5
 * Silicon Graphics Computer Systems, Inc.
sl@0
     6
 *
sl@0
     7
 * Copyright (c) 1999
sl@0
     8
 * Boris Fomitchev
sl@0
     9
 *
sl@0
    10
 * This material is provided "as is", with absolutely no warranty expressed
sl@0
    11
 * or implied. Any use is at your own risk.
sl@0
    12
 *
sl@0
    13
 * Permission to use or copy this software for any purpose is hereby granted
sl@0
    14
 * without fee, provided the above notices are retained on all copies.
sl@0
    15
 * Permission to modify the code and to distribute modified code is granted,
sl@0
    16
 * provided the above notices are retained, and a notice that the code was
sl@0
    17
 * modified is included with the above copyright notice.
sl@0
    18
 *
sl@0
    19
 */
sl@0
    20
sl@0
    21
#include "stlport_prefix.h"
sl@0
    22
sl@0
    23
#include <locale>
sl@0
    24
#include <ostream>
sl@0
    25
sl@0
    26
#ifdef __SYMBIAN32__WSD__
sl@0
    27
#include <libstdcppwsd.h>
sl@0
    28
#endif //__SYMBIAN32__WSD__
sl@0
    29
sl@0
    30
_STLP_BEGIN_NAMESPACE
sl@0
    31
sl@0
    32
// Note that grouping[0] is the number of digits in the *rightmost* group.
sl@0
    33
// We assume, without checking, that *last is null and that there is enough
sl@0
    34
// space in the buffer to extend the number past [first, last).
sl@0
    35
template <class Char>
sl@0
    36
static ptrdiff_t
sl@0
    37
__insert_grouping_aux(Char* first, Char* last, const string& grouping,
sl@0
    38
                      Char separator, Char Plus, Char Minus,
sl@0
    39
                      int basechars) {
sl@0
    40
  typedef string::size_type str_size;
sl@0
    41
sl@0
    42
  if (first == last)
sl@0
    43
    return 0;
sl@0
    44
sl@0
    45
  int sign = 0;
sl@0
    46
sl@0
    47
  if (*first == Plus || *first == Minus) {
sl@0
    48
    sign = 1;
sl@0
    49
    ++first;
sl@0
    50
  }
sl@0
    51
sl@0
    52
  first += basechars;
sl@0
    53
  str_size n = 0;               // Index of the current group.
sl@0
    54
  Char* cur_group = last;       // Points immediately beyond the rightmost
sl@0
    55
                                // digit of the current group.
sl@0
    56
  int groupsize = 0;            // Size of the current group.
sl@0
    57
sl@0
    58
  for (;;) {
sl@0
    59
    groupsize = n < grouping.size() ? grouping[n] : groupsize;
sl@0
    60
    ++n;
sl@0
    61
sl@0
    62
    if (groupsize <= 0 || groupsize >= cur_group - first)
sl@0
    63
      break;
sl@0
    64
sl@0
    65
    // Insert a separator character just before position cur_group - groupsize
sl@0
    66
    cur_group -= groupsize;
sl@0
    67
    ++last;
sl@0
    68
    copy_backward(cur_group, last, last + 1);
sl@0
    69
    *cur_group = separator;
sl@0
    70
  }
sl@0
    71
sl@0
    72
  return (last - first) + sign + basechars;
sl@0
    73
}
sl@0
    74
sl@0
    75
//Dynamic output buffer version.
sl@0
    76
template <class Char, class Str>
sl@0
    77
static void
sl@0
    78
__insert_grouping_aux( /* __basic_iostring<Char> */ Str& iostr, size_t __group_pos,
sl@0
    79
                      const string& grouping,
sl@0
    80
                      Char separator, Char Plus, Char Minus,
sl@0
    81
                      int basechars) {
sl@0
    82
  typedef string::size_type str_size;
sl@0
    83
sl@0
    84
  if (iostr.size() < __group_pos)
sl@0
    85
    return;
sl@0
    86
sl@0
    87
#ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
sl@0
    88
  int __first_pos = 0;
sl@0
    89
#else
sl@0
    90
  size_t __first_pos = 0;
sl@0
    91
#endif
sl@0
    92
  Char __first = *iostr.begin();
sl@0
    93
sl@0
    94
  if (__first == Plus || __first == Minus) {
sl@0
    95
    ++__first_pos;
sl@0
    96
  }
sl@0
    97
sl@0
    98
  __first_pos += basechars;
sl@0
    99
#ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
sl@0
   100
  typename basic_string<Char>::iterator cur_group(iostr.begin() + __group_pos);  // Points immediately beyond the rightmost
sl@0
   101
  																	// digit of the current group.
sl@0
   102
  int groupsize = 0; // Size of the current group (if grouping.size() == 0, size
sl@0
   103
                     // of group unlimited: we force condition (groupsize <= 0))
sl@0
   104
sl@0
   105
  for ( str_size n = 0; ; ) { // Index of the current group
sl@0
   106
    if ( n < grouping.size() ) {
sl@0
   107
      groupsize = __STATIC_CAST( int, grouping[n++] );
sl@0
   108
    }
sl@0
   109
sl@0
   110
    if ( (groupsize <= 0) || (groupsize >= ((cur_group - iostr.begin()) - __first_pos)) ||
sl@0
   111
         (groupsize == CHAR_MAX)) {
sl@0
   112
      break;
sl@0
   113
    }
sl@0
   114
#else
sl@0
   115
  str_size n = 0;                                                   // Index of the current group.
sl@0
   116
  typename basic_string<Char>::iterator cur_group(iostr.begin() + __group_pos);  // Points immediately beyond the rightmost
sl@0
   117
                                                                    // digit of the current group.
sl@0
   118
  unsigned int groupsize = 0;                                       // Size of the current group.
sl@0
   119
sl@0
   120
  for (;;) {
sl@0
   121
    groupsize = n < grouping.size() ? grouping[n] : groupsize;
sl@0
   122
    ++n;
sl@0
   123
sl@0
   124
    if (groupsize <= 0 || groupsize >= ((cur_group - iostr.begin()) + __first_pos))
sl@0
   125
      break;
sl@0
   126
#endif
sl@0
   127
sl@0
   128
    // Insert a separator character just before position cur_group - groupsize
sl@0
   129
    cur_group -= groupsize;
sl@0
   130
    cur_group = iostr.insert(cur_group, separator);
sl@0
   131
  }
sl@0
   132
}
sl@0
   133
sl@0
   134
//----------------------------------------------------------------------
sl@0
   135
// num_put
sl@0
   136
sl@0
   137
_STLP_MOVE_TO_PRIV_NAMESPACE
sl@0
   138
sl@0
   139
_STLP_DECLSPEC const char* _STLP_CALL __hex_char_table_lo()
sl@0
   140
{ return "0123456789abcdefx"; }
sl@0
   141
sl@0
   142
_STLP_DECLSPEC const char* _STLP_CALL __hex_char_table_hi()
sl@0
   143
{ return "0123456789ABCDEFX"; }
sl@0
   144
sl@0
   145
char* _STLP_CALL
sl@0
   146
__write_integer(char* buf, ios_base::fmtflags flags, long x) {
sl@0
   147
  char tmp[64];
sl@0
   148
  char* bufend = tmp+64;
sl@0
   149
  char* beg = __write_integer_backward(bufend, flags, x);
sl@0
   150
  return copy(beg, bufend, buf);
sl@0
   151
}
sl@0
   152
sl@0
   153
///-------------------------------------
sl@0
   154
sl@0
   155
_STLP_DECLSPEC ptrdiff_t _STLP_CALL
sl@0
   156
__insert_grouping(char * first, char * last, const string& grouping,
sl@0
   157
                  char separator, char Plus, char Minus, int basechars) {
sl@0
   158
  return __insert_grouping_aux(first, last, grouping,
sl@0
   159
                               separator, Plus, Minus, basechars);
sl@0
   160
}
sl@0
   161
sl@0
   162
_STLP_DECLSPEC void _STLP_CALL
sl@0
   163
__insert_grouping(__iostring &str, size_t group_pos, const string& grouping,
sl@0
   164
                  char separator, char Plus, char Minus, int basechars) {
sl@0
   165
  __insert_grouping_aux(str, group_pos, grouping, separator, Plus, Minus, basechars);
sl@0
   166
}
sl@0
   167
sl@0
   168
#if !defined (_STLP_NO_WCHAR_T)
sl@0
   169
_STLP_DECLSPEC ptrdiff_t _STLP_CALL
sl@0
   170
__insert_grouping(wchar_t* first, wchar_t* last, const string& grouping,
sl@0
   171
                  wchar_t separator, wchar_t Plus, wchar_t Minus,
sl@0
   172
                  int basechars) {
sl@0
   173
  return __insert_grouping_aux(first, last, grouping, separator,
sl@0
   174
                               Plus, Minus, basechars);
sl@0
   175
}
sl@0
   176
sl@0
   177
_STLP_DECLSPEC void _STLP_CALL
sl@0
   178
__insert_grouping(__iowstring &str, size_t group_pos, const string& grouping,
sl@0
   179
                  wchar_t separator, wchar_t Plus, wchar_t Minus,
sl@0
   180
                  int basechars) {
sl@0
   181
  __insert_grouping_aux(str, group_pos, grouping, separator, Plus, Minus, basechars);
sl@0
   182
}
sl@0
   183
#endif
sl@0
   184
sl@0
   185
_STLP_MOVE_TO_STD_NAMESPACE
sl@0
   186
sl@0
   187
#if defined (__SYMBIAN32__WSD__)
sl@0
   188
template <>
sl@0
   189
_STLP_DECLSPEC locale::id& num_put<char, ostreambuf_iterator<char, char_traits<char> > >::GetFacetLocaleId()
sl@0
   190
    {
sl@0
   191
	return get_libcpp_wsd().num_put_char_ostreambuf_iterator_id;
sl@0
   192
    }
sl@0
   193
#  ifndef _STLP_NO_WCHAR_T
sl@0
   194
template <>
sl@0
   195
_STLP_DECLSPEC locale::id& num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::GetFacetLocaleId()
sl@0
   196
    {
sl@0
   197
	return get_libcpp_wsd().num_put_wchar_ostreambuf_iterator_id;
sl@0
   198
    }
sl@0
   199
#  endif /* _STLP_NO_WCHAR_T */
sl@0
   200
#endif /* __SYMBIAN32__WSD__ */
sl@0
   201
sl@0
   202
//----------------------------------------------------------------------
sl@0
   203
// Force instantiation of num_put<>
sl@0
   204
#if !defined(_STLP_NO_FORCE_INSTANTIATE)
sl@0
   205
template class _STLP_CLASS_DECLSPEC ostreambuf_iterator<char, char_traits<char> >;
sl@0
   206
// template class num_put<char, char*>;
sl@0
   207
template class num_put<char, ostreambuf_iterator<char, char_traits<char> > >;
sl@0
   208
# ifndef _STLP_NO_WCHAR_T
sl@0
   209
template class ostreambuf_iterator<wchar_t, char_traits<wchar_t> >;
sl@0
   210
template class num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
sl@0
   211
// template class num_put<wchar_t, wchar_t*>;
sl@0
   212
# endif /* INSTANTIATE_WIDE_STREAMS */
sl@0
   213
#endif
sl@0
   214
sl@0
   215
#if defined(__EPOC32__)
sl@0
   216
template <>
sl@0
   217
locale::id num_put<char, ostreambuf_iterator<char, char_traits<char> > >::id={14};
sl@0
   218
sl@0
   219
# if !defined (_STLP_NO_WCHAR_T)
sl@0
   220
template <>
sl@0
   221
locale::id num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id={33};
sl@0
   222
# endif
sl@0
   223
sl@0
   224
#endif
sl@0
   225
sl@0
   226
_STLP_END_NAMESPACE
sl@0
   227
sl@0
   228
// Local Variables:
sl@0
   229
// mode:C++
sl@0
   230
// End: