Design Pattern Examples
Overview of object-oriented design patterns
conststringlist.h File Reference

Declaration of the ConstStringList structure and supporting functions to work with a list of constant strings. More...

#include <stdbool.h>
Include dependency graph for conststringlist.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _ConstStringList
 Represents a list of pointers to zero-terminated strings that are to remain constant and never deleted. In other words, we assume the lifetime of the strings is at least as long as the instance of this structure that points to those strings. More...
 

Macros

#define __CONSTSTRINGLIST_H__
 

Typedefs

typedef struct _ConstStringList ConstStringList
 Represents a list of pointers to zero-terminated strings that are to remain constant and never deleted. In other words, we assume the lifetime of the strings is at least as long as the instance of this structure that points to those strings.
 

Functions

void ConstStringList_Initialize (ConstStringList *stringList)
 Initialize 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 deleted. The string list can then be used to add new strings.
 
bool ConstStringList_AddString (ConstStringList *stringList, const char *string)
 Add a string to the given string list.
 
bool ConstStringList_AddStrings (ConstStringList *stringList, const char **strings, size_t numStrings)
 Add an array of strings to the given string list.
 
void ConstStringList_Remove (ConstStringList *stringList, int removeIndex)
 Remove the specified string from the given string list.
 
int ConstStringList_Find (ConstStringList *stringList, const char *string)
 Search the given string list for the given string. If found, return the index of the found string.
 
bool ConstStringList_AreListsEqual (ConstStringList *left, ConstStringList *right)
 Compare two strings lists to determine if they have the same contents.
 

Detailed Description

Declaration of the ConstStringList structure and supporting functions to work with a list of constant strings.

Definition in file conststringlist.h.

Macro Definition Documentation

◆ __CONSTSTRINGLIST_H__

#define __CONSTSTRINGLIST_H__

Definition at line 8 of file conststringlist.h.

Typedef Documentation

◆ ConstStringList

Represents a list of pointers to zero-terminated strings that are to remain constant and never deleted. In other words, we assume the lifetime of the strings is at least as long as the instance of this structure that points to those strings.

Function Documentation

◆ ConstStringList_AddString()

bool ConstStringList_AddString ( ConstStringList stringList,
const char *  string 
)

Add a string to the given string list.

Parameters
stringListA pointer to a ConstStringList representing the list of strings.
stringThe string to add to the list. The string is added as is to the list, so its lifetime must be at least as long as the ConstStringList object.
Returns
Returns true if the string was added to the list; otherwise, returns false, indicating an out of memory condition has occurred (the original string list is untouched in this case).

Definition at line 45 of file conststringlist.c.

References _ConstStringList::allocated_count, _ConstStringList::strings, and _ConstStringList::strings_count.

Referenced by Shop_PickupOrder(), Shop_PlaceOrder(), and Visitor_Exercise().

◆ ConstStringList_AddStrings()

bool ConstStringList_AddStrings ( ConstStringList stringList,
const char **  strings,
size_t  numStrings 
)

Add an array of strings to the given string list.

Parameters
stringListA pointer to a ConstStringList representing the list of strings.
stringsPointer to an array of constant strings to add to the list. The pointers to the strings are copied into the list.
numStringsNumber of strings in the strings parameter to add.
Returns
Returns true if the string was added to the list; otherwise, returns false, indicating an out of memory condition has occurred (the original string list is untouched in this case).

Definition at line 82 of file conststringlist.c.

References _ConstStringList::allocated_count, _ConstStringList::strings, and _ConstStringList::strings_count.

Referenced by MapOfStrings_AddArray(), and Shop_PlaceOrder().

◆ ConstStringList_AreListsEqual()

bool ConstStringList_AreListsEqual ( ConstStringList left,
ConstStringList right 
)

Compare two strings lists to determine if they have the same contents.

Parameters
leftA pointer to the first ConstStringList representing a list of strings to compare.
rightA pointer to the second ConstStringList representing a list of strings to compare.
Returns
Returns true if the two lists have the same content, regardless of the order of the content in either list; otherwise, returns false.

Definition at line 163 of file conststringlist.c.

References ConstStringList_Find(), _ConstStringList::strings, and _ConstStringList::strings_count.

Referenced by Shop_PlaceOrder().

◆ ConstStringList_Clear()

void ConstStringList_Clear ( ConstStringList stringList)

Clear the specified string list. The strings in the list are left alone, but the list itself is deleted. The string list can then be used to add new strings.

Parameters
stringListThe ConstStringList to clear.

Definition at line 28 of file conststringlist.c.

References ConstStringList_Initialize(), _ConstStringList::strings, and _ConstStringList::strings_count.

Referenced by MapOfStrings_Clear(), OrderVisitor_Clear(), Shop_PickupOrder(), and Shop_PlaceOrder().

◆ ConstStringList_Find()

int ConstStringList_Find ( ConstStringList stringList,
const char *  string 
)

Search the given string list for the given string. If found, return the index of the found string.

Parameters
stringListA pointer to a ConstStringList representing the list of string to search.
stringThe string to search for.
Returns
Returns an index into the string list of the string if found; otherwise, returns -1 if the string is not found.

Definition at line 141 of file conststringlist.c.

References _ConstStringList::strings, and _ConstStringList::strings_count.

Referenced by ConstStringList_AreListsEqual().

◆ ConstStringList_Initialize()

void ConstStringList_Initialize ( ConstStringList stringList)

Initialize the given string list.

Parameters
stringListPointer to a ConstStringList structure that is to be initialized. Cannot be NULL

Definition at line 15 of file conststringlist.c.

References _ConstStringList::allocated_count, _ConstStringList::strings, and _ConstStringList::strings_count.

Referenced by ConstStringList_Clear(), OrderVisitor_Initialize(), and Shop_PickupOrder().

◆ ConstStringList_Remove()

void ConstStringList_Remove ( ConstStringList stringList,
int  removeIndex 
)

Remove the specified string from the given string list.

Parameters
stringListA pointer to a ConstStringList representing the list of strings.
removeIndexIndex of the string to remove from the list. Call ConstStringList_Find() to get the index.

Definition at line 123 of file conststringlist.c.

References _ConstStringList::allocated_count, _ConstStringList::strings, and _ConstStringList::strings_count.