1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 
3 /*
4  *  Main authors:
5  *     Gleb Belov <gleb.belov@monash.edu>
6  */
7 
8 /* This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11 
12 #pragma once
13 
14 #include <cstdio>
15 
16 namespace MiniZinc {
17 
18 /// Helper class to redirect, e.g., stdout to stderr
19 class StreamRedir {
20   /// The stream to be changed
21   FILE* const _file0;
22   /*
23    * Structure for retaining information about a stream, sufficient to
24    * recreate that stream later on.
25    * See
26    * https://stackoverflow.com/questions/4760201/how-do-i-suppress-output-while-using-a-dynamic-library
27    */
28   struct StreamInfo {
29     int fd = -1;
30     fpos_t pos;
31   };
32   /// The original stream
33   StreamInfo _streamInfo;
34 
35 public:
36   /// Constructs with the stream to be changed
37   //    StreamRedir(FILE* s0);
38   /// Constructs with s0 and replaces it by s1
39   StreamRedir(FILE* s0, FILE* s1, bool fFlush = true);
40   ~StreamRedir();
41   /// Restore original
42   void restore(bool fFLush = true);
43 
44 protected:
45   /// Replace & save stream by s1
46   void replaceStream(FILE* s1, bool fFlush = true);
47 };
48 
49 }  // namespace MiniZinc
50