Branch
Hash :
adb9e235
Author :
Thomas de Grivel
Date :
2023-01-14T18:26:30
license
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
/* rtbuf
* Copyright 2018-2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software excepted
* on Apple computers granted the above copyright notice and
* this permission paragraph are included in all copies and
* substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "type.h"
#include "symbol.h"
s_data_type g_rtbuf_data_type_type = {
sizeof(s_rtbuf_type),
DATA_TYPE_BYTES
};
s_data_alloc g_rtbuf_data_type_alloc;
s_rtbuf_type *g_rtbuf_data_type;
void rtbuf_type_init ()
{
data_alloc_init(&g_rtbuf_data_type_alloc, &g_rtbuf_data_type_type,
RTBUF_TYPE_MAX, 0, 0);
g_rtbuf_data_type = g_rtbuf_data_type_alloc.mem;
assert(g_rtbuf_data_type);
rtbuf_type_define("char" , sizeof(char));
rtbuf_type_define("unsigned char" , sizeof(unsigned char));
rtbuf_type_define("short" , sizeof(short));
rtbuf_type_define("unsigned short", sizeof(unsigned short));
rtbuf_type_define("int" , sizeof(int));
rtbuf_type_define("unsigned int" , sizeof(unsigned int));
rtbuf_type_define("long" , sizeof(long));
rtbuf_type_define("unsigned long" , sizeof(unsigned long));
rtbuf_type_define("float" , sizeof(float));
rtbuf_type_define("double" , sizeof(double));
}
void rtbuf_type_shutdown ()
{
if (g_rtbuf_data_type) {
rtbuf_type_delete_all();
g_rtbuf_data_type = 0;
data_alloc_clean(&g_rtbuf_data_type_alloc);
}
}
s_rtbuf_type * rtbuf_type_new (const char *name, unsigned int size)
{
s_rtbuf_type *rt = data_new(&g_rtbuf_data_type_alloc);
if (!rt)
return 0;
rt->name = symbol_intern(name);
rt->t.bytes = size;
rt->t.type = DATA_TYPE_BYTES;
return rt;
}
void rtbuf_type_delete (s_rtbuf_type *rt)
{
assert(rt);
data_delete(&g_rtbuf_data_type_alloc, rt);
}
void rtbuf_type_delete_all ()
{
unsigned i = 0;
int n = g_rtbuf_data_type_alloc.n;
while (i < g_rtbuf_data_type_alloc.max && n > 0) {
if (g_rtbuf_data_type[i].name) {
rtbuf_type_delete(g_rtbuf_data_type + i);
n--;
}
i++;
}
}
s_rtbuf_type * rtbuf_type_find (symbol name)
{
unsigned int i = 0;
unsigned int n =
g_rtbuf_data_type_alloc.n - g_rtbuf_data_type_alloc.free_n;
while (i < g_rtbuf_data_type_alloc.n && n > 0) {
if (g_rtbuf_data_type[i].name) {
if (name == g_rtbuf_data_type[i].name)
return &g_rtbuf_data_type[i];
n--;
}
i++;
}
return 0;
}
s_rtbuf_type * rtbuf_type_define (const char *name, unsigned int size)
{
s_rtbuf_type *found = rtbuf_type_find(name);
if (found && found->t.bytes == size)
return found;
return rtbuf_type_new(name, size);
}
s_rtbuf_type * rtbuf_type_parse_array (const char *name)
{
char *bracket = strchr(name, '[');
char *right_bracket = bracket ? strchr(bracket, ']') : 0;
if (right_bracket) {
char buf[1024];
unsigned int lb = bracket - name;
unsigned int rb = right_bracket - name;
s_rtbuf_type *element_type;
unsigned int size;
strlcpy(buf, name, sizeof(buf));
buf[lb] = 0;
if ((element_type = rtbuf_type(buf)) == 0)
return 0;
buf[rb] = 0;
size = atoi(&buf[lb + 1]);
return rtbuf_type_new(name, element_type->t.bytes * size);
}
return 0;
}
s_rtbuf_type * rtbuf_type_parse_pointer (const char *name)
{
char *star = strchr(name, '*');
if (star && star[1] == 0)
return rtbuf_type_new(name, sizeof(void*));
return 0;
}
s_rtbuf_type * rtbuf_type (const char *name)
{
const char *sym = symbol_find(name);
s_rtbuf_type *rt;
if (!(rt = sym ? rtbuf_type_find(sym) : 0) &&
!(rt = rtbuf_type_parse_array(name)) &&
!(rt = rtbuf_type_parse_pointer(name)))
printf("type not found: %s\n", name);
return rt;
}
s_rtbuf_type * rtbuf_type_array (const char *element_type,
unsigned int size)
{
char buf[1024];
snprintf(buf, sizeof(buf), "%s[%u]", element_type, size);
return rtbuf_type(buf);
}