Design Pattern Examples
Overview of object-oriented design patterns
Command_TextObject.c
Go to the documentation of this file.
1
5
6
7#include <memory.h>
8#include <stdlib.h>
9#include <string.h>
10
11#include "helpers/strdup.h"
12
13#include "Command_TextObject.h"
14
16// Command_TextObject_Initialize()
18bool Command_TextObject_Initialize(Command_TextObject* textObject, const char* startingText)
19{
20 bool initialized = false;
21
22 Command_TextObject_Clear(textObject);
23 if (textObject != NULL && startingText != NULL)
24 {
25 textObject->startingText = startingText;
26 textObject->text = STRDUP(textObject->startingText);
27 initialized = true;
28 }
29 return initialized;
30}
31
33// Command_TextObject_Clear()
36{
37 if (textObject != NULL)
38 {
39 free(textObject->text);
40 textObject->startingText = NULL;
41 textObject->text = NULL;
42 }
43}
44
46// Command_TextObject_Reset()
49{
50 if (textObject != NULL)
51 {
52 free(textObject->text);
53 textObject->text = STRDUP(textObject->startingText);
54 }
55}
56
58// Command_TextObject_GetText()
61{
62 char* text = NULL;
63
64 if (textObject != NULL)
65 {
66 text = textObject->text;
67 }
68
69 return text;
70}
71
73// Command_TextObject_SetText()
75void Command_TextObject_SetText(Command_TextObject* textObject, const char* newText)
76{
77 if (textObject != NULL && newText != NULL)
78 {
79 free(textObject->text);
80 textObject->text = STRDUP(newText);
81 }
82}
83
85// Command_TextObject_ToString()
88{
89 const char* text = NULL;
90
91 if (textObject != NULL)
92 {
93 text = textObject->text;
94 }
95
96 return text;
97}
const char * Command_TextObject_ToString(Command_TextObject *textObject)
Converts the Command_TextObject to a string (basically, returns the current text from the Command_Tex...
void Command_TextObject_Clear(Command_TextObject *textObject)
Clear the contents of the specified Command_TextObject, releasing any allocated resources associated ...
bool Command_TextObject_Initialize(Command_TextObject *textObject, const char *startingText)
Initialize a Command_TextObject with the specified text.
void Command_TextObject_Reset(Command_TextObject *textObject)
Resets the Command_TextObject to the starting string.
char * Command_TextObject_GetText(Command_TextObject *textObject)
Gets the text in the specified Command_TextObject.
void Command_TextObject_SetText(Command_TextObject *textObject, const char *newText)
Sets the text in the specified Command_TextObject.
Declaration of the Command_TextObject structure and associated functions as used in the Command Patte...
Declaration of the STRDUP macro that hides the differences between how strdup() is declared in differ...
#define STRDUP
Define STRDUP to be the operating system-specific version of strdup().
Definition: strdup.h:17
Container for a string. Need to use a structure to keep the starting text and the current text togeth...
char * text
The text that can change.
const char * startingText
Starting string text so we can reset the text to a known point.