From 5f84f400c548ebd28b4dac8554e3a839d988f144 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 7 Mar 2012 21:53:36 -0600 Subject: [PATCH] Update: rework and cleanup serialized project Simplify the serialize functions by: - replace f_serialized data type with f_dynamic_string - remove the serialized strategy and change serialized function names to communicate the strategy. Fixed a few logic flaws and mistakes with the serialized functions. --- level_0/f_serialized/c/serialized.h | 59 +++------------------- level_1/fl_serialized/c/serialized.c | 96 +++++++++++++++--------------------- level_1/fl_serialized/c/serialized.h | 16 +++--- 3 files changed, 55 insertions(+), 116 deletions(-) diff --git a/level_0/f_serialized/c/serialized.h b/level_0/f_serialized/c/serialized.h index 9d5effb..ae90e3e 100644 --- a/level_0/f_serialized/c/serialized.h +++ b/level_0/f_serialized/c/serialized.h @@ -18,68 +18,25 @@ // fll-0 includes #include #include -#include #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 diff --git a/level_1/fl_serialized/c/serialized.c b/level_1/fl_serialized/c/serialized.c index e9db221..dfed03c 100644 --- a/level_1/fl_serialized/c/serialized.c +++ b/level_1/fl_serialized/c/serialized.c @@ -10,77 +10,64 @@ 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++; @@ -88,19 +75,14 @@ extern "C" { 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; @@ -111,17 +93,17 @@ extern "C" { 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++; } @@ -130,7 +112,7 @@ extern "C" { return f_none; } -#endif // _di_fl_unserialize_get_ +#endif // _di_fl_unserialize_simple_get_ #ifdef __cplusplus } // extern "C" diff --git a/level_1/fl_serialized/c/serialized.h b/level_1/fl_serialized/c/serialized.h index 1992450..36ccc05 100644 --- a/level_1/fl_serialized/c/serialized.h +++ b/level_1/fl_serialized/c/serialized.h @@ -25,20 +25,20 @@ 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" -- 1.8.3.1