~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Merge Stewart's dead code removal

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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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
 
#include "CSSys.h"
38
 
 
39
 
class CSOutputStream;
40
 
class CSInputStream;
41
 
 
42
 
class CSFile : public CSSysFile, public CSRefObject {
43
 
public:
44
 
        CSPath *myFilePath;
45
 
 
46
 
        static const int DEFAULT = 0;   // Open for read/write, error if it does not exist.
47
 
        static const int READONLY = 1;  // Open for readonly
48
 
        static const int CREATE = 2;    // Create if it does not exist
49
 
        static const int TRUNCATE = 4;  // After open, set EOF to zero  
50
 
 
51
 
        CSFile(): myFilePath(NULL), iMode(-1), iLocked(0) { }
52
 
 
53
 
        virtual ~CSFile(); 
54
 
 
55
 
        CSOutputStream *getOutputStream();
56
 
        CSOutputStream *getOutputStream(off64_t offset);
57
 
 
58
 
        CSInputStream *getInputStream();
59
 
        CSInputStream *getInputStream(off64_t offset);
60
 
 
61
 
        /*
62
 
         * Open the file in the specified
63
 
         * mode.
64
 
         */
65
 
        virtual void open(int mode);
66
 
 
67
 
        /* Lock the file. The file will be unlocked
68
 
         * when closed.
69
 
         */
70
 
        virtual void lock();
71
 
 
72
 
        virtual void unlock();
73
 
 
74
 
        /*
75
 
         * Close the file.
76
 
         */
77
 
        virtual void close();
78
 
 
79
 
        /*
80
 
         * Calculate the Md5 digest for the file.
81
 
         */
82
 
        void md5Digest(Md5Digest *digest);
83
 
 
84
 
        /*
85
 
         * Move the current position to
86
 
         * the end of the file.
87
 
         */
88
 
        virtual off64_t getEOF();
89
 
 
90
 
        virtual void setEOF(off64_t offset);
91
 
 
92
 
        /*
93
 
         * Read a given number of bytes. This function
94
 
         * throws an error if the number of bytes read
95
 
         * ius less than 'min_size'.
96
 
         */
97
 
        virtual size_t read(void *data, off64_t offset, size_t size, size_t min_size);
98
 
 
99
 
        /*
100
 
         * Write the given number of bytes.
101
 
         * Throws IOException if an error occurs.
102
 
         */
103
 
        virtual void write(const void *data, off64_t offset, size_t size);
104
 
 
105
 
        /*
106
 
         * Flush the data written.
107
 
         */
108
 
        virtual void flush();
109
 
 
110
 
        /* Flush the OS buffers: */
111
 
        virtual void sync() ;
112
 
 
113
 
        /* Resets access and modification times of the file: */
114
 
        virtual void touch();
115
 
 
116
 
        /*
117
 
         * Return a platform specific prefered 
118
 
         * line ending for text files.
119
 
         */
120
 
        virtual const char *getEOL() { return "\n"; };
121
 
 
122
 
        virtual const char *getPathString() { return myFilePath->getCString(); }
123
 
 
124
 
        bool exists() { return myFilePath->exists(); }
125
 
 
126
 
        friend class CSReadBufferedFile;
127
 
 
128
 
private:
129
 
        int             iMode;
130
 
        int             iLocked;
131
 
 
132
 
        virtual void openFile(int mode);
133
 
        bool try_CreateAndOpen(CSThread *self, int mode, bool retry);
134
 
 
135
 
public:
136
 
        void streamOut(CSOutputStream *dst_stream, off64_t src_offset, off64_t size, char *buffer, size_t buffer_size);
137
 
        void streamIn(CSInputStream *src_stream, off64_t dst_offset, off64_t size, char *buffer, size_t buffer_size);
138
 
 
139
 
        static bool isDirNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
140
 
        static bool isDirExists(CSException *e) { return e->getErrorCode() == EEXIST; }
141
 
 
142
 
        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);
143
 
 
144
 
        static CSFile *newFile(CSPath *path);
145
 
 
146
 
        static CSFile *newFile(const char *path);
147
 
 
148
 
        static CSFile *newFile(const char *dir_str, const char *path_str);
149
 
};
150
 
 
151
 
 
152
 
// This stuff needs to be retought.
153
 
 
154
 
#ifdef DEBUG
155
 
#define SC_DEFAULT_FILE_BUFFER_SIZE                     127
156
 
#else
157
 
#define SC_DEFAULT_FILE_BUFFER_SIZE                     (64 * 1024)
158
 
#endif
159
 
 
160
 
class CSReadBufferedFile : public CSRefObject {
161
 
public:
162
 
 
163
 
        CSReadBufferedFile();
164
 
 
165
 
        ~CSReadBufferedFile();
166
 
        
167
 
        void setFile(CSFile     *file) {myFile = file;}
168
 
 
169
 
        const char *getPathString() { return myFile->getPathString(); }
170
 
        void open(int mode) {myFile->open(mode); }
171
 
 
172
 
        void close();
173
 
 
174
 
        off64_t getEOF();
175
 
 
176
 
        void setEOF(off64_t offset);
177
 
 
178
 
        size_t read(void *data, off64_t offset, size_t size, size_t min_size);
179
 
 
180
 
        void write(const void *data, off64_t offset, size_t size);
181
 
 
182
 
        void flush();
183
 
 
184
 
        void sync();
185
 
 
186
 
        const char *getEOL();
187
 
 
188
 
private:
189
 
        CSFile  *myFile;
190
 
 
191
 
        char    iFileBuffer[SC_DEFAULT_FILE_BUFFER_SIZE];
192
 
        off64_t iFileBufferOffset;
193
 
        size_t  iBufferDataLen;
194
 
 
195
 
        virtual void openFile(int mode);
196
 
};
197
 
 
198
 
 
199
 
#endif