Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_writer.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Adaptive Framework Helpers for interfaces afw_writer*
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw_internal.h"
15 
16 
17 
18 /* FIXME Add include for headers and anything else you want in this part of code. */
19 
20 /* Declares and rti/inf defines for interface afw_writer */
21 #define AFW_IMPLEMENTATION_ID "afw_writer_fd"
23 
24 
25 /* FIXME If you prefer, you can move this struct to a .h file. */
26 /* Self typedef for afw_writer_fd implementation of afw_writer. */
27 typedef struct
29  afw_writer_t pub;
30  FILE *fd;
31  int tab_size;
32  afw_boolean_t close_on_release;
34 
35 
36 /*
37  * Implementation of method release for interface afw_writer.
38  */
39 void
40 impl_afw_writer_release(
41  const afw_writer_t *instance,
42  afw_xctx_t *xctx)
43 {
46 
47  if (self->close_on_release) {
48  fclose(self->fd);
49  }
50 }
51 
52 /*
53  * Implementation of method flush for interface afw_writer.
54  */
55 void
56 impl_afw_writer_flush(
57  const afw_writer_t *instance,
58  afw_xctx_t *xctx)
59 {
60  /* Ignore flush. */
61 }
62 
63 /*
64  * Implementation of method write for interface afw_writer.
65  */
66 void
68  const afw_writer_t *instance,
69  const void *buffer,
70  afw_size_t size,
71  afw_xctx_t *xctx)
72 {
75  size_t len;
76 
78  if (size != 0) {
79  len = fwrite(buffer, size, 1, self->fd);
80  if (len == 0) {
81  AFW_THROW_ERROR_RV_Z(general, NULL, ferror(self->fd),
82  "fwrite() failed",
83  xctx);
84  }
85  }
86 }
87 
88 /*
89  * Implementation of method write_eol for interface afw_writer.
90  */
91 void
93  const afw_writer_t *instance,
94  afw_xctx_t *xctx)
95 {
98  size_t len;
99 
101  len = fwrite("\n", 1, 1, self->fd);
102  if (len == 0) {
103  AFW_THROW_ERROR_RV_Z(general, NULL, ferror(self->fd),
104  "fwrite() failed",
105  xctx);
106  }
107 }
108 
109 /*
110  * Implementation of method increment_indent for interface afw_writer.
111  */
112 void
113 impl_afw_writer_increment_indent(
114  const afw_writer_t *instance,
115  afw_xctx_t *xctx)
116 {
119 
120  (self->pub.indent)++;
121 }
122 
123 /*
124  * Implementation of method decrement_indent for interface afw_writer.
125  */
126 void
127 impl_afw_writer_decrement_indent(
128  const afw_writer_t *instance,
129  afw_xctx_t *xctx)
130 {
133 
134  if (self->pub.indent == 0) {
135  AFW_THROW_ERROR_Z(general,
136  "afw_writer_decrement_indent() call when indent is already 0",
137  xctx);
138  }
139  (self->pub.indent)--;
140 }
141 
142 
143 
144 /* Call afw_writer_write() with an integer. */
145 AFW_DEFINE(void)
147  const afw_writer_t *writer,
148  afw_integer_t integer,
149  afw_xctx_t *xctx)
150 {
151  char buf[AFW_INTEGER_MAX_BUFFER];
152 
153  snprintf(buf, AFW_INTEGER_MAX_BUFFER, "%" AFW_INTEGER_FMT,
154  integer);
155  afw_writer_write_z(writer, buf, xctx);
156 }
157 
158 
159 
160 /* Call afw_writer_write() with a size. */
161 AFW_DEFINE(void)
163  const afw_writer_t *writer,
164  afw_size_t size,
165  afw_xctx_t *xctx)
166 {
167  char buf[AFW_SIZE_T_MAX_BUFFER];
168 
169  snprintf(buf, AFW_SIZE_T_MAX_BUFFER, "%" AFW_SIZE_T_FMT,
170  size);
171  afw_writer_write_z(writer, buf, xctx);
172 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
Interface afw_interface implementation declares.
#define AFW_SIZE_T_MAX_BUFFER
this is the maximum number of digits that can be produced by afw_size_t plus null terminator.
Definition: afw_common.h:362
#define AFW_INTEGER_FMT
Format string specifier used for afw_integer_t.
Definition: afw_common.h:326
_Bool afw_boolean_t
Definition: afw_common.h:373
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
#define AFW_SIZE_T_FMT
Format string specifier used for afw_size_t.
Definition: afw_common.h:341
#define AFW_INTEGER_MAX_BUFFER
this is the maximum number of digits that can be produced by afw_integer_t plus negative sign plus nu...
Definition: afw_common.h:275
apr_int64_t afw_integer_t
typedef for big signed int.
Definition: afw_common.h:321
#define AFW_THROW_ERROR_RV_Z(code, rv_source_id, rv, message_z, xctx)
Macro used to set error and rv in xctx and throw it.
Definition: afw_error.h:301
#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
void impl_afw_writer_write(const afw_writer_t *instance, const void *buffer, afw_size_t size, afw_xctx_t *xctx)
Definition: afw_writer.c:67
void impl_afw_writer_write_eol(const afw_writer_t *instance, afw_xctx_t *xctx)
Definition: afw_writer.c:92
afw_writer_write_integer(const afw_writer_t *writer, afw_integer_t integer, afw_xctx_t *xctx)
Call afw_writer_write() with an integer.
Definition: afw_writer.c:146
#define afw_writer_write_z(writer, s_z, xctx)
Call afw_writer_write() with zero terminated string.
Definition: afw_writer.h:35
afw_writer_write_size(const afw_writer_t *writer, afw_size_t size, afw_xctx_t *xctx)
Call afw_writer_write() with an size.
Definition: afw_writer.c:162
Interface afw_writer public struct.
Interface afw_xctx public struct.