2 \brief IR Memory Implementation
3 \details Intermediary memory management
6 ----------------------------------------------------------------------------*/
8 #include <stdlib.h> //exit, malloc
9 #include <stdio.h> //print
10 #include <stdarg.h> //va_args
11 #include <stdint.h> //uint64_t
12 #include <string.h> //memset, str*
14 #include <unistd.h> //u8_* functions
15 #include <unitypes.h> //uint8_t as a char
16 #include <unistr.h> //u32_cpy
23 int ir_condenser(void);
25 ir_class
ir_class_findchild(ir_class
, uint8_t*);
26 ir_set
ir_class_rootset(ir_class
);
27 ir_class
ir_class_nextsib(ir_class
);
28 ir_class
ir_class_nextchild(ir_class
);
29 uint8_t* ir_class_name(ir_class
);
30 ir_set
ir_class_findset(ir_class
,uint8_t*);
31 ir_set
ir_set_findchild(ir_set
, uint8_t*);
32 ir_set
ir_set_nextsib(ir_set
);
33 ir_set
ir_set_nextchild(ir_set
);
34 ir_set
ir_set_findref(long long);
35 uint8_t* ir_set_name(ir_set
);
39 uint8_t bytes_identical(uint8_t*,uint8_t*);
41 /* Enumerated types */
42 enum dtype
{ FSDAT
, MSDAT
, ADAT
, LDAT
};
43 enum ltype
{ OLINK
, MLINK
, VLINK
, ALINK
};
47 uint8_t* src_file_name
;
54 { struct sdat_header_t header
;
55 struct frameinfo_t frameinfo
;
58 { struct framedata_t framesheets
[FACE_MAX
];
59 struct framedata_t mapsheets
[FACE_MAX
];
63 { struct sdat_header_t header
;
66 { struct sdat_header_t header
;
67 struct set_t
*src
, *trg
;
71 { struct sdat_header_t header
;
72 struct framedata_t framesheet
;
73 struct framedata_t mapsheet
;
74 struct simplex_t audio
;
78 { struct class_t
*parent
, *nextchild
, *nextsib
;
79 struct set_t
*root_set
;
83 { struct set_t
*parent
, *nextchild
, *nextsib
;
84 struct class_t
*class;
86 struct framebox_t
**sprites
;
87 struct framebox_t
**maps
;
88 struct simplex_t
**audio
;
89 struct link_t
**links
;
91 /* Function-Like Macros */
92 #define do_warn() do { \
94 #define wprint(str) do { \
95 fprintf(stderr, str); \
98 #define wprintf(fmt,...) do { \
99 fprintf(stderr, fmt, __VA_ARGS__); \
102 #define do_error() do { \
105 #define eprint(str) do { \
106 fprintf(stderr, str); \
109 #define eprintf(fmt,...) do { \
110 fprintf(stderr, fmt, __VA_ARGS__); \
115 struct ir_class root_class
= { .name
= &"." };
122 /* Return a pointer to the root class */
123 struct ir_class_t
* ir_class_root
125 { return &root_class
; }
127 /* Add a subclass to a class
128 Attempts to create a new subclass in the provided class, exiting with an
129 error if the class already exists
131 #define ERR_DUPECLASS "Subclass %s of class %s already exists!", name, *class.name
132 struct ir_class_t
* ir_class_addchild
133 ( struct ir_class_t
* class,
136 { struct ir_class_t
* iter
= *class.nextchild
;
138 return *class.nextchild
= class_alloc(name
);
140 if (bytes_identical(*iter
.name
, name
))
141 { fprintf(stderr
, ERR_DUPECLASS
);
144 if (*iter
.nextsib
!= NULL
)
145 { iter
= *iter
.nextsib
;
148 return *iter
.nextsib
= class_alloc(name
);
152 /* Return a pointer to the parent of the provided class */
153 struct ir_class_t
* ir_class_parent
154 ( struct ir_class_t
* class )
155 { return class.parent
; }
157 /* Add a set to a class
158 Attempts to create a new root set in the specified class, exiting with an
159 error if the set already exists
161 #define ERR_DUPESET "Root set %s of class %s already exists!", name, *class.name
162 struct ir_set_t
* ir_class_addset
163 ( struct ir_class_t
* class,
166 { struct ir_set_t
* iter
= *class.root_set
;
168 return *class.root_set
= set_alloc(name
);
170 if (bytes_identical(*iter
.name
, name
))
171 { fprintf(stderr
, ERR_DUPSET
);
174 if (*iter
.nextsib
!= NULL
)
175 { iter
= *iter
.nextsib
;
178 return *iter
.nextsib
= set_alloc(name
);
181 struct ir_set_t
* ir_set_addchild
182 ( struct ir_set_t
* set
,
187 /* Match two null-terminated bytestrings
188 Return 1 if the two bytestrings are identical, else 0
191 uint8_t bytes_identical
195 { while (*stra
&& *strb
)
196 if (*stra
++ != *strb
++)
198 return *stra
== *strb
;