AtxStreams.h

00001 /*****************************************************************
00002 |
00003 |   Atomix - Byte Streams
00004 |
00005 |   (c) 2002-2006 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008  ****************************************************************/
00009 
00010 #ifndef _ATX_STREAMS_H_
00011 #define _ATX_STREAMS_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |   includes
00015 +---------------------------------------------------------------------*/
00016 #include "AtxInterfaces.h"
00017 #include "AtxTypes.h"
00018 #include "AtxResults.h"
00019 #include "AtxDataBuffer.h"
00020 #include "AtxString.h"
00021 
00022 /*----------------------------------------------------------------------
00023 |   error codes
00024 +---------------------------------------------------------------------*/
00025 #define ATX_ERROR_EOS (ATX_ERROR_BASE_BYTE_STREAM - 0)
00026 
00027 /*----------------------------------------------------------------------
00028 |   ATX_InputStream
00029 +---------------------------------------------------------------------*/
00030 ATX_DECLARE_INTERFACE(ATX_InputStream)
00031 ATX_BEGIN_INTERFACE_DEFINITION(ATX_InputStream)
00032     ATX_Result (*Read)(ATX_InputStream* self, 
00033                        ATX_Any          buffer,
00034                        ATX_Size         bytes_to_read,
00035                        ATX_Size*        bytes_read);
00036     ATX_Result (*Seek)(ATX_InputStream* self, ATX_Position  offset);
00037     ATX_Result (*Tell)(ATX_InputStream* self, ATX_Position* offset);
00038     ATX_Result (*GetSize)(ATX_InputStream* self, ATX_Size* size);
00039     ATX_Result (*GetAvailable)(ATX_InputStream* self, 
00040                                ATX_Size*        available);
00041 ATX_END_INTERFACE_DEFINITION
00042 
00043 /*----------------------------------------------------------------------
00044 |   ATX_OutputStream
00045 +---------------------------------------------------------------------*/
00046 ATX_DECLARE_INTERFACE(ATX_OutputStream)
00047 ATX_BEGIN_INTERFACE_DEFINITION(ATX_OutputStream)
00048     ATX_Result (*Write)(ATX_OutputStream* self,
00049                         ATX_AnyConst      buffer,
00050                         ATX_Size          bytes_to_write,
00051                         ATX_Size*         bytes_written);
00052     ATX_Result (*Seek)(ATX_OutputStream* self, ATX_Position  offset);
00053     ATX_Result (*Tell)(ATX_OutputStream* self, ATX_Position* offset);
00054     ATX_Result (*Flush)(ATX_OutputStream* self);
00055 ATX_END_INTERFACE_DEFINITION
00056 
00057 /*----------------------------------------------------------------------
00058 |   base class implementations
00059 +---------------------------------------------------------------------*/
00060 #ifdef __cplusplus
00061 extern "C" {
00062 #endif /* __cplusplus */
00063 
00064 ATX_Result ATX_OutputStream_WriteFully(ATX_OutputStream* stream,
00065                                        ATX_AnyConst      buffer,
00066                                        ATX_Size          bytes_to_write);
00067 ATX_Result ATX_OutputStream_WriteString(ATX_OutputStream* stream,
00068                                         ATX_CString       string);
00069 ATX_Result ATX_OutputStream_WriteLine(ATX_OutputStream* stream,
00070                                       ATX_CString       line);
00071 ATX_Result ATX_InputStream_ReadLine(ATX_InputStream* stream,
00072                                     char*            line,
00073                                     ATX_Size         line_size,
00074                                     ATX_Size*        chars_read);
00075 
00076 ATX_Result ATX_InputStream_ReadLineString(ATX_InputStream* stream,
00077                                           ATX_String*      string,
00078                                           ATX_Size         max_length);
00079 
00080 ATX_Result ATX_InputStream_ReadFully(ATX_InputStream* stream,
00081                                      ATX_Any          buffer,
00082                                      ATX_Size         bytes_to_read);
00083 
00084 ATX_Result ATX_InputStream_Skip(ATX_InputStream* stream,
00085                                 ATX_Size         count);
00086 
00087 ATX_Result ATX_InputStream_Load(ATX_InputStream* stream, 
00088                                 ATX_Size         max_read, /* = 0 if no limit */
00089                                 ATX_DataBuffer** buffer);
00090 #ifdef __cplusplus
00091 }
00092 #endif /* __cplusplus */
00093 
00094 /*----------------------------------------------------------------------
00095 |   convenience macros
00096 +---------------------------------------------------------------------*/
00097 #define ATX_InputStream_Read(object, buffer, bytes_to_read, bytes_read)\
00098 ATX_INTERFACE(object)->Read(object,                                    \
00099                             buffer,                                    \
00100                             bytes_to_read,                             \
00101                             bytes_read)
00102 
00103 #define ATX_InputStream_Seek(object, where) \
00104 ATX_INTERFACE(object)->Seek(object, where)
00105 
00106 #define ATX_InputStream_Tell(object, where) \
00107 ATX_INTERFACE(object)->Tell(object, where)
00108 
00109 #define ATX_InputStream_GetSize(object, size) \
00110 ATX_INTERFACE(object)->GetSize(object, size)
00111 
00112 #define ATX_InputStream_GetAvailable(object, available) \
00113 ATX_INTERFACE(object)->GetAvailable(object, available)
00114 
00115 #define ATX_OutputStream_Write(object, buffer, bytes_to_write, bytes_written) \
00116 ATX_INTERFACE(object)->Write(object,                                          \
00117                              buffer,                                          \
00118                              bytes_to_write,                                  \
00119                              bytes_written)       
00120 
00121 #define ATX_OutputStream_Seek(object, where) \
00122 ATX_INTERFACE(object)->Seek(object, where)
00123 
00124 #define ATX_OutputStream_Tell(object, where) \
00125 ATX_INTERFACE(object)->Tell(object, where)
00126 
00127 #define ATX_OutputStream_Flush(object, where) \
00128 ATX_INTERFACE(object)->Flush(object, where)
00129 
00130 /*----------------------------------------------------------------------
00131 |   ATX_StreamTransformer
00132 +---------------------------------------------------------------------*/
00133 ATX_DECLARE_INTERFACE(ATX_StreamTransformer)
00134 ATX_BEGIN_INTERFACE_DEFINITION(ATX_StreamTransformer)
00135     ATX_Result (*Transform)(ATX_StreamTransformer* self,
00136                             ATX_AnyConst              buffer,
00137                             ATX_Size                  size);
00138 ATX_END_INTERFACE_DEFINITION
00139 
00140 #define ATX_StreamTransformer_Transform(object, buffer, size) \
00141 ATX_INTERFACE(object)->Transform(object, buffer, size)
00142 
00143 /*----------------------------------------------------------------------
00144 |   ATX_MemoryStream
00145 +---------------------------------------------------------------------*/
00146 typedef struct ATX_MemoryStream ATX_MemoryStream;
00147 
00148 #ifdef __cplusplus
00149 extern "C" {
00150 #endif /* __cplusplus */
00151 
00152 ATX_Result 
00153 ATX_MemoryStream_Create(ATX_Size size, ATX_MemoryStream** stream);
00154 
00155 ATX_Result 
00156 ATX_MemoryStream_Destroy(ATX_MemoryStream* self);
00157 
00158 ATX_Result 
00159 ATX_MemoryStream_GetBuffer(ATX_MemoryStream*      self,
00160                            const ATX_DataBuffer** buffer);
00161 
00162 ATX_Result 
00163 ATX_MemoryStream_GetInputStream(ATX_MemoryStream* self,
00164                                 ATX_InputStream** stream);
00165 
00166 ATX_Result 
00167 ATX_MemoryStream_GetOutputStream(ATX_MemoryStream* self,
00168                                  ATX_OutputStream** stream);
00169 #ifdef __cplusplus
00170 }
00171 #endif /* __cplusplus */
00172 
00173 /*----------------------------------------------------------------------
00174 |   functions
00175 +---------------------------------------------------------------------*/
00176 #ifdef __cplusplus
00177 extern "C" {
00178 #endif /* __cplusplus */
00179 
00180 ATX_Result ATX_SubInputStream_Create(ATX_InputStream*       parent,
00181                                      ATX_Position           offset,
00182                                      ATX_Size               size,
00183                                      ATX_StreamTransformer* transformer,
00184                                      ATX_InputStream**      stream);
00185 
00186 ATX_Result ATX_SubOutputStream_Create(ATX_OutputStream*      parent,
00187                                       ATX_Position           offset,
00188                                       ATX_Size               size,
00189                                       ATX_StreamTransformer* transformer,
00190                                       ATX_OutputStream**     stream);
00191 
00192 #ifdef __cplusplus
00193 }
00194 #endif /* __cplusplus */
00195 
00196 #endif /* _ATX_STREAMS_H_ */
00197