00001 /*************************************************************************** 00002 * Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider, * 00003 * Matthias Hardt, Guido Madaus * 00004 * * 00005 * Copyright (C) 2007-2008 BerLinux Solutions GbR * 00006 * Stefan Schwarzer & Guido Madaus * 00007 * * 00008 * Copyright (C) 2009-2013 BerLinux Solutions GmbH * 00009 * * 00010 * Authors: * 00011 * Stefan Schwarzer <stefan.schwarzer@diskohq.org>, * 00012 * Matthias Hardt <matthias.hardt@diskohq.org>, * 00013 * Jens Schneider <jens.schneider@diskohq.org>, * 00014 * Guido Madaus <guido.madaus@diskohq.org>, * 00015 * Patrick Helterhoff <patrick.helterhoff@diskohq.org>, * 00016 * René Bählkow <rene.baehlkow@diskohq.org> * 00017 * * 00018 * This library is free software; you can redistribute it and/or * 00019 * modify it under the terms of the GNU Lesser General Public * 00020 * License version 2.1 as published by the Free Software Foundation. * 00021 * * 00022 * This library is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00025 * Lesser General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU Lesser General Public * 00028 * License along with this library; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00031 **************************************************************************/ 00032 00033 #ifndef MMSFILE_H_ 00034 #define MMSFILE_H_ 00035 00036 /** 00037 * @file mmsfile.h 00038 * 00039 * Header file for MMSFile class. 00040 * 00041 * @ingroup mmstools 00042 */ 00043 00044 #ifdef __HAVE_CURL__ 00045 #include <curl/curl.h> 00046 #endif 00047 #include <sys/stat.h> 00048 #include "mmstools/mmserror.h" 00049 00050 MMS_CREATEERROR(MMSFileError); 00051 00052 00053 //! Specifies supported types of files. 00054 typedef enum { 00055 //! not set 00056 MMSFT_NOTSET = 0, 00057 //! normal file 00058 MMSFT_FILE = 1, 00059 //! url 00060 MMSFT_URL = 2 00061 } MMSFileType; 00062 00063 //! Specifies supported modes for working with files. 00064 typedef enum { 00065 //! read binary (file must exist) 00066 MMSFM_READ = 0, 00067 //! write binary (file size will be set to 0) 00068 MMSFM_WRITE = 1, 00069 //! append binary (always writing to the end of the file) 00070 MMSFM_APPEND = 2, 00071 //! read & write binary (file must exist) 00072 MMSFM_READWRITE = 3, 00073 //! write & read binary (file size will be set to 0) 00074 MMSFM_WRITEREAD = 4, 00075 //! append & read binary (always writing to the end of the file) 00076 MMSFM_APPENDREAD= 5 00077 } MMSFileMode; 00078 00079 //! Specifies the origins for function setFilePos(). 00080 typedef enum { 00081 //! from beginning of file 00082 MMSFPO_SET = 0, 00083 //! from current position of file pointer 00084 MMSFPO_CUR = 1, 00085 //! from end of file 00086 MMSFPO_END = 2 00087 } MMSFilePosOrigin; 00088 00089 //! A file class. 00090 /*! 00091 With this class you can access normal files (/mms/data/file) and 00092 urls (http://address/file) with one interface. 00093 \author Jens Schneider 00094 */ 00095 class MMSFile { 00096 private: 00097 //! ALL files: name of the file 00098 string name; 00099 00100 //! ALL files: open mode 00101 MMSFileMode mode; 00102 00103 //! ALL files: use a separate cache for all file types? 00104 bool usecache; 00105 00106 //! ALL files: type of file 00107 MMSFileType type; 00108 00109 //! ALL files: last error (0 if last call was sucessfull) 00110 int lasterror; 00111 00112 //! MMSFT_FILE: pointer to a file 00113 FILE *file; 00114 00115 #ifdef __HAVE_CURL__ 00116 //! MMSFT_URL: pointer to a multi handle, if an url is used 00117 CURLM *mhandle; 00118 00119 //! MMSFT_URL: pointer to a curl, if an url is used 00120 CURL *curl; 00121 #else 00122 //! MMSFT_URL: pointer to a multi handle, if an url is used 00123 void *mhandle; 00124 00125 //! MMSFT_URL: pointer to a curl, if an url is used 00126 void *curl; 00127 #endif 00128 00129 //! MMSFT_URL: buffer to cached data from url 00130 char *buffer; 00131 00132 //! MMSFT_URL: buffer length 00133 unsigned buf_len; 00134 00135 //! MMSFT_URL: fill pointer within buffer 00136 unsigned buf_pos; 00137 00138 //! MMSFT_URLL: url fetch is not finished and working in background 00139 int still_progr; 00140 00141 //! USECACHE=TRUE: pointer to the whole file data 00142 char *cache; 00143 00144 //! USECACHE=TRUE: file size 00145 size_t cache_fsize; 00146 00147 //! USECACHE=TRUE: current position in cache 00148 size_t cache_fpos; 00149 00150 //! Internal function which resets the most private variables. 00151 void resetAll(); 00152 00153 //! Internal function which work with the own curl buffer. 00154 bool fillCurlBuffer(size_t want, unsigned waittime=20); 00155 00156 //! Internal function which work with the own curl buffer. 00157 void freeCurlBuffer(size_t want); 00158 00159 //! Internal function which opens a file. 00160 bool openFile(); 00161 00162 //! Internal function which closes a file. 00163 bool closeFile(); 00164 00165 public: 00166 #ifdef __HAVE_CURL__ 00167 00168 //! Virtual function for the curl write callback. 00169 virtual size_t write_cb(char *buffer, size_t size, size_t nitems, void *outstream); 00170 #endif 00171 //! Constructor of class MMSFile. 00172 /*! 00173 \param name name of the file ("/mms/data/file" or "http://address/file") 00174 \param mode optional, open mode (default is MMSFM_READ) see definition of MMSFileMode 00175 \param usecache optional, enable caching for this file (default is true) 00176 */ 00177 MMSFile(string name, MMSFileMode mode=MMSFM_READ, bool usecache=true); 00178 00179 //! Destructor of class MMSFile. 00180 virtual ~MMSFile(); 00181 00182 /** 00183 * Returns the name of the file which was set with the constructor MMSFile(). 00184 * 00185 * @param effectiveUrl [in] if set and filetype is MMSFT_URL the effective URL will be returned. 00186 * 00187 */ 00188 string getName(const bool effectiveUrl = false); 00189 00190 //! Returns the open mode of the file which was set with the constructor MMSFile(). 00191 MMSFileMode getMode(); 00192 00193 //! Returns the type of the file. If set to MMSFT_NOTSET, the file was not opened. 00194 MMSFileType getType(); 00195 00196 //! Returns the error of the last method call or 0, if no error has occured. 00197 /*! 00198 \return EBADF, EINVAL, ENOENT, ENOMEM, EOF 00199 */ 00200 int getLastError(); 00201 00202 //! Returns the end of file status. 00203 /*! 00204 It returns EOF (value -1) if the end of the file ist reached. If no error has 00205 occured but the end is not reached, the method returns 0. If the return value 00206 is 1, a error has occured, use getLastError() to determine the error. 00207 \return EOF(-1), 0, 1 00208 */ 00209 int endOfFile(); 00210 00211 //! Moves the file pointer to the beginning of the file. 00212 bool rewindFile(); 00213 00214 //! Moves the file pointer to a specified position. 00215 /*! 00216 \param offset number of bytes from the origin 00217 \param origin optional, initial position, see definiton of MMSFilePosOrigin 00218 \return true if successful 00219 */ 00220 bool setFilePos(long offset, MMSFilePosOrigin origin=MMSFPO_SET); 00221 00222 //! Gets the current postion of the file pointer. 00223 /*! 00224 \param pos address of a long for storing the current position 00225 \return true if successful 00226 */ 00227 bool getFilePos(long *pos); 00228 00229 //! Reads data from the file. 00230 /*! 00231 \param ptr storage location for data 00232 \param ritems address of a size_t for returning the number of full items actually read 00233 \param size item size in bytes 00234 \param nitems maximum number of items to be read 00235 \return true if successful 00236 \note The pointer ptr must addresses size*nitems bytes in memory. 00237 */ 00238 bool readBuffer(void *ptr, size_t *ritems, size_t size, size_t nitems); 00239 00240 //! Reads data from the file. 00241 /*! 00242 \param ptr address of a pointer for returning the allocated memory 00243 \param ritems address of a size_t for returning the number of full items actually read 00244 \param size optional, item size in bytes 00245 \param nitems optional, maximum number of items to be read 00246 \return true if successful 00247 \note This method allocates memory for the data and returns the pointer to it. 00248 \note If you do not use the optional parameters, the method reads the whole file. 00249 */ 00250 bool readBufferEx(void **ptr, size_t *ritems, size_t size=1, size_t nitems=0xffffffff); 00251 00252 //! Gets a string from the file. 00253 /*! 00254 This method reads up to the next \n or up to size bytes in the current line. 00255 If a \n is found, it is the last byte in the returned storage. 00256 \param ptr storage location for data 00257 \param size maximum number of bytes to be read 00258 \return true if successful 00259 \note The pointer ptr must addresses size bytes in memory. 00260 */ 00261 bool getString(char *ptr, size_t size); 00262 00263 //! Gets a string from the file. 00264 /*! 00265 This method reads up to the next \n or up to size bytes in the current line. 00266 If a \n is found, it is the last byte in the returned storage. 00267 \param ptr address of a pointer for returning the allocated memory 00268 \param size optional, maximum number of bytes to be read 00269 \return true if successful 00270 \note This method allocates memory for the data and returns the pointer to it. 00271 \note If you do not use the parameter size, the method reads the whole line up to the next \n. 00272 */ 00273 bool getStringEx(char **ptr, size_t size=0xffffffff); 00274 00275 //! Gets a line from the file. 00276 /*! 00277 This method reads up to the next \n in the current line. The \n will be 00278 stripped from the returned storage. 00279 \param ptr address of a pointer for returning the allocated memory 00280 \return true if successful 00281 \note This method allocates memory for the data and returns the pointer to it. 00282 */ 00283 bool getLine(char **ptr); 00284 00285 //! Gets a line from the file. 00286 /*! 00287 This method reads up to the next \n in the current line. The \n will be 00288 stripped from the returned storage. 00289 \param ptr address of a pointer for returning the allocated memory 00290 \return true if successful 00291 \note This method allocates memory for the data and returns the pointer to it. 00292 */ 00293 bool getLine(string &line); 00294 00295 //! Read a character from the file. 00296 /*! 00297 \param ptr address of a character for returning the byte which is to read 00298 \return true if successful 00299 */ 00300 bool getChar(char *ptr); 00301 00302 //! Writes data to the file. 00303 /*! 00304 \param ptr storage location for data 00305 \param ritems address of a size_t for returning the number of full items actually written 00306 \param size item size in bytes 00307 \param nitems number of items to be written 00308 \return true if successful 00309 \note The pointer ptr must addresses size*nitems bytes in memory. 00310 */ 00311 bool writeBuffer(void *ptr, size_t *ritems, size_t size, size_t nitems); 00312 00313 00314 /** 00315 * TODO: have to implement!!!!!!!!!!! flush(); 00316 */ 00317 }; 00318 00319 #endif /*MMSFILE_H_*/