/+junk/codegen

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/codegen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "utils.h"



ptr_t
malloc0 (size_t sz) {
  ptr_t retval = malloc (sz);
  if (retval != NULL) {
    memset (retval, 0, sz);
  }
  return retval;
} 

str_t
string_vacuum (str_t s) {
  str_t out_buff;
  int index, i;

  index = -1;
  i = 0;
  for (auto c = s[i]; c != '\0'; c = s[i++]) {
    if (c != ' ' && c != '\n' && c != '\t' && c != '\r') {
        index = i;
    }
  }
  s[index + 1] = '\0';
  out_buff = realloc (s, strlen (s) + 1);
  return out_buff;
}

str_t
string_concat_list_nl (str_t* list) {
  ptr_t tmp;
  str_t buff = malloc0 (strlen (*list) * sizeof (list));
  if (buff == NULL) {
    err_print ("buffer is for some reason Null, this is an error.\n");
    goto error1;
  }
  for (str_t s = *list; s != NULL; s = *++list) {
    if (sizeof (buff) <= strlen (buff) + strlen (s) + 1 ) {
      size_t next_size = strlen (buff) + strlen (s) + 1;
      fprintf (stdout, "nz: %zu\n", next_size);
      tmp = realloc (buff, next_size);
      if (tmp == NULL || errno == ENOMEM) {
        goto error2;
      }
    }
    strcat (buff, "\n");
    strcat (buff, s);
  }
  buff = string_vacuum (buff);
  return buff;

error2:
  free (tmp);
error1:
  free (buff);
  return NULL;
}

void
err_print (str_t fmt, ...) {
  va_list vlst;
  va_start (vlst, fmt);
  vfprintf (stderr, fmt, vlst);
  va_end (vlst);
}


/* ************************************************************************** */

struct ByteStream {
  byte * bytes;
  size_t length;
  long int current;
};

ByteStream *
byte_stream_new (size_t length) {
  ByteStream * retval = malloc0 (sizeof (ByteStream));
  
  if (retval == NULL) {
    err_print ("Something went wrong with allocating the ByteStream. Have you run out of memory?\n");
    goto error1;
  }

  retval->bytes = malloc0 (length);
  if (retval->bytes == NULL) {
    err_print ("Cound not allocate ByteStream buffer. Have you run out of memory?\n");
    goto error2;
  }

  retval->length = length;
  retval->current = 0;

  return retval;

error2:
  free (retval);
error1:
  return NULL;
}

ByteStream *
byte_stream_new_from_file (UNUSED FILE * f) {
  NOT_IMPLEMENTED;
  return NULL;
}


void
byte_stream_seek (ByteStream * bstream, long int offset, SEEK_OFFSET origin) {
  switch (origin) {
    case (SEEK_OFFSET_SET):
      assert (offset > 0 && "Offset must be positive when using SEEK_OFFSET_SET.");
      bstream->current = offset;
      return;
    case (SEEK_OFFSET_CUR): 
      assert (((long) bstream->length >= (bstream->current + offset)) &&
              ((bstream->current + offset) >= 0) &&
              "Trying to seek outside range valid range.");
        bstream->current += offset;
      return;
    case (SEEK_OFFSET_END):
      assert (offset < 0 && "Offset must be negative when using SEEK_OFFSET_END.");
      bstream->current = bstream->length - offset;
      return;
    default:
      assert (false && "How did you end up here?");
  }
}

size_t
byte_stream_read (ByteStream * bstream, OUT ptr_t ptr, size_t size, size_t count) {
  /* Based on the Minix implementation: https://stackoverflow.com/a/8590471 */
  byte * cp = ptr;
  byte * bytes = bstream->bytes;
  size_t length = bstream->length;
  int c;
  size_t node = 0;
  size_t s;

  // Set the pointer correctly.
  bytes += bstream->current;

  if (size) {
    while (node < count) {
      s = 0;
      do {
        if ((s + (node * size)) <= length) {
          c = *bytes++;
          *cp++ = c;
          // update read byte.
          bstream->current++;
        }
        s++;
      } while (s <= size);
      node++;
    }
  }

  return node;
}

size_t
byte_stream_write (ByteStream * bstream, IN ptr_t ptr, size_t size, size_t count) {
  byte * cp = ptr;
  byte * bytes = bstream->bytes;
  int c;
  size_t node = 0;
  size_t s;
  size_t length = bstream->length;
  size_t read_length = size * count; 
  size_t current = bstream->current;


  if ((read_length + current) >= length) {
    ptr_t tmp = realloc (bstream->bytes, read_length + current);
    if (tmp == NULL) {
      err_print ("Could not reallocate ByteStream buffer. Have you run out of memory?\n");
      goto error;
    }
    
    else bstream->bytes = tmp;
  }

  if (size) {
    while (node < count) {
      s = 0;
      do {
        if ((s + (node * size)) <= read_length) {
          c = *cp++;
          *bytes++ = c;
        }
        s++;
      } while (s <= size);
      node++;
      current += s;
    }
  }
   
error:
  return node;
}

size_t
byte_stream_length (ByteStream * bstream) {
  return bstream->length;
}