1 #include "rheolef/rheostream.h"
22 #include "rheolef/iorheo.h"
27 #include <boost/iostreams/filter/gzip.hpp>
28 #include <boost/iostreams/device/file.hpp>
30 #ifdef _RHEOLEF_HAVE_UNISTD_H
34 #ifdef _RHEOLEF_HAVE_SYMLINK_H
47 namespace ios = boost::iostreams;
51 char* c_tmpdir = std::getenv (
"TMPDIR");
52 return (c_tmpdir == 0) ?
"/tmp" : c_tmpdir;
57 std::ostringstream
out;
65 : ios::filtering_stream<ios::output>(),
80 if (suffix.length() == 0) {
90 bool verbose = iorheo::getverbose(clog);
92 std::string action = (mode &
io::app) ?
"appended" :
"created";
93 clog <<
"! file \"" <<
_full_name <<
"\" " << action << endl;
102 filtering_stream<output>::push (gzip_compressor());
108 filtering_stream<output>::push (ofs);
114 if (filtering_stream<ios::output>::empty()) {
117 #define _RHEOLEF_HAVE_BOOST_IOSTREAMS_GZIP_EMPTY_FILE_BUG
118 #ifdef _RHEOLEF_HAVE_BOOST_IOSTREAMS_GZIP_EMPTY_FILE_BUG
123 #ifdef _RHEOLEF_HAVE_BOOST_IOSTREAMS_GZIP_EMPTY_FILE_BUG_OLD
128 trace_macro (
"_close_internal: _full_name="<<
_full_name<<
": gziped => add a carriage return before closing...");
129 static char dummy =
'\n';
130 static size_t length = 1;
131 filtering_stream<ios::output>::component<gzip_compressor>(0)->write(*filtering_stream<ios::output>::component<file_sink>(1), &
dummy, length);
134 while (! filtering_stream<ios::output>::empty()) {
135 filtering_stream<ios::output>::pop();
164 if (full_name.length() == 0) {
165 if (suffix.length() != 0) {
166 error_macro (
"file \"" <<
name <<
"[." << suffix <<
"[.gz]]\" not found");
174 _ifs.open (full_name.c_str(), ios_base::in);
176 _ifs.open (full_name.c_str(), ios_base::in | ios_base::binary);
178 bool verbose = iorheo::getverbose(clog);
179 if (
verbose) clog <<
"! load \"" << full_name <<
"\"\n";
183 filtering_stream<input>::push (gzip_decompressor());
185 filtering_stream<input>::push (_ifs);
191 while (! filtering_stream<ios::input>::empty()) {
192 filtering_stream<ios::input>::pop();
197 : ios::filtering_stream<ios::input>(), _ifs()
210 size_t ln =
name.length();
211 size_t ls = suffix.length();
212 if (ln <= ls+1)
return false;
214 if (
name[ln-ls-1] !=
'.')
return false;
215 for (
size_t i = ln-ls, j = 0; i < ln; i++, j++)
216 if (
name [i] != suffix [j])
return false;
225 return string(
name, 0,
name.length() - suffix.length() - 1);
231 size_t ln =
name.length();
232 if (ln == 0)
return false;
233 for (
size_t i = ln-1; i > 0 &&
name[i] !=
'/'; --i)
234 if (
name [i] ==
'.')
return true;
241 size_t ln =
name.length();
242 if (ln == 0)
return name;
244 for (
size_t i = ln-1; i > 0 &&
name[i] !=
'/'; --i) {
245 if (
name [i] ==
'.') {
250 if (i_dot == 0)
return name;
251 return string(
name, 0, i_dot);
258 if (i >= l)
return name;
259 string b = string(
name, i+1, l-i-1);
267 if (i >= l)
return ".";
268 string d = string(
name, 0, i);
285 get_dir_from_path (
const string& path,
unsigned int& i_pos)
288 unsigned int last = path.length();
293 while (i_pos < last && path [i_pos] ==
':')
301 unsigned int i_last = i_pos;
302 for (i_last = i_pos; i_last < last && path [i_last] !=
':'; i_last++);
305 current_dir = string(path, i_pos, i_last-i_pos+1);
307 current_dir = string(path, i_pos, i_last-i_pos);
326 rheo_path = new_tab_macro(
char, strlen(s1)+1);
349 have_name_in_dir (
const string& dir,
const string&
name,
string& full_path)
364 if (unzip_status && zip_status) {
365 warning_macro (
"both compressed and uncompressed files exists:");
368 error_macro (
"unrecoverable ambiguous situation (HINT: rename one of these files)");
372 full_path = zip_full_name;
376 full_path = unzip_full_name;
381 if (stat(dir.c_str(), &sd) != 0) {
386 if ((sd.st_mode & S_IFDIR) == 0) {
392 DIR*
d = opendir(dir.c_str());
399 while ((dp = readdir(
d)) != 0) {
401 string subdir = dir +
"/" + (dp -> d_name);
403 if (strcmp(dp -> d_name,
".") == 0 ||
404 strcmp(dp -> d_name,
"..") == 0)
continue;
407 if (stat(subdir.c_str(), &s) != 0) {
411 if ((s.st_mode & S_IFLNK) == 0) {
415 char* subdir_cstr = (
char*)(subdir.c_str());
416 int linksize = readlink (subdir_cstr, linkname,
PATH_MAX + 1);
428 linkname [linksize] =
'\0';
429 if (strcmp(linkname,
".") == 0 ||
430 strcmp(linkname,
"..") == 0) {
435 if ((s.st_mode & S_IFDIR) != 0) {
437 if (have_name_in_dir (subdir,
name, full_path)) {
447 if (rootname ==
"") {
451 if (suffix !=
"")
name +=
"." + suffix;
454 if (rootname [0] ==
'.' || rootname[0] ==
'/') {
455 if (have_name_in_dir (
"",
name, full_path)) {
464 unsigned int i_dir = 0;
465 string dir = get_dir_from_path (
rheo_path, i_dir);
466 while (dir.length() != 0) {
468 if (have_name_in_dir (dir,
name, full_path)) {
471 dir = get_dir_from_path (
rheo_path, i_dir);
484 unsigned int l = s.length();
485 if (l < 1)
return false;
486 if (!isdigit(s[0]) && s[0] !=
'-' && s[0] !=
'.')
return false;
488 if (l < 2)
return false;
489 if (!isdigit(s[1]) && s[1] !=
'.')
return false;
field::size_type size_type
see the Float page for the full documentation
void open(const std::string &name, const std::string &suffix=std::string())
void _open_internal(io::mode_type mode)
void open(const std::string &name, const std::string &suffix=std::string(), io::mode_type mode=io::out)
static const char * default_rheo_path
static const char * rheo_path_name
static iorheo::force_initialization dummy
#define trace_macro(message)
#define error_macro(message)
#define warning_macro(message)
This file is part of Rheolef.
string get_full_name_from_rheo_path(const string &rootname, const string &suffix)
get_full_name_from_rheo_path: see the rheostream page for the full documentation
string delete_any_suffix(const string &name)
delete_any_suffix: see the rheostream page for the full documentation
string delete_suffix(const string &name, const string &suffix)
delete_suffix: see the rheostream page for the full documentation
bool has_any_suffix(const string &name)
has_any_suffix: see the rheostream page for the full documentation
void prepend_dir_to_rheo_path(const string &dir)
prepend_dir_to_rheo_path: see the rheostream page for the full documentation
string get_basename(const string &name)
get_basename: see the rheostream page for the full documentation
bool is_float(const string &s)
is_float: see the rheostream page for the full documentation
void append_dir_to_rheo_path(const string &dir)
append_dir_to_rheo_path: see the rheostream page for the full documentation
string ftos(const Float &x)
itof: see the rheostream page for the full documentation
string get_dirname(const string &name)
get_dirname: see the rheostream page for the full documentation
std::string get_tmpdir()
get_tmpdir: see the rheostream page for the full documentation
bool file_exists(const std::string &filename)
file_exists: see the rheostream page for the full documentation
bool has_suffix(const string &name, const string &suffix)
has_suffix: see the rheostream page for the full documentation
Float to_float(const string &s)
to_float: see the rheostream page for the full documentation