<commit_before>/*
 * ngfd - Non-graphical feedback daemon
 *
 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
 *
 * Contact: <NAME> <<EMAIL>>
 *
 * This software, including documentation, is protected by copyright
 * controlled by Nokia Corporation. All rights are reserved.
 * Copying, including reproducing, storing, adapting or translating,
 * any or all of this material requires the prior written consent of
 * Nokia Corporation. This material also contains confidential
 * information which may not be disclosed to others without the prior
 * written consent of Nokia.
 */

#include <string.h>

#include "ngf-log.h"
#include "ngf-daemon.h"
#include "ngf-properties.h"
#include "ngf-event-prototype.h"

#define GROUP_GENERAL    "general"
#define GROUP_VIBRATOR   "vibra"
#define GROUP_VOLUME     "volume_pattern"
#define GROUP_BACKLIGHT  "backlight_pattern"
#define GROUP_DEFINITION "event"
#define GROUP_PROTOTYPE  "prototype"

#define STREAM_RESTORE_ID "module-stream-restore.id"

/** Temporary structure when configuration is being parsed */
typedef struct _SettingsData
{
    NgfDaemon  *self;
    gchar     **allowed_keys;
} SettingsData;

/**
 * Strip the group type (event, prototype) from the group name.
 *
 * @param group Group name
 * @returns Newly allocated string without the group type or NULL.
 */

static gchar*
_strip_group_type (const char *group)
{
    gchar *ptr = NULL;

    ptr = (gchar*) group;
    while (*ptr != '\0' && *ptr != ' ')
        ptr++;

    if (*ptr == ' ')
        ptr++;

    if (*ptr == '\0')
        return NULL;

    return g_strdup (ptr);
}

/**
 * Get the name from the group. The name represents the name in the group
 * name without the optional parent inheritance.
 *
 * @param group Group name
 * @returns Newly allocated string with name or NULL if no name.
 *
 * @code
 * name = _parse_group_name ("event my_event@parent_event");
 * g_assert (g_str_equal (name, "my_event");
 * @endcode
 */

static gchar*
_parse_group_name (const char *group)
{
    gchar **split  = NULL;
    gchar  *name   = NULL;
    gchar  *result = NULL;

    name = _strip_group_type (group);
    if (name == NULL)
        return NULL;

    if ((split = g_strsplit (name, "@", 2)) == NULL) {
        g_free (name);
        return NULL;
    }

    result = g_strdup (split[0]);
    g_strfreev (split);
    return result;
}

/**
 * Get the parent name of the group. To support simple inheritance, optional
 * parent name can be provided by using the "@" character as a separator.
 *
 * @param group Group name
 * @returns Newly allocated string with parent name or NULL if no parent.
 *
 * @code
 * parent = _parse_group_parent ("event my_event@parent_event");
 * g_assert (g_str_equal (parent, "parent_event");
 * @endcode
 */

static gchar*
_parse_group_parent (const char *group)
{<commit_msg>daemon-settings: Fix minor memory leak.<commit_after>/*
 * ngfd - Non-graphical feedback daemon
 *
 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
 *
 * Contact: <NAME> <<EMAIL>>
 *
 * This software, including documentation, is protected by copyright
 * controlled by Nokia Corporation. All rights are reserved.
 * Copying, including reproducing, storing, adapting or translating,
 * any or all of this material requires the prior written consent of
 * Nokia Corporation. This material also contains confidential
 * information which may not be disclosed to others without the prior
 * written consent of Nokia.
 */

#include <string.h>

#include "ngf-log.h"
#include "ngf-daemon.h"
#include "ngf-properties.h"
#include "ngf-event-prototype.h"

#define GROUP_GENERAL    "general"
#define GROUP_VIBRATOR   "vibra"
#define GROUP_VOLUME     "volume_pattern"
#define GROUP_BACKLIGHT  "backlight_pattern"
#define GROUP_DEFINITION "event"
#define GROUP_PROTOTYPE  "prototype"

#define STREAM_RESTORE_ID "module-stream-restore.id"

/** Temporary structure when configuration is being parsed */
typedef struct _SettingsData
{
    NgfDaemon  *self;
    gchar     **allowed_keys;
} SettingsData;

/**
 * Strip the group type (event, prototype) from the group name.
 *
 * @param group Group name
 * @returns Newly allocated string without the group type or NULL.
 */

static gchar*
_strip_group_type (const char *group)
{
    gchar *ptr = NULL;

    ptr = (gchar*) group;
    while (*ptr != '\0' && *ptr != ' ')
        ptr++;

    if (*ptr == ' ')
        ptr++;

    if (*ptr == '\0')
        return NULL;

    return g_strdup (ptr);
}

/**
 * Get the name from the group. The name represents the name in the group
 * name without the optional parent inheritance.
 *
 * @param group Group name
 * @returns Newly allocated string with name or NULL if no name.
 *
 * @code
 * name = _parse_group_name ("event my_event@parent_event");
 * g_assert (g_str_equal (name, "my_event");
 * @endcode
 */

static gchar*
_parse_group_name (const char *group)
{
    gchar **split  = NULL;
    gchar  *name   = NULL;
    gchar  *result = NULL;

    name = _strip_group_type (group);
    if (name == NULL)
        return NULL;

    if ((split = g_strsplit (name, "@", 2)) == NULL) {
        g_free (name);
        return NULL;
    }

    result = g_strdup (split[0]);
    g_strfreev (split);
    g_free (name);
    return result;
}

/**
 * Get the parent name of the group. To support simple inheritance, optional
 * parent name can be provided by using the "@" character as a separator.
 *
 * @param group Group name
 * @returns Newly allocated string with parent name or NULL if no parent.
 *
 * @code
 * parent = _parse_group_parent ("event my_event@parent_event");
 * g_assert (g_str_equal (parent, "parent_event");
 * @endcode
 */

static gchar*
_parse_group_parent (const char *group)
{
{'fraction_non_alphanumeric': 0.1098478222844149, 'fraction_numerical': 0.002798670631450061, 'mean_word_length': 3.4847058823529413, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2980087', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1744, 'n_words': 630}