MP8 Web Server
All Data Structures Files Functions Variables Pages
libdictionary.c File Reference
#include <search.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "libdictionary.h"

Functions

void dictionary_init (dictionary_t *d)
 Must be called first, initializes the dictionary data structure.
int dictionary_add (dictionary_t *d, const char *key, const char *value)
 Adds a (key, value) pair to the dictionary.
const char * dictionary_get (dictionary_t *d, const char *key)
 Retrieves the value from the dictionary for a given key.
int dictionary_parse (dictionary_t *d, char *key_value)
 Parses the key_value string and add the parsed key and value to the dictionary.
int dictionary_remove (dictionary_t *d, const char *key)
 Removes the (key, value) entry from the dictionary.
void dictionary_destroy (dictionary_t *d)
 Frees all internal memory associated with the dictionary.
void dictionary_destroy_all (dictionary_t *d)
 Frees all internal memory associated with the dictionary and key/values pairs.

Detailed Description

Function Documentation

int dictionary_add ( dictionary_t d,
const char *  key,
const char *  value 
)

Adds a (key, value) pair to the dictionary.

Returns
0 on success or KEY_EXISTS if the key already exists in the dictionary.
void dictionary_destroy ( dictionary_t d)

Frees all internal memory associated with the dictionary.

Must be called last.

void dictionary_destroy_all ( dictionary_t d)

Frees all internal memory associated with the dictionary and key/values pairs.

Must be called last.

const char* dictionary_get ( dictionary_t d,
const char *  key 
)

Retrieves the value from the dictionary for a given key.

Returns
The stored value associated with the key if the key exists in the dictionary. If the key does not exist, this function will return NULL.
void dictionary_init ( dictionary_t d)

Must be called first, initializes the dictionary data structure.

Same as MP1.

int dictionary_parse ( dictionary_t d,
char *  key_value 
)

Parses the key_value string and add the parsed key and value to the dictionary.

If successful, the key_value string will be modified in-place in order to be added to the dictionary.

This function only accepts key_value strings in the general format of "Key: Value".

Returns
On success, the return value is zero. If the key already exists in the dictionary, KEY_EXISTS is returned; if the format of key_value is illegal, ILLEGAL_FORMAL is returned.
int dictionary_remove ( dictionary_t d,
const char *  key 
)

Removes the (key, value) entry from the dictionary.

Returns
0 on success or NO_KEY_EXISTS if the key was not present in the dictionary. This function does not free the memory used by key or value.