11#include "helpers/formatstring.h"
13#include "helpers/replace.h"
37 if (text != NULL && operation != NULL)
56 if (source != NULL && searchPattern != NULL && replaceText != NULL)
78 size_t textSize = strlen(text);
79 size_t halftextSize = textSize / 2;
80 for (
size_t index = 0; index < halftextSize; ++index)
83 text[index] = text[textSize - index - 1];
84 text[textSize - index - 1] = c;
120 if (text != NULL && searchPattern != NULL && replaceText != NULL)
122 char *operationName =
formatstring(
"Replace '%s' with '%s'", searchPattern, replaceText);
123 if (operationName != NULL)
132 printf(
" Error! Out of memory while creating operation name for replace operation!\n");
146 const char* operationName =
"Reverse";
182 printf(
"\nMemento_Exercise\n");
198 printf(
" Now perform undo until back to original\n");
void Memento_Exercise(void)
Example of using the Memento design pattern.
static void Memento_Undo(Memento_TextObject *text)
Perform an undo on the given Command_TextObject, using the mementos in the "global" undo list....
static void Memento_ApplyReverseOperation(Memento_TextObject *text)
Helper function to reverse the order of the characters in the given Memento_TextObject after adding a...
static void Memento_Operation_Replace(Memento_TextObject *source, const char *searchPattern, const char *replaceText)
An operation to search and replace text in a Memento_TextObject.
static void Memento_SaveForUndo(Memento_TextObject *text, const char *operation)
Take a snapshot of the given text object associated with the name of given operation.
static void Memento_ApplyReplaceOperation(Memento_TextObject *text, const char *searchPattern, const char *replaceText)
Helper function to replace a pattern with another string in the given Memento_TextObject after adding...
static StackEntry * _mementoUndoList
The list of memento objects that form a series of snapshots in time of a Memento_TextObject.
static void Memento_Operation_Reverse(Memento_TextObject *source)
An operation to reverse the characters in the given Memento_TextObject.
void Memento_Destroy(Memento *memento)
Destroy an existing instance of the Memento structure. After this function returns,...
Memento * Memento_Create(const char *text, const char *name)
Create a new instance of the Memento structure, initialized to the given text and name.
Declaration of the Memento structure and support functions, Memento_Create() and Memento_Destroy(),...
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 Memento_Exercise() function as used in the Memento Pattern.
char * replace_str(const char *s, const char *str1, const char *str2)
Replace all occurrences of narrow string str1 with narrow string str2 in s, using case-sensitive sear...
void Stack_Push(StackEntry **stack, void *item)
Push the given entry onto the given stack.
void * Stack_Pop(StackEntry **stack)
Pop the last entry from the given stack, returning the item.
Declaration of the StackEntry structure and the supporting functions that represents a simple stack.
Represents a single memento (snapshot) of the text state before an operation is applied....
char * text
The snapshot to be remembered by the Memento.
const char * name
The operation name that triggered the need for this Memento.
Represents an entry on a simple stack that wraps an "item" represented by an opaque pointer.