Design Pattern Examples
Overview of object-oriented design patterns
mapofstrings.c File Reference

Implementation of the supporting functions for the MapOfStrings structure for managing a map of strings keyed by another string. More...

#include <memory.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "mapofstrings.h"
Include dependency graph for mapofstrings.c:

Go to the source code of this file.

Functions

void MapOfStrings_Initialize (MapOfStrings *map)
 Initialize the given MapOfStrings structure so it is ready for use.
 
void MapOfStrings_Clear (MapOfStrings *map)
 Clear the given MapOfStrings object, releasing all memory associated with it. Leaves the object in an initialize state to be reused, if desired.
 
bool MapOfStrings_AddStringList (MapOfStrings *map, const char *key, ConstStringList *value)
 Add a key/value association to the given MapOfStrings object. The MapOfStrings object takes ownership of the key and value arguments and will free them when MapOfStrings_Clear() is called in the object.
 
bool MapOfStrings_AddArray (MapOfStrings *map, const char *key, const char **value)
 Add a key/value association to the given MapOfStrings object, where the value is provided as a NULL-terminated array of string pointer. This array is copied into a StringList object for storage in the map.
 
int MapOfStrings_Find (MapOfStrings *map, const char *key)
 Find the specified key in the given MapOfStrings object, returning an index into the object.
 

Detailed Description

Implementation of the supporting functions for the MapOfStrings structure for managing a map of strings keyed by another string.

Definition in file mapofstrings.c.

Function Documentation

◆ MapOfStrings_AddArray()

bool MapOfStrings_AddArray ( MapOfStrings map,
const char *  key,
const char **  value 
)

Add a key/value association to the given MapOfStrings object, where the value is provided as a NULL-terminated array of string pointer. This array is copied into a StringList object for storage in the map.

Parameters
mapThe MapOfStrings object to add to.
keyThe key value to be added. This must be allocated, directly or indirectly, with malloc() as it will be freed when the MapOfStrings object is cleared.
valueAn array of string pointers terminated by a NULL pointer.
Returns
Returns true if the key/value was successfully added; otherwise, returns false, indicating an out of memory condition (or NULL argument).

Definition at line 77 of file mapofstrings.c.

References ConstStringList_AddStrings(), and MapOfStrings_AddStringList().

Referenced by Village_Load().

◆ MapOfStrings_AddStringList()

bool MapOfStrings_AddStringList ( MapOfStrings map,
const char *  key,
ConstStringList value 
)

Add a key/value association to the given MapOfStrings object. The MapOfStrings object takes ownership of the key and value arguments and will free them when MapOfStrings_Clear() is called in the object.

Parameters
mapThe MapOfStrings object to add to.
keyThe key value to be added. This must be allocated, directly or indirectly, with malloc() as it will be freed when the MapOfStrings object is cleared.
valueThe ConstStringList object to add as the value. This must be allocated with malloc() as it will be freed when the MapOfStrings object is cleared.
Returns
Returns true if the key/value was successfully added; otherwise, returns false, indicating an out of memory condition (or NULL argument).

Definition at line 44 of file mapofstrings.c.

References MapOfStrings::entries, MapOfStrings::entries_count, MapOfStringsEntry::key, and MapOfStringsEntry::value.

Referenced by MapOfStrings_AddArray().

◆ MapOfStrings_Clear()

void MapOfStrings_Clear ( MapOfStrings map)

Clear the given MapOfStrings object, releasing all memory associated with it. Leaves the object in an initialize state to be reused, if desired.

Parameters
mapThe MapOfStrings object to clear.

Definition at line 28 of file mapofstrings.c.

References ConstStringList_Clear(), MapOfStrings::entries, MapOfStrings::entries_count, MapOfStrings_Initialize(), and MapOfStringsEntry::value.

Referenced by Shop_Destroy().

◆ MapOfStrings_Find()

int MapOfStrings_Find ( MapOfStrings map,
const char *  key 
)

Find the specified key in the given MapOfStrings object, returning an index into the object.

Parameters
mapThe MapOfStrings object to search.
keyThe key to search for.
Returns
Returns the index of the found item if successful; otherwise, returns -1, indicating the key was not found.

Definition at line 105 of file mapofstrings.c.

References MapOfStrings::entries, MapOfStrings::entries_count, and MapOfStringsEntry::key.

Referenced by Shop_DoesShopSellItem(), and Shop_PlaceOrder().

◆ MapOfStrings_Initialize()

void MapOfStrings_Initialize ( MapOfStrings map)

Initialize the given MapOfStrings structure so it is ready for use.

Alternatively, declare the MapOfStrings instance like this:

MapOfStrings map = { 0 };
Represents a list of structures that map strings to ConstStringList objects.
Parameters
mapThe MapOfStrings object to initialize.

Definition at line 16 of file mapofstrings.c.

References MapOfStrings::entries, and MapOfStrings::entries_count.

Referenced by MapOfStrings_Clear(), and Shop_Create().