Implementation of the Command_Exercise() function as used in the Command Pattern. More...
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "helpers/replace.h"
#include "helpers/stack.h"
#include "Command_Exercise.h"
#include "Command_Command.h"
Go to the source code of this file.
Functions | |
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 command was created. | |
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. | |
static void | Command_Operation_Reverse (Command_TextObject *source) |
An operation to reverse the characters in the given Command_TextObject. | |
static void | Command_Undo (Command_TextObject *text) |
Perform an undo on the given Command_TextObject, using the commands in the "global" undo list. If the undo list is empty, nothing happens. | |
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, adds the command to the undo list and then applies the command to the Command_TextObject. Finally, it shows off what was done. | |
static void | Command_ApplyReverseCommand (Command_TextObject *text) |
Helper function to create a Command object that reverses the order of the characters in the given Command_TextObject, adds the command to the undo list and then applies the command to the Command_TextObject. Finally, it shows what was done. | |
void | Command_Exercise (void) |
Example of using the Command Pattern. | |
Variables | |
static StackEntry * | _commandUndoList = NULL |
The stack used to remember the commands for undo. | |
Implementation of the Command_Exercise() function as used in the Command Pattern.
Definition in file Command_Exercise.c.
|
static |
Helper function to create a Command object that replaces text in the given Command_TextObject, adds the command to the undo list and then applies the command to the Command_TextObject. Finally, it shows off what was done.
text | The Command_TextObject to affect. |
searchPattern | What to look for in the Command_TextObject. |
replaceText | What to replace the searchPattern with. |
Definition at line 133 of file Command_Exercise.c.
References Command_Create_Two_Parameters(), Command_Operation_Replace(), Command_Save_And_Execute(), Command_TextObject_ToString(), and Command_ToString().
Referenced by DesignPatternExamples_cpp::Command_Exercise(), and Command_Exercise().
|
static |
Helper function to create a Command object that reverses the order of the characters in the given Command_TextObject, adds the command to the undo list and then applies the command to the Command_TextObject. Finally, it shows what was done.
text | The Command_TextObject to affect. |
Definition at line 155 of file Command_Exercise.c.
References Command_Create_No_Parameters(), Command_Operation_Reverse(), Command_Save_And_Execute(), Command_TextObject_ToString(), and Command_ToString().
Referenced by DesignPatternExamples_cpp::Command_Exercise(), and Command_Exercise().
void Command_Exercise | ( | void | ) |
Example of using the Command Pattern.
The Command pattern is used to encapsulate an operation or command associated with an object so that the command can be applied to the object at a later time.
In this exercise, an undo list is implemented using Commands that associate commands defined in this file with a text object. The commands are applied to the text object in succession then effectively undone.
Definition at line 185 of file Command_Exercise.c.
References Command_ApplyReplaceCommand(), Command_ApplyReverseCommand(), Command_TextObject_Clear(), Command_TextObject_Initialize(), Command_TextObject_ToString(), and Command_Undo().
|
static |
An operation to search and replace text in a Command_TextObject.
source | The Command_TextObject to affect. |
searchPattern | What to look for in the Command_TextObject. |
replaceText | What to replace the searchPattern with. |
Definition at line 45 of file Command_Exercise.c.
References Command_TextObject_GetText(), Command_TextObject_SetText(), and replace_str().
Referenced by Command_ApplyReplaceCommand().
|
static |
An operation to reverse the characters in the given Command_TextObject.
source | The Command_TextObject to affect. |
Definition at line 62 of file Command_Exercise.c.
References Command_TextObject_GetText(), and Command_TextObject::text.
Referenced by Command_ApplyReverseCommand().
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 command was created.
command | The Command object to apply to the text object. |
Definition at line 32 of file Command_Exercise.c.
References _commandUndoList, Command_Execute(), and Stack_Push().
Referenced by Command_ApplyReplaceCommand(), and Command_ApplyReverseCommand().
|
static |
Perform an undo on the given Command_TextObject, using the commands in the "global" undo list. If the undo list is empty, nothing happens.
text | The Command_TextObject to affect. |
Definition at line 85 of file Command_Exercise.c.
References _commandUndoList, Command_Destroy(), Command_Execute(), Command_TextObject_Reset(), Command_TextObject_ToString(), Command_ToString(), Stack_IsEmpty(), Stack_Pop(), and Stack_Push().
Referenced by DesignPatternExamples_cpp::Command_Exercise(), and Command_Exercise().
|
static |
The stack used to remember the commands for undo.
Definition at line 21 of file Command_Exercise.c.
Referenced by Command_Save_And_Execute(), and Command_Undo().