2019-08-17 21:19:32 -04:00
|
|
|
/*
|
2020-01-06 19:42:52 -05:00
|
|
|
* Copyright (C) 2019-2020 Scoopta
|
2019-08-17 21:19:32 -04:00
|
|
|
* This file is part of Wofi
|
|
|
|
* Wofi 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Wofi 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 Wofi. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <map.h>
|
|
|
|
|
2020-01-20 19:22:14 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gmodule.h>
|
|
|
|
|
2019-08-17 21:19:32 -04:00
|
|
|
struct map {
|
2020-01-20 01:09:04 -05:00
|
|
|
GTree* tree;
|
2019-08-17 21:19:32 -04:00
|
|
|
bool mman;
|
|
|
|
};
|
|
|
|
|
2020-01-20 01:09:04 -05:00
|
|
|
static gint compare(gconstpointer p1, gconstpointer p2, gpointer data) {
|
|
|
|
(void) data;
|
|
|
|
const char* str1 = p1;
|
|
|
|
const char* str2 = p2;
|
|
|
|
return strcmp(str1, str2);
|
|
|
|
}
|
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
struct map* map_init(void) {
|
2020-01-20 01:16:15 -05:00
|
|
|
struct map* map = malloc(sizeof(struct map));
|
2020-01-20 01:09:04 -05:00
|
|
|
map->tree = g_tree_new_full(compare, NULL, free, free);
|
2019-08-17 21:19:32 -04:00
|
|
|
map->mman = true;
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
struct map* map_init_void(void) {
|
2020-01-20 01:16:15 -05:00
|
|
|
struct map* map = malloc(sizeof(struct map));
|
2020-01-20 01:09:04 -05:00
|
|
|
map->tree = g_tree_new_full(compare, NULL, free, NULL);
|
2019-08-17 21:19:32 -04:00
|
|
|
map->mman = false;
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
void map_free(struct map* map) {
|
2020-01-20 01:09:04 -05:00
|
|
|
g_tree_destroy(map->tree);
|
2019-08-17 21:19:32 -04:00
|
|
|
free(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put(struct map* map, const char* key, void* value) {
|
2020-01-20 01:09:04 -05:00
|
|
|
char* k = strdup(key);
|
|
|
|
char* v = value;
|
|
|
|
if(map->mman && value != NULL) {
|
|
|
|
v = strdup(value);
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|
2020-01-20 01:09:04 -05:00
|
|
|
g_tree_insert(map->tree, k, v);
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool map_put(struct map* map, const char* key, char* value) {
|
2020-01-20 01:09:04 -05:00
|
|
|
if(map->mman) {
|
2019-08-17 21:19:32 -04:00
|
|
|
put(map, key, value);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "This is an unmanaged map please use map_put_void\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool map_put_void(struct map* map, const char* key, void* value) {
|
2020-01-20 01:09:04 -05:00
|
|
|
if(map->mman) {
|
2019-08-17 21:19:32 -04:00
|
|
|
fprintf(stderr, "This is an managed map please use map_put\n");
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
put(map, key, value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* map_get(struct map* map, const char* key) {
|
2020-01-20 01:09:04 -05:00
|
|
|
return g_tree_lookup(map->tree, key);
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|
|
|
|
|
2019-08-18 23:49:12 -04:00
|
|
|
bool map_contains(struct map* map, const char* key) {
|
|
|
|
return map_get(map, key) != NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-17 21:19:32 -04:00
|
|
|
size_t map_size(struct map* map) {
|
2020-01-20 01:09:04 -05:00
|
|
|
return g_tree_nnodes(map->tree);
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|