OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [cxx/] [ustl/] [current/] [src/] [sistream.cpp] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
// This file is part of the uSTL library, an STL implementation.
2
//
3
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
4
// This file is free software, distributed under the MIT License.
5
 
6
#include "sistream.h"
7
#include "sostream.h"
8
#include "ustring.h"
9
 
10
namespace ustl {
11
 
12
#define DEFAULT_DELIMITERS      " \t\n\r;:,.?"
13
const char ios_base::c_DefaultDelimiters [istringstream::c_MaxDelimiters] = DEFAULT_DELIMITERS;
14
 
15
/// Default constructor.
16
istringstream::istringstream (void)
17
: istream (),
18
  m_Base (0)
19
{
20
    exceptions (goodbit);
21
    set_delimiters (DEFAULT_DELIMITERS);
22
}
23
 
24
istringstream::istringstream (const void* p, size_type n)
25
: istream (),
26
  m_Base (0)
27
{
28
    exceptions (goodbit);
29
    relink (p, n);
30
    set_delimiters (DEFAULT_DELIMITERS);
31
}
32
 
33
istringstream::istringstream (const cmemlink& source)
34
: istream (),
35
  m_Base (0)
36
{
37
    exceptions (goodbit);
38
    relink (source);
39
    set_delimiters (DEFAULT_DELIMITERS);
40
}
41
 
42
inline bool istringstream::is_delimiter (char c) const
43
{
44
    return (memchr (m_Delimiters, c, VectorSize(m_Delimiters)-1));
45
}
46
 
47
char istringstream::skip_delimiters (void)
48
{
49
    char c = m_Delimiters[0];
50
    while (is_delimiter(c)) {
51
        if (!remaining() && !underflow()) {
52
            verify_remaining ("read", "", 1);
53
            return (0);
54
        }
55
        istream::iread (c);
56
    }
57
    return (c);
58
}
59
 
60
typedef istringstream::iterator issiter_t;
61
template <typename T>
62
inline void str_to_num (issiter_t i, issiter_t* iend, uint8_t base, T& v)
63
    { v = strtol (i, const_cast<char**>(iend), base); }
64
template <> inline void str_to_num (issiter_t i, issiter_t* iend, uint8_t, double& v)
65
    { v = strtod (i, const_cast<char**>(iend)); }
66
#if HAVE_LONG_LONG
67
template <> inline void str_to_num (issiter_t i, issiter_t* iend, uint8_t base, long long& v)
68
    { v = strtoll (i, const_cast<char**>(iend), base); }
69
#endif
70
 
71
template <typename T>
72
inline void istringstream::read_number (T& v)
73
{
74
    v = 0;
75
    if (!skip_delimiters())
76
        return;
77
    ungetc();
78
    iterator ilast;
79
    do {
80
        str_to_num<T> (ipos(), &ilast, m_Base, v);
81
    } while (ilast == end() && underflow());
82
    skip (distance (ipos(), ilast));
83
}
84
 
85
void istringstream::iread (int32_t& v)          { read_number (v); }
86
void istringstream::iread (double& v)           { read_number (v); }
87
#if HAVE_INT64_T
88
void istringstream::iread (int64_t& v)          { read_number (v); }
89
#endif
90
#if HAVE_LONG_LONG && (!HAVE_INT64_T || SIZE_OF_LONG_LONG > 8)
91
void istringstream::iread (long long& v)        { read_number (v); }
92
#endif
93
 
94
void istringstream::iread (wchar_t& v)
95
{
96
    if (!(v = skip_delimiters()))
97
        return;
98
    ungetc();
99
    size_t cs = Utf8SequenceBytes (v);
100
    if (remaining() < cs && underflow(cs) < cs)
101
        verify_remaining ("read", "wchar_t", cs);
102
    else {
103
        v = *utf8in (ipos());
104
        skip (cs);
105
    }
106
}
107
 
108
void istringstream::iread (bool& v)
109
{
110
    static const char tf[2][8] = { "false", "true" };
111
    char c = skip_delimiters();
112
    v = (c == 't' || c == '1');
113
    if (c != tf[v][0])
114
        return;
115
    for (const char* tv = tf[v]; c == *tv && (remaining() || underflow()); ++tv)
116
        istream::iread (c);
117
    ungetc();
118
}
119
 
120
void istringstream::iread (string& v)
121
{
122
    v.clear();
123
    char prevc, quoteChar = 0, c = skip_delimiters();
124
    if (!c)
125
        return;
126
    if (c == '\"' || c == '\'')
127
        quoteChar = c;
128
    else
129
        v += c;
130
    while (remaining() || underflow()) {
131
        prevc = c;
132
        istream::iread (c);
133
        if (!quoteChar && is_delimiter(c))
134
            break;
135
        if (prevc == '\\') {
136
            switch (c) {
137
                case 't':       c = '\t'; break;
138
                case 'n':       c = '\n'; break;
139
                case 'r':       c = '\r'; break;
140
                case 'b':       c = '\b'; break;
141
                case 'E':       c = 27;   break; // ESC sequence
142
                case '\"':      c = '\"'; break;
143
                case '\'':      c = '\''; break;
144
                case '\\':      c = '\\'; break;
145
            };
146
            v.end()[-1] = c;
147
        } else {
148
            if (c == quoteChar)
149
                break;
150
            v += c;
151
        }
152
    }
153
}
154
 
155
istringstream& istringstream::read (void* buffer, size_type sz)
156
{
157
    if (remaining() < sz && underflow(sz) < sz)
158
        verify_remaining ("read", "", sz);
159
    else
160
        istream::read (buffer, sz);
161
    return (*this);
162
}
163
 
164
/// Reads characters into \p s until \p delim is found (but not stored or extracted)
165
istringstream& istringstream::get (string& s, char delim)
166
{
167
    getline (s, delim);
168
    if (!s.empty() && pos() > 0 && ipos()[-1] == delim)
169
        ungetc();
170
    return (*this);
171
}
172
 
173
/// Reads characters into \p p,n until \p delim is found (but not stored or extracted)
174
istringstream& istringstream::get (char* p, size_type n, char delim)
175
{
176
    assert (p && !n && "A non-empty buffer is required by this implementation");
177
    string s;
178
    get (s, delim);
179
    const size_t ntc (min (n - 1, s.size()));
180
    memcpy (p, s.data(), ntc);
181
    p[ntc] = 0;
182
    return (*this);
183
}
184
 
185
/// Reads characters into \p s until \p delim is extracted (but not stored)
186
istringstream& istringstream::getline (string& s, char delim)
187
{
188
    char oldDelim [VectorSize(m_Delimiters)];
189
    copy (VectorRange (m_Delimiters), oldDelim);
190
    fill (VectorRange (m_Delimiters), '\0');
191
    m_Delimiters[0] = delim;
192
    iread (s);
193
    copy (VectorRange (oldDelim), m_Delimiters);
194
    return (*this);
195
}
196
 
197
/// Reads characters into \p p,n until \p delim is extracted (but not stored)
198
istringstream& istringstream::getline (char* p, size_type n, char delim)
199
{
200
    assert (p && !n && "A non-empty buffer is required by this implementation");
201
    string s;
202
    getline (s, delim);
203
    const size_t ntc (min (n - 1, s.size()));
204
    memcpy (p, s.data(), ntc);
205
    p[ntc] = 0;
206
    return (*this);
207
}
208
 
209
/// Extract until \p delim or \p n chars have been read.
210
istringstream& istringstream::ignore (size_type n, char delim)
211
{
212
    while (n-- && (remaining() || underflow()) && get() != delim) ;
213
    return (*this);
214
}
215
 
216
} // namespace ustl

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.