1 /* stdiobuf 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 "stdiobuf_defs.hh"
26 #include "globals_defs.hh"
27 #include "assertions.hh"
28 #include <cstddef>
29 
30 namespace Parma_Polyhedra_Library {
31 
32 stdiobuf::int_type
uflow()33 stdiobuf::uflow() {
34   unget_char_buf = getc(fp);
35   return unget_char_buf;
36 }
37 
38 stdiobuf::int_type
underflow()39 stdiobuf::underflow() {
40   const int_type c = getc(fp);
41   return ungetc(c, fp);
42 }
43 
44 std::streamsize
xsgetn(char_type * s,std::streamsize n)45 stdiobuf::xsgetn(char_type* s, std::streamsize n) {
46   PPL_ASSERT(n >= 0);
47   const size_t r = fread(s, 1, static_cast<size_t>(n), fp);
48   if (r > 0) {
49     unget_char_buf = traits_type::to_int_type(s[r - 1]);
50   }
51   else {
52     unget_char_buf = traits_type::eof();
53   }
54   return static_cast<std::streamsize>(r);
55 }
56 
57 stdiobuf::int_type
pbackfail(int_type c)58 stdiobuf::pbackfail(int_type c) {
59   const int_type eof = traits_type::eof();
60   const int_type u = traits_type::eq_int_type(c, eof) ? unget_char_buf : c;
61   unget_char_buf = eof;
62   return traits_type::eq_int_type(u, eof) ? eof : ungetc(u, fp);
63 }
64 
65 std::streamsize
xsputn(const char_type * s,std::streamsize n)66 stdiobuf::xsputn(const char_type* s, std::streamsize n) {
67   PPL_ASSERT(n >= 0);
68   const size_t r = fwrite(s, 1, static_cast<size_t>(n), fp);
69   return static_cast<std::streamsize>(r);
70 }
71 
72 stdiobuf::int_type
overflow(int_type c)73 stdiobuf::overflow(int_type c) {
74   const int_type eof = traits_type::eof();
75   if (traits_type::eq_int_type(c, eof)) {
76     return (fflush(fp) != 0) ? eof : traits_type::not_eof(c);
77   }
78   else {
79     return putc(c, fp);
80   }
81 }
82 
83 int
sync()84 stdiobuf::sync() {
85   return fflush(fp);
86 }
87 
88 } // namespace Parma_Polyhedra_Library
89