Created
February 4, 2018 17:42
-
-
Save adrianparvino/8f1adfb17336358f25cf058b2131493a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 *left; | |
struct string_map *right; | |
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 = (cmp < 0) ? | |
&(*string_map)->left : | |
&(*string_map)->right; | |
} | |
} | |
size_t n; | |
null: | |
n = strlen(key); | |
*string_map = malloc(sizeof **string_map + n); | |
**string_map = (struct string_map) { | |
.left = NULL, | |
.right = 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