Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wsstream.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  *
59  * wsstream.c
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 1999-2000 WAPIT OY LTD.
64  * All rights reserved.
65  *
66  * Generic input / output stream.
67  *
68  */
69 
70 #include "wsint.h"
71 
72 /********************* Global functions *********************************/
73 
74 WsBool ws_stream_getc(WsStream *stream, WsUInt32 *ch_return)
75 {
76  if (stream->ungetch_valid) {
77  *ch_return = stream->ungetch;
78  stream->ungetch_valid = WS_FALSE;
79 
80  return WS_TRUE;
81  }
82 
83  if (stream->buffer_pos >= stream->data_in_buffer) {
84  /* Read more data to the buffer. */
85  stream->buffer_pos = 0;
86  stream->data_in_buffer = (*stream->io)(stream->context,
87  stream->buffer,
89  if (stream->data_in_buffer == 0)
90  /* EOF reached. */
91  return WS_FALSE;
92  }
93 
94  /* Return the next character. */
95  *ch_return = stream->buffer[stream->buffer_pos++];
96 
97  return WS_TRUE;
98 }
99 
100 
102 {
103  stream->ungetch = ch;
104  stream->ungetch_valid = WS_TRUE;
105 }
106 
107 
109 {
110  if (stream->flush)
111  return (*stream->flush)(stream->context);
112 
113  return WS_TRUE;
114 }
115 
116 
118 {
119  if (stream->close)
120  (*stream->close)(stream->context);
121 
122  ws_free(stream);
123 }
124 
125 
128 {
129  WsStream *stream = ws_calloc(1, sizeof(*stream));
130 
131  if (stream == NULL)
132  return NULL;
133 
134  stream->io = io;
135  stream->flush = flush;
136  stream->close = close;
137  stream->context = context;
138 
139  return stream;
140 }
void * ws_calloc(size_t num, size_t size)
Definition: wsalloc.c:83
WsUInt32 buffer[WS_STREAM_BUFFER_SIZE]
Definition: wsstream.h:109
Definition: wsint.h:131
Definition: parse.c:65
unsigned long WsUInt32
Definition: wsint.h:122
#define WS_STREAM_BUFFER_SIZE
Definition: wsstream.h:78
void ws_free(void *ptr)
Definition: wsalloc.c:139
WsBool ws_stream_getc(WsStream *stream, WsUInt32 *ch_return)
Definition: wsstream.c:74
WsBool ungetch_valid
Definition: wsstream.h:114
size_t data_in_buffer
Definition: wsstream.h:111
WsStreamIOProc io
Definition: wsstream.h:101
WsStream * ws_stream_new(void *context, WsStreamIOProc io, WsStreamFlushProc flush, WsStreamCloseProc close)
Definition: wsstream.c:126
size_t buffer_pos
Definition: wsstream.h:110
void ws_stream_close(WsStream *stream)
Definition: wsstream.c:117
WsStreamCloseProc close
Definition: wsstream.h:103
void ws_stream_ungetc(WsStream *stream, WsUInt32 ch)
Definition: wsstream.c:101
void * context
Definition: wsstream.h:106
size_t(* WsStreamIOProc)(void *context, WsUInt32 *buf, size_t buflen)
Definition: wsstream.h:87
WsStreamFlushProc flush
Definition: wsstream.h:102
WsBool ws_stream_flush(WsStream *stream)
Definition: wsstream.c:108
WsBool
Definition: wsint.h:128
WsUInt32 ungetch
Definition: wsstream.h:115
void(* WsStreamCloseProc)(void *context)
Definition: wsstream.h:95
WsBool(* WsStreamFlushProc)(void *context)
Definition: wsstream.h:92
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.