Drizzled Public API Documentation

CSFile.h
00001 /* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
00002  *
00003  * PrimeBase Media Stream for MySQL
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018  *
00019  * Original author: Paul McCullagh (H&G2JCtL)
00020  * Continued development: Barry Leslie
00021  *
00022  * 2007-06-07
00023  *
00024  * CORE SYSTEM:
00025  * Basic file I/O.
00026  *
00027  */
00028 
00029 #pragma once
00030 #ifndef __CSFILE_H__
00031 #define __CSFILE_H__
00032 
00033 #include <stdio.h>
00034 
00035 #include "CSDefs.h"
00036 #include "CSPath.h"
00037 #include "CSException.h"
00038 #include "CSSys.h"
00039 
00040 class CSOutputStream;
00041 class CSInputStream;
00042 
00043 class CSFile : public CSSysFile, public CSRefObject {
00044 public:
00045   CSPath *myFilePath;
00046 
00047   static const int DEFAULT = 0; // Open for read/write, error if it does not exist.
00048   static const int READONLY = 1;  // Open for readonly
00049   static const int CREATE = 2;  // Create if it does not exist
00050   static const int TRUNCATE = 4;  // After open, set EOF to zero  
00051 
00052   CSFile(): myFilePath(NULL), iMode(-1), iLocked(0) { }
00053 
00054   virtual ~CSFile(); 
00055 
00056   CSOutputStream *getOutputStream();
00057   CSOutputStream *getOutputStream(off64_t offset);
00058 
00059   CSInputStream *getInputStream();
00060   CSInputStream *getInputStream(off64_t offset);
00061 
00062   /*
00063    * Open the file in the specified
00064    * mode.
00065    */
00066   virtual void open(int mode);
00067 
00068   /* Lock the file. The file will be unlocked
00069    * when closed.
00070    */
00071   virtual void lock();
00072 
00073   virtual void unlock();
00074 
00075   /*
00076    * Close the file.
00077    */
00078   virtual void close();
00079 
00080   /*
00081    * Calculate the Md5 digest for the file.
00082    */
00083   void md5Digest(Md5Digest *digest);
00084 
00085   /*
00086    * Move the current position to
00087    * the end of the file.
00088    */
00089   virtual off64_t getEOF();
00090 
00091   virtual void setEOF(off64_t offset);
00092 
00093   /*
00094    * Read a given number of bytes. This function
00095    * throws an error if the number of bytes read
00096    * ius less than 'min_size'.
00097    */
00098   virtual size_t read(void *data, off64_t offset, size_t size, size_t min_size);
00099 
00100   /*
00101    * Write the given number of bytes.
00102    * Throws IOException if an error occurs.
00103    */
00104   virtual void write(const void *data, off64_t offset, size_t size);
00105 
00106   /*
00107    * Flush the data written.
00108    */
00109   virtual void flush();
00110 
00111   /* Flush the OS buffers: */
00112   virtual void sync() ;
00113 
00114   /* Resets access and modification times of the file: */
00115   virtual void touch();
00116 
00117   /*
00118    * Return a platform specific prefered 
00119    * line ending for text files.
00120    */
00121   virtual const char *getEOL() { return "\n"; };
00122 
00123   virtual const char *getPathString() { return myFilePath->getCString(); }
00124 
00125   bool exists() { return myFilePath->exists(); }
00126 
00127   friend class CSReadBufferedFile;
00128 
00129 private:
00130   int   iMode;
00131   int   iLocked;
00132 
00133   virtual void openFile(int mode);
00134   bool try_CreateAndOpen(CSThread *self, int mode, bool retry);
00135 
00136 public:
00137   void streamOut(CSOutputStream *dst_stream, off64_t src_offset, off64_t size, char *buffer, size_t buffer_size);
00138   void streamIn(CSInputStream *src_stream, off64_t dst_offset, off64_t size, char *buffer, size_t buffer_size);
00139 
00140   static bool isDirNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
00141   static bool isDirExists(CSException *e) { return e->getErrorCode() == EEXIST; }
00142 
00143   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);
00144 
00145   static CSFile *newFile(CSPath *path);
00146 
00147   static CSFile *newFile(const char *path);
00148 
00149   static CSFile *newFile(const char *dir_str, const char *path_str);
00150 };
00151 
00152 
00153 // This stuff needs to be retought.
00154 
00155 #ifdef DEBUG
00156 #define SC_DEFAULT_FILE_BUFFER_SIZE     127
00157 #else
00158 #define SC_DEFAULT_FILE_BUFFER_SIZE     (64 * 1024)
00159 #endif
00160 
00161 class CSReadBufferedFile : public CSRefObject {
00162 public:
00163 
00164   CSReadBufferedFile();
00165 
00166   ~CSReadBufferedFile();
00167   
00168   void setFile(CSFile *file) {myFile = file;}
00169 
00170   const char *getPathString() { return myFile->getPathString(); }
00171   void open(int mode) {myFile->open(mode); }
00172 
00173   void close();
00174 
00175   off64_t getEOF();
00176 
00177   void setEOF(off64_t offset);
00178 
00179   size_t read(void *data, off64_t offset, size_t size, size_t min_size);
00180 
00181   void write(const void *data, off64_t offset, size_t size);
00182 
00183   void flush();
00184 
00185   void sync();
00186 
00187   const char *getEOL();
00188 
00189 private:
00190   CSFile  *myFile;
00191 
00192   char  iFileBuffer[SC_DEFAULT_FILE_BUFFER_SIZE];
00193   off64_t iFileBufferOffset;
00194   size_t  iBufferDataLen;
00195 
00196   virtual void openFile(int mode);
00197 };
00198 
00199 
00200 #endif