1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
36
#include "CSException.h"
40
extern int unix_file_close(int fh);
45
class CSFile : public CSRefObject {
49
static const int DEFAULT = 0; // Open for read/write, error if it does not exist.
50
static const int READONLY = 1; // Open for readonly
51
static const int CREATE = 2; // Create if it does not exist
52
static const int TRUNCATE = 4; // After open, set EOF to zero
54
CSFile(): myFilePath(NULL), iFH(-1) { }
58
CSOutputStream *getOutputStream();
59
CSOutputStream *getOutputStream(off64_t offset);
61
CSInputStream *getInputStream();
62
CSInputStream *getInputStream(off64_t offset);
65
* Open the file in the specified
68
virtual void open(int mode);
76
* Move the current position to
77
* the end of the file.
79
virtual off64_t getEOF();
81
virtual void setEOF(off64_t offset);
84
* Read a given number of bytes. This function
85
* throws an error if the number of bytes read
86
* ius less than 'min_size'.
88
virtual size_t read(void *data, off64_t offset, size_t size, size_t min_size);
91
* Write the given number of bytes.
92
* Throws IOException if an error occurs.
94
virtual void write(const void *data, off64_t offset, size_t size);
97
* Flush the data written.
101
/* Flush the OS buffers: */
102
virtual void sync() ;
105
* Return a platform specific prefered
106
* line ending for text files.
108
virtual const char *getEOL() { return "\n"; };
110
virtual const char *getPathString() { return myFilePath->getCString(); }
112
friend class CSReadBufferedFile;
113
friend class CSBufferedFile;
118
virtual void openFile(int mode);
121
void streamOut(CSOutputStream *dst_stream, off64_t src_offset, off64_t size, char *buffer, size_t buffer_size);
123
static bool isDirNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
124
static bool isDirExists(CSException *e) { return e->getErrorCode() == EEXIST; }
126
static bool transfer(CSFile *dst_file, off64_t dst_offset, CSFile *src_file, off64_t src_offset, off64_t size, char *buffer, size_t buffer_size);
128
static CSFile *newFile(CSPath *path);
130
static CSFile *newFile(const char *path);
134
#define SC_DEFAULT_FILE_BUFFER_SIZE 127
136
#define SC_DEFAULT_FILE_BUFFER_SIZE (64 * 1024)
139
class CSReadBufferedFile : public CSFile {
143
CSReadBufferedFile();
145
virtual ~CSReadBufferedFile();
147
virtual void close();
149
virtual off64_t getEOF();
151
virtual void setEOF(off64_t offset);
153
virtual size_t read(void *data, off64_t offset, size_t size, size_t min_size);
155
virtual void write(const void *data, off64_t offset, size_t size);
157
virtual void flush();
161
virtual const char *getEOL();
163
friend class CSBufferedFile;
166
char iFileBuffer[SC_DEFAULT_FILE_BUFFER_SIZE];
167
off64_t iFileBufferOffset;
168
size_t iBufferDataLen;
170
virtual void openFile(int mode);
172
static CSFile *newFile(CSFile *file);
175
class CSBufferedFile : public CSReadBufferedFile {
177
CSBufferedFile(): CSReadBufferedFile(), iBufferDirty(false) { }
179
virtual ~CSBufferedFile() { };
181
virtual void write(const void *data, off64_t offset, size_t size);
183
virtual void flush();