summaryrefslogtreecommitdiff
path: root/misc/uClibc++/include/uClibc++/char_traits
blob: ee05be24d23ba339c84928b4b04ca15d9eda401e (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//***************************************************************************
// include/cxx/cerrno
//
//   Copyright (C) 2009 Gregory Nutt. All rights reserved.
//   Author: Gregory Nutt <gnutt@nuttx.org>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in
//    the documentation and/or other materials provided with the
//    distribution.
// 3. Neither the name NuttX nor the names of its contributors may be
//    used to endorse or promote products derived from this software
//    without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//***************************************************************************

#include <basic_definitions>
#include <string.h>
#include <exception>
#include <memory>

#ifdef __UCLIBCXX_HAS_WCHAR__
#include <cwchar>
#include <cwctype>
#endif

#ifndef __HEADER_CHAR_TRAITS
#define __HEADER_CHAR_TRAITS 1

extern "C++"
{

namespace std
{
  /* Inlining all wrapped function calls to shrink the amount of code generated */
  // Typedefs to use for stuff

  typedef signed int char_traits_off_type;

  // Generic char_traits

  template<class charT> struct _UCXXEXPORT char_traits { };

  // Specialize for char

  template<> struct _UCXXEXPORT char_traits<char>
  {
    typedef char char_type;
    typedef short int int_type;
    typedef char_traits_off_type off_type;
    typedef char_traits_off_type pos_type;
    typedef char state_type;

    inline static void assign(char_type & c, const char_type & d) { c = d; }

    static bool eq(const char_type& c1, const char_type& c2);

    static char_type to_char_type(const int_type & i);

    inline static int_type to_int_type(const char_type & c)
    {
      return (short int)(unsigned char)c;
    }

    inline static bool eq_int_type(const int_type & a, const int_type & b)
    {
      if (a==b)
      {
        return true;
      }

      return false;
    }

    inline static bool lt(const char_type& c1, const char_type& c2)
    {
      if (strncmp(&c1, &c2, 1) < 0)
      {
        return true;
      }

      return false;
    }

    inline static char_type* move(char_type* s1, const char_type* s2, size_t n)
    {
      return (char*) memmove(s1, s2, n);
    }

    inline static char_type* copy(char_type* s1, const char_type* s2, size_t n)
    {
      for (unsigned long int i=0; i< n; ++i)
      {
        assign(s1[i], s2[i]);
      }

      return s1 + n;
    }

    inline static char_type* assign(char_type* s, size_t n, char_type a)
    {
      return (char *)memset(s, a, n);
    }

    inline static int compare(const char_type* s1, const char_type* s2, size_t n)
    {
      return strncmp(s1, s2, n);
    }

    inline static size_t length(const char_type* s)
    {
      return strlen(s);
    }

    static const char_type* find(const char_type* s, int n, const char_type& a);

    inline static char_type eos() { return 0; }
    inline static int_type eof() { return -1; }

    inline static int_type not_eof(const int_type & i)
    {
      if (i == -1)
      {
        return 0;
      }
      else
      {
        return i;
      }
    }

    static state_type get_state(pos_type p)
    {
      p = p;
      state_type a;
      return a;
    }
  };

#ifdef __UCLIBCXX_HAS_WCHAR__
  template<> struct _UCXXEXPORT char_traits<wchar_t>
  {
    typedef wchar_t char_type;
    typedef wint_t int_type;
    typedef char_traits_off_type off_type;
    typedef char_traits_off_type pos_type;
    typedef mbstate_t state_type;

    static void assign(char_type & c, const char_type & d){ c=d; }

    static char_type to_char_type(const int_type & i)
    {
      return i;
    }

    static int_type to_int_type(const char_type & c)
    {
      return c;
    }

    inline static bool eq_int_type(const int_type & a, const int_type & b)
    {
      if (a == b)
      {
        return true;
      }

      return false;
    }

    inline static bool eq(const char_type& c1, const char_type& c2)
    {
      if (wcsncmp(&c1, &c2, 1) == 0)
      {
        return true;
      }

      return false;
    }

    inline static bool lt(const char_type& c1, const char_type& c2)
    {
      if(wcsncmp(&c1, &c2, 1) < 0)
      {
        return true;
      }

      return false;
    }

    inline static char_type* move(char_type* s1, const char_type* s2, size_t n)
    {
      return (char_type*) memmove(s1, s2, n * sizeof(char_type));
    }

    inline static char_type* copy(char_type* s1, const char_type* s2, size_t n)
    {
      for (unsigned long int i = 0; i< n; ++i)
      {
        assign(s1[i], s2[i]);
      }

      return s1 + n;
    }

    inline static char_type* assign(char_type* s, size_t n, char_type a)
    {
      return (char_type *)memset(s, a, n);  /*FIXME*/
    }

    inline static int compare(const char_type* s1, const char_type* s2, size_t n)
    {
      return wcsncmp(s1, s2, n);
    }

    inline static size_t length(const char_type* s)
    {
      return wcslen(s);
    }

    static const char_type* find(const char_type* s, int n, const char_type& a);

    inline static char_type eos() { return 0; }
    inline static int_type eof() { return WEOF; }

    inline static int_type not_eof(const int_type & i)
    {
      if (i == WEOF)
      {
        return (int_type)0;
      }
      else
      {
        return i;
      }
    }

    static state_type get_state(pos_type)
    {
      state_type a;
      return a;
    }
  };
#endif // __UCLIBCXX_HAS_WCHAR__

} // namespace
} // extern "C++"

#endif // __HEADER_CHAR_TRAITS