// fll-0 includes
#include <level_0/strings.h>
#include <level_0/types.h>
-#include <level_0/errors.h>
#ifdef __cplusplus
extern "C"{
#endif
-#ifndef _di_f_serialized_strategies_
- // possible strategies.
- enum {
- f_serialized_strategy_simple,
- f_serialized_strategy_delimited, // FIXME: not implemented
- f_last_serialized_strategy,
- }; // enum
+#ifndef _di_f_serialized_splitters_
+ #define f_serialized_simple_splitter ':'
+ #define f_serialized_delimited_splitter '\''
+ #define f_serialized_delimited_delimiter '\\'
- #define f_serialized_strategy_simple_splitter ":"
- #define f_serialized_strategy_delimited_splitter "'"
- #define f_serialized_strategy_delimited_delimiter "\\"
-#endif // _di_f_serialized_strategies_
+ #define f_serialized_simple_splitter_string ":"
+ #define f_serialized_delimited_splitter_string "'"
+ #define f_serialized_delimited_delimiter_string "\\"
+#endif // _di_f_serialized_splitters_
#ifndef _di_f_serialized_default_allocation_step_
#define f_serialized_default_allocation_step f_memory_default_allocation_step
#endif // _di_f_serialized_default_allocation_step_
-#ifndef _di_f_serialized_
- typedef struct {
- f_string string;
- f_string_length size; // total amount of allocated space
- f_string_length used; // total number of allocated spaces used
- f_array_length variables; // total number of serialized variables stored
- } f_serialized;
-
- #define f_serialized_initialize { f_string_initialize, f_string_length_initialize, f_string_length_initialize, f_array_length_initialize }
-
- #define f_delete_serialized(status, serialized) \
- status = f_delete((void **) & serialized.string, sizeof(f_string), serialized.size); \
- if (status == f_none){ \
- serialized.size = 0; \
- serialized.used = 0; \
- }
-
- #define f_destroy_serialized(status, serialized) \
- status = f_destroy((void **) & serialized.string, sizeof(f_string), serialized.size); \
- if (status == f_none){ \
- serialized.size = 0; \
- serialized.used = 0; \
- }
-
- #define f_resize_serialized(status, serialized, new_length) \
- status = f_resize((void **) & serialized.string, sizeof(f_string), serialized.size, new_length); \
- if (status == f_none){ \
- serialized.size = new_length; \
- if (serialized.used > serialized.size) serialized.used = new_length; \
- }
-
- #define f_adjust_serialized(status, serialized, new_length) \
- status = f_adjust((void **) & serialized.string, sizeof(f_string), serialized.size, new_length); \
- if (status == f_none){ \
- serialized.size = new_length; \
- if (serialized.used > serialized.size) serialized.used = new_length; \
- }
-#endif // _di_f_serialized_
-
#ifdef __cplusplus
} // extern "C"
#endif
extern "C" {
#endif
-#ifndef _di_fl_serialize_
- f_return_status fl_serialize(const f_u_short strategy, const f_dynamic_string value, f_serialized *serialized) {
+#ifndef _di_fl_serialize_simple_
+ f_return_status fl_serialize_simple(const f_dynamic_string value, f_dynamic_string *serialized) {
#ifndef _di_level_0_parameter_checking_
if (serialized == f_null) return f_invalid_parameter;
#endif // _di_level_0_parameter_checking_
- if (strategy != f_serialized_strategy_simple) {
- return f_unsupported;
- }
-
f_status status = f_status_initialize;
+
if (serialized->used + value.used + 1 >= serialized->size) {
- f_resize_serialized(status, (*serialized), serialized->size + value.used + f_serialized_default_allocation_step);
+ f_resize_dynamic_string(status, (*serialized), serialized->size + value.used + f_serialized_default_allocation_step);
if (f_macro_test_for_allocation_errors(status)) return status;
}
- memcpy(serialized->string + serialized->used, f_serialized_strategy_simple_splitter, sizeof(f_autochar));
- memcpy(serialized->string + serialized->used + 1, value->string, sizeof(f_autochar) * value.used);
- serialized->used += value.used + 1;
+ if (serialized->used == 0) {
+ memcpy(serialized->string + serialized->used, value.string, sizeof(f_autochar) * value.used);
+ serialized->used += value.used;
+ } else {
+ memcpy(serialized->string + serialized->used, f_serialized_simple_splitter_string, sizeof(f_autochar));
+ memcpy(serialized->string + serialized->used + 1, value.string, sizeof(f_autochar) * value.used);
+ serialized->used += value.used + 1;
+ }
return f_none;
}
-#endif // _di_fl_serialize_
+#endif // _di_fl_serialize_simple_
-#ifndef _di_fl_unserialize_
- f_return_status fl_unserialize(const f_u_short strategy, const f_serialized *serialized, f_string_locations *locations) {
+#ifndef _di_fl_unserialize_simple_
+ f_return_status fl_unserialize_simple(const f_dynamic_string serialized, f_string_locations *locations) {
#ifndef _di_level_0_parameter_checking_
- if (serialized == f_null) return f_invalid_parameter;
- if (locations == f_null) return f_invalid_parameter;
+ if (locations == f_null) return f_invalid_parameter;
#endif // _di_level_0_parameter_checking_
- if (strategy != f_serialized_strategy_simple) {
- return f_unsupported;
- }
-
f_status status = f_status_initialize;
- f_array_length i = 0;
- f_array_length current = 0;
+ f_array_length i = 0;
+ f_array_length start = 0;
- f_string_length start = 1;
- f_string_length stop = 0;
+ while (i <= serialized.used) {
+ if (serialized.string[i] == f_serialized_simple_splitter || i == serialized.used) {
+ if (locations->used + 1 >= locations->size) {
+ f_resize_string_locations(status, (*locations), locations->size + f_serialized_default_allocation_step);
- while (i < serialized.used) {
- if (current == index) {
- if (start > stop) {
- start = i;
- stop = i;
+ if (f_macro_test_for_allocation_errors(status)) return status;
}
- if (serialized.string[i] == f_serialized_strategy_simple_splitter) {
- stop = i - 1;
-
- if (locations->used + 1 >= locations->size) {
- f_resize_string_locations(status, (*locations), locations->size + f_serialized_default_allocation_step);
-
- if (f_macro_test_for_allocation_errors(status)) return status;
- }
-
+ if (start == i) {
+ locations->array[locations->used].start = 1;
+ locations->array[locations->used].stop = 0;
+ locations->used++;
+ } else {
locations->array[locations->used].start = start;
- locations->array[locations->used].stop = stop;
+ locations->array[locations->used].stop = i - 1;
locations->used++;
-
- start = 1;
- stop = 0;
}
- }
- else if (serialized.string[i] == f_serialized_strategy_simple_splitter) {
- current++;
+
+ start = i + 1;
}
i++;
return f_none;
}
-#endif // _di_fl_unserialize_
+#endif // _di_fl_unserialize_simple_
-#ifndef _di_fl_unserialize_get_
- f_return_status fl_unserialize_get(const f_u_short strategy, const f_serialized serialized, const f_array_length index, f_string_location *location) {
+#ifndef _di_fl_unserialize_simple_get_
+ f_return_status fl_unserialize_simple_get(const f_dynamic_string serialized, const f_array_length index, f_string_location *location) {
#ifndef _di_level_0_parameter_checking_
- if (serialized == f_null) return f_invalid_parameter;
- if (location == f_null) return f_invalid_parameter;
+ if (location == f_null) return f_invalid_parameter;
#endif // _di_level_0_parameter_checking_
- if (strategy != f_serialized_strategy_simple) {
- return f_unsupported;
- }
-
f_status status = f_status_initialize;
f_array_length i = 0;
while (i < serialized.used) {
if (current == index){
- if (location->start > location->stop){
+ if (location->start > location->stop) {
location->start = i;
location->stop = i;
}
- if (serialized.string[i] == f_serialized_strategy_simple_splitter) {
+ if (serialized.string[i] == f_serialized_simple_splitter) {
location->stop = i - 1;
break;
}
}
- else if (serialized.string[i] == f_serialized_strategy_simple_splitter) {
+ else if (serialized.string[i] == f_serialized_simple_splitter) {
current++;
}
return f_none;
}
-#endif // _di_fl_unserialize_get_
+#endif // _di_fl_unserialize_simple_get_
#ifdef __cplusplus
} // extern "C"
extern "C" {
#endif
-#ifndef _di_fl_serialize_
+#ifndef _di_fl_serialize_simple_
// this function will append a string to the serialize.
- extern f_return_status fl_serialize(const f_u_short strategy, const f_dynamic_string value, f_serialized *serialized);
-#endif // _di_fl_serialize_
+ extern f_return_status fl_serialize_simple(const f_dynamic_string value, f_dynamic_string *serialized);
+#endif // _di_fl_serialize_simple_
-#ifndef _di_fl_unserialize_
+#ifndef _di_fl_unserialize_simple_
// this function will unserialize a serialized string and store the results in an array of strings.
- extern f_return_status fl_unserialize(const f_u_short strategy, const f_serialized serialized, f_string_locations *locations);
+ extern f_return_status fl_unserialize_simple(const f_dynamic_string serialized, f_string_locations *locations);
#endif // _di_fl_unserialize_
-#ifndef _di_fl_unserialize_get_
+#ifndef _di_fl_unserialize_simple_get_
// this function will pull a single serialized value from the serialized string at the given index.
- extern f_return_status fl_unserialize_get(const f_u_short strategy, const f_serialized serialized, const f_array_length index, f_string_location *location);
-#endif // _di_fl_unserialize_get_
+ extern f_return_status fl_unserialize_simple_get(const f_dynamic_string serialized, const f_array_length index, f_string_location *location);
+#endif // _di_fl_unserialize_simple_get_
#ifdef __cplusplus
} // extern "C"