7
ptr_t retval = malloc (sz);
9
memset (retval, 0, sz);
15
string_vacuum (str_t s) {
21
for (auto c = s[i]; c != '\0'; c = s[i++]) {
22
if (c != ' ' && c != '\n' && c != '\t' && c != '\r') {
27
out_buff = realloc (s, strlen (s) + 1);
32
string_concat_list_nl (str_t* list) {
34
str_t buff = malloc0 (strlen (*list) * sizeof (list));
36
err_print ("buffer is for some reason Null, this is an error.\n");
39
for (str_t s = *list; s != NULL; s = *++list) {
40
if (sizeof (buff) <= strlen (buff) + strlen (s) + 1 ) {
41
size_t next_size = strlen (buff) + strlen (s) + 1;
42
fprintf (stdout, "nz: %zu\n", next_size);
43
tmp = realloc (buff, next_size);
44
if (tmp == NULL || errno == ENOMEM) {
51
buff = string_vacuum (buff);
62
err_print (str_t fmt, ...) {
65
vfprintf (stderr, fmt, vlst);
70
/* ************************************************************************** */
79
byte_stream_new (size_t length) {
80
ByteStream * retval = malloc0 (sizeof (ByteStream));
83
err_print ("Something went wrong with allocating the ByteStream. Have you run out of memory?\n");
87
retval->bytes = malloc0 (length);
88
if (retval->bytes == NULL) {
89
err_print ("Cound not allocate ByteStream buffer. Have you run out of memory?\n");
93
retval->length = length;
105
byte_stream_new_from_file (UNUSED FILE * f) {
112
byte_stream_seek (ByteStream * bstream, long int offset, SEEK_OFFSET origin) {
114
case (SEEK_OFFSET_SET):
115
assert (offset > 0 && "Offset must be positive when using SEEK_OFFSET_SET.");
116
bstream->current = offset;
118
case (SEEK_OFFSET_CUR):
119
assert (((long) bstream->length >= (bstream->current + offset)) &&
120
((bstream->current + offset) >= 0) &&
121
"Trying to seek outside range valid range.");
122
bstream->current += offset;
124
case (SEEK_OFFSET_END):
125
assert (offset < 0 && "Offset must be negative when using SEEK_OFFSET_END.");
126
bstream->current = bstream->length - offset;
129
assert (false && "How did you end up here?");
134
byte_stream_read (ByteStream * bstream, OUT ptr_t ptr, size_t size, size_t count) {
135
/* Based on the Minix implementation: https://stackoverflow.com/a/8590471 */
137
byte * bytes = bstream->bytes;
138
size_t length = bstream->length;
143
// Set the pointer correctly.
144
bytes += bstream->current;
147
while (node < count) {
150
if ((s + (node * size)) <= length) {
166
byte_stream_write (ByteStream * bstream, IN ptr_t ptr, size_t size, size_t count) {
168
byte * bytes = bstream->bytes;
172
size_t length = bstream->length;
173
size_t read_length = size * count;
174
size_t current = bstream->current;
177
if ((read_length + current) >= length) {
178
ptr_t tmp = realloc (bstream->bytes, read_length + current);
180
err_print ("Could not reallocate ByteStream buffer. Have you run out of memory?\n");
184
else bstream->bytes = tmp;
188
while (node < count) {
191
if ((s + (node * size)) <= read_length) {
207
byte_stream_length (ByteStream * bstream) {
208
return bstream->length;