SCIP-SDP  3.1.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ScipStreamBuffer.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of SCIPSDP - a solving framework for mixed-integer */
4 /* semidefinite programs based on SCIP. */
5 /* */
6 /* Copyright (C) 2011-2013 Discrete Optimization, TU Darmstadt */
7 /* EDOM, FAU Erlangen-Nürnberg */
8 /* 2014-2018 Discrete Optimization, TU Darmstadt */
9 /* */
10 /* */
11 /* This program is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public License */
13 /* as published by the Free Software Foundation; either version 3 */
14 /* of the License, or (at your option) any later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
19 /* GNU Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with this program; if not, write to the Free Software */
23 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
24 /* */
25 /* */
26 /* Based on SCIP - Solving Constraint Integer Programs */
27 /* Copyright (C) 2002-2018 Zuse Institute Berlin */
28 /* SCIP is distributed under the terms of the SCIP Academic Licence, */
29 /* see file COPYING in the SCIP distribution. */
30 /* */
31 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
32 
38 #include "ScipStreamBuffer.h"
39 
40 #include <algorithm> // for min
41 #include <cstring> // for memcpy
42 #include "scip/scip.h" // for SCIPallocBufferArray, etc
43 
44 #define SCIPSTREAMBUFFERSIZE 256
45 
46 ScipStreamBuffer::ScipStreamBuffer(SCIP* scip, SCIP_FILE* file, bool close_on_exit) : scip_(scip), file_(file), g_buffer_(NULL),
47  g_buffer_size_(SCIPSTREAMBUFFERSIZE), close_on_exit_(close_on_exit)
48 {
49  SCIP_CALL_ABORT(SCIPallocBufferArray(scip_, &g_buffer_, g_buffer_size_));
52 }
53 
55 {
56  SCIPfreeBufferArray(scip_, &g_buffer_);
57  scip_ = NULL;
58  g_buffer_= NULL;
59  g_buffer_size_ = 0;
60 
61  if (close_on_exit_)
62  {
63  SCIPfclose(file_);
64  }
65 }
66 
68 {
69  if (gptr() < egptr())
70  {
71  // still unused chars in the buffer
72  // just return
73  return *gptr();
74  }
75 
76  // refill the buffer
77  int nresult = SCIPfread(g_buffer_, 1, g_buffer_size_, file_);
78 
79  if (nresult <= 0)
80  {
81  return EOF;
82  }
83 
84  // set up buffer, we read nresult bytes, that's where our new end is
85  setg(g_buffer_, g_buffer_, g_buffer_ + nresult);
86 
87  return *gptr();
88 }
89 
90 std::streamsize ScipStreamBuffer::xsgetn(char *dest, std::streamsize request)
91 {
92  int ndone = 0;
93 
94  while (request != 0)
95  {
96  if (!in_avail())
97  {
98  // we have an empty buffer, so refresh
99  if (underflow() == EOF)
100  {
101  // buffer remains empty
102  break;
103  }
104  }
105 
106  int available = std::min(in_avail(), request);
107 
108  // copy the available bytes
109  memcpy(dest + ndone, gptr(), available);
110  // shift read pointer
111  gbump(available);
112 
113  ndone += available;
114  request -= available;
115  }
116 
117  return ndone;
118 }
An std::streambuf that uses SCIP I/O routines (suitable for reading)
size_t g_buffer_size_
pointer to the get-buffer
#define SCIPSTREAMBUFFERSIZE
virtual int underflow()
the underflow function is responsible for the refilling of the buffer
bool close_on_exit_
size of the get-buffer
ScipStreamBuffer(SCIP *scip, SCIP_FILE *file, bool close_on_exit)
virtual std::streamsize xsgetn(char *dest, std::streamsize request)