diff --git a/src/kwil_def.h b/src/kwil_def.h new file mode 100644 index 0000000..3502418 --- /dev/null +++ b/src/kwil_def.h @@ -0,0 +1,126 @@ +#ifndef _kwil_def_H +#define _kwil_def_H + +#include + +// Type tag for kwil fields +enum kwil_field_tag_t { + KWIL_FIELD_UNKNOWN = 0, // the field's type is invalid/not known to kwil + KWIL_FIELD_FLOAT = 1, // the field is a float + KWIL_FIELD_INT = 2, // the field is an int + KWIL_FIELD_UNSIGNED = 3, // the field is an unsigned int + KWIL_FIELD_CHAR = 4, // the field is a character + KWIL_FIELD_CUSTOM = 6 // the field is a KWIL_STRUCT or KWIL_ENUM +}; +extern enum kwil_field_tag_t kwil_field_tag_from_string(const char* str); + +enum kwil_type_tag_t { + KWIL_TYPE_STRUCT, + KWIL_TYPE_ENUM +}; + +struct kwil_enum_value_t { + char name[48]; +}; + +struct kwil_enum_t { + // an array of strings with the possible enum values + struct kwil_enum_value_t* enum_values; + // the length of the enum_values array + size_t enum_values_len; + // the capacity of the enum_values array + size_t enum_values_cap; +}; + +// Instantiate a new kwil enum with a name and no values yet. +extern void kwil_init_enum(struct kwil_enum_t* self); +// Register an enum value with an enum +extern int kwil_enum_add_value(struct kwil_enum_t* self, const char* name); +// Free all memory related to a kwil enum. +// DOES NOT FREE self*, if the self* was heap-allocated, make sure to free it as well. +extern int kwil_free_enum(struct kwil_enum_t* self); + +// Data of a marked field of a marked struct. +struct kwil_field_t { + // name of the field + char name_str[48]; + // the type string of the field + char type_str[48]; + // how to serialize the type + enum kwil_field_tag_t type_tag; + // the length of the static sized array, >0 if type_tag is an array + size_t array_length; + int array_dynamic; + + // if type_tag is + struct kwil_type_t* type_ptr; +}; + +// Instantiate a new kwil field with a name and type. +extern int kwil_init_field(struct kwil_field_t* self, const char* field_name, const char* type_str); + +struct kwil_struct_t { + // the list of all fields in the type + struct kwil_field_t* fields; + // the used length of the fields array + size_t fields_len; + // the capacity of the fields array + size_t fields_cap; +}; + +// Instantiate new kwil struct data without fields +// NOTE: intended to be used in tandem with kwil_type_t to store the struct's name. +// kwil_init_struct does not store the name of the type. +extern int kwil_init_struct(struct kwil_struct_t* self); +// Register a field to a kwil struct +extern int kwil_struct_add_field(struct kwil_struct_t* self, struct kwil_field_t* new); +// Free all memory allocated for a kwil struct +// DOES NOT FREE self*, if the self* was heap allocated, make sure to free it as well. +extern int kwil_free_struct(struct kwil_struct_t* self); + +// A type that can be serialized with kwil +struct kwil_type_t { + // the declared name of the type + char type_name[48]; + // the type name switched to a tag + enum kwil_type_tag_t type_tag; + union { + // if type_tag is KWIL_FIELD_ENUM + struct kwil_enum_t enum_type; + // if type_tag is KWIL_FIELD_STRUCT + struct kwil_struct_t struct_type; + }; +}; + +// Create a new kwil type from an existing kwil struct. +// Copies the contents of the 'from' pointer. +// 'from' pointer only needs to be valid for the duration of the function. +extern int kwil_type_from_struct(struct kwil_type_t* self, struct kwil_struct_t* from, const char* name); +// Create a new type from an existing enum. +// Copies the contents of the 'from' pointer. +// 'from' pointer only needs to be valid for the duration of the function. +extern int kwil_type_from_enum(struct kwil_type_t* self, struct kwil_enum_t* from, const char* name); + +// All kwil-serializable data in a header +struct kwil_header_t { + // name of the header without any other path elements + char file_name[48]; + // array of kwil types defined in this header + struct kwil_type_t* types; + // length of kwil_types + size_t types_len; + // capacity of types + size_t types_cap; +}; + +// Instantiate a new kwil header with a filename +extern int kwil_init_header(struct kwil_header_t* self, const char* filename); +// Add an existing kwil type to the kwil header. +// Copies the kwil_type_t and inserts it into kwil_types. +// The 'new' pointer only needs to remain valid until the function returns. +extern int kwil_header_add_type(struct kwil_header_t* self, struct kwil_type_t* new); +// Free kwil header data and all associated memory. +// Invalidates all pointers stored in kwil_types. +extern int kwil_free_header(struct kwil_header_t* self); + +#endif // _kwil_def_H