this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
3/*
4 * Main authors:
5 * Guido Tack <guido.tack@monash.edu>
6 * Gleb Belov <gleb.belov@monash.edu>
7 */
8
9/* This Source Code Form is subject to the terms of the Mozilla Public
10 * License, v. 2.0. If a copy of the MPL was ! distributed with this
11 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
12
13#ifdef _MSC_VER
14#define _CRT_SECURE_NO_WARNINGS
15#include <io.h>
16#else
17#include <unistd.h>
18#endif
19
20#include <minizinc/utils_savestream.hh>
21
22#include <iostream>
23
24using namespace std;
25using namespace MiniZinc;
26
27// StreamRedir::StreamRedir(FILE *s0) : d_s0(s0) { }
28
29StreamRedir::StreamRedir(FILE* s0, FILE* s1, bool fFlush) : d_s0(s0) { replaceStream(s1, fFlush); }
30StreamRedir::~StreamRedir() { restore(); }
31
32void StreamRedir::replaceStream(FILE* s1, bool fFlush) {
33 if (fFlush) fflush(d_s0);
34 fgetpos(d_s0, &(d_si.pos));
35 d_si.fd = dup(fileno(d_s0));
36 dup2(fileno(s1), fileno(d_s0));
37}
38
39void StreamRedir::restore(bool fFLush) {
40 if (fFLush) fflush(d_s0);
41 dup2(d_si.fd, fileno(d_s0));
42 close(d_si.fd);
43 clearerr(d_s0);
44 fsetpos(d_s0, &(d_si.pos));
45}