ChibiOS/RT
2.5.1
chfiles.h
Go to the documentation of this file.
00001 /*
00002     ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
00003                  2011,2012 Giovanni Di Sirio.
00004 
00005     This file is part of ChibiOS/RT.
00006 
00007     ChibiOS/RT is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     ChibiOS/RT is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 /**
00022  * @file    chfiles.h
00023  * @brief   Data files.
00024  * @details This header defines abstract interfaces useful to access generic
00025  *          data files in a standardized way.
00026  *
00027  * @addtogroup data_files
00028  * @details This module define an abstract interface for generic data files by
00029  *          extending the @p BaseSequentialStream interface. Note that no code
00030  *          is present, data files are just abstract interface-like structures,
00031  *          you should look at the systems as to a set of abstract C++ classes
00032  *          (even if written in C). This system has the advantage to make the
00033  *          access to streams independent from the implementation logic.<br>
00034  *          The data files interface can be used as base class for high level
00035  *          object types such as an API for a File System implementation.
00036  * @{
00037  */
00038 
00039 #ifndef _CHFILES_H_
00040 #define _CHFILES_H_
00041 
00042 /**
00043  * @brief   No error return code.
00044  */
00045 #define FILE_OK         0
00046 
00047 /**
00048  * @brief   Error code from the file stream methods.
00049  */
00050 #define FILE_ERROR      0xFFFFFFFFUL
00051 
00052 /**
00053  * @brief   File offset type.
00054  */
00055 typedef uint32_t fileoffset_t;
00056 
00057 /**
00058  * @brief   BaseFileStream specific methods.
00059  */
00060 #define _base_file_stream_methods                                           \
00061   _base_sequential_stream_methods                                           \
00062   /* File close method.*/                                                   \
00063   uint32_t (*close)(void *instance);                                        \
00064   /* Get last error code method.*/                                          \
00065   int (*geterror)(void *instance);                                          \
00066   /* File get size method.*/                                                \
00067   fileoffset_t (*getsize)(void *instance);                                  \
00068   /* File get current position method.*/                                    \
00069   fileoffset_t (*getposition)(void *instance);                              \
00070   /* File seek method.*/                                                    \
00071   uint32_t (*lseek)(void *instance, fileoffset_t offset);
00072 
00073 /**
00074  * @brief   @p BaseFileStream specific data.
00075  * @note    It is empty because @p BaseFileStream is only an interface
00076  *          without implementation.
00077  */
00078 #define _base_file_stream_data                                              \
00079   _base_sequential_stream_data
00080 
00081 /**
00082  * @extends BaseSequentialStreamVMT
00083  *
00084  * @brief   @p BaseFileStream virtual methods table.
00085  */
00086 struct BaseFileStreamVMT {
00087   _base_file_stream_methods
00088 };
00089 
00090 /**
00091  * @extends BaseSequentialStream
00092  *
00093  * @brief   Base file stream class.
00094  * @details This class represents a generic file data stream.
00095  */
00096 typedef struct {
00097   /** @brief Virtual Methods Table.*/
00098   const struct BaseFileStreamVMT *vmt;
00099   _base_file_stream_data
00100 } BaseFileStream;
00101 
00102 /**
00103  * @name    Macro Functions (BaseFileStream)
00104  * @{
00105  */
00106 /**
00107  * @brief   Base file Stream close.
00108  * @details The function closes a file stream.
00109  *
00110  * @param[in] ip        pointer to a @p BaseFileStream or derived class
00111  * @return              The operation status.
00112  * @retval FILE_OK      no error.
00113  * @retval FILE_ERROR   operation failed.
00114  *
00115  * @api
00116  */
00117 #define chFileStreamClose(ip) ((ip)->vmt->close(ip))
00118 
00119 /**
00120  * @brief   Returns an implementation dependent error code.
00121  *
00122  * @param[in] ip        pointer to a @p BaseFileStream or derived class
00123  * @return              Implementation dependent error code.
00124  *
00125  * @api
00126  */
00127 #define chFileStreamGetError(ip) ((ip)->vmt->geterror(ip))
00128 
00129 /**
00130  * @brief   Returns the current file size.
00131  *
00132  * @param[in] ip        pointer to a @p BaseFileStream or derived class
00133  * @return              The file size.
00134  *
00135  * @api
00136  */
00137 #define chFileStreamGetSize(ip) ((ip)->vmt->getsize(ip))
00138 
00139 /**
00140  * @brief   Returns the current file pointer position.
00141  *
00142  * @param[in] ip        pointer to a @p BaseFileStream or derived class
00143  * @return              The current position inside the file.
00144  *
00145  * @api
00146  */
00147 #define chFileStreamGetPosition(ip) ((ip)->vmt->getposition(ip))
00148 
00149 /**
00150  * @brief   Moves the file current pointer to an absolute position.
00151  *
00152  * @param[in] ip        pointer to a @p BaseFileStream or derived class
00153  * @param[in] offset    new absolute position
00154  * @return              The operation status.
00155  * @retval FILE_OK      no error.
00156  * @retval FILE_ERROR   operation failed.
00157  *
00158  * @api
00159  */
00160 #define chFileStreamSeek(ip, offset) ((ip)->vmt->lseek(ip, offset))
00161 /** @} */
00162 
00163 #endif /* _CHFILES_H_ */
00164 
00165 /** @} */