~ubuntu-branches/ubuntu/jaunty/ecasound2.2/jaunty

« back to all changes in this revision

Viewing changes to libecasound/eca-fileio-stream.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Junichi Uekawa
  • Date: 2005-04-14 09:15:48 UTC
  • Revision ID: james.westby@ubuntu.com-20050414091548-o7kgb47z0tcunh0s
Tags: upstream-2.4.1
ImportĀ upstreamĀ versionĀ 2.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ------------------------------------------------------------------------
 
2
// eca-fileio-stream.cpp: File-I/O and buffering routines using normal
 
3
//                        file streams.
 
4
// Copyright (C) 1999-2002 Kai Vehmanen
 
5
//
 
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.
 
10
// 
 
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.
 
15
// 
 
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
// ------------------------------------------------------------------------
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include <config.h>
 
23
#endif
 
24
 
 
25
#include <cstdio>
 
26
#include <cstring>
 
27
#include <climits> /* LONG_MAX */
 
28
#include <errno.h>
 
29
#include <unistd.h> /* stat() */
 
30
#include <sys/mman.h>
 
31
#include <sys/stat.h> /* stat() */
 
32
#ifdef HAVE_SYS_TYPES_H
 
33
#include <sys/types.h> /* off_t */
 
34
#endif
 
35
 
 
36
#include <kvu_dbc.h>
 
37
 
 
38
#include "eca-logger.h"
 
39
#include "eca-fileio.h"
 
40
#include "eca-fileio-stream.h"
 
41
 
 
42
ECA_FILE_IO_STREAM::~ECA_FILE_IO_STREAM(void)
 
43
 
44
  if (mode_rep != "") close_file(); 
 
45
}
 
46
 
 
47
void ECA_FILE_IO_STREAM::open_file(const std::string& fname, 
 
48
                                   const std::string& fmode)
 
49
 
50
  fname_rep = fname;
 
51
  f1 = std::fopen(fname_rep.c_str(), fmode.c_str());
 
52
  if (!f1) {
 
53
    mode_rep = "";
 
54
  }
 
55
  else {
 
56
    mode_rep = fmode;
 
57
  }
 
58
  standard_mode = false;
 
59
  curpos_rep = 0;
 
60
}
 
61
 
 
62
void ECA_FILE_IO_STREAM::open_stdin(void) { 
 
63
  f1 = stdin;
 
64
  mode_rep = "rb";
 
65
  standard_mode = true;
 
66
  curpos_rep = 0;
 
67
}
 
68
 
 
69
void ECA_FILE_IO_STREAM::open_stdout(void) 
 
70
{
 
71
  f1 = stdout;
 
72
  mode_rep = "wb";
 
73
  standard_mode = true;
 
74
  curpos_rep = 0;
 
75
}
 
76
 
 
77
void ECA_FILE_IO_STREAM::open_stderr(void)
 
78
{
 
79
  f1 = stderr;
 
80
  mode_rep = "wb";
 
81
  standard_mode = true;
 
82
  curpos_rep = 0;
 
83
}
 
84
 
 
85
void ECA_FILE_IO_STREAM::close_file(void)
 
86
{
 
87
  if (standard_mode != true) std::fclose(f1);
 
88
  mode_rep = "";
 
89
}
 
90
 
 
91
void ECA_FILE_IO_STREAM::read_to_buffer(void* obuf, off_t bytes)
 
92
{
 
93
  if (is_file_ready() == true) {
 
94
    last_rep = std::fread(obuf, 1, bytes, f1);
 
95
    curpos_rep += last_rep;
 
96
  }
 
97
  else {
 
98
    last_rep = 0;
 
99
  }
 
100
}
 
101
 
 
102
void ECA_FILE_IO_STREAM::write_from_buffer(void* obuf, off_t bytes)
 
