NCBI C Toolkit Cross Reference

C/connect/ncbi_memory_connector.c


  1 /* $Id: ncbi_memory_connector.c,v 6.12 2009/06/23 16:04:40 kazimird Exp $
  2  * ===========================================================================
  3  *
  4  *                            PUBLIC DOMAIN NOTICE
  5  *               National Center for Biotechnology Information
  6  *
  7  *  This software/database is a "United States Government Work" under the
  8  *  terms of the United States Copyright Act.  It was written as part of
  9  *  the author's official duties as a United States Government employee and
 10  *  thus cannot be copyrighted.  This software/database is freely available
 11  *  to the public for use. The National Library of Medicine and the U.S.
 12  *  Government have not placed any restriction on its use or reproduction.
 13  *
 14  *  Although all reasonable efforts have been taken to ensure the accuracy
 15  *  and reliability of the software and data, the NLM and the U.S.
 16  *  Government do not and cannot warrant the performance or results that
 17  *  may be obtained by using this software or data. The NLM and the U.S.
 18  *  Government disclaim all warranties, express or implied, including
 19  *  warranties of performance, merchantability or fitness for any particular
 20  *  purpose.
 21  *
 22  *  Please cite the author in any work or product based on this material.
 23  *
 24  * ===========================================================================
 25  *
 26  * Author:  Anton Lavrentiev
 27  *
 28  * File Description:
 29  *   In-memory CONNECTOR
 30  *
 31  *   See <connect/ncbi_connector.h> for the detailed specification of
 32  *   the connector's methods and structures.
 33  *
 34  */
 35 
 36 #include <connect/ncbi_memory_connector.h>
 37 #include <assert.h>
 38 #include <stdlib.h>
 39 
 40 
 41 /***********************************************************************
 42  *  INTERNAL -- Auxiliary types and static functions
 43  ***********************************************************************/
 44 
 45 /* All internal data necessary to perform the (re)connect and i/o
 46  */
 47 typedef struct {
 48     BUF         buf;
 49     int/*bool*/ own_buf;
 50     EIO_Status  r_status;
 51     EIO_Status  w_status;
 52 } SMemoryConnector;
 53 
 54 
 55 /***********************************************************************
 56  *  INTERNAL -- "s_VT_*" functions for the "virt. table" of connector methods
 57  ***********************************************************************/
 58 
 59 #ifdef __cplusplus
 60 extern "C" {
 61 #endif /* __cplusplus */
 62     static const char* s_VT_GetType (CONNECTOR       connector);
 63     static EIO_Status  s_VT_Open    (CONNECTOR       connector,
 64                                      const STimeout* timeout);
 65     static EIO_Status  s_VT_Wait    (CONNECTOR       connector,
 66                                      EIO_Event       event,
 67                                      const STimeout* timeout);
 68     static EIO_Status  s_VT_Write   (CONNECTOR       connector,
 69                                      const void*     buf,
 70                                      size_t          size,
 71                                      size_t*         n_written,
 72                                      const STimeout* timeout);
 73     static EIO_Status  s_VT_Read    (CONNECTOR       connector,
 74                                      void*           buf,
 75                                      size_t          size,
 76                                      size_t*         n_read,
 77                                      const STimeout* timeout);
 78     static EIO_Status  s_VT_Status  (CONNECTOR       connector,
 79                                      EIO_Event       dir);
 80     static EIO_Status  s_VT_Close   (CONNECTOR       connector,
 81                                      const STimeout* timeout);
 82     static void        s_Setup      (SMetaConnector* meta,
 83                                      CONNECTOR       connector);
 84     static void        s_Destroy    (CONNECTOR       connector);
 85 #  ifdef IMPLEMENTED__CONN_WaitAsync
 86     static EIO_Status s_VT_WaitAsync(void*                   connector,
 87                                      FConnectorAsyncHandler  func,
 88                                      SConnectorAsyncHandler* data);
 89 #  endif
 90 #ifdef __cplusplus
 91 } /* extern "C" */
 92 #endif /* __cplusplus */
 93 
 94 
 95 /*ARGSUSED*/
 96 static const char* s_VT_GetType
 97 (CONNECTOR connector)
 98 {
 99     return "MEMORY";
100 }
101 
102 
103 /*ARGSUSED*/
104 static EIO_Status s_VT_Open
105 (CONNECTOR       connector,
106  const STimeout* timeout)
107 {
108     SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
109     xxx->r_status = eIO_Success;
110     xxx->w_status = eIO_Success;
111     return eIO_Success;
112 }
113 
114 
115 /*ARGSUSED*/
116 static EIO_Status s_VT_Write
117 (CONNECTOR       connector,
118  const void*     buf,
119  size_t          size,
120  size_t*         n_written,
121  const STimeout* timeout)
122 {
123     SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
124 
125     if ( !size )
126         return eIO_Success;
127 
128     if (BUF_Write(&xxx->buf, buf, size)) {
129         *n_written    = size;
130         xxx->w_status = eIO_Success;
131     } else
132         xxx->w_status = eIO_Unknown;
133 
134     return xxx->w_status;
135 }
136 
137 
138 /*ARGSUSED*/
139 static EIO_Status s_VT_Read
140 (CONNECTOR       connector,
141  void*           buf,
142  size_t          size,
143  size_t*         n_read,
144  const STimeout* timeout)
145 {
146     SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
147 
148     if ( !size )
149         return eIO_Success;
150 
151     xxx->r_status = (!(*n_read = BUF_Read(xxx->buf, buf, size))
152                      ? eIO_Closed : eIO_Success);
153 
154     return xxx->r_status;
155 }
156 
157 
158 /*ARGSUSED*/
159 static EIO_Status s_VT_Wait
160 (CONNECTOR       connector,
161  EIO_Event       event,
162  const STimeout* timeout)
163 {
164     SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
165     return event == eIO_Read  &&  !BUF_Size(xxx->buf)
166         ? eIO_Closed : eIO_Success;
167 }
168 
169 
170 static EIO_Status s_VT_Status
171 (CONNECTOR connector,
172  EIO_Event dir)
173 {
174     SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
175     switch (dir) {
176     case eIO_Read:
177         return xxx->r_status;
178     case eIO_Write:
179         return xxx->w_status;
180     default:
181         assert(0); /* should never happen as checked by connection */
182         return eIO_InvalidArg;
183     }
184 }
185 
186 
187 /*ARGSUSED*/
188 static EIO_Status s_VT_Close
189 (CONNECTOR       connector,
190  const STimeout* timeout)
191 {
192     SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
193     BUF_Erase(xxx->buf);
194     return eIO_Success;
195 }
196 
197 
198 static void s_Setup
199 (SMetaConnector* meta,
200  CONNECTOR       connector)
201 {
202     /* initialize virtual table */
203     CONN_SET_METHOD(meta, get_type,   s_VT_GetType,   connector);
204     CONN_SET_METHOD(meta, open,       s_VT_Open,      connector);
205     CONN_SET_METHOD(meta, wait,       s_VT_Wait,      connector);
206     CONN_SET_METHOD(meta, write,      s_VT_Write,     connector);
207     CONN_SET_METHOD(meta, flush,      0,              0);
208     CONN_SET_METHOD(meta, read,       s_VT_Read,      connector);
209     CONN_SET_METHOD(meta, status,     s_VT_Status,    connector);
210     CONN_SET_METHOD(meta, close,      s_VT_Close,     connector);
211 #ifdef IMPLEMENTED__CONN_WaitAsync
212     CONN_SET_METHOD(meta, wait_async, s_VT_WaitAsync, connector);
213 #endif
214     meta->default_timeout = 0/*infinite*/;
215 }
216 
217 
218 static void s_Destroy
219 (CONNECTOR connector)
220 {
221     SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
222     if (xxx->own_buf)
223         BUF_Destroy(xxx->buf);
224     free(xxx);
225     connector->handle = 0;
226     free(connector);
227 }
228 
229 
230 /***********************************************************************
231  *  EXTERNAL -- the connector's "constructors"
232  ***********************************************************************/
233 
234 extern CONNECTOR MEMORY_CreateConnector(void)
235 {
236     return MEMORY_CreateConnectorEx(0);
237 }
238 
239 
240 extern CONNECTOR MEMORY_CreateConnectorEx(BUF buf)
241 {
242     CONNECTOR         ccc = (SConnector*)       malloc(sizeof(SConnector));
243     SMemoryConnector* xxx = (SMemoryConnector*) malloc(sizeof(*xxx));
244 
245     /* initialize internal data structures */
246     xxx->buf     = buf;
247     xxx->own_buf = buf ? 0/*false*/ : 1/*true*/;
248 
249     /* initialize connector data */
250     ccc->handle  = xxx;
251     ccc->next    = 0;
252     ccc->meta    = 0;
253     ccc->setup   = s_Setup;
254     ccc->destroy = s_Destroy;
255 
256     return ccc;
257 }
258 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.