Design Pattern Examples
Overview of object-oriented design patterns
helpers/mapofstrings.h File Reference

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

#include <stdlib.h>
#include "conststringlist.h"
Include dependency graph for helpers/mapofstrings.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  MapOfStringsEntry
 Represents an entry in the MapOfStrings structure, associating a string "key" with a StringList "value". More...
 
struct  MapOfStrings
 Represents a list of structures that map strings to ConstStringList objects. More...
 

Macros

#define __MAPOFSTRINGS_H__
 

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

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

Definition in file helpers/mapofstrings.h.

Macro Definition Documentation

◆ __MAPOFSTRINGS_H__

#define __MAPOFSTRINGS_H__

Definition at line 8 of file helpers/mapofstrings.h.

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().