Design Pattern Examples
Overview of object-oriented design patterns
Decorator_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/formatstring.h"
13
14#include "Decorator_Exercise.h"
15
16
17//=============================================================================
18//=============================================================================
19
29DynamicString* _Decorate(DynamicString* s, const char* decoration)
30{
31 if (s != NULL && decoration != NULL)
32 {
33 char *line = formatstring(decoration, s->string);
34 if (line != NULL)
35 {
36 DynamicString_Set(s, line);
37 free(line);
38 }
39 }
40 return s;
41}
42
43
53{
54 return _Decorate(s, "\x1b[31m%s\x1b[39m");
55}
56
66{
67 return _Decorate(s, "\x1b[47m%s\x1b[49m");
68}
69
79{
80 return _Decorate(s, "\x1b[4m%s\x1b[24m");
81}
82
83
84//=============================================================================
85//=============================================================================
86
96// ! [Using Decorator in C]
98{
99 printf("\nDecorator_Exercise\n");
100
101 const char* text = "This is raw text";
102
103 DynamicString string;
105 DynamicString_Set(&string, text);
106
107 // Wrap the base element in three decorators.
108 DynamicString* rendering = NULL;
110
111 printf(" Base Text element: \"%s\"\n", text);
112 printf(" Decorated element: \"%s\"\n", rendering->string);
113 DynamicString_Clear(&string);
114
115 printf(" Done.\n");
116}
117// ! [Using Decorator in C]
DynamicString * UnderlineDecorator(DynamicString *s)
Represents the Underline decorator, which alters the wrapped content to render it as underlined.
DynamicString * RedForegroundDecorator(DynamicString *s)
Represents the RedForeground decorator, which alters the wrapped content to render as red text.
DynamicString * WhiteBackgroundDecorator(DynamicString *s)
Represents the WhiteBackground decorator, which alters the wrapped content to render the background c...
void Decorator_Exercise(void)
Example of using the Decorator Pattern.
DynamicString * _Decorate(DynamicString *s, const char *decoration)
Helper function for applying decorations to the given string.
Declaration of the Decorator_Exercise() function as used in the Decorator Pattern.
void DynamicString_Clear(DynamicString *string)
Clear a DynamicString object, releasing any allocated memory. Resets to an empty string.
Definition: dynamicstring.c:27
void DynamicString_Initialize(DynamicString *string)
Initialize a DynamicString object to an empty string.
Definition: dynamicstring.c:15
bool DynamicString_Set(DynamicString *string, const char *s)
Set the DynamicString object to the specified string, replacing whatever is in the DynamicString obje...
Definition: dynamicstring.c:73
Declaration of the DynamicString structure and supporting functions to work with dynamic strings.
char * formatstring(const char *format,...)
Use the given string and arguments to return a buffer containing the formatted string....
Definition: formatstring.c:15
Represents a string that can be grown dynamically.
Definition: dynamicstring.h:16
char * string
The string that can grow.
Definition: dynamicstring.h:17