1 /* c_streambuf class implementation (non-inline functions).
2    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #include "ppl-config.h"
25 #include "c_streambuf_defs.hh"
26 #include "globals_defs.hh"
27 #include "assertions.hh"
28 
29 namespace Parma_Polyhedra_Library {
30 
~c_streambuf()31 c_streambuf::~c_streambuf() {
32 }
33 
34 c_streambuf::int_type
uflow()35 c_streambuf::uflow() {
36   const int_type c = underflow();
37   next_char_buf = traits_type::eof();
38   return c;
39 }
40 
41 c_streambuf::int_type
underflow()42 c_streambuf::underflow() {
43   const int_type eof = traits_type::eof();
44   if (traits_type::eq_int_type(next_char_buf, eof)) {
45     char buf;
46     if (cb_read(&buf, 1) == 1) {
47       next_char_buf = buf;
48     }
49     else {
50       next_char_buf = eof;
51     }
52   }
53   return next_char_buf;
54 }
55 
56 std::streamsize
xsgetn(char_type * s,std::streamsize n)57 c_streambuf::xsgetn(char_type* s, std::streamsize n) {
58   PPL_ASSERT(n >= 0);
59   if (n == 0) {
60     return n;
61   }
62   const int_type eof = traits_type::eof();
63   const size_t sz_n = static_cast<size_t>(n);
64   size_t a;
65   if (traits_type::eq_int_type(next_char_buf, eof)) {
66     a = 0;
67   }
68   else {
69     s[0] = static_cast<char_type>(next_char_buf);
70     a = 1;
71   }
72   const size_t r = cb_read(s + a, sz_n - a) + a;
73   if (r > 0) {
74     unget_char_buf = traits_type::to_int_type(s[r - 1]);
75   }
76   else {
77     unget_char_buf = traits_type::eof();
78   }
79   return static_cast<std::streamsize>(r);
80 }
81 
82 c_streambuf::int_type
pbackfail(int_type c)83 c_streambuf::pbackfail(int_type c) {
84   const int_type eof = traits_type::eof();
85   next_char_buf = traits_type::eq_int_type(c, eof) ? unget_char_buf : c;
86   unget_char_buf = eof;
87   return next_char_buf;
88 }
89 
90 std::streamsize
xsputn(const char_type * s,std::streamsize n)91 c_streambuf::xsputn(const char_type* s, std::streamsize n) {
92   PPL_ASSERT(n >= 0);
93   const size_t r = cb_write(s, static_cast<size_t>(n));
94   return static_cast<std::streamsize>(r);
95 }
96 
97 c_streambuf::int_type
overflow(int_type c)98 c_streambuf::overflow(int_type c) {
99   const int_type eof = traits_type::eof();
100   if (traits_type::eq_int_type(c, eof)) {
101     return (sync() != 0) ? eof : traits_type::not_eof(c);
102   }
103   else {
104     char buf = static_cast<char>(c);
105     if (cb_write(&buf, 1) == 1) {
106       return c;
107     }
108     else {
109       return eof;
110     }
111   }
112 }
113 
114 int
sync()115 c_streambuf::sync() {
116   return cb_sync();
117 }
118 
119 } // namespace Parma_Polyhedra_Library
120