Design Pattern Examples
Overview of object-oriented design patterns
State_Exercise.c
Go to the documentation of this file.
1
5
6#include <stdbool.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
12#include "helpers/split.h"
13#include "helpers/strdup.h"
14
16
17#include "State_Exercise.h"
18
19//=============================================================================
20//=============================================================================
21
27static void _State_DisplayText(const char* textToDisplay)
28{
29 SplitList lines = { 0 };
30 char* text = STRDUP(textToDisplay);
31 if (text != NULL)
32 {
33 split(text, "\n", &lines);
34 int lineNumber = 1;
35 for (size_t index = 0; index < lines.strings_count; index++)
36 {
37 printf(" %2d) %s\n", lineNumber, lines.strings[index]);
38 lineNumber++;
39 }
40 free(text);
41 }
42 else
43 {
44 printf(" Error! Out of memory duplicating text to display with lines!\n");
45 }
46}
47
48
49//=============================================================================
50//=============================================================================
51
52static const char* textToFilter =
53"/*#################### Block Comment #################################*/\n"
54"//#################### Line Comment ####################################\n"
55"// A comment. /* A nested comment */\n"
56"\n"
57"void State_Exercise() // An exercise in state machines\n"
58"{\n"
59" char character = '\\\"';\n"
60" printf(\"\\n\");\n"
61" printf(\"\\\"State\\\" /*Exercise*/\\n\");\n"
62"\n"
63" bool success = State_RemoveComments(textToFilter, &filteredText);\n"
64"\n"
65" printf(\"\\t\\tDone. //(No, really)//\\n\");\n"
66"}";
67
68
82// ! [Using State in C]
84{
85 printf("\nState_Exercise\n");
86
87 printf(" Text to filter:\n");
89
90 printf(" Filtering text...\n");
91 DynamicString filteredText = { 0 };
92 bool success = State_RemoveComments(textToFilter, &filteredText);
93 if (success)
94 {
95 printf(" Filtered text:\n");
96 _State_DisplayText(filteredText.string);
97 }
98
99 DynamicString_Clear(&filteredText);
100
101 printf(" Done.\n");
102}
103// ! [Using State in C]
void State_Exercise(void)
Example of using the State Pattern.
static const char * textToFilter
static void _State_DisplayText(const char *textToDisplay)
Helper function to display text from the State exercise. Text is displayed with line numbers.
bool State_RemoveComments(const char *text, DynamicString *filteredText)
Entry point for callers to filter text. Removes C++-style line and block comments from the text.
Declaration of the State_RemoveComments() function that uses a state machine to filter out comments o...
Declaration of the State_Exercise() function as used in the State Pattern.
void DynamicString_Clear(DynamicString *string)
Clear a DynamicString object, releasing any allocated memory. Resets to an empty string.
Definition: dynamicstring.c:27
Declaration of the DynamicString structure and supporting functions to work with dynamic strings.
void split(char *s, const char *splitChars, SplitList *components)
Split the given path into multiple strings based on the given delimiter. The pointers to each string ...
Definition: split.c:72
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
Represents a string that can be grown dynamically.
Definition: dynamicstring.h:16
char * string
The string that can grow.
Definition: dynamicstring.h:17
Represents a collection of sub-strings split from a single string using the split() function.
Definition: helpers/split.h:17
size_t strings_count
Number of sub-strings.
Definition: helpers/split.h:19
const char ** strings
Pointers to each sub-string.
Definition: helpers/split.h:18