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(off_t offset);
61
CSInputStream *getInputStream();
62
CSInputStream *getInputStream(off_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 off_t getEOF();
81
virtual void setEOF(off_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, off_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, off_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
static bool isDirNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
122
static bool isDirExists(CSException *e) { return e->getErrorCode() == EEXIST; }
124
static bool transfer(CSFile *dst_file, off_t dst_offset, CSFile *src_file, off_t src_offset, off_t size, char *buffer, size_t buffer_size);
126
static CSFile *newFile(CSPath *path);
128
static CSFile *newFile(const char *path);
132
#define SC_DEFAULT_FILE_BUFFER_SIZE 127
134
#define SC_DEFAULT_FILE_BUFFER_SIZE (64 * 1024)
137
class CSReadBufferedFile : public CSFile {
141
CSReadBufferedFile();
143
virtual ~CSReadBufferedFile();
145
virtual void close();
147
virtual off_t getEOF();
149
virtual void setEOF(off_t offset);
151
virtual size_t read(void *data, off_t offset, size_t size, size_t min_size);
153
virtual void write(const void *data, off_t offset, size_t size);
155
virtual void flush();
159
virtual const char *getEOL();
161
friend class CSBufferedFile;
164
char iFileBuffer[SC_DEFAULT_FILE_BUFFER_SIZE];
165
off_t iFileBufferOffset;
166
size_t iBufferDataLen;
168
virtual void openFile(int mode);
170
static CSFile *newFile(CSFile *file);
173
class CSBufferedFile : public CSReadBufferedFile {
175
CSBufferedFile(): CSReadBufferedFile(), iBufferDirty(false) { }
177
virtual ~CSBufferedFile() { };
179
virtual void write(const void *data, off_t offset, size_t size);
181
virtual void flush();