1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-28 09:53:46 +00:00
codecrypt/src/iohelpers.cpp
2013-04-22 12:30:21 +02:00

53 lines
1.5 KiB
C++

/*
* This file is part of Codecrypt.
*
* Codecrypt is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Codecrypt is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Codecrypt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "iohelpers.h"
bool redirect_cin (const std::string& fn)
{
static std::ifstream alt_cin;
alt_cin.open (fn.c_str(), std::ios::in | std::ios::binary);
if (alt_cin.fail() ) return false;
std::cin.rdbuf (alt_cin.rdbuf() );
return true;
}
bool redirect_cout (const std::string& fn)
{
static std::ofstream alt_cout;
alt_cout.open (fn.c_str(), std::ios::out | std::ios::binary);
if (alt_cout.fail() ) return false;
std::cout.rdbuf (alt_cout.rdbuf() );
return true;
}
#define bufsize 1024
bool read_all_input (std::string&data, std::istream&input)
{
data.clear();
char buf[bufsize];
for (;;) {
input.read (buf, bufsize);
if (input) data.append (buf, bufsize);
else if (input.eof() ) {
data.append (buf, input.gcount() );
return true;
} else return false;
}
}