UniMRCP  1.7.0
apt_text_stream.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2015 Arsen Chaloyan
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef APT_TEXT_STREAM_H
18 #define APT_TEXT_STREAM_H
19 
20 /**
21  * @file apt_text_stream.h
22  * @brief Text Stream Parse/Generate Routine
23  */
24 
25 #include "apt_string_table.h"
26 #include "apt_pair.h"
27 
29 
30 /** Space */
31 #define APT_TOKEN_SP 0x20
32 /** Horizontal tab */
33 #define APT_TOKEN_HTAB 0x09
34 /** Carrige return */
35 #define APT_TOKEN_CR 0x0D
36 /** Line feed */
37 #define APT_TOKEN_LF 0x0A
38 
39 /** Text stream declaration */
41 
42 /** Text stream is used for message parsing and generation */
44  /** Text stream */
46 
47  /** Current position in the stream */
48  char *pos;
49  /** End of stream pointer */
50  const char *end;
51  /** Is end of stream reached */
53 };
54 
55 
56 /**
57  * Read entire line of the text stream.
58  * @param stream the text stream to navigate on
59  * @param line the read line to return
60  * @return TRUE if the line is successfully read, otherwise FALSE
61  * @remark To be used to navigate through the lines of the text stream (message).
62  */
64 
65 /**
66  * Read header field (name-value pair) of the text stream by scanning entire line.
67  * @param stream the text stream to navigate
68  * @param pair the read pair to return
69  * @return TRUE if the header is successfully read, otherwise FALSE
70  * @remark To be used to navigate through the lines and read header fields
71  * (name:value pairs) of the text stream (message).
72  */
74 
75 /**
76  * Read the field terminated with specified separator.
77  * @param stream the text stream to navigate
78  * @param separator the field separator
79  * @param skip_spaces whether to skip spaces or not
80  * @param field the read field to return
81  * @return TRUE if the read field isn't empty, otherwise FALSE
82  * @remark To be used to navigate through the fields of the text stream (message).
83  */
84 APT_DECLARE(apt_bool_t) apt_text_field_read(apt_text_stream_t *stream, char separator, apt_bool_t skip_spaces, apt_str_t *field);
85 
86 /** Generate name-value pair line */
88 
89 /** Generate only the name ("name:") of the header field */
91 
92 /** Parse array of name-value pairs */
93 APT_DECLARE(apt_bool_t) apt_pair_array_parse(apt_pair_arr_t *arr, const apt_str_t *value, apr_pool_t *pool);
94 /** Generate array of name-value pairs */
95 APT_DECLARE(apt_bool_t) apt_pair_array_generate(const apt_pair_arr_t *arr, apt_str_t *str, apr_pool_t *pool);
96 
97 
98 /** Parse boolean-value */
100 /** Generate apr_size_t value from pool (buffer is allocated from pool) */
102 
103 /** Parse apr_size_t value */
104 APT_DECLARE(apr_size_t) apt_size_value_parse(const apt_str_t *str);
105 /** Generate apr_size_t value from pool (buffer is allocated from pool) */
106 APT_DECLARE(apt_bool_t) apt_size_value_generate(apr_size_t value, apt_str_t *str, apr_pool_t *pool);
107 
108 /** Insert apr_size_t value */
110 
111 /** Parse float value */
112 APT_DECLARE(float) apt_float_value_parse(const apt_str_t *str);
113 /** Generate float value (buffer is allocated from pool) */
114 APT_DECLARE(apt_bool_t) apt_float_value_generate(float value, apt_str_t *str, apr_pool_t *pool);
115 
116 /** Insert float value */
118 /** Insert string value */
120 
121 /** Reset navigation related data of the text stream */
122 static APR_INLINE void apt_text_stream_reset(apt_text_stream_t *stream)
123 {
124  stream->pos = stream->text.buf;
125  stream->end = stream->text.buf + stream->text.length;
126  stream->is_eos = FALSE;
127 }
128 
129 /** Initialize text stream */
130 static APR_INLINE void apt_text_stream_init(apt_text_stream_t *stream, char *buffer, apr_size_t size)
131 {
132  stream->text.buf = buffer;
133  stream->text.length = size;
134  apt_text_stream_reset(stream);
135 }
136 
137 /** Insert end of the line symbol(s) */
138 static APR_INLINE apt_bool_t apt_text_eol_insert(apt_text_stream_t *stream)
139 {
140  if(stream->pos + 2 < stream->end) {
141  *stream->pos++ = APT_TOKEN_CR;
142  *stream->pos++ = APT_TOKEN_LF;
143  return TRUE;
144  }
145  return FALSE;
146 }
147 
148 /** Insert character */
149 static APR_INLINE apt_bool_t apt_text_char_insert(apt_text_stream_t *stream, char ch)
150 {
151  if(stream->pos + 1 < stream->end) {
152  *stream->pos++ = ch;
153  return TRUE;
154  }
155  return FALSE;
156 }
157 
158 /** Insert space */
159 static APR_INLINE apt_bool_t apt_text_space_insert(apt_text_stream_t *stream)
160 {
161  return apt_text_char_insert(stream,APT_TOKEN_SP);
162 }
163 
164 /** Insert space */
165 static APR_INLINE apt_bool_t apt_text_htab_insert(apt_text_stream_t *stream)
166 {
167  return apt_text_char_insert(stream,APT_TOKEN_HTAB);
168 }
169 
170 /** Check whether specified character is a white space (WSP = SP / HTAB) */
171 static APR_INLINE apt_bool_t apt_text_is_wsp(char ch)
172 {
173  return (ch == APT_TOKEN_SP || ch == APT_TOKEN_HTAB) ? TRUE : FALSE;
174 }
175 
176 /** Skip sequence of spaces */
177 static APR_INLINE void apt_text_spaces_skip(apt_text_stream_t *stream)
178 {
179  while(stream->pos < stream->end && *stream->pos == APT_TOKEN_SP)
180  stream->pos++;
181 }
182 
183 /** Skip sequence of white spaces (WSP = SP / HTAB) */
184 static APR_INLINE void apt_text_white_spaces_skip(apt_text_stream_t *stream)
185 {
186  while(stream->pos < stream->end && apt_text_is_wsp(*stream->pos) == TRUE)
187  stream->pos++;
188 }
189 
190 /** Skip specified character */
191 static APR_INLINE void apt_text_char_skip(apt_text_stream_t *stream, char ch)
192 {
193  if(stream->pos < stream->end && *stream->pos == ch) stream->pos++;
194 }
195 
196 /** Skip sequence of specified characters */
197 static APR_INLINE void apt_text_chars_skip(apt_text_stream_t *stream, char ch)
198 {
199  while(stream->pos < stream->end && *stream->pos == ch) stream->pos++;
200 }
201 
202 /** Skip to specified character */
203 static APR_INLINE void apt_text_skip_to_char(apt_text_stream_t *stream, char ch)
204 {
205  while(stream->pos < stream->end && *stream->pos != ch) stream->pos++;
206 }
207 
208 /** Check whether end of stream is reached */
209 static APR_INLINE apt_bool_t apt_text_is_eos(const apt_text_stream_t *stream)
210 {
211  return (stream->pos >= stream->end || stream->is_eos == TRUE) ? TRUE : FALSE;
212 }
213 
214 /** Scroll text stream */
216 
217 /** Parse id at resource string */
218 APT_DECLARE(apt_bool_t) apt_id_resource_parse(const apt_str_t *str, char separator, apt_str_t *id, apt_str_t *resource, apr_pool_t *pool);
219 /** Generate id at resource string */
220 APT_DECLARE(apt_bool_t) apt_id_resource_generate(const apt_str_t *id, const apt_str_t *resource, char separator, apt_str_t *str, apr_pool_t *pool);
221 
222 /** Generate value plus the length (number of digits) of the value itself */
223 APT_DECLARE(apt_bool_t) apt_var_length_value_generate(apr_size_t *value, apr_size_t max_count, apt_str_t *str);
224 
225 /** Generate completion-cause */
226 APT_DECLARE(apt_bool_t) apt_completion_cause_generate(const apt_str_table_item_t table[], apr_size_t size, apr_size_t cause, apt_str_t *str, apr_pool_t *pool);
227 
228 /**
229  * Generate unique identifier (hex string)
230  * @param id the id to generate
231  * @param length the length of hex string to generate
232  * @param pool the pool to allocate memory from
233  */
234 APT_DECLARE(apt_bool_t) apt_unique_id_generate(apt_str_t *id, apr_size_t length, apr_pool_t *pool);
235 
236 
238 
239 #endif /* APT_TEXT_STREAM_H */
apr_array_header_t apt_pair_arr_t
Definition: apt_pair.h:42
Definition: apt_pair.h:34
Definition: apt_text_stream.h:43
apt_bool_t apt_id_resource_parse(const apt_str_t *str, char separator, apt_str_t *id, apt_str_t *resource, apr_pool_t *pool)
apr_size_t apt_size_value_parse(const apt_str_t *str)
#define APT_END_EXTERN_C
Definition: apt.h:38
apt_bool_t apt_id_resource_generate(const apt_str_t *id, const apt_str_t *resource, char separator, apt_str_t *str, apr_pool_t *pool)
#define APT_TOKEN_CR
Definition: apt_text_stream.h:35
int apt_bool_t
Definition: apt.h:57
apt_bool_t apt_text_string_insert(apt_text_stream_t *stream, const apt_str_t *str)
apt_bool_t apt_completion_cause_generate(const apt_str_table_item_t table[], apr_size_t size, apr_size_t cause, apt_str_t *str, apr_pool_t *pool)
Generic String Table.
apt_bool_t is_eos
Definition: apt_text_stream.h:52
apt_bool_t apt_text_field_read(apt_text_stream_t *stream, char separator, apt_bool_t skip_spaces, apt_str_t *field)
apt_bool_t apt_pair_array_generate(const apt_pair_arr_t *arr, apt_str_t *str, apr_pool_t *pool)
apt_bool_t apt_text_float_value_insert(apt_text_stream_t *stream, float value)
#define APT_TOKEN_LF
Definition: apt_text_stream.h:37
apt_bool_t apt_text_line_read(apt_text_stream_t *stream, apt_str_t *line)
apt_bool_t apt_text_header_read(apt_text_stream_t *stream, apt_pair_t *pair)
apr_size_t length
Definition: apt_string.h:40
#define APT_BEGIN_EXTERN_C
Definition: apt.h:36
#define APT_DECLARE(type)
Definition: apt.h:53
apt_bool_t apt_boolean_value_parse(const apt_str_t *str, apt_bool_t *value)
char * pos
Definition: apt_text_stream.h:48
apt_bool_t apt_pair_array_parse(apt_pair_arr_t *arr, const apt_str_t *value, apr_pool_t *pool)
apt_bool_t apt_text_size_value_insert(apt_text_stream_t *stream, apr_size_t value)
apt_bool_t apt_text_stream_scroll(apt_text_stream_t *stream)
apt_bool_t apt_var_length_value_generate(apr_size_t *value, apr_size_t max_count, apt_str_t *str)
apt_bool_t apt_unique_id_generate(apt_str_t *id, apr_size_t length, apr_pool_t *pool)
#define APT_TOKEN_HTAB
Definition: apt_text_stream.h:33
Definition: apt_string.h:36
Generic Name-Value Pair.
#define APT_TOKEN_SP
Definition: apt_text_stream.h:31
apt_bool_t apt_size_value_generate(apr_size_t value, apt_str_t *str, apr_pool_t *pool)
apt_bool_t apt_boolean_value_generate(apt_bool_t value, apt_str_t *str, apr_pool_t *pool)
apt_str_t text
Definition: apt_text_stream.h:45
const char * end
Definition: apt_text_stream.h:50
apt_bool_t apt_text_header_name_insert(apt_text_stream_t *stream, const apt_str_t *name)
apt_bool_t apt_text_name_value_insert(apt_text_stream_t *stream, const apt_str_t *name, const apt_str_t *value)
float apt_float_value_parse(const apt_str_t *str)
apt_bool_t apt_float_value_generate(float value, apt_str_t *str, apr_pool_t *pool)
char * buf
Definition: apt_string.h:38
Definition: apt_string_table.h:34