~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSFile.h

Added the PBMS daemon plugin.

(Augen zu und durch!)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 * Original author: Paul McCullagh (H&G2JCtL)
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-06-07
 
23
 *
 
24
 * CORE SYSTEM:
 
25
 * Basic file I/O.
 
26
 *
 
27
 */
 
28
 
 
29
#ifndef __CSFILE_H__
 
30
#define __CSFILE_H__
 
31
 
 
32
#include <stdio.h>
 
33
 
 
34
#include "CSDefs.h"
 
35
#include "CSPath.h"
 
36
#include "CSException.h"
 
37
 
 
38
using namespace std;
 
39
 
 
40
extern int unix_file_close(int fh);
 
41
 
 
42
class CSOutputStream;
 
43
class CSInputStream;
 
44
 
 
45
class CSFile : public CSRefObject {
 
46
public:
 
47
        CSPath *myFilePath;
 
48
 
 
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  
 
53
 
 
54
        CSFile(): myFilePath(NULL), iFH(-1) { }
 
55
 
 
56
        virtual ~CSFile(); 
 
57
 
 
58
        CSOutputStream *getOutputStream();
 
59
        CSOutputStream *getOutputStream(off_t offset);
 
60
 
 
61
        CSInputStream *getInputStream();
 
62
        CSInputStream *getInputStream(off_t offset);
 
63
 
 
64
        /*
 
65
         * Open the file in the specified
 
66
         * mode.
 
67
         */
 
68
        virtual void open(int mode);
 
69
 
 
70
        /*
 
71
         * Close the file.
 
72
         */
 
73
        virtual void close();
 
74
 
 
75
        /*
 
76
         * Move the current position to
 
77
         * the end of the file.
 
78
         */
 
79
        virtual off_t getEOF();
 
80
 
 
81
        virtual void setEOF(off_t offset);
 
82
 
 
83
        /*
 
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'.
 
87
         */
 
88
        virtual size_t read(void *data, off_t offset, size_t size, size_t min_size);
 
89
 
 
90
        /*
 
91
         * Write the given number of bytes.
 
92
         * Throws IOException if an error occurs.
 
93
         */
 
94
        virtual void write(const void *data, off_t offset, size_t size);
 
95
 
 
96
        /*
 
97
         * Flush the data written.
 
98
         */
 
99
        virtual void flush();
 
100
 
 
101
        /* Flush the OS buffers: */
 
102
        virtual void sync() ;
 
103
 
 
104
        /*
 
105
         * Return a platform specific prefered 
 
106
         * line ending for text files.
 
107
         */
 
108
        virtual const char *getEOL() { return "\n"; };
 
109
 
 
110
        virtual const char *getPathString() { return myFilePath->getCString(); }
 
111
 
 
112
        friend class CSReadBufferedFile;
 
113
        friend class CSBufferedFile;
 
114
 
 
115
private:
 
116
        int             iFH;
 
117
 
 
118
        virtual void openFile(int mode);
 
119
 
 
120
public:
 
121
        static bool isDirNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
 
122
        static bool isDirExists(CSException *e) { return e->getErrorCode() == EEXIST; }
 
123
 
 
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);
 
125
 
 
126
        static CSFile *newFile(CSPath *path);
 
127
 
 
128
        static CSFile *newFile(const char *path);
 
129
};
 
130
 
 
131
#ifdef DEBUG
 
132
#define SC_DEFAULT_FILE_BUFFER_SIZE                     127
 
133
#else
 
134
#define SC_DEFAULT_FILE_BUFFER_SIZE                     (64 * 1024)
 
135
#endif
 
136
 
 
137
class CSReadBufferedFile : public CSFile {
 
138
public:
 
139
        CSFile  *myFile;
 
140
 
 
141
        CSReadBufferedFile();
 
142
 
 
143
        virtual ~CSReadBufferedFile();
 
144
 
 
145
        virtual void close();
 
146
 
 
147
        virtual off_t getEOF();
 
148
 
 
149
        virtual void setEOF(off_t offset);
 
150
 
 
151
        virtual size_t read(void *data, off_t offset, size_t size, size_t min_size);
 
152
 
 
153
        virtual void write(const void *data, off_t offset, size_t size);
 
154
 
 
155
        virtual void flush();
 
156
 
 
157
        virtual void sync();
 
158
 
 
159
        virtual const char *getEOL();
 
160
 
 
161
        friend class CSBufferedFile;
 
162
 
 
163
private:
 
164
        char    iFileBuffer[SC_DEFAULT_FILE_BUFFER_SIZE];
 
165
        off_t   iFileBufferOffset;
 
166
        size_t  iBufferDataLen;
 
167
 
 
168
        virtual void openFile(int mode);
 
169
public:
 
170
        static CSFile *newFile(CSFile *file);
 
171
};
 
172
 
 
173
class CSBufferedFile : public CSReadBufferedFile {
 
174
public:
 
175
        CSBufferedFile(): CSReadBufferedFile(), iBufferDirty(false) { }
 
176
 
 
177
        virtual ~CSBufferedFile() { };
 
178
 
 
179
        virtual void write(const void *data, off_t offset, size_t size);
 
180
 
 
181
        virtual void flush();
 
182
 
 
183
private:
 
184
        bool    iBufferDirty;
 
185
};
 
186
 
 
187
#endif