Piotr Czajkowski 5 years ago
parent
commit
de82d19bff
4 changed files with 0 additions and 100 deletions
  1. 0 46
      dict.c
  2. 0 15
      dict.h
  3. 0 29
      keyval.c
  4. 0 10
      keyval.h

+ 0 - 46
dict.c

@@ -1,46 +0,0 @@
-// Borrowed from https://github.com/b-k/21st-Century-Examples
-#include <stdio.h>
-#include <stdlib.h>
-#include "dict.h"
-
-void *dictionary_not_found;
-
-dictionary *dictionary_new (void){
-	static int dnf;
-	if (!dictionary_not_found) dictionary_not_found = &dnf;
-	dictionary *out= malloc(sizeof(dictionary));
-	*out= (dictionary){ .pairs=NULL };                          
-	return out;
-} 
-
-static void dictionary_add_keyval(dictionary *in, keyval *kv){
-	in->length++;
-	in->pairs = realloc(in->pairs, sizeof(keyval*)*in->length);
-	in->pairs[in->length-1] = kv;
-}
-
-void dictionary_add(dictionary *in, char *key, void *value){
-	if (!key){fprintf(stderr, "NULL is not a valid key.\n"); abort();}
-	dictionary_add_keyval(in, keyval_new(key, value));
-}
-
-void *dictionary_find(dictionary const *in, char const *key){
-	for (int i=0; i< in->length; i++)
-		if (keyval_matches(in->pairs[i], key))    
-			return in->pairs[i]->value;
-	return NULL;
-}
-
-dictionary *dictionary_copy(dictionary *in){
-	dictionary *out = dictionary_new();
-	for (int i=0; i< in->length; i++)
-		dictionary_add_keyval(out, keyval_copy(in->pairs[i]));
-	return out;
-}
-
-void dictionary_free(dictionary *in){
-	for (int i=0; i< in->length; i++)
-		keyval_free(in->pairs[i]);
-	free(in->pairs);
-	free(in);
-}

+ 0 - 15
dict.h

@@ -1,15 +0,0 @@
-// Borrowed from https://github.com/b-k/21st-Century-Examples
-#include "keyval.h"
-
-extern void *dictionary_not_found;
-
-typedef struct dictionary{
-	keyval **pairs;
-	int length;
-} dictionary;
-
-dictionary *dictionary_new (void);
-dictionary *dictionary_copy(dictionary *in);
-void dictionary_free(dictionary *in);
-void dictionary_add(dictionary *in, char *key, void *value);
-void *dictionary_find(dictionary const *in, char const *key);

+ 0 - 29
keyval.c

@@ -1,29 +0,0 @@
-// Borrowed from https://github.com/b-k/21st-Century-Examples
-#include <stdlib.h> //malloc
-#include <string.h>
-#include <strings.h> //strcasecmp (from POSIX)
-#include "keyval.h"
-
-keyval *keyval_new(char *key, void *value){
-	keyval *out = malloc(sizeof(keyval));
-	*out = (keyval){.key = strdup(key), .value=strdup(value)};
-	return out;
-}
-
-/** Copy a key/value pair. The new pair has pointers to
-  the values in the old pair, not copies of their data.  */
-keyval *keyval_copy(keyval const *in){
-	keyval *out = malloc(sizeof(keyval));
-	*out = *in;
-	return out;
-}
-
-void keyval_free(keyval *in){
-	free(in->key);
-	free(in->value);
-	free(in);
-}
-
-int keyval_matches(keyval const *in, char const *key){
-	return !strcasecmp(in->key, key);
-}

+ 0 - 10
keyval.h

@@ -1,10 +0,0 @@
-// Borrowed from https://github.com/b-k/21st-Century-Examples
-typedef struct keyval{
-	char *key;
-	void *value;
-} keyval;
-
-keyval *keyval_new(char *key, void *value);
-keyval *keyval_copy(keyval const *in);
-void keyval_free(keyval *in);
-int keyval_matches(keyval const *in, char const *key);