Added ability to serialize/deserialize strings and config objects(mc_config_t)

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2011-06-06 14:55:07 +03:00
parent be0f9dda56
commit e7bb47cc2d
5 changed files with 637 additions and 1 deletions

View File

@ -43,6 +43,7 @@ libmc_la_SOURCES = \
global.c global.h \
keybind.c keybind.h \
lock.c lock.h \
serialize.c serialize.h \
timefmt.c timefmt.h
if USE_MAINTAINER_MODE

346
lib/serialize.c Normal file
View File

@ -0,0 +1,346 @@
/*
Provides a serialize/unserialize functionality for INI-like formats.
Copyright (C) 2011 Free Software Foundation, Inc.
Written:
2011 Slava Zanko <slavazanko@gmail.com>.
This file is part of the Midnight Commander.
The Midnight Commander is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
/** \file serialize.c
* \brief Source: serialize/unserialize functionality for INI-like formats.
*/
#include <config.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "lib/global.h"
#include "lib/serialize.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
#define SRLZ_DELIM_C ':'
#define SRLZ_DELIM_S ":"
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static void
prepend_error_message (GError ** error, const char *format, ...)
{
char *prepend_str;
char *split_str;
va_list ap;
if ((error == NULL) || (*error == NULL))
return;
va_start (ap, format);
prepend_str = g_strdup_vprintf (format, ap);
va_end (ap);
split_str = g_strdup_printf ("%s: %s", prepend_str, (*error)->message);
g_free (prepend_str);
g_free ((*error)->message);
(*error)->message = split_str;
}
/* --------------------------------------------------------------------------------------------- */
static const char *
go_to_end_of_serialized_string (const char *non_serialized_data,
const char *already_serialized_part, size_t * offset)
{
size_t calculated_offset;
const char *semi_ptr = strchr (non_serialized_data + 1, SRLZ_DELIM_C);
calculated_offset = (semi_ptr - non_serialized_data) + 1 + strlen (already_serialized_part);
if (calculated_offset >= strlen (non_serialized_data))
return NULL;
non_serialized_data += calculated_offset;
*offset += calculated_offset;
return non_serialized_data;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/**
* Serialize some string object to string
*
* @param prefix prefix for serailization
* @param data data for serialization
* @param error contain pointer to object for handle error code and message
*
* @return serialized data as newly allocated string
*/
char *
mc_serialize_str (const char prefix, const char *data, GError ** error)
{
if (data == NULL)
{
g_set_error (error, MC_ERROR, -1, "mc_serialize_str(): Input data is NULL.");
return NULL;
}
return g_strdup_printf ("%c%zd" SRLZ_DELIM_S "%s", prefix, strlen (data), data);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Deserialize string to string object
*
* @param prefix prefix for deserailization
* @param data data for deserialization
* @param error contain pointer to object for handle error code and message
*
* @return newly allocated string
*/
#define FUNC_NAME "mc_serialize_str()"
char *
mc_deserialize_str (const char prefix, const char *data, GError ** error)
{
size_t data_len;
if ((data == NULL) || (strlen (data) == 0))
{
g_set_error (error, MC_ERROR, -1, FUNC_NAME ": Input data is NULL or empty.");
return NULL;
}
if (*data != prefix)
{
g_set_error (error, MC_ERROR, -2, FUNC_NAME ": String prefix doesn't equal to '%c'",
prefix);
return NULL;
}
{
char buffer[BUF_TINY];
char *semi_ptr;
size_t semi_offset;
semi_ptr = strchr (data + 1, SRLZ_DELIM_C);
if (semi_ptr == NULL)
{
g_set_error (error, MC_ERROR, -3,
FUNC_NAME ": Length delimiter '%c' doesn't exists", SRLZ_DELIM_C);
return NULL;
}
semi_offset = semi_ptr - (data + 1);
if (semi_offset >= BUF_TINY)
{
g_set_error (error, MC_ERROR, -3, FUNC_NAME ": Too big string length");
return NULL;
}
strncpy (buffer, data + 1, semi_offset);
buffer[semi_offset] = '\0';
data_len = atol (buffer);
data += semi_offset + 2;
}
if (data_len > strlen (data))
{
g_set_error (error, MC_ERROR, -3,
FUNC_NAME ": Specified data length (%zd) is greater than actual data length (%zd)",
data_len, strlen (data));
return NULL;
}
return g_strndup (data, data_len);
}
#undef FUNC_NAME
/* --------------------------------------------------------------------------------------------- */
/**
* Serialize mc_config_t object to string
*
* @param data data for serialization
* @param error contain pointer to object for handle error code and message
*
* @return serialized data as newly allocated string
*/
char *
mc_serialize_config (const mc_config_t * data, GError ** error)
{
gchar **groups, **group_iterator;
size_t group_count;
GString *buffer;
buffer = g_string_new ("");
group_iterator = groups = mc_config_get_groups (data, &group_count);
while (group_count-- != 0)
{
char *serialized_str;
gchar **params, **param_iterator;
size_t param_count;
serialized_str = mc_serialize_str ('g', *group_iterator, error);
if (serialized_str == NULL)
{
g_string_free (buffer, TRUE);
g_strfreev (groups);
return NULL;
}
g_string_append (buffer, serialized_str);
g_free (serialized_str);
param_iterator = params = mc_config_get_keys (data, *group_iterator, &param_count);
while (param_count-- != 0)
{
char *value;
serialized_str = mc_serialize_str ('p', *param_iterator, error);
if (serialized_str == NULL)
{
g_string_free (buffer, TRUE);
g_strfreev (params);
g_strfreev (groups);
return NULL;
}
g_string_append (buffer, serialized_str);
g_free (serialized_str);
value = mc_config_get_string_raw (data, *group_iterator, *param_iterator, "");
serialized_str = mc_serialize_str ('v', value, error);
g_free (value);
if (serialized_str == NULL)
{
g_string_free (buffer, TRUE);
g_strfreev (params);
g_strfreev (groups);
return NULL;
}
g_string_append (buffer, serialized_str);
g_free (serialized_str);
param_iterator++;
}
g_strfreev (params);
group_iterator++;
}
return g_string_free (buffer, FALSE);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Deserialize string to mc_config_t object
*
* @param data data for serialization
* @param error contain pointer to object for handle error code and message
*
* @return newly allocated mc_config_t object
*/
#define FUNC_NAME "mc_deserialize_config()"
#define prepend_error_and_exit() { \
prepend_error_message (error, FUNC_NAME " at %lu", current_position + 1); \
mc_config_deinit (ret_data); \
return NULL; \
}
mc_config_t *
mc_deserialize_config (const char *data, GError ** error)
{
char *current_group = NULL, *current_param = NULL, *current_value = NULL;
size_t current_position = 0;
mc_config_t *ret_data = mc_config_init (NULL);
enum automat_status
{
WAIT_GROUP,
WAIT_PARAM,
WAIT_VALUE
} current_status = WAIT_GROUP;
while (data != NULL)
{
if ((current_status == WAIT_GROUP) && (*data == 'p') && (current_group != NULL))
current_status = WAIT_PARAM;
switch (current_status)
{
case WAIT_GROUP:
g_free (current_group);
current_group = mc_deserialize_str ('g', data, error);
if (current_group == NULL)
prepend_error_and_exit ();
data = go_to_end_of_serialized_string (data, current_group, &current_position);
current_status = WAIT_PARAM;
break;
case WAIT_PARAM:
g_free (current_param);
current_param = mc_deserialize_str ('p', data, error);
if (current_param == NULL)
{
g_free (current_group);
prepend_error_and_exit ();
}
data = go_to_end_of_serialized_string (data, current_param, &current_position);
current_status = WAIT_VALUE;
break;
case WAIT_VALUE:
current_value = mc_deserialize_str ('v', data, error);
if (current_param == NULL)
{
g_free (current_group);
g_free (current_param);
prepend_error_and_exit ();
}
mc_config_set_string (ret_data, current_group, current_param, current_value);
data = go_to_end_of_serialized_string (data, current_value, &current_position);
g_free (current_value);
current_status = WAIT_GROUP;
break;
}
}
g_free (current_group);
g_free (current_param);
return ret_data;
}
#undef FUNC_NAME
/* --------------------------------------------------------------------------------------------- */

27
lib/serialize.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef MC__SERIALIZE_H
#define MC__SERIALIZE_H
#include <config.h>
#include "lib/global.h"
#include "lib/mcconfig.h"
/*** typedefs(not structures) and defined constants **********************************************/
/*** enums ***************************************************************************************/
/*** structures declarations (and typedefs of structures)*****************************************/
/*** global variables defined in .c file *********************************************************/
/*** declarations of public functions ************************************************************/
char *mc_serialize_str (const char prefix, const char *data, GError ** error);
char *mc_deserialize_str (const char prefix, const char *data, GError ** error);
char *mc_serialize_config (const mc_config_t * data, GError ** error);
mc_config_t *mc_deserialize_config (const char *data, GError ** error);
/*** inline functions ****************************************************************************/
#endif

View File

@ -4,9 +4,13 @@ AM_CFLAGS = $(GLIB_CFLAGS) -I$(top_srcdir) @CHECK_CFLAGS@
LIBS=@CHECK_LIBS@ $(top_builddir)/lib/libmc.la
TESTS = \
library_independ
library_independ \
serialize
check_PROGRAMS = $(TESTS)
library_independ_SOURCES = \
library_independ.c
serialize_SOURCES = \
serialize.c

258
lib/tests/serialize.c Normal file
View File

@ -0,0 +1,258 @@
/* lib/vfs - common serialize/deserialize functions
Copyright (C) 2011 Free Software Foundation, Inc.
Written by:
Slava Zanko <slavazanko@gmail.com>, 2011
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define TEST_SUITE_NAME "/lib"
#include <check.h>
#include "lib/global.h"
#include "lib/strutil.h"
#include "lib/serialize.h"
static void
setup (void)
{
str_init_strings (NULL);
}
static void
teardown (void)
{
str_uninit_strings ();
}
/* --------------------------------------------------------------------------------------------- */
#define deserialize_check_incorrect( etalon_code, etalon_str ) { \
if (actual != NULL) \
{ \
fail("actual value is '%s', but should be NULL", actual); \
g_free(actual); \
} \
else \
{ \
fail_unless (error->code == etalon_code && strcmp(error->message, etalon_str) == 0, \
"\nerror code is %d (should be %d);\nerror message is '%s' (should be '%s')", \
error->code, etalon_code, error->message, etalon_str); \
g_clear_error(&error); \
} \
}
START_TEST (test_serialize_deserialize_str)
{
GError *error = NULL;
char *actual;
actual = mc_serialize_str('s', "some test string", &error);
if (actual == NULL)
{
fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
g_clear_error(&error);
return;
}
fail_unless (strcmp(actual,"s16:some test string") == 0, "Actual value(%s) doesn't equal to etalon(s16:some test string)", actual);
g_free(actual);
actual = mc_deserialize_str('s', NULL, &error);
deserialize_check_incorrect( -1, "mc_serialize_str(): Input data is NULL or empty." );
actual = mc_deserialize_str('s', "incorrect string", &error);
deserialize_check_incorrect( -2, "mc_serialize_str(): String prefix doesn't equal to 's'" );
actual = mc_deserialize_str('s', "s12345string without delimiter", &error);
deserialize_check_incorrect( -3, "mc_serialize_str(): Length delimiter ':' doesn't exists" );
actual = mc_deserialize_str('s', "s1234567890123456789012345678901234567890123456789012345678901234567890:too big number", &error);
deserialize_check_incorrect( -3, "mc_serialize_str(): Too big string length" );
actual = mc_deserialize_str('s', "s500:actual string length less that specified length", &error);
deserialize_check_incorrect( -3, "mc_serialize_str(): Specified data length (500) is greater than actual data length (47)" );
actual = mc_deserialize_str('s', "s10:actual string length great that specified length", &error);
fail_unless (actual != NULL && strcmp(actual, "actual str") == 0, "actual (%s) doesn't equal to etalon(actual str)", actual);
g_free(actual);
actual = mc_deserialize_str('s', "s21:The right test string", &error);
fail_unless (actual != NULL && strcmp(actual, "The right test string") == 0, "actual (%s) doesn't equal to etalon(The right test string)", actual);
g_free(actual);
}
END_TEST
/* --------------------------------------------------------------------------------------------- */
#define etalon_str "g6:group1p6:param1v10:some valuep6:param2v11:some value " \
"g6:group2p6:param1v4:truep6:param2v6:123456" \
"g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n" \
"g6:group4p6:param1v5:falsep6:param2v6:654321"
START_TEST (test_serialize_config)
{
mc_config_t *test_data;
GError *error = NULL;
char *actual;
test_data = mc_config_init (NULL);
mc_config_set_string_raw (test_data, "group1", "param1", "some value");
mc_config_set_string (test_data, "group1", "param2", "some value ");
mc_config_set_bool (test_data, "group2", "param1", TRUE);
mc_config_set_int (test_data, "group2", "param2", 123456);
mc_config_set_string_raw (test_data, "group3", "param1", "::bla-bla::");
mc_config_set_string (test_data, "group3", "param2", "bla-:p1:w:v2:12:g3:123:bla-bla\n");
mc_config_set_bool (test_data, "group4", "param1", FALSE);
mc_config_set_int (test_data, "group4", "param2", 654321);
actual = mc_serialize_config (test_data, &error);
mc_config_deinit (test_data);
if (actual == NULL)
{
fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
g_clear_error(&error);
return;
}
fail_unless(strcmp(actual, etalon_str) == 0, "Not equal:\nactual (%s)\netalon (%s)", actual, etalon_str);
g_free(actual);
}
END_TEST
/* --------------------------------------------------------------------------------------------- */
#undef deserialize_check_incorrect
#define deserialize_check_incorrect( etalon_code, etalon_str ) { \
if (actual != NULL) \
{ \
fail("actual value but should be NULL", actual); \
mc_config_deinit(actual); \
} \
else \
{ \
fail_unless (error->code == etalon_code && strcmp(error->message, etalon_str) == 0, \
"\nerror code is %d (should be %d);\nerror message is '%s' (should be '%s')", \
error->code, etalon_code, error->message, etalon_str); \
g_clear_error(&error); \
} \
}
START_TEST (test_deserialize_config)
{
mc_config_t *actual;
GError *error = NULL;
char *actual_value;
actual = mc_deserialize_config ("g123error in group name", &error);
deserialize_check_incorrect( -3,
"mc_deserialize_config() at 1: mc_serialize_str(): Length delimiter ':' doesn't exists");
actual = mc_deserialize_config ("p6:param1v10:some valuep6:param2v11:some value ", &error);
deserialize_check_incorrect( -2,
"mc_deserialize_config() at 1: mc_serialize_str(): String prefix doesn't equal to 'g'");
actual = mc_deserialize_config ("g6:group1v10:some valuep6:param2v11:some value ", &error);
deserialize_check_incorrect( -2,
"mc_deserialize_config() at 10: mc_serialize_str(): String prefix doesn't equal to 'p'");
actual = mc_deserialize_config ("g6:group1p6000:param2v11:some value ", &error);
deserialize_check_incorrect( -3,
"mc_deserialize_config() at 10: mc_serialize_str(): Specified data length (6000) is greater than actual data length (21)");
actual = mc_deserialize_config (etalon_str, &error);
if (actual == NULL)
{
fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
g_clear_error(&error);
return;
}
actual_value = mc_config_get_string_raw(actual, "group1", "param1", "");
fail_unless( strcmp(actual_value, "some value") == 0,
"group1->param1(%s) should be equal to 'some value'", actual_value);
g_free(actual_value);
actual_value = mc_config_get_string(actual, "group1", "param2", "");
fail_unless( strcmp(actual_value, "some value ") == 0,
"group1->param2(%s) should be equal to 'some value '", actual_value);
g_free(actual_value);
fail_unless( mc_config_get_bool(actual, "group2", "param1", FALSE) == TRUE,
"group2->param1(FALSE) should be equal to TRUE");
fail_unless( mc_config_get_int(actual, "group2", "param2", 0) == 123456,
"group2->param2(%d) should be equal to 123456", mc_config_get_int(actual, "group2", "param2", 0));
actual_value = mc_config_get_string_raw(actual, "group3", "param1", "");
fail_unless( strcmp(actual_value, "::bla-bla::") == 0,
"group3->param1(%s) should be equal to '::bla-bla::'", actual_value);
g_free(actual_value);
actual_value = mc_config_get_string(actual, "group3", "param2", "");
fail_unless( strcmp(actual_value, "bla-:p1:w:v2:12:g3:123:bla-bla\n") == 0,
"group3->param2(%s) should be equal to 'bla-:p1:w:v2:12:g3:123:bla-bla\n'", actual_value);
g_free(actual_value);
fail_unless( mc_config_get_bool(actual, "group4", "param1", TRUE) == FALSE,
"group4->param1(TRUE) should be equal to FALSE");
fail_unless( mc_config_get_int(actual, "group4", "param2", 0) == 654321,
"group4->param2(%d) should be equal to 654321", mc_config_get_int(actual, "group4", "param2", 0));
mc_config_deinit (actual);
}
END_TEST
/* --------------------------------------------------------------------------------------------- */
int
main (void)
{
int number_failed;
Suite *s = suite_create (TEST_SUITE_NAME);
TCase *tc_core = tcase_create ("Core");
SRunner *sr;
tcase_add_checked_fixture (tc_core, setup, teardown);
/* Add new tests here: *************** */
tcase_add_test (tc_core, test_serialize_deserialize_str);
tcase_add_test (tc_core, test_serialize_config);
tcase_add_test (tc_core, test_deserialize_config);
/* *********************************** */
suite_add_tcase (s, tc_core);
sr = srunner_create (s);
srunner_set_log (sr, "serialize.log");
srunner_run_all (sr, CK_NORMAL);
number_failed = srunner_ntests_failed (sr);
srunner_free (sr);
return (number_failed == 0) ? 0 : 1;
}
/* --------------------------------------------------------------------------------------------- */