1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
#include "io.H"
#include <boost/filesystem/operations.hpp>
#include "myexception.H"
using namespace std;
namespace fs = boost::filesystem;
/// \brief Read a line from a file with their UNIX or DOS or Mac line endings.
///
/// \param file The input stream
/// \param s The line that was read.
///
istream& portable_getline(istream& file,string& s)
{
const char CR = 13;
const char LF = 10;
s.clear();
ios_base::iostate err = ios_base::goodbit;
int n_extracted = 0;
// Make sure that while(getline()) terminates:
// - read at least one char in order to set failbit on an empty file.
try
{
std::streambuf* sb = file.rdbuf();
int c = sb->sgetc();
while (c != std::streambuf::traits_type::eof() and c != CR and c != LF)
{
s += c;
n_extracted++;
c = sb->snextc();
}
// If the line ends with an EOF, then set eofbit, and we're done.
if (c == std::streambuf::traits_type::eof() )
err |= ios::eofbit;
else
{
// If then line ends with an EOL then move the pointer to the next character.
n_extracted++;
char c2 = sb->snextc();
// If this is the LF of a CR LF, then skip the LF
if (c == CR and c2 == LF)
{
n_extracted++;
sb->sbumpc();
}
// NOTE: Any EOF's that we discover here will end the NEXT line.
}
}
catch (...)
{
file.setstate(ios::badbit);
}
// If we didn't read any characters, then set the failbit.
if (not n_extracted)
err |= ios::failbit;
// Set any error bits.
if (err)
file.setstate(err);
return file;
}
int num_lines(istream& file)
{
string s;
int count = 0;
while(portable_getline(file,s))
count++;
return count;
}
void scan_lines(istream& file,int skip,int subsample, int max,
accumulator<string>& op)
{
int n_lines=0;
string line;
for(int line_number=0;portable_getline(file,line);line_number++)
{
// don't start if we haven't skipped enough trees
if (line_number < skip) continue;
// skip trees unless they are a multiple of 'subsample'
if ((line_number-skip) % subsample != 0) continue;
// quit if we've read in 'max' trees
if (max >= 0 and n_lines == max) break;
// should this be protected by a try { } catch(...) {} block?
op(line);
n_lines++;
}
}
vector<string> load_lines(istream& file,int skip,int subsample, int max)
{
vector_accumulator<string> lines;
scan_lines(file,skip,subsample,max,lines);
return std::move(lines);
}
/// \brief Get the basename of a filename (i.e. remove parent directories.)
///
/// \param filename The filename.
///
string get_basename(string filename)
{
// remove the pathname
while(filename.find('/') != -1)
filename = filename.substr(filename.find('/')+1);
return filename;
}
/// \brief Remove the extension from a filename
///
/// \param filename The filename.
///
string remove_extension(string filename)
{
// remove the extension
int dot = filename.rfind('.');
string name = filename;
if (dot != -1)
name = filename.substr(0,dot);
return name;
}
void checked_filebuf::report_open_error(const string& filename, ios_base::openmode mode, bool existed_before)
{
myexception e;
bool exists_now = fs::exists(filename);
close();
if (fs::is_directory(filename))
{
e<<"Refusing to open '"<<filename<<"' because it is a directory.";
throw e;
}
if ((mode&ios_base::out) and not (mode&ios_base::in))
e<<"Failed to write to "<<description<<" '"<<filename<<"'";
else if ((mode&ios_base::in) and not (mode&ios_base::out))
e<<"Failed to read from "<<description<<" '"<<filename<<"'";
else
e<<"Failed to open "<<description<<" '"<<filename<<"'";
if (mode&ios_base::out)
{
if (existed_before and not (mode|ios_base::trunc))
e<<": file exists, refusing to over-write.";
if (existed_before and mode|ios_base::trunc)
e<<": file exists, do you have permission to overwrite?";
else if (not existed_before and exists_now)
e<<": file created, but cannot be written to.";
else if (not existed_before and not exists_now)
e<<": perhaps you don't have permission to create a file here?";
}
else if (mode&ios_base::in)
{
if (not fs::exists(filename))
e<<": file does not exist.";
else
e<<": file exists, but can't be opened.";
}
throw e;
}
checked_filebuf * checked_filebuf::open ( const std::string& filename, std::ios_base::openmode mode )
{
bool already_existed = fs::exists(filename);
bool is_dir = fs::is_directory(filename);
std::filebuf* buf = 0;
// open the file if either we're not going to overwrite it, or we're opening the truncate flag
if (!is_dir and (!already_existed or not (mode&ios_base::out) or (mode&ios_base::trunc)))
buf = std::filebuf::open(filename.c_str(), mode);
if (!buf)
report_open_error(filename, mode, already_existed);
return this;
}
checked_filebuf::checked_filebuf()
:description("file")
{
}
checked_filebuf::checked_filebuf(const string& s)
:description(s)
{
}
checked_ifstream::checked_ifstream(const string& filename)
:istream(nullptr),
buf("file")
{
this->init(&buf);
buf.open(filename, ios_base::in);
}
checked_ifstream::checked_ifstream(const string& filename, const string& description)
:istream(nullptr),
buf(description)
{
this->init(&buf);
buf.open(filename, ios_base::in);
}
void istream_or_ifstream::open(std::istream& is, const std::string& is_name, const std::string& filename, const std::string& description)
{
if (buf)
throw myexception()<<"Cannot reopen file!\n";
if (filename == is_name)
this->init(is.rdbuf());
else
{
buf = claim(new checked_filebuf(description));
this->init(buf.get());
buf->open(filename, ios_base::in);
}
}
istream_or_ifstream::istream_or_ifstream()
:istream(nullptr)
{
this->init(&buf_null);
}
istream_or_ifstream::istream_or_ifstream(std::istream& is, const std::string& is_name, const std::string& filename)
:istream(nullptr)
{
open(is,is_name,filename,"file");
}
istream_or_ifstream::istream_or_ifstream(std::istream& is, const std::string& is_name, const std::string& filename,
const std::string& description)
:istream(nullptr)
{
open(is,is_name,filename,description);
}
checked_ofstream::checked_ofstream(const string& filename,bool trunc)
:buf("file")
{
this->init(&buf);
std::ios_base::openmode flags = ios_base::out;
if (trunc)
flags |= ios_base::trunc;
buf.open(filename, flags);
}
checked_ofstream::checked_ofstream(const string& filename, const string& description, bool trunc)
:buf(description)
{
this->init(&buf);
std::ios_base::openmode flags = ios_base::out;
if (trunc)
flags |= ios_base::trunc;
buf.open(filename, flags);
}
void ostream_or_ofstream::open(std::ostream& os, const std::string& os_name, const std::string& filename, const std::string& description)
{
if (buf)
throw myexception()<<"Cannot reopen file!\n";
if (filename == os_name)
this->init(os.rdbuf());
else
{
buf = claim(new checked_filebuf(description));
this->init(buf.get());
buf->open(filename, ios_base::out|ios_base::trunc);
}
}
ostream_or_ofstream::ostream_or_ofstream()
{
this->init(&buf_null);
}
ostream_or_ofstream::ostream_or_ofstream(std::ostream& os, const std::string& os_name, const std::string& filename)
{
open(os,os_name,filename,"file");
}
ostream_or_ofstream::ostream_or_ofstream(std::ostream& os, const std::string& os_name, const std::string& filename,
const std::string& description)
{
open(os,os_name,filename,description);
}
null_ostream::null_ostream()
:ostream(&buf)
{ }
string read_file(const string& filename)
{
checked_ifstream file(filename);
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
string read_file(const string& filename, const string& description)
{
checked_ifstream file(filename,description);
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
|