Line data Source code
1 : /* File: universal_memory_output_stream.c; Copyright and License: see below */
2 :
3 : #include "u8stream/universal_memory_output_stream.h"
4 : #include "u8stream/universal_output_stream_if.h"
5 : #include "u8/u8_trace.h"
6 : #include "u8/u8_log.h"
7 : #include <string.h>
8 : #include <assert.h>
9 :
10 : /* the vmt implementing the interface */
11 : static const universal_output_stream_if_t universal_memory_output_stream_private_if
12 : = {
13 : .write = (u8_error_t (*)(universal_output_stream_impl_t*, const void*, size_t)) &universal_memory_output_stream_write,
14 : .flush = (u8_error_t (*)(universal_output_stream_impl_t*)) &universal_memory_output_stream_flush
15 : };
16 :
17 1255 : void universal_memory_output_stream_init ( universal_memory_output_stream_t *this_,
18 : void* mem_buf_start,
19 : size_t mem_buf_size,
20 : universal_memory_output_stream_0term_t mode )
21 : {
22 1255 : U8_TRACE_BEGIN();
23 1255 : assert( mem_buf_start != NULL );
24 :
25 1255 : (*this_).mem_buf_start = mem_buf_start;
26 1255 : (*this_).mem_buf_size = mem_buf_size;
27 1255 : (*this_).mem_buf_filled = 0;
28 1255 : (*this_).mode = mode;
29 1255 : universal_output_stream_private_init( &((*this_).output_stream), &universal_memory_output_stream_private_if, this_ );
30 :
31 1255 : U8_TRACE_END();
32 1255 : }
33 :
34 1248 : u8_error_t universal_memory_output_stream_destroy( universal_memory_output_stream_t *this_ )
35 : {
36 1248 : U8_TRACE_BEGIN();
37 :
38 1248 : const u8_error_t err = universal_memory_output_stream_flush( this_ );
39 :
40 1248 : (*this_).mem_buf_start = NULL;
41 1248 : (*this_).mem_buf_size = 0;
42 1248 : (*this_).mem_buf_filled = 0;
43 1248 : universal_output_stream_private_destroy( &((*this_).output_stream) );
44 :
45 1248 : U8_TRACE_END_ERR(err);
46 1248 : return err;
47 : }
48 :
49 2080 : u8_error_t universal_memory_output_stream_reset ( universal_memory_output_stream_t *this_ )
50 : {
51 2080 : U8_TRACE_BEGIN();
52 2080 : assert( (*this_).mem_buf_start != NULL );
53 2080 : const u8_error_t err = U8_ERROR_NONE;
54 :
55 2080 : (*this_).mem_buf_filled = 0;
56 :
57 2080 : U8_TRACE_END_ERR(err);
58 2080 : return err;
59 : }
60 :
61 14051 : u8_error_t universal_memory_output_stream_write ( universal_memory_output_stream_t *this_, const void *start, size_t length )
62 : {
63 : /*U8_TRACE_BEGIN();*/
64 14051 : assert( start != NULL );
65 14051 : assert( (*this_).mem_buf_start != NULL );
66 14051 : u8_error_t err = U8_ERROR_NONE;
67 :
68 14051 : const size_t space_left = ( (*this_).mem_buf_size - (*this_).mem_buf_filled );
69 14051 : char *const buf_first_free = &( (*( (char(*)[])(*this_).mem_buf_start ))[(*this_).mem_buf_filled] );
70 14051 : if ( length <= space_left )
71 : {
72 13991 : memcpy( buf_first_free, start, length );
73 13991 : (*this_).mem_buf_filled += length;
74 : }
75 : else
76 : {
77 60 : U8_TRACE_BEGIN();
78 60 : memcpy( buf_first_free, start, space_left );
79 60 : (*this_).mem_buf_filled += space_left;
80 60 : U8_TRACE_INFO_INT( "not all bytes could be written. missing:", length-space_left );
81 60 : err = U8_ERROR_AT_FILE_WRITE;
82 60 : U8_TRACE_END_ERR(err);
83 : }
84 :
85 : /*U8_TRACE_END_ERR(err);*/
86 14051 : return err;
87 : }
88 :
89 15305 : u8_error_t universal_memory_output_stream_flush( universal_memory_output_stream_t *this_ )
90 : {
91 15305 : U8_TRACE_BEGIN();
92 15305 : assert( (*this_).mem_buf_start != NULL );
93 15305 : u8_error_t err = U8_ERROR_NONE;
94 :
95 15305 : switch( (*this_).mode )
96 : {
97 2 : case UNIVERSAL_MEMORY_OUTPUT_STREAM_0TERM_BYTE:
98 : {
99 2 : err = universal_memory_output_stream_private_write_0term( this_, false );
100 : }
101 2 : break;
102 :
103 15302 : case UNIVERSAL_MEMORY_OUTPUT_STREAM_0TERM_UTF8:
104 : {
105 15302 : err = universal_memory_output_stream_private_write_0term( this_, true );
106 : }
107 15302 : break;
108 :
109 1 : default:
110 : case UNIVERSAL_MEMORY_OUTPUT_STREAM_0TERM_NONE:
111 : {
112 : /* no 0term to be appended */
113 : }
114 1 : break;
115 : }
116 :
117 15305 : U8_TRACE_END_ERR(err);
118 15305 : return err;
119 : }
120 :
121 1251 : universal_output_stream_t* universal_memory_output_stream_get_output_stream( universal_memory_output_stream_t *this_ )
122 : {
123 1251 : U8_TRACE_BEGIN();
124 :
125 1251 : universal_output_stream_t* result = &((*this_).output_stream);
126 :
127 1251 : U8_TRACE_END();
128 1251 : return result;
129 : }
130 :
131 :
132 : /*
133 : Copyright 2020-2025 Andreas Warnke
134 :
135 : Licensed under the Apache License, Version 2.0 (the "License");
136 : you may not use this file except in compliance with the License.
137 : You may obtain a copy of the License at
138 :
139 : http://www.apache.org/licenses/LICENSE-2.0
140 :
141 : Unless required by applicable law or agreed to in writing, software
142 : distributed under the License is distributed on an "AS IS" BASIS,
143 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144 : See the License for the specific language governing permissions and
145 : limitations under the License.
146 : */
|