Skip to content

Instantly share code, notes, and snippets.

@adrianparvino
Created February 4, 2018 17:36
Show Gist options
  • Save adrianparvino/652463e32d0c135535fc0454cff91eba to your computer and use it in GitHub Desktop.
Save adrianparvino/652463e32d0c135535fc0454cff91eba to your computer and use it in GitHub Desktop.
/* This file is part of ASCIIVN.
*
* Copyright (C) 2018 Adrian Parvin D. Ouano
*
* This program 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, version 3 of the License.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "slides.h"
#include "slidebuilder.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
struct string_map
{
struct string_map *children[2];
struct slide *value;
char *key;
char in_key[];
};
/*
* If `key` already exists, then replaces the node
*/
void
string_map_insert(struct string_map **string_map,
char *key,
struct slide *value)
{
while(*string_map != NULL)
{
int cmp;
switch(cmp = strcmp(key, (*string_map)->key))
{
case 0:
if (*string_map != NULL) {
(*string_map)->value = value;
return;
}
goto null;
default:
string_map = &(*string_map)->children[!(cmp < 0)];
}
}
null:;
size_t n = strlen(key);
*string_map = malloc(sizeof **string_map + n);
**string_map = (struct string_map) {
.children = { NULL, NULL },
.value = value,
.key = (*string_map)->in_key
};
memcpy((*string_map)->key, key, n);
return;
}
struct string_map *
string_map_init(char *key,
struct slide *value)
{
struct string_map *string_map = NULL;
string_map_insert(&string_map, key, value);
return string_map;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment