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
|
/*
* Copyright (C) 2000-2008 Lorenzo Bettini <http://www.lorenzobettini.it>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "fileutil.h"
#include "verbosity.h"
#include "ioexception.h"
#ifndef CHROOT_INPUT_DIR
#define CHROOT_INPUT_DIR ""
#endif
using namespace std;
namespace srchilite {
static bool verbose = false;
void set_file_util_verbose(bool b) {
verbose = b;
}
// global for the path to start to look for if no
// path was specified
// FIXME avoid using a global variable
std::string start_path;
string readFile(const string &fileName) throw (IOException) {
ifstream file(fileName.c_str());
if (!file.is_open()) {
throw IOException("cannot open", fileName);
}
string s, line;
while (getline(file, line)) {
s += line + "\n";
}
return s;
}
/*
char * read_file(const string &fileName) {
char *buffer = 0;
long int char_count;
// we open it as binary otherwise we may experience problems under
// Windows system: when we fread, the number of char read can be
// less then char_count, and thus we'd get an error...
ifstream file(fileName.c_str(), ios_base::binary);
if (!file.is_open() )
file_error("Error operning", fileName);
else {
// let's go to the end of the file...
file.seekg(0, ios::end);
if (!file)
file_error("Error positioning", fileName);
// ...to read the dimension
char_count = file.tellg();
if (!file)
file_error("Error reading position", fileName);
buffer = new char [char_count + 1];
if (!buffer)
internal_error("Memory allocation failed");
file.seekg(0, ios::beg);
if (!file)
file_error("Error positioning to start", fileName);
//copy file into memory
file.read(buffer, char_count);
buffer[char_count] = '\0';
file.close();
}
return buffer;
}
*/
string createOutputFileName(const string &inputFileName,
const string &outputDir, const string &ext) {
string input_file_name;
char path_separator = '/';
if (!outputDir.size())
input_file_name = inputFileName;
else {
string::size_type pos_of_sep;
pos_of_sep = inputFileName.find_last_of('/');
if (pos_of_sep == string::npos) // try with DOS separator
{
pos_of_sep = inputFileName.find_last_of('\\');
if (pos_of_sep != string::npos)
path_separator = '\\';
}
if (pos_of_sep != string::npos)
input_file_name = inputFileName.substr(pos_of_sep + 1);
else
input_file_name = inputFileName;
}
string outputFileName;
if (outputDir.size()) {
outputFileName += outputDir;
outputFileName += path_separator;
}
outputFileName += input_file_name;
outputFileName += (ext.size() ? "." + ext : "");
return outputFileName;
}
unsigned int get_line_count(istream &input) {
unsigned int count = 0;
string line;
while (true) {
getline(input, line);
if (!input.eof())
++count;
else
break;
}
return count;
}
string get_file_extension(const string &s) {
string::size_type pos_of_sep;
pos_of_sep = s.rfind(".");
if (pos_of_sep == string::npos)
return ""; // no extension
return s.substr(pos_of_sep + 1);
}
FILE *open_file_stream(const string &input_file_name) {
FILE *in = fopen(input_file_name.c_str(), "r");
return in;
}
istream *open_file_istream(const string &input_file_name) {
ifstream *in = new ifstream(input_file_name.c_str());
if (!in || !(*in)) {
if (!in)
throw IOException("no more free memory", "");
else
delete in;
return 0;
}
return in;
}
istream *open_file_istream_or_error(const string &input_file_name) {
istream *in = open_file_istream(input_file_name);
if (!in)
throw IOException("cannot open", input_file_name);
return in;
}
istream *_open_data_file_istream(const string &path,
const string &input_file_name) {
const string file = (path.size() ? path + "/" : "") + input_file_name;
VERBOSELN("opening " + file);
return open_file_istream(file);
}
istream *open_data_file_istream(const string &path,
const string &input_file_name, const string &start) {
if (!input_file_name.size())
throw IOException("empty file name", input_file_name);
istream *in = 0;
if (input_file_name.size() && contains_path(input_file_name)) {
in = _open_data_file_istream("", input_file_name);
if (!in) {
throw IOException("cannot open", input_file_name);
}
} else if (path.size() && input_file_name.size()) {
const string file = (path.size() ? path + "/" : "") + input_file_name;
in = _open_data_file_istream(path, input_file_name);
if (!in) {
throw IOException("cannot open", file);
}
} else {
string _path = path;
string _file = input_file_name;
bool has_path = contains_path(input_file_name);
if (!path.size() && !has_path)
_path = ".";
in = _open_data_file_istream(_path, _file);
if (!in && !path.size() && !has_path)
in = _open_data_file_istream(start, _file);
}
if (!in)
throw IOException("cannot find input file anywhere", input_file_name);
return in;
}
// FIXME: duplicate for istream and FILE *, make it uniform!
FILE *_open_data_file_stream(const string &path, const string &input_file_name) {
const string file = (path.size() ? path + "/" : "") + input_file_name;
VERBOSELN("opening " + file);
return open_file_stream(file);
}
FILE *open_data_file_stream(const string &path, const string &input_file_name,
const string &start) {
if (!input_file_name.size())
throw IOException("empty file name", input_file_name);
FILE *in = 0;
if (input_file_name.size() && contains_path(input_file_name)) {
in = _open_data_file_stream("", input_file_name);
if (!in) {
throw IOException("cannot open", input_file_name);
}
} else if (path.size() && input_file_name.size()) {
const string file = (path.size() ? path + "/" : "") + input_file_name;
in = _open_data_file_stream(path, input_file_name);
if (!in) {
throw IOException("cannot open", file);
}
} else {
string _path = path;
string _file = input_file_name;
bool has_path = contains_path(input_file_name);
if (!path.size() && !has_path)
_path = ".";
in = _open_data_file_stream(_path, _file);
if (!in && !path.size() && !has_path)
in = _open_data_file_stream(start, _file);
}
if (!in)
throw IOException("cannot find input file anywhere", input_file_name);
return in;
}
bool read_line(istream *in, string &line) {
if (in->eof())
return false;
getline(*in, line);
return true;
}
string get_file_path(const string &s) {
string::size_type pos_of_sep;
pos_of_sep = s.rfind("/");
if (pos_of_sep == string::npos)
pos_of_sep = s.rfind("\\"); // try also with DOS path sep
if (pos_of_sep == string::npos)
return ""; // no path
return s.substr(0, pos_of_sep + 1);
}
bool contains_path(const string &s) {
return (get_file_path(s).size() > 0);
}
string strip_file_path(const string &s) {
string::size_type pos_of_sep;
pos_of_sep = s.rfind("/");
if (pos_of_sep == string::npos)
pos_of_sep = s.rfind("\\"); // try also with DOS path sep
if (pos_of_sep == string::npos)
return s; // no path
return s.substr(pos_of_sep + 1);
}
string get_input_file_name(const string &file_name) {
if (!file_name.size())
return "";
return CHROOT_INPUT_DIR + file_name;
}
}
|