gwenhywfar  4.99.8beta
syncio_memory.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Tue Apr 27 2010
3  copyright : (C) 2010 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #define DISABLE_DEBUGLOG
30 
31 
32 
33 #include "syncio_memory_p.h"
34 #include "i18n_l.h"
35 
36 #include <gwenhywfar/misc.h>
37 #include <gwenhywfar/debug.h>
38 #include <gwenhywfar/gui.h>
39 
40 #include <assert.h>
41 #include <errno.h>
42 #include <string.h>
43 
44 
45 
46 GWEN_INHERIT(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY)
47 
48 
49 
51  GWEN_SYNCIO *sio;
52  GWEN_SYNCIO_MEMORY *xio;
53 
55  GWEN_NEW_OBJECT(GWEN_SYNCIO_MEMORY, xio);
56  GWEN_INHERIT_SETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio, xio, GWEN_SyncIo_Memory_FreeData);
57 
60 
61  if (buffer) {
62  xio->buffer=buffer;
63  xio->own=take?1:0;
64  }
65  else {
66  xio->buffer=GWEN_Buffer_new(0, 256, 0, 1);
67  xio->own=1;
68  }
69 
71 
72  return sio;
73 }
74 
75 
76 
77 GWEN_SYNCIO *GWEN_SyncIo_Memory_fromBuffer(const uint8_t *buffer, int size) {
78  GWEN_SYNCIO *sio;
79  GWEN_SYNCIO_MEMORY *xio;
80 
82  GWEN_NEW_OBJECT(GWEN_SYNCIO_MEMORY, xio);
83  GWEN_INHERIT_SETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio, xio, GWEN_SyncIo_Memory_FreeData);
84 
87 
88  /* adapt size, if necessary */
89  if (size==-1) {
90  if (buffer)
91  size=strlen((const char*) buffer)+1;
92  else
93  size=0;
94  }
95 
96  xio->buffer=GWEN_Buffer_new(0, size, 0, 1);
97  xio->own=1;
98  if (buffer && size>0) {
99  GWEN_Buffer_AppendBytes(xio->buffer, (const char*) buffer, size);
100  GWEN_Buffer_Rewind(xio->buffer);
101  }
103  return sio;
104 }
105 
106 
107 
109  GWEN_SYNCIO_MEMORY *xio;
110 
111  xio=(GWEN_SYNCIO_MEMORY*) p;
112  if (xio->buffer && xio->own)
113  GWEN_Buffer_free(xio->buffer);
114  GWEN_FREE_OBJECT(xio);
115 }
116 
117 
118 
120  GWEN_SYNCIO_MEMORY *xio;
121 
122  assert(sio);
123  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
124  assert(xio);
125 
126  return xio->buffer;
127 }
128 
129 
130 
132  GWEN_SYNCIO_MEMORY *xio;
133  GWEN_BUFFER *buf;
134 
135  assert(sio);
136  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
137  assert(xio);
138 
139  if (xio->own==0 || xio->buffer==NULL) {
140  DBG_ERROR(GWEN_LOGDOMAIN, "Can't give away buffer, object does not own it");
141  return NULL;
142  }
143  buf=xio->buffer;
144  xio->buffer=NULL;
145  xio->own=0;
146  return buf;
147 }
148 
149 
150 
152  uint8_t *buffer,
153  uint32_t size) {
154  GWEN_SYNCIO_MEMORY *xio;
155  uint32_t bytesLeft;
156 
157  assert(sio);
158  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
159  assert(xio);
160 
161  if (xio->buffer==NULL) {
162  DBG_ERROR(GWEN_LOGDOMAIN, "No buffer");
163  return GWEN_ERROR_INTERNAL;
164  }
165 
166  bytesLeft=GWEN_Buffer_GetBytesLeft(xio->buffer);
167  if (bytesLeft==0) {
168  DBG_VERBOUS(GWEN_LOGDOMAIN, "EOF met");
169  return 0;
170  }
171 
172  if (size>bytesLeft)
173  size=bytesLeft;
174  memmove(buffer, GWEN_Buffer_GetPosPointer(xio->buffer), size);
175  GWEN_Buffer_IncrementPos(xio->buffer, size);
176 
177  return size;
178 }
179 
180 
181 
183  const uint8_t *buffer,
184  uint32_t size) {
185  GWEN_SYNCIO_MEMORY *xio;
186 
187  assert(sio);
188  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
189  assert(xio);
190 
191  if (xio->buffer==NULL) {
192  DBG_ERROR(GWEN_LOGDOMAIN, "No socket");
193  return GWEN_ERROR_INTERNAL;
194  }
195 
196  if (size) {
197  int rv;
198 
199  rv=GWEN_Buffer_AppendBytes(xio->buffer, (const char*) buffer, size);
200  if (rv<0) {
201  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
202  return rv;
203  }
204  }
205 
206  return size;
207 }
208 
209 
210 
211 
212 
int GWENHYWFAR_CB GWEN_SyncIo_Memory_Write(GWEN_SYNCIO *sio, const uint8_t *buffer, uint32_t size)
uint32_t GWEN_Buffer_GetBytesLeft(GWEN_BUFFER *bf)
Definition: buffer.c:577
#define GWEN_SYNCIO_MEMORY_TYPE
Definition: syncio_memory.h:33
GWEN_SYNCIO_WRITE_FN GWEN_SyncIo_SetWriteFn(GWEN_SYNCIO *sio, GWEN_SYNCIO_WRITE_FN fn)
Definition: syncio.c:283
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:92
#define NULL
Definition: binreloc.c:290
#define DBG_VERBOUS(dbg_logger, format, args...)
Definition: debug.h:200
int GWENHYWFAR_CB GWEN_SyncIo_Memory_Read(GWEN_SYNCIO *sio, uint8_t *buffer, uint32_t size)
#define GWEN_LOGDOMAIN
Definition: logger.h:35
void GWEN_SyncIo_SetStatus(GWEN_SYNCIO *sio, GWEN_SYNCIO_STATUS st)
Definition: syncio.c:193
void GWENHYWFAR_CB GWEN_SyncIo_Memory_FreeData(void *bp, void *p)
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:38
char * GWEN_Buffer_GetPosPointer(const GWEN_BUFFER *bf)
Definition: buffer.c:588
int GWEN_Buffer_IncrementPos(GWEN_BUFFER *bf, uint32_t i)
Definition: buffer.c:495
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:86
struct GWEN_SYNCIO GWEN_SYNCIO
Definition: syncio.h:40
GWEN_SYNCIO * GWEN_SyncIo_Memory_new(GWEN_BUFFER *buffer, int take)
Definition: syncio_memory.c:50
#define GWENHYWFAR_CB
Definition: gwenhywfarapi.h:89
GWEN_BUFFER * GWEN_SyncIo_Memory_GetBuffer(const GWEN_SYNCIO *sio)
GWEN_SYNCIO * GWEN_SyncIo_Memory_fromBuffer(const uint8_t *buffer, int size)
Definition: syncio_memory.c:77
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:83
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:41
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
GWEN_SYNCIO * GWEN_SyncIo_new(const char *typeName, GWEN_SYNCIO *baseIo)
Definition: syncio.c:51
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:164
int GWEN_Buffer_AppendBytes(GWEN_BUFFER *bf, const char *buffer, uint32_t size)
Definition: buffer.c:348
void GWEN_Buffer_Rewind(GWEN_BUFFER *bf)
Definition: buffer.c:693
GWEN_BUFFER * GWEN_SyncIo_Memory_TakeBuffer(const GWEN_SYNCIO *sio)
#define GWEN_INHERIT(bt, t)
Definition: inherit.h:264
GWEN_SYNCIO_READ_FN GWEN_SyncIo_SetReadFn(GWEN_SYNCIO *sio, GWEN_SYNCIO_READ_FN fn)
Definition: syncio.c:271
#define GWEN_ERROR_INTERNAL
Definition: error.h:125
#define GWEN_INHERIT_SETDATA(bt, t, element, data, fn)
Definition: inherit.h:292
#define GWEN_INHERIT_GETDATA(bt, t, element)
Definition: inherit.h:271