source: mainline/uspace/lib/cpp/include/__bits/string/string_io.hpp@ 8fd0675f

Last change on this file since 8fd0675f was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef LIBCPP_BITS_STRING_IO
8#define LIBCPP_BITS_STRING_IO
9
10#include <__bits/string/string.hpp>
11#include <ios>
12
13namespace std
14{
15 /**
16 * 21.4.8.9, inserters and extractors:
17 */
18
19 template<class Char, class Traits, class Allocator>
20 basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is,
21 basic_string<Char, Traits, Allocator>& str)
22 {
23 using sentry = typename basic_istream<Char, Traits>::sentry;
24 sentry sen{is, false};
25
26 if (sen)
27 {
28 str.erase();
29
30 auto max_size = is.width();
31 if (max_size <= 0)
32 max_size = static_cast<streamsize>(str.max_size());
33
34 streamsize i{};
35 for(; i < max_size; ++i)
36 {
37 auto ic = is.rdbuf()->sgetc();
38 if (Traits::eq_int_type(ic, Traits::eof()))
39 {
40 is.setstate(ios_base::eofbit);
41 break;
42 }
43
44 auto c = Traits::to_char_type(ic);
45 if(isspace(c, is.getloc()))
46 break;
47
48 str.push_back(c);
49 is.rdbuf()->sbumpc();
50 }
51
52 if (i == 0)
53 is.setstate(ios_base::failbit);
54 }
55 else
56 is.setstate(ios_base::failbit);
57
58 return is;
59 }
60
61 template<class Char, class Traits, class Allocator>
62 basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os,
63 const basic_string<Char, Traits, Allocator>& str)
64 {
65 // TODO: determine padding as described in 27.7.3.6.1
66 using sentry = typename basic_ostream<Char, Traits>::sentry;
67 sentry sen{os};
68
69 if (sen)
70 {
71 auto width = os.width();
72 auto size = str.size();
73
74 size_t to_pad{};
75 if (width > 0)
76 to_pad = (static_cast<size_t>(width) - size);
77
78 if (to_pad > 0)
79 {
80 if ((os.flags() & ios_base::adjustfield) != ios_base::left)
81 {
82 for (std::size_t i = 0; i < to_pad; ++i)
83 os.put(os.fill());
84 }
85
86 os.rdbuf()->sputn(str.data(), size);
87
88 if ((os.flags() & ios_base::adjustfield) == ios_base::left)
89 {
90 for (std::size_t i = 0; i < to_pad; ++i)
91 os.put(os.fill());
92 }
93 }
94 else
95 os.rdbuf()->sputn(str.data(), size);
96
97 os.width(0);
98 }
99
100 return os;
101 }
102
103 template<class Char, class Traits, class Allocator>
104 basic_istream<Char, Traits>& getline(basic_istream<Char, Traits>& is,
105 basic_string<Char, Traits, Allocator>& str,
106 Char delim)
107 {
108 typename basic_istream<Char, Traits>::sentry sen{is, true};
109
110 if (sen)
111 {
112 str.clear();
113 streamsize count{};
114
115 while (true)
116 {
117 auto ic = is.rdbuf()->sbumpc();
118 if (Traits::eq_int_type(ic, Traits::eof()))
119 {
120 is.setstate(ios_base::eofbit);
121 break;
122 }
123
124 auto c = Traits::to_char_type(ic);
125 if (Traits::eq(c, delim))
126 break;
127
128 str.push_back(c);
129 ++count;
130
131 if (count >= static_cast<streamsize>(str.max_size()))
132 {
133 is.setstate(ios_base::failbit);
134 break;
135 }
136 }
137
138 if (count == 0)
139 is.setstate(ios_base::failbit);
140 }
141 else
142 is.setstate(ios_base::failbit);
143
144 return is;
145 }
146
147 template<class Char, class Traits, class Allocator>
148 basic_istream<Char, Traits>& getline(basic_istream<Char, Traits>&& is,
149 basic_string<Char, Traits, Allocator>& str,
150 Char delim)
151 {
152 return getline(is, str, delim);
153 }
154
155 template<class Char, class Traits, class Allocator>
156 basic_istream<Char, Traits>& getline(basic_istream<Char, Traits>& is,
157 basic_string<Char, Traits, Allocator>& str)
158 {
159 return getline(is, str, is.widen('\n'));
160 }
161
162 template<class Char, class Traits, class Allocator>
163 basic_istream<Char, Traits>& getline(basic_istream<Char, Traits>&& is,
164 basic_string<Char, Traits, Allocator>& str)
165 {
166 return getline(is, str, is.widen('\n'));
167 }
168}
169
170#endif
Note: See TracBrowser for help on using the repository browser.