1 | /*
|
---|
2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <ios>
|
---|
30 |
|
---|
31 | namespace std
|
---|
32 | {
|
---|
33 | ios_base::ios_base()
|
---|
34 | : iarray_{}, parray_{}, iarray_size_{}, parray_size_{},
|
---|
35 | flags_{}, precision_{}, width_{}, locale_{/* TODO: use locale()? */},
|
---|
36 | callbacks_{}
|
---|
37 | { /* DUMMY BODY */ }
|
---|
38 |
|
---|
39 | ~ios_base::ios_base()
|
---|
40 | {
|
---|
41 | for (auto& callback: callbacks_)
|
---|
42 | callback.first(erase_event, *this, callback.second);
|
---|
43 | }
|
---|
44 |
|
---|
45 | auto ios_base::flags() -> fmtflags const
|
---|
46 | {
|
---|
47 | return flags_;
|
---|
48 | }
|
---|
49 |
|
---|
50 | auto ios_base::flags(fmtflags fmtfl) -> fmtflags
|
---|
51 | {
|
---|
52 | auto old = flags_;
|
---|
53 | flags_ = fmtfl;
|
---|
54 |
|
---|
55 | return old;
|
---|
56 | }
|
---|
57 |
|
---|
58 | auto ios_base::setf(fmtflags fmtfl) -> fmtflags
|
---|
59 | {
|
---|
60 | auto old = flags_;
|
---|
61 | flags_ |= fmtfl;
|
---|
62 |
|
---|
63 | return old;
|
---|
64 | }
|
---|
65 |
|
---|
66 | auto ios_base::setf(fmtflags fmtfl, fmtflags mask) -> fmtflags
|
---|
67 | {
|
---|
68 | auto old = flags_;
|
---|
69 | flags_ &= ~mask;
|
---|
70 | flags_ |= fmtfl & mask;
|
---|
71 |
|
---|
72 | return old;
|
---|
73 | }
|
---|
74 |
|
---|
75 | void ios_base::unsetf(fmtflags fmtfl)
|
---|
76 | {
|
---|
77 | flags_ &= ~fmtflags;
|
---|
78 | }
|
---|
79 |
|
---|
80 | streamsize ios_base::precision() const
|
---|
81 | {
|
---|
82 | return precision_;
|
---|
83 | }
|
---|
84 |
|
---|
85 | streamsize ios_base::precision(streamsize prec)
|
---|
86 | {
|
---|
87 | auto old = precision_;
|
---|
88 | precision_ = prec;
|
---|
89 |
|
---|
90 | return old;
|
---|
91 | }
|
---|
92 |
|
---|
93 | streamsize ios_base::width() const
|
---|
94 | {
|
---|
95 | return width_;
|
---|
96 | }
|
---|
97 |
|
---|
98 | streamsize ios_base::width(streamsize wide)
|
---|
99 | {
|
---|
100 | auto old = width_;
|
---|
101 | width_ = wide;
|
---|
102 |
|
---|
103 | return old;
|
---|
104 | }
|
---|
105 |
|
---|
106 | locale ios_base::imbue(const locale& loc)
|
---|
107 | {
|
---|
108 | auto old = locale_;
|
---|
109 | locale_ = loc;
|
---|
110 |
|
---|
111 | for (auto& callback: callbacks_)
|
---|
112 | callback.first(imbue_event, *this, callback.second);
|
---|
113 |
|
---|
114 | return old;
|
---|
115 | }
|
---|
116 |
|
---|
117 | locale ios_base::get_loc() const
|
---|
118 | {
|
---|
119 | return locale_;
|
---|
120 | }
|
---|
121 |
|
---|
122 | long& ios_base::iword(int index)
|
---|
123 | {
|
---|
124 | if (!iarray_)
|
---|
125 | {
|
---|
126 | iarray_ = new long[initial_size_];
|
---|
127 | iarray_size_ = initial_size_;
|
---|
128 | }
|
---|
129 |
|
---|
130 | auto idx = static_cast<size_t>(index);
|
---|
131 | if (idx > iarray_size_)
|
---|
132 | { // TODO: Enclose in try block and set failbit if needed
|
---|
133 | // and return ierror_.
|
---|
134 | size_t new_size = max(iarray_size_ * 2, idx + 1);
|
---|
135 | auto tmp = new long[new_size];
|
---|
136 |
|
---|
137 | for (size_t i = 0; i < iarray_size_; ++i)
|
---|
138 | tmp[i] = iarray_[i];
|
---|
139 | for (size_t i = iarray_size_; i < new_size; ++i)
|
---|
140 | tmp[i] = 0;
|
---|
141 |
|
---|
142 | swap(tmp, iarray_);
|
---|
143 | delete[] tmp;
|
---|
144 | iarray_size_ = new_size;
|
---|
145 |
|
---|
146 | return iarray_[idx];
|
---|
147 | }
|
---|
148 | else
|
---|
149 | return iarray_[idx];
|
---|
150 |
|
---|
151 | return ierror_;
|
---|
152 | }
|
---|
153 |
|
---|
154 | void*& ios_base::pword(int index)
|
---|
155 | {
|
---|
156 | if (!parray_)
|
---|
157 | {
|
---|
158 | parray_ = new long[initial_size_];
|
---|
159 | parray_size_ = initial_size_;
|
---|
160 | }
|
---|
161 |
|
---|
162 | auto idx = static_cast<size_t>(index);
|
---|
163 | if (idx > parray_size_)
|
---|
164 | { // TODO: Enclose in try block and set failbit if needed
|
---|
165 | // and return perror_.
|
---|
166 | size_t new_size = max(parray_size_ * 2, idx + 1);
|
---|
167 | auto tmp = new long[new_size];
|
---|
168 |
|
---|
169 | for (size_t i = 0; i < parray_size_; ++i)
|
---|
170 | tmp[i] = parray_[i];
|
---|
171 | for (size_t i = parray_size_; i < new_size; ++i)
|
---|
172 | tmp[i] = nullptr;
|
---|
173 |
|
---|
174 | swap(tmp, parray_);
|
---|
175 | delete[] tmp;
|
---|
176 | parray_size_ = new_size;
|
---|
177 |
|
---|
178 | return parray_[idx];
|
---|
179 | }
|
---|
180 | else
|
---|
181 | return parray_[idx];
|
---|
182 |
|
---|
183 | return perror_;
|
---|
184 | }
|
---|
185 |
|
---|
186 | void ios_base::register_callback(event_callback fn, int index)
|
---|
187 | {
|
---|
188 | callbacks.emplace_back(fn, index);
|
---|
189 | }
|
---|
190 | }
|
---|