Design Pattern Examples
Overview of object-oriented design patterns
mapofstrings.c
Go to the documentation of this file.
1
5
6#include <memory.h>
7#include <stdbool.h>
8#include <stdlib.h>
9#include <string.h>
10
11#include "mapofstrings.h"
12
14// MapOfStrings_Initialize()
17{
18 if (map != NULL)
19 {
20 map->entries = NULL;
21 map->entries_count = 0;
22 }
23}
24
26// MapOfStrings_Clear()
29{
30 if (map != NULL)
31 {
32 for (size_t index = 0; index < map->entries_count; index++)
33 {
35 }
36 free(map->entries);
38 }
39}
40
42// MapOfStrings_AddStringList()
44bool MapOfStrings_AddStringList(MapOfStrings* map, const char* key, ConstStringList* value)
45{
46 bool success = false;
47
48 if (map != NULL && key != NULL && value != NULL)
49 {
50 MapOfStringsEntry* new_list;
51 if (map->entries == NULL)
52 {
53 new_list = calloc(1, sizeof(MapOfStringsEntry));
54 }
55 else
56 {
57 size_t new_count = map->entries_count + 1;
58 size_t new_size = new_count * sizeof(MapOfStringsEntry);
59 new_list = realloc(map->entries, new_size);
60 }
61 if (new_list != NULL)
62 {
63 map->entries = new_list;
64 map->entries[map->entries_count].key = key;
65 map->entries[map->entries_count].value = value;
66 map->entries_count++;
67 success = true;
68 }
69 }
70
71 return success;
72}
73
75// MapOfStrings_AddArray()
77bool MapOfStrings_AddArray(MapOfStrings* map, const char* key, const char** value)
78{
79 bool success = false;
80
81 if (map != NULL && key != NULL && value != NULL)
82 {
83 size_t numStrings = 0;
84 for (size_t index = 0; value[index] != NULL; index++)
85 {
86 numStrings++;
87 }
88 ConstStringList* stringList = calloc(1, sizeof(ConstStringList));
89 if (stringList != NULL)
90 {
91 success = ConstStringList_AddStrings(stringList, value, numStrings);
92 if (success)
93 {
94 success = MapOfStrings_AddStringList(map, key, stringList);
95 }
96 }
97 }
98
99 return success;
100}
101
103// MapOfStrings_Find()
105int MapOfStrings_Find(MapOfStrings* map, const char* key)
106{
107 int foundIndex = -1;
108
109 if (map != NULL && key != NULL)
110 {
111 for (size_t index = 0; index < map->entries_count; index++)
112 {
113 if (strcmp(map->entries[index].key, key) == 0)
114 {
115 foundIndex = (int)index;
116 break;
117 }
118 }
119 }
120
121 return foundIndex;
122}
bool ConstStringList_AddStrings(ConstStringList *stringList, const char **strings, size_t numStrings)
Add an array of strings to the given string list.
void ConstStringList_Clear(ConstStringList *stringList)
Clear the specified string list. The strings in the list are left alone, but the list itself is delet...
Declaration of the MapOfStrings typedef for declaring a map of strings keyed by another string.
void MapOfStrings_Initialize(MapOfStrings *map)
Initialize the given MapOfStrings structure so it is ready for use.
Definition: mapofstrings.c:16
void MapOfStrings_Clear(MapOfStrings *map)
Clear the given MapOfStrings object, releasing all memory associated with it. Leaves the object in an...
Definition: mapofstrings.c:28
int MapOfStrings_Find(MapOfStrings *map, const char *key)
Find the specified key in the given MapOfStrings object, returning an index into the object.
Definition: mapofstrings.c:105
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...
Definition: mapofstrings.c:44
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-t...
Definition: mapofstrings.c:77
Represents a list of pointers to zero-terminated strings that are to remain constant and never delete...
Represents an entry in the MapOfStrings structure, associating a string "key" with a StringList "valu...
ConstStringList * value
The "value" that is a ConstStringList object.
const char * key
A string that is associated with the value field.
Represents a list of structures that map strings to ConstStringList objects.
MapOfStringsEntry * entries
List of MapOfStringsEntry for each mapping.
size_t entries_count
Number of items in the entries list.