12#include "helpers/replace.h"
54 printf(
" Error replacing text, probably out of memory.\n");
64 if (source != NULL && source->
text != NULL)
68 size_t textSize = strlen(text);
69 size_t halftextSize = textSize / 2;
70 for (
size_t index = 0; index < halftextSize; ++index)
73 text[index] = text[textSize - index - 1];
74 text[textSize - index - 1] = c;
143 printf(
" Error! Out of memory allocating a Command structure for replace operation.\n");
165 printf(
" Error! Out of memory allocating a Command structure for reverse operation.\n");
187 printf(
"\nCommand_Exercise\n");
200 printf(
" Now perform undo until back to original\n");
Command * Command_Create_No_Parameters(const char *commandName, Command_TextObject *receiver, no_parameter_operation operation)
Create a new Command object with the given parameters, creating a command that uses no additional par...
Command * Command_Create_Two_Parameters(const char *commandName, Command_TextObject *receiver, two_parameter_operation operation, const char *arg1, const char *arg2)
Create a new Command object with the given parameters, creating a command that uses two additional pa...
void Command_Execute(Command *commandObject)
Execute the given command on the Command_TextObject it knows about.
const char * Command_ToString(Command *commandObject)
Convert the given command object to a string representation.
void Command_Destroy(Command *commandObject)
Destroy the given command object, releasing it and any associated resources.
Declaration of the Command structure and associated functions as used in the Command Pattern.
void Command_Save_And_Execute(Command *command)
Save the given command on the undo list then execute the command on the text object with which the co...
static void Command_ApplyReplaceCommand(Command_TextObject *text, const char *searchPattern, const char *replaceText)
Helper function to create a Command object that replaces text in the given Command_TextObject,...
static StackEntry * _commandUndoList
The stack used to remember the commands for undo.
static void Command_Operation_Replace(Command_TextObject *source, const char *searchPattern, const char *replaceText)
An operation to search and replace text in a Command_TextObject.
void Command_Exercise(void)
Example of using the Command Pattern.
static void Command_ApplyReverseCommand(Command_TextObject *text)
Helper function to create a Command object that reverses the order of the characters in the given Com...
static void Command_Undo(Command_TextObject *text)
Perform an undo on the given Command_TextObject, using the commands in the "global" undo list....
static void Command_Operation_Reverse(Command_TextObject *source)
An operation to reverse the characters in the given Command_TextObject.
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_Exercise() function as used in the Command 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...
bool Stack_IsEmpty(StackEntry **stack)
Determines if the given stack is empty.
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.
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.
Represents an operation that can be applied to a Command_TextObject. Can hold one of two kinds of ope...
Represents an entry on a simple stack that wraps an "item" represented by an opaque pointer.