/+junk/codegen

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/codegen

« back to all changes in this revision

Viewing changes to src/utils.c

  • Committer: Gustav Hartvigsson
  • Date: 2022-10-19 21:06:55 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20221019210655-9iqu2dhjbpvedoma
Beep Boop, nothin yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "utils.h"
 
2
 
 
3
 
 
4
 
 
5
ptr_t
 
6
malloc0 (size_t sz) {
 
7
  ptr_t retval = malloc (sz);
 
8
  if (retval != NULL) {
 
9
    memset (retval, 0, sz);
 
10
  }
 
11
  return retval;
 
12
 
13
 
 
14
str_t
 
15
string_vacuum (str_t s) {
 
16
  str_t out_buff;
 
17
  int index, i;
 
18
 
 
19
  index = -1;
 
20
  i = 0;
 
21
  for (auto c = s[i]; c != '\0'; c = s[i++]) {
 
22
    if (c != ' ' && c != '\n' && c != '\t' && c != '\r') {
 
23
        index = i;
 
24
    }
 
25
  }
 
26
  s[index + 1] = '\0';
 
27
  out_buff = realloc (s, strlen (s) + 1);
 
28
  return out_buff;
 
29
}
 
30
 
 
31
str_t
 
32
string_concat_list_nl (str_t* list) {
 
33
  ptr_t tmp;
 
34
  str_t buff = malloc0 (strlen (*list) * sizeof (list));
 
35
  if (buff == NULL) {
 
36
    err_print ("buffer is for some reason Null, this is an error.\n");
 
37
    goto error1;
 
38
  }
 
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) {
 
45
        goto error2;
 
46
      }
 
47
    }
 
48
    strcat (buff, "\n");
 
49
    strcat (buff, s);
 
50
  }
 
51
  buff = string_vacuum (buff);
 
52
  return buff;
 
53
 
 
54
error2:
 
55
  free (tmp);
 
56
error1:
 
57
  free (buff);
 
58
  return NULL;
 
59
}
 
60
 
 
61
void
 
62
err_print (str_t fmt, ...) {
 
63
  va_list vlst;
 
64
  va_start (vlst, fmt);
 
65
  vfprintf (stderr, fmt, vlst);
 
66
  va_end (vlst);
 
67
}
 
68
 
 
69
 
 
70
/* ************************************************************************** */
 
71
 
 
72
struct ByteStream {
 
73
  byte * bytes;
 
74
  size_t length;
 
75
  long int current;
 
76
};
 
77
 
 
78
ByteStream *
 
79
byte_stream_new (size_t length) {
 
80
  ByteStream * retval = malloc0 (sizeof (ByteStream));
 
81
  
 
82
  if (retval == NULL) {
 
83
    err_print ("Something went wrong with allocating the ByteStream. Have you run out of memory?\n");
 
84
    goto error1;
 
85
  }
 
86
 
 
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");
 
90
    goto error2;
 
91
  }
 
92
 
 
93
  retval->length = length;
 
94
  retval->current = 0;
 
95
 
 
96
  return retval;
 
97
 
 
98
error2:
 
99
  free (retval);
 
100
error1:
 
101
  return NULL;
 
102
}
 
103
 
 
104
ByteStream *
 
105
byte_stream_new_from_file (UNUSED FILE * f) {
 
106
  NOT_IMPLEMENTED;
 
107
  return NULL;
 
108
}
 
109
 
 
110
 
 
111
void
 
112
byte_stream_seek (ByteStream * bstream, long int offset, SEEK_OFFSET origin) {
 
113
  switch (origin) {
 
114
    case (SEEK_OFFSET_SET):
 
115
      assert (offset > 0 && "Offset must be positive when using SEEK_OFFSET_SET.");
 
116
      bstream->current = offset;
 
117
      return;
 
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;
 
123
      return;
 
124
    case (SEEK_OFFSET_END):
 
125
      assert (offset < 0 && "Offset must be negative when using SEEK_OFFSET_END.");
 
126
      bstream->current = bstream->length - offset;
 
127
      return;
 
128
    default:
 
129
      assert (false && "How did you end up here?");
 
130
  }
 
131
}
 
132
 
 
133
size_t
 
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 */
 
136
  byte * cp = ptr;
 
137
  byte * bytes = bstream->bytes;
 
138
  size_t length = bstream->length;
 
139
  int c;
 
140
  size_t node = 0;
 
141
  size_t s;
 
142
 
 
143
  // Set the pointer correctly.
 
144
  bytes += bstream->current;
 
145
 
 
146
  if (size) {
 
147
    while (node < count) {
 
148
      s = 0;
 
149
      do {
 
150
        if ((s + (node * size)) <= length) {
 
151
          c = *bytes++;
 
152
          *cp++ = c;
 
153
          // update read byte.
 
154
          bstream->current++;
 
155
        }
 
156
        s++;
 
157
      } while (s <= size);
 
158
      node++;
 
159
    }
 
160
  }
 
161
 
 
162
  return node;
 
163
}
 
164
 
 
165
size_t
 
166
byte_stream_write (ByteStream * bstream, IN ptr_t ptr, size_t size, size_t count) {
 
167
  byte * cp = ptr;
 
168
  byte * bytes = bstream->bytes;
 
169
  int c;
 
170
  size_t node = 0;
 
171
  size_t s;
 
172
  size_t length = bstream->length;
 
173
  size_t read_length = size * count; 
 
174
  size_t current = bstream->current;
 
175
 
 
176
 
 
177
  if ((read_length + current) >= length) {
 
178
    ptr_t tmp = realloc (bstream->bytes, read_length + current);
 
179
    if (tmp == NULL) {
 
180
      err_print ("Could not reallocate ByteStream buffer. Have you run out of memory?\n");
 
181
      goto error;
 
182
    }
 
183
    
 
184
    else bstream->bytes = tmp;
 
185
  }
 
186
 
 
187
  if (size) {
 
188
    while (node < count) {
 
189
      s = 0;
 
190
      do {
 
191
        if ((s + (node * size)) <= read_length) {
 
192
          c = *cp++;
 
193
          *bytes++ = c;
 
194
        }
 
195
        s++;
 
196
      } while (s <= size);
 
197
      node++;
 
198
      current += s;
 
199
    }
 
200
  }
 
201
   
 
202
error:
 
203
  return node;
 
204
}
 
205
 
 
206
size_t
 
207
byte_stream_length (ByteStream * bstream) {
 
208
  return bstream->length;
 
209
}