1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
/*
Copyright (C) 2019, 2020 SUSE LLC
Author: Pascal Arlt <parlt@suse.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "libeconf.h"
#include "defines.h"
#include "helpers.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
// Combine file path and file name
char *combine_strings(const char *string_one, const char *string_two,
const char delimiter) {
size_t combined_len = strlen(string_one) + strlen(string_two) + 2;
char *combined = malloc(combined_len);
snprintf(combined, combined_len, "%s%c%s", string_one, delimiter, string_two);
return combined;
}
// Set null value defined in include/defines.h
void initialize(econf_file *key_file, size_t num) {
key_file->file_entry[num].group = strdup(KEY_FILE_NULL_VALUE);
key_file->file_entry[num].key = strdup(KEY_FILE_NULL_VALUE);
key_file->file_entry[num].value = strdup(KEY_FILE_NULL_VALUE);
key_file->file_entry[num].comment_before_key = NULL;
key_file->file_entry[num].comment_after_value = NULL;
key_file->file_entry[num].quotes = false;
}
// Remove whitespace from beginning and end, append string terminator
char *clearblank(size_t *vlen, char *string) {
if (!*vlen) return string;
char *buffer = string, *ptr = string;
string[*vlen] = 0;
while (*string != 0) {
if (ptr == buffer && (*string == ' ' || *string == '\t')) {
(*vlen)--;
} else {
*ptr++ = *string;
}
string++;
}
while (buffer[*vlen - 1] == ' ' || buffer[*vlen - 1] == '\t')
(*vlen)--;
buffer[*vlen] = 0;
return buffer;
}
char *get_absolute_path(const char *path, econf_err *error) {
char *absolute_path;
if(*path != '/') {
char buffer[PATH_MAX];
if(!realpath(path, buffer)) {
if (error)
*error = ECONF_NOFILE;
return NULL;
}
absolute_path = strdup(buffer);
} else {
absolute_path = strdup(path);
}
if (absolute_path == NULL && error)
*error = ECONF_NOMEM;
return absolute_path;
}
// Remove '[' and ']' from beginning and end
char *stripbrackets(char *string) {
if (!string)
return NULL;
char *ptr = string, *buffer = string;
size_t length = strlen(string) - 1;
if (*string == '[' && string[length] == ']') {
while (*(++string) != ']') { *buffer++ = *string; }
*buffer = 0;
}
return ptr;
}
// Add '[' and ']' to the given string
char *addbrackets(const char *string) {
if (string == NULL)
return NULL;
size_t length = strlen(string);
if (!(*string == '[' && string[length - 1] == ']')) {
char *buffer = malloc(length + 3);
if (buffer == NULL)
return NULL;
char *cp = buffer;
*cp++ = '[';
cp = stpcpy (cp, string);
*cp++ = ']';
*cp = '\0';
return buffer;
}
return strdup(string);
}
// Turn the given string into lower case
char *toLowerCase(char *string) {
char *ptr = string;
while (*string)
{
*string = tolower(*string);
string++;
}
return ptr;
}
// Turn the given string into a hash value
// Hash function djb2 from Dan J. Bernstein
size_t hashstring(const char *string) {
size_t hash = 5381;
char c;
while ((c = *string++)) { hash = ((hash << 5) + hash) + c; }
return hash;
}
// Look for matching key
econf_err find_key(econf_file key_file, const char *group, const char *key, size_t *num) {
char *grp = (!group || !*group) ? strdup(KEY_FILE_NULL_VALUE) : strdup(group);
if (grp == NULL)
return ECONF_NOMEM;
if (!key || !*key) {
free(grp);
return ECONF_ERROR;
}
for (size_t i = 0; i < key_file.length; i++) {
if (!strcmp(key_file.file_entry[i].group, grp) &&
!strcmp(key_file.file_entry[i].key, key)) {
free(grp);
*num = i;
return ECONF_SUCCESS;
}
}
// Key not found
free(grp);
return ECONF_NOKEY;
}
// Append a new key to an existing econf_file
// TODO: If the group is already known the new key should be appended
// at the end of that group not at the end of the file.
static econf_err
new_key (econf_file *key_file, const char *group, const char *key) {
econf_err error;
char *grp = (!group || !*group) ? strdup(KEY_FILE_NULL_VALUE) : strdup(group);
if (grp == NULL)
return ECONF_NOMEM;
if (key_file == NULL || key == NULL)
{
free(grp);
return ECONF_ERROR;
}
if ((error = key_file_append(key_file))) {
free(grp);
return error;
}
if ((error = setGroup(key_file, key_file->length - 1, grp))) {
free(grp);
return error;
}
free(grp);
return setKey(key_file, key_file->length - 1, key);
}
// Set value for the given group, key combination. If the combination
// does not exist it is created.
// TODO: function/void pointer might not be necessary if the value is converted
// into a string beforehand.
econf_err setKeyValue(econf_err (*function) (econf_file*, size_t, const void*),
econf_file *kf, const char *group, const char *key,
const void *value)
{
size_t num;
econf_err error = find_key(*kf, group, key, &num);
if (error) {
if (error != ECONF_NOKEY) {
return error;
}
if ((error = new_key(kf, group, key))) {
return error;
}
num = kf->length - 1;
}
return function(kf, num, value);
}
struct file_entry cpy_file_entry(struct file_entry fe) {
struct file_entry copied_fe;
copied_fe.group = strdup(fe.group);
copied_fe.key = strdup(fe.key);
if (fe.value)
copied_fe.value = strdup(fe.value);
else
copied_fe.value = NULL;
if (fe.comment_before_key)
copied_fe.comment_before_key = strdup(fe.comment_before_key);
else
copied_fe.comment_before_key = NULL;
if (fe.comment_after_value)
copied_fe.comment_after_value = strdup(fe.comment_after_value);
else
copied_fe.comment_after_value = NULL;
copied_fe.line_number = fe.line_number;
return copied_fe;
}
|