SCIP-SDP  2.1.0
 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 programms based on SCIP. */
5 /* */
6 /* Copyright (C) 2011-2013 Discrete Optimization, TU Darmstadt */
7 /* EDOM, FAU Erlangen-Nürnberg */
8 /* 2014-2016 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-2016 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 <cstdio> // for EOF
41 #include <algorithm> // for min
42 #include <cstring> // for memcpy
43 #include "scip/scip.h" // for SCIPallocBufferArray, etc
44 
45 #define SCIPSTREAMBUFFERSIZE 256
46 
47 ScipStreamBuffer::ScipStreamBuffer(SCIP* scip, SCIP_FILE* file, bool close_on_exit) : scip_(scip), file_(file), g_buffer_(NULL),
48  g_buffer_size_(SCIPSTREAMBUFFERSIZE), close_on_exit_(close_on_exit)
49 {
50  SCIP_CALL_ABORT(SCIPallocBufferArray(scip_, &g_buffer_, g_buffer_size_));
53 }
54 
56 {
57  SCIPfreeBufferArray(scip_, &g_buffer_);
58  scip_ = NULL;
59  g_buffer_= NULL;
60  g_buffer_size_ = 0;
61 
62  if (close_on_exit_)
63  {
64  SCIPfclose(file_);
65  }
66 }
67 
69 {
70  if (gptr() < egptr())
71  {
72  // still unused chars in the buffer
73  // just return
74  return *gptr();
75  }
76 
77  // refill the buffer
78  int nresult = SCIPfread(g_buffer_, 1, g_buffer_size_, file_);
79 
80  if (nresult <= 0)
81  {
82  return EOF;
83  }
84 
85  // set up buffer, we read nresult bytes, that's where our new end is
86  setg(g_buffer_, g_buffer_, g_buffer_ + nresult);
87 
88  return *gptr();
89 }
90 
91 std::streamsize ScipStreamBuffer::xsgetn(char *dest, std::streamsize request)
92 {
93  int ndone = 0;
94 
95  while (request != 0)
96  {
97  if (!in_avail())
98  {
99  // we have an empty buffer, so refresh
100  if (underflow() == EOF)
101  {
102  // buffer remains empty
103  break;
104  }
105  }
106 
107  int available = std::min(in_avail(), request);
108 
109  // copy the available bytes
110  memcpy(dest + ndone, gptr(), available);
111  // shift read pointer
112  gbump(available);
113 
114  ndone += available;
115  request -= available;
116  }
117 
118  return ndone;
119 }
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)