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

Subversion Repositories openrisc

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

Go to most recent revision | 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 "mistream.h"   // for istream_iterator, referenced in utf8.h
7
#include "sostream.h"
8
#include "ustring.h"
9
#include "ulimits.h"
10
#include <stdio.h>
11
 
12
namespace ustl {
13
 
14
/// Creates an output string stream linked to the given memory area.
15
ostringstream::ostringstream (void* p, size_t n)
16
: ostream (),
17
  m_Buffer (),
18
  m_Flags (0),
19
  m_Width (0),
20
  m_Base (10),
21
  m_Precision (2)
22
{
23
    exceptions (goodbit);
24
    link (p, n);
25
}
26
 
27
/// Creates an output string stream, initializing the buffer with v.
28
ostringstream::ostringstream (const string& v)
29
: ostream (),
30
  m_Buffer (v),
31
  m_Flags (0),
32
  m_Width (0),
33
  m_Base (10),
34
  m_Precision (2)
35
{
36
    exceptions (goodbit);
37
    ostream::link (m_Buffer);
38
}
39
 
40
/// Copies \p s to the internal buffer.
41
void ostringstream::str (const string& s)
42
{
43
    m_Buffer = s;
44
    ostream::link (m_Buffer);
45
    SetPos (m_Buffer.size());
46
}
47
 
48
/// Writes a single character into the stream.
49
void ostringstream::iwrite (uint8_t v)
50
{
51
    if (remaining() >= 1 || overflow() >= 1)
52
        ostream::iwrite (v);
53
}
54
 
55
/// Writes the contents of \p buffer of \p size into the stream.
56
ostringstream& ostringstream::write (const void* buffer, size_type sz)
57
{
58
    const char* buf = (const char*) buffer;
59
    for (size_type bw = 0; (bw = min(sz, remaining() ? remaining() : overflow(sz))); buf += bw, sz -= bw)
60
        ostream::write (buf, bw);
61
    return (*this);
62
}
63
 
64
/// Simple decimal encoding of \p n into \p fmt.
65
inline char* ostringstream::encode_dec (char* fmt, uint32_t n) const
66
{
67
    do {
68
        *fmt++ = '0' + n % 10;
69
    } while (n /= 10);
70
    return (fmt);
71
}
72
 
73
/// Generates a sprintf format string for the given type.
74
void ostringstream::fmtstring (char* fmt, const char* typestr, bool bInteger) const
75
{
76
    *fmt++ = '%';
77
    if (m_Width)
78
        fmt = encode_dec (fmt, m_Width);
79
    if (m_Flags & left)
80
        *fmt++ = '-';
81
    if (!bInteger) {
82
        *fmt++ = '.';
83
        fmt = encode_dec (fmt, m_Precision);
84
    }
85
    while (*typestr)
86
        *fmt++ = *typestr++;
87
    if (bInteger) {
88
        if (m_Base == 16)
89
            fmt[-1] = 'X';
90
        else if (m_Base == 8)
91
            fmt[-1] = 'o';
92
    } else {
93
        if (m_Flags & scientific)
94
            fmt[-1] = 'E';
95
    }
96
    *fmt = 0;
97
}
98
 
99
/// Writes \p v into the stream as utf8
100
void ostringstream::iwrite (wchar_t v)
101
{
102
    char buffer [8];
103
    *utf8out(buffer) = v;
104
    write (buffer, Utf8Bytes(v));
105
}
106
 
107
/// Writes value \p v into the stream as text.
108
void ostringstream::iwrite (bool v)
109
{
110
    static const char tf[2][8] = { "false", "true" };
111
    write (tf[v], 5 - v);
112
}
113
 
114
/// Equivalent to a vsprintf on the string.
115
int ostringstream::vformat (const char* fmt, va_list args)
116
{
117
#if HAVE_VA_COPY
118
    va_list args2;
119
#else
120
    #define args2 args
121
    #undef __va_copy
122
    #define __va_copy(x,y)
123
#endif
124
    size_t rv, space;
125
    do {
126
        space = remaining();
127
        __va_copy (args2, args);
128
        rv = vsnprintf (ipos(), space, fmt, args2);
129
        if (ssize_t(rv) < 0)
130
            rv = space;
131
    } while (rv >= space && rv < overflow(rv + 1));
132
    SetPos (pos() + min (rv, space));
133
    return (rv);
134
}
135
 
136
/// Equivalent to a sprintf on the string.
137
int ostringstream::format (const char* fmt, ...)
138
{
139
    va_list args;
140
    va_start (args, fmt);
141
    const int rv = vformat (fmt, args);
142
    va_end (args);
143
    return (rv);
144
}
145
 
146
/// Links to string \p l as resizable.
147
void ostringstream::link (void* p, size_type n)
148
{
149
    assert ((p || !n) && "The output string buffer must not be read-only");
150
    ostream::link (p, n);
151
    m_Buffer.link (p, n);
152
}
153
 
154
/// Attempts to create more output space. Returns remaining().
155
ostringstream::size_type ostringstream::overflow (size_type n)
156
{
157
    if (n > remaining()) {
158
        const uoff_t oldPos (pos());
159
        m_Buffer.reserve (oldPos + n, false);
160
        m_Buffer.resize (oldPos + n);
161
        ostream::link (m_Buffer);
162
        SetPos (oldPos);
163
    }
164
    verify_remaining ("write", "text", n);
165
    return (remaining());
166
}
167
 
168
} // namespace ustl

powered by: WebSVN 2.1.0

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