2011-06-06 15:55:07 +04:00
|
|
|
/*
|
|
|
|
Provides a serialize/unserialize functionality for INI-like formats.
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
Copyright (C) 2011
|
|
|
|
The Free Software Foundation, Inc.
|
2011-06-06 15:55:07 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2011
|
2011-06-06 15:55:07 +04:00
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
2011-06-06 15:55:07 +04:00
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
2011-10-15 14:56:47 +04:00
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2011-06-06 15:55:07 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
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.
|
2011-06-06 15:55:07 +04:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-06-06 15:55:07 +04:00
|
|
|
*/
|
|
|
|
|
2012-08-31 18:05:29 +04:00
|
|
|
/** \file lib/serialize.c
|
2011-06-06 15:55:07 +04:00
|
|
|
* \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,
|
2011-10-15 14:56:47 +04:00
|
|
|
FUNC_NAME
|
|
|
|
": Specified data length (%zd) is greater than actual data length (%zd)",
|
2011-06-06 15:55:07 +04:00
|
|
|
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, ¶m_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;
|
2012-07-28 14:19:27 +04:00
|
|
|
mc_config_t *ret_data;
|
2011-06-06 15:55:07 +04:00
|
|
|
enum automat_status
|
|
|
|
{
|
|
|
|
WAIT_GROUP,
|
|
|
|
WAIT_PARAM,
|
|
|
|
WAIT_VALUE
|
|
|
|
} current_status = WAIT_GROUP;
|
|
|
|
|
2012-07-28 14:19:27 +04:00
|
|
|
ret_data = mc_config_init (NULL, FALSE);
|
|
|
|
|
2011-06-06 15:55:07 +04:00
|
|
|
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, ¤t_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, ¤t_position);
|
|
|
|
current_status = WAIT_VALUE;
|
|
|
|
break;
|
|
|
|
case WAIT_VALUE:
|
|
|
|
current_value = mc_deserialize_str ('v', data, error);
|
2011-12-09 23:42:08 +04:00
|
|
|
if (current_value == NULL)
|
2011-06-06 15:55:07 +04:00
|
|
|
{
|
|
|
|
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, ¤t_position);
|
|
|
|
g_free (current_value);
|
|
|
|
current_status = WAIT_GROUP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free (current_group);
|
|
|
|
g_free (current_param);
|
|
|
|
|
|
|
|
return ret_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|