103
{
 
104
  if (is_file_ready() == true) {
 
105
    last_rep = std::fwrite(obuf, 1, bytes, f1);
 
106
    curpos_rep += last_rep;
 
107
  }
 
108
  else {
 
109
    last_rep = 0;
 
110
  }
 
111
}
 
112
 
 
113
off_t ECA_FILE_IO_STREAM::file_bytes_processed(void) const { return(last_rep); }
 
114
 
 
115
bool ECA_FILE_IO_STREAM::is_file_ready(void) const
 
116
{
 
117
  if (mode_rep == "" ||
 
118
      std::feof(f1) ||
 
119
      std::ferror(f1)) return(false);
 
120
  return(true);
 
121
}
 
122
 
 
123
bool ECA_FILE_IO_STREAM::is_file_error(void) const
 
124
 
125
  if (std::ferror(f1)) return(true);
 
126
  return(false);
 
127
}
 
128
 
 
129
void ECA_FILE_IO_STREAM::set_file_position(off_t newpos)
 
130
{
 
131
  curpos_rep = newpos;
 
132
  if (standard_mode != true) {
 
133
/* fseeko doesn't seem to work with glibc 2.1.x */
 
134
#if _LARGEFILE_SOURCE
 
135
    off_t seekpos = 0;
 
136
    off_t seekstep = 0;
 
137
    int whence = SEEK_SET;
 
138
    while(curpos_rep - seekpos >= 0) {
 
139
      if (curpos_rep - seekpos > LONG_MAX)
 
140
        seekstep = LONG_MAX;
 
141
      else
 
142
        seekstep = curpos_rep - seekpos;
 
143
 
 
144
      /* null seek, break */
 
145
      if (seekstep == 0 && whence == SEEK_CUR) break;
 
146
 
 
147
      // std::cerr << "(eca-fileio-stream) fw-seeking from " << seekpos << " to " << seekpos+seekstep << std::endl;
 
148
      int res = std::fseek(f1, seekstep, whence);
 
149
      if (res != 0) {
 
150
          ECA_LOG_MSG(ECA_LOGGER::info, "(eca-fileio-stream) fseek() error! (lfs).");
 
151
          curpos_rep = 0;
 
152
          std::fseek(f1, 0, SEEK_SET);
 
153
          break;
 
154
      }
 
155
      if (seekpos == 0) whence = SEEK_CUR;
 
156
      seekpos += seekstep;
 
157
    }
 
158
#else
 
159
    DBC_CHECK(sizeof(long int) == sizeof(off_t));
 
160
    std::fseek(f1, static_cast<long>(curpos_rep), SEEK_SET);
 
161
#endif
 
162
  }
 
163
}
 
164
 
 
165
void ECA_FILE_IO_STREAM::set_file_position_advance(off_t fw)
 
166
{
 
167
  if (standard_mode != true) {
 
168
    set_file_position(curpos_rep + fw);
 
169
  }
 
170
}
 
171
 
 
172
void ECA_FILE_IO_STREAM::set_file_position_end(void)
 
173
 
174
  if (standard_mode == false) {
 
175
    int res = std::fseek(f1, 0, SEEK_END);
 
176
    if (res != 0) {
 
177
      ECA_LOG_MSG(ECA_LOGGER::info, "(eca-fileio-stream) fseek() error! (seek_end).");
 
178
    }
 
179
    else {
 
180
      curpos_rep = get_file_length();
 
181
    }
 
182
  }
 
183
}
 
184
 
 
185
off_t ECA_FILE_IO_STREAM::get_file_position(void) const
 
186
{
 
187
  if (standard_mode == true) return(0);
 
188
  return(curpos_rep);
 
189
}
 
190
 
 
191
off_t ECA_FILE_IO_STREAM::get_file_length(void) const
 
192
{
 
193
  if (standard_mode == true) return(0);
 
194
  
 
195
  struct stat temp;
 
196
  stat(fname_rep.c_str(), &temp);
 
197
  off_t lentemp = temp.st_size;
 
198
 
 
199
  return(lentemp); 
 
200
}