Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / third_party / om-file-format-sys / c / include / om_variable.h

om_variable.h177 lines · 6.6 KiBRaw
1//
2// om_variable.h
3// OpenMeteoApi
4//
5// Created by Patrick Zippenfenig on 16.11.2024.
6//
7
8#ifndef OM_VARIABLE_H
9#define OM_VARIABLE_H
10
11#include "om_common.h"
12#include "om_file.h"
13
14/**
15 TODO:
16 - String array support
17 */
18
19/// =========== Structures describing the data layout ===============
20
21typedef struct {
22 uint8_t data_type; // OmDataType_t
23 uint8_t compression_type; // OmCompression_t
24 uint16_t name_size; // maximum 65k characters in name strings
25 uint32_t children_count;
26
27 // Followed by payload
28 //uint32_t[children_count] children_length;
29 //uint32_t[children_count] children_offset;
30
31 // Scalars are now set
32 //void* value;
33
34 // name is always last
35 //char[name_size] name;
36} OmVariableV3_t;
37
38typedef struct {
39 uint8_t data_type; // OmDataType_t
40 uint8_t compression_type; // OmCompression_t
41 uint16_t name_size; // maximum 65k characters in name strings
42 uint32_t children_count;
43 uint64_t lut_size;
44 uint64_t lut_offset;
45 uint64_t dimension_count;
46
47 float scale_factor;
48 float add_offset;
49
50 // Followed by payload: NOTE: Lets to try 64 bit align it somehow
51 //uint32_t[children_count] children_length;
52 //uint32_t[children_count] children_offset;
53
54 // Afterwards additional payload from value types
55 //uint64_t[dimension_count] dimensions;
56 //uint64_t[dimension_count] chunks;
57
58 // name is always last
59 //char[name_size] name;
60} OmVariableArrayV3_t;
61
62/// only expose an opaque pointer
63typedef void* OmVariable_t;
64
65
66
67/// =========== Functions for reading ===============
68
69
70/// After reading data for the variable, initialize it. This is literally a simple cast to an opaque pointer. Source memory must remain accessible!
71const OmVariable_t* om_variable_init(const void* src);
72
73/// Get the name of of a given variable. No guarantee for zero termination! The length in bytes of the string is placed into `length`.
74const char* om_variable_get_name(const OmVariable_t* variable, uint16_t* length);
75
76/// Get the type of the current variable
77OmDataType_t om_variable_get_type(const OmVariable_t* variable);
78
79/// Get the compression type of the current variable
80OmCompression_t om_variable_get_compression(const OmVariable_t* variable);
81
82float om_variable_get_scale_factor(const OmVariable_t* variable);
83
84float om_variable_get_add_offset(const OmVariable_t* variable);
85
86/// Get number of dimensions
87uint64_t om_variable_get_dimensions_count(const OmVariable_t* variable);
88
89/// Get a pointer to the dimensions of a OM variable
90const uint64_t* om_variable_get_dimensions(const OmVariable_t* variable);
91
92/// Get a pointer to the chunk dimensions of an OM Variable
93const uint64_t* om_variable_get_chunks(const OmVariable_t* variable);
94
95/// Return how many children are available for a given variable
96uint32_t om_variable_get_children_count(const OmVariable_t* variable);
97
98/// Get the file offset where a specified child or children can be read
99bool om_variable_get_children(const OmVariable_t* variable, uint32_t children_offset, uint32_t children_count, uint64_t* children_offsets, uint64_t* children_sizes);
100
101/// Read a variable as a scalar. Returns the size and value into the value and size field. `value` needs to be a pointer that then points to the value
102OmError_t om_variable_get_scalar(const OmVariable_t* variable, void** value, uint64_t* size);
103
104/// Validate that the variable metadata is consistent with the buffer size.
105/// This checks that dimension arrays, chunk arrays, children arrays, and name
106/// all fit within the given buffer. Returns ERROR_OK if valid.
107OmError_t om_variable_validate(const void* src, uint64_t buffer_size);
108
109
110
111
112/// =========== Functions for writing ===============
113
114/// Get the length of a scalar variable if written to a file.
115/// If the scalar is a string, we need to know the length of the string.
116size_t om_variable_write_scalar_size(uint16_t name_size, uint32_t children_count, OmDataType_t data_type, uint64_t string_size);
117
118/// Write a scalar variable with name and children variables to a destination buffer
119///
120/// This function supports the following data types:
121/// - DATA_TYPE_NONE: No value storage (passing a NULL pointer to value is allowed)
122/// - DATA_TYPE_INT8/UINT8: 8-bit integer value
123/// - DATA_TYPE_INT16/UINT16: 16-bit integer value
124/// - DATA_TYPE_INT32/UINT32: 32-bit integer value
125/// - DATA_TYPE_INT64/UINT64: 64-bit integer value
126/// - DATA_TYPE_FLOAT: 32-bit floating point value
127/// - DATA_TYPE_DOUBLE: 64-bit floating point value
128///
129/// @param dst Destination buffer to write the variable to
130/// @param name_size Length of the variable name in bytes
131/// @param children_count Number of child variables
132/// @param children_offsets Array of offsets to child variables (can be NULL if children_count is 0)
133/// @param children_sizes Array of sizes of child variables (can be NULL if children_count is 0)
134/// @param name Pointer to the variable name string
135/// @param data_type Type of the data to be stored (see OmDataType_t)
136/// @param value Pointer to the value to be stored. For DATA_TYPE_NONE, this should be NULL.
137/// For other types, this should point to a value of the corresponding C type.
138///
139/// @note The destination buffer must be large enough to hold the variable.
140/// Use om_variable_write_scalar_size() to calculate the required size.
141void om_variable_write_scalar(
142 void* dst,
143 uint16_t name_size,
144 uint32_t children_count,
145 const uint64_t* children_offsets,
146 const uint64_t* children_sizes,
147 const char* name,
148 OmDataType_t data_type,
149 const void* value,
150 size_t string_size
151);
152
153/// Get the size of meta attributes of a numeric array if written to a file. Does not contain any data. Only offsets for the actual data.
154size_t om_variable_write_numeric_array_size(uint16_t name_size, uint32_t children_count, uint64_t dimension_count);
155
156/// Write meta data for a numeric array to file
157void om_variable_write_numeric_array(void* dst, uint16_t name_size, uint32_t children_count, const uint64_t* children_offsets, const uint64_t* children_sizes, const char* name, OmDataType_t data_type, OmCompression_t compression_type, float scale_factor, float add_offset, uint64_t dimension_count, const uint64_t *dimensions, const uint64_t *chunks, uint64_t lut_size, uint64_t lut_offset);
158
159
160
161/// =========== Internal functions ===============
162
163/// Memory layout types
164typedef enum {
165 OM_MEMORY_LAYOUT_LEGACY = 0,
166 OM_MEMORY_LAYOUT_ARRAY = 1,
167 OM_MEMORY_LAYOUT_SCALAR = 3,
168 //OM_MEMORY_LAYOUT_STRING_ARRAY = 5,
169} OmMemoryLayout_t;
170
171/// Check if a variable is legacy or version 3 array of scalar. Legacy files are the entire header containing magic number and version.
172OmMemoryLayout_t _om_variable_memory_layout(const OmVariable_t* variable);
173
174
175
176
177#endif // OM_VARIABLE_H