Design Pattern Examples
Overview of object-oriented design patterns
Memento_TextObject.c
Go to the documentation of this file.
1
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <memory.h>
12#include <string.h>
13
14#include "helpers/strdup.h"
15
16#include "Memento_TextObject.h"
17
19// Memento_TextObject_Create()
22{
23 Memento_TextObject* textObject = NULL;
24
25 if (text != NULL)
26 {
27 textObject = calloc(1, sizeof(Memento_TextObject));
28 if (textObject != NULL)
29 {
30 textObject->text = STRDUP(text);
31 if (textObject->text == NULL)
32 {
33 printf(" Error! Out of memory duplicating text for Memento_TextObject!\n");
34 free(textObject);
35 textObject = NULL;
36 }
37 }
38 else
39 {
40 printf(" Error! Out of memory creating a Memento_TextObject!\n");
41 }
42 }
43
44 return textObject;
45}
46
48// Memento_TextObject_Destroy()
51{
52 if (textObject != NULL)
53 {
54 free(textObject->text);
55 free(textObject);
56 }
57}
58
60// Memento_TextObject_GetText()
63{
64 char* text = NULL;
65 if (textObject != NULL)
66 {
67 text = textObject->text;
68 }
69
70 return text;
71}
72
73
75// Memento_TextObject_SetText()
77void Memento_TextObject_SetText(Memento_TextObject* textObject, const char* newText)
78{
79 if (textObject != NULL && newText != NULL)
80 {
81 free(textObject->text);
82 textObject->text = STRDUP(newText);
83 if (textObject->text == NULL)
84 {
85 printf(" Error! Out of memory setting text on a Memento_TextObject!\n");
86 }
87 }
88}
89
90
92// Memento_TextObject_ToString()
95{
96 const char* text = NULL;
97
98 if (textObject != NULL)
99 {
100 text = textObject->text;
101 }
102
103 return text;
104}
char * Memento_TextObject_GetText(Memento_TextObject *textObject)
Retrieve a pointer to the text contained within the Memento_TextObject. The text can be altered throu...
void Memento_TextObject_Destroy(Memento_TextObject *textObject)
Destroy the given Memento_TextObject object and release any used memory. After this function returns,...
Memento_TextObject * Memento_TextObject_Create(const char *text)
Create a new instance of the Memento_TextObject structure and initialize it with the given text.
void Memento_TextObject_SetText(Memento_TextObject *textObject, const char *newText)
Replace the existing text in the Memento_TextObject object with the given text.
const char * Memento_TextObject_ToString(Memento_TextObject *textObject)
Return a string representation of the Memento_TextObject. In this case, it is just the underlying tex...
Declaration of the Memento_TextObject structure and support functions, Memento_TextObject_Create(),...
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.
char * text
The text object contained within this Memento_TextObject.