1
// ------------------------------------------------------------------------
2
// eca-fileio-stream.cpp: File-I/O and buffering routines using normal
4
// Copyright (C) 1999-2002 Kai Vehmanen
6
// This program is free software; you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation; either version 2 of the License, or
9
// (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
// ------------------------------------------------------------------------
27
#include <climits> /* LONG_MAX */
29
#include <unistd.h> /* stat() */
31
#include <sys/stat.h> /* stat() */
32
#ifdef HAVE_SYS_TYPES_H
33
#include <sys/types.h> /* off_t */
38
#include "eca-logger.h"
39
#include "eca-fileio.h"
40
#include "eca-fileio-stream.h"
42
ECA_FILE_IO_STREAM::~ECA_FILE_IO_STREAM(void)
44
if (mode_rep != "") close_file();
47
void ECA_FILE_IO_STREAM::open_file(const std::string& fname,
48
const std::string& fmode)
51
f1 = std::fopen(fname_rep.c_str(), fmode.c_str());
58
standard_mode = false;
62
void ECA_FILE_IO_STREAM::open_stdin(void) {
69
void ECA_FILE_IO_STREAM::open_stdout(void)
77
void ECA_FILE_IO_STREAM::open_stderr(void)
85
void ECA_FILE_IO_STREAM::close_file(void)
87
if (standard_mode != true) std::fclose(f1);
91
void ECA_FILE_IO_STREAM::read_to_buffer(void* obuf, off_t bytes)
93
if (is_file_ready() == true) {
94
last_rep = std::fread(obuf, 1, bytes, f1);
95
curpos_rep += last_rep;
102
void ECA_FILE_IO_STREAM::write_from_buffer(void* obuf, off_t bytes)
104
if (is_file_ready() == true) {
105
last_rep = std::fwrite(obuf, 1, bytes, f1);
106
curpos_rep += last_rep;
113
off_t ECA_FILE_IO_STREAM::file_bytes_processed(void) const { return(last_rep); }
115
bool ECA_FILE_IO_STREAM::is_file_ready(void) const
117
if (mode_rep == "" ||
119
std::ferror(f1)) return(false);
123
bool ECA_FILE_IO_STREAM::is_file_error(void) const
125
if (std::ferror(f1)) return(true);
129
void ECA_FILE_IO_STREAM::set_file_position(off_t newpos)
132
if (standard_mode != true) {
133
/* fseeko doesn't seem to work with glibc 2.1.x */
134
#if _LARGEFILE_SOURCE
137
int whence = SEEK_SET;
138
while(curpos_rep - seekpos >= 0) {
139
if (curpos_rep - seekpos > LONG_MAX)
142
seekstep = curpos_rep - seekpos;
144
/* null seek, break */
145
if (seekstep == 0 && whence == SEEK_CUR) break;
147
// std::cerr << "(eca-fileio-stream) fw-seeking from " << seekpos << " to " << seekpos+seekstep << std::endl;
148
int res = std::fseek(f1, seekstep, whence);
150
ECA_LOG_MSG(ECA_LOGGER::info, "(eca-fileio-stream) fseek() error! (lfs).");
152
std::fseek(f1, 0, SEEK_SET);
155
if (seekpos == 0) whence = SEEK_CUR;
159
DBC_CHECK(sizeof(long int) == sizeof(off_t));
160
std::fseek(f1, static_cast<long>(curpos_rep), SEEK_SET);
165
void ECA_FILE_IO_STREAM::set_file_position_advance(off_t fw)
167
if (standard_mode != true) {
168
set_file_position(curpos_rep + fw);
172
void ECA_FILE_IO_STREAM::set_file_position_end(void)
174
if (standard_mode == false) {
175
int res = std::fseek(f1, 0, SEEK_END);
177
ECA_LOG_MSG(ECA_LOGGER::info, "(eca-fileio-stream) fseek() error! (seek_end).");
180
curpos_rep = get_file_length();
185
off_t ECA_FILE_IO_STREAM::get_file_position(void) const
187
if (standard_mode == true) return(0);
191
off_t ECA_FILE_IO_STREAM::get_file_length(void) const
193
if (standard_mode == true) return(0);
196
stat(fname_rep.c_str(), &temp);
197
off_t lentemp = temp.st_size;