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) : _file0(s0) {
30 replaceStream(s1, fFlush);
31}
32StreamRedir::~StreamRedir() { restore(); }
33
34void StreamRedir::replaceStream(FILE* s1, bool fFlush) {
35 if (fFlush) {
36 fflush(_file0);
37 }
38 fgetpos(_file0, &(_streamInfo.pos));
39 _streamInfo.fd = dup(fileno(_file0));
40 dup2(fileno(s1), fileno(_file0));
41}
42
43void StreamRedir::restore(bool fFLush) {
44 if (fFLush) {
45 fflush(_file0);
46 }
47 dup2(_streamInfo.fd, fileno(_file0));
48 close(_streamInfo.fd);
49 clearerr(_file0);
50 fsetpos(_file0, &(_streamInfo.pos));
51}