Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_utf8_stream.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Adaptive Framework Value UTF-8 Stream
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw_internal.h"
15 
16 
17 
18 typedef struct {
19  afw_stream_t pub;
20  apr_array_header_t *ary;
21  afw_boolean_t needs_leading_white_space;
23 
24 
25 #define AFW_IMPLEMENTATION_ID "afw_utf8_stream"
27 
28 /*
29  * Implementation of method write_eol for interface afw_stream.
30  */
31 static afw_size_t
32 impl_afw_stream_write_cb(
33  void *context,
34  const void *buffer,
35  afw_size_t size,
36  const afw_pool_t *p,
37  afw_xctx_t *xctx)
38 {
40  afw_size_t i;
41 
42  for (i = 0; i < size; i++) {
43  APR_ARRAY_PUSH(self->ary, char) =
44  ((const afw_octet_t *)buffer)[i];
45  }
46  return size;
47 }
48 
49 
50 /* Create UTF-8 stream */
51 AFW_DEFINE(const afw_stream_t *)
53  const afw_utf8_t *streamId,
54  const afw_pool_t *p,
55  afw_xctx_t *xctx)
56 {
58 
59  /* Create self and ary in its own pool. */
60  p = afw_pool_create(p, xctx);
61  self = afw_pool_calloc_type(p,
63  xctx);
64  self->pub.inf = &impl_afw_stream_inf;
65  self->pub.p = p;
66  self->pub.streamId = streamId;
67  self->pub.write_cb = impl_afw_stream_write_cb;
68  self->ary = apr_array_make(afw_pool_get_apr_pool(p), 4000, 1);
69 
70  /* Return new instance. */
71  return (const afw_stream_t *)self;
72 }
73 
74 
75 
76 /* Get the current string in a UTF-8 stream. */
77 AFW_DEFINE(void)
79  const afw_stream_t *stream,
80  afw_utf8_t *current_cached_string,
81  afw_xctx_t *xctx)
82 {
84 
85  if (stream->inf != &impl_afw_stream_inf) {
86  AFW_THROW_ERROR_Z(general,
87  "afw_utf8_stream_get_current_cached_string() can only be called "
88  "by stream created by afw_utf8_stream_create()", xctx);
89  }
90  current_cached_string->s = self->ary->elts;
91  current_cached_string->len = self->ary->nelts;
92 }
93 
94 
95 /*
96  * Implementation of method release for interface afw_stream.
97  */
98 void
99 impl_afw_stream_release(
100  const afw_stream_t * instance,
101  afw_xctx_t *xctx)
102 {
104  (impl_utf8_stream_self_t *)instance;
105 
106  /* Release stream resources. */
107  afw_pool_release(self->pub.p, xctx);
108 }
109 
110 /*
111  * Implementation of method flush for interface afw_stream.
112  */
113 void
114 impl_afw_stream_flush(
115  const afw_stream_t *instance,
116  afw_xctx_t *xctx)
117 {
118  /* Ignore flush. */
119 }
120 
121 /*
122  * Implementation of method read for interface afw_stream.
123  */
124 void
126  const afw_stream_t *instance,
127  const void *buffer,
128  afw_size_t size,
129  afw_xctx_t *xctx)
130 {
132  AFW_THROW_ERROR_Z(general, "Method not implemented.", xctx);
133 
134 }
135 
136 /*
137  * Implementation of method write for interface afw_stream.
138  */
139 void
141  const afw_stream_t * instance,
142  const void * buffer,
143  afw_size_t size,
144  afw_xctx_t *xctx)
145 {
147  (impl_utf8_stream_self_t *)instance;
148  const afw_octet_t *c;
149  afw_size_t count;
150 
151  if (size == 0) {
152  return;
153  }
154 
155  c = (const afw_octet_t *)buffer;
156  for (count = 0; count < size; count++) {
157  APR_ARRAY_PUSH(self->ary, char) = *c++;
158  }
159 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
Interface afw_interface implementation declares.
_Bool afw_boolean_t
Definition: afw_common.h:373
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
unsigned char afw_octet_t
8 bits (unsigned).
Definition: afw_common.h:211
#define AFW_THROW_ERROR_Z(code, message_z, xctx)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:283
#define afw_pool_get_apr_pool(instance)
Call method get_apr_pool of interface afw_pool.
#define afw_pool_release(instance, xctx)
Call method release of interface afw_pool.
#define afw_pool_calloc_type(instance, type, xctx)
Macro to allocate cleared memory to hold type in pool.
Definition: afw_pool.h:167
const afw_pool_t * afw_pool_create(const afw_pool_t *parent, afw_xctx_t *xctx)
Create a new pool.
void impl_afw_stream_write(const afw_stream_t *instance, const void *buffer, afw_size_t size, afw_xctx_t *xctx)
void impl_afw_stream_read(const afw_stream_t *instance, const void *buffer, afw_size_t size, afw_xctx_t *xctx)
afw_utf8_stream_get_current_cached_string(const afw_stream_t *stream, afw_utf8_t *current_cached_string, afw_xctx_t *xctx)
Get the current string in a UTF-8 writer.
afw_utf8_stream_create(const afw_utf8_t *streamId, const afw_pool_t *p, afw_xctx_t *xctx)
Create UTF-8 stream.
Interface afw_pool public struct.
Interface afw_stream public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_xctx public struct.