Design Pattern Examples
Overview of object-oriented design patterns
State_RemoveComments.c File Reference

Implementation of the State_RemoveComments() function, along with the state machine used to filter out comments out of a piece of source code, as used in the State Pattern. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "State_RemoveComments.h"
Include dependency graph for State_RemoveComments.c:

Go to the source code of this file.

Classes

struct  StateContext
 Represents the context in which the state machine runs. More...
 
struct  StateHandler
 Maps a value from the CurrentState enumeration to a function that handles the transition from that state to the next state based on the context. More...
 

Typedefs

typedef CurrentState(* StateFunctionPtr) (struct StateContext *)
 Alias for a function pointer that takes a StateContext object and returns a value from the CurrentState enumeration.
 
typedef struct StateContext StateContext
 Represents the context in which the state machine runs.
 

Enumerations

enum  CurrentState {
  State_Initial , State_NormalText , State_DoubleQuotedText , State_SingleQuotedText ,
  State_EscapedDoubleQuoteText , State_EscapedSingleQuoteText , State_StartComment , State_LineComment ,
  State_BlockComment , State_EndBlockComment , State_Done , State_Error
}
 Represents the current state of the state machine. More...
 
enum  _Constants { EOF_CHAR = (char)-1 }
 Contains constants used in the state machine. More...
 

Functions

static const char * _CurrentStateToString (CurrentState state)
 Convert the CurrentState enumeration to a string for output purposes.
 
static char _GetNextCharacter (StateContext *context)
 Retrieve the next character from the input.
 
static void _OutputCharacter (StateContext *context, char character)
 Save the character to the accumulation of the filtered text.
 
static CurrentState _State_NormalText_GoNextState (StateContext *context)
 Handles the state of normal text behavior.
 
static CurrentState _State_DoubleQuotedText_GoNextState (StateContext *context)
 Handles the state of being inside a double-quote string where filtering is essentially turned off until the end of the string is reached.
 
static CurrentState _State_SingleQuotedText_GoNextState (StateContext *context)
 Handles the state of being inside a single-quoted string where filtering is effectively turned off until the end of the string is reached.
 
static CurrentState _State_EscapedDoubleQuoteText_GoNextState (StateContext *context)
 Handles the state of being in an escaped character sequence inside a double-quoted string. We don't do anything with the escaped character other than output it. Handling escaped characters allows us to more accurately detect the end of the string.
 
static CurrentState _State_EscapedSingleQuoteText_GoNextState (StateContext *context)
 Handles the state of being in an escaped character sequence inside a single-quoted string. We don't do anything with the escaped character other than output it. Handling escaped characters allows us to more accurately detect the end of the string.
 
static CurrentState _State_StartComment_GoNextState (StateContext *context)
 Handles the state of being at the possible start of a line or block comment.
 
static CurrentState _State_LineComment_GoNextState (StateContext *context)
 Handles the state of being in a line comment.
 
static CurrentState _State_BlockComment_GoNextState (StateContext *context)
 Handles the state of being in a block comment.
 
static CurrentState _State_EndBlockComment_GoNextState (StateContext *context)
 Handles the state of possibly being at the end of a block comment.
 
static CurrentState _State_Done_GoNextState (StateContext *context)
 Handles the state of being done with input.
 
static StateFunctionPtr _GetStateFunction (CurrentState state)
 Retrieve the function that is used to transition from the given state to another state.
 
static bool _SetNextState (StateContext *context, CurrentState newState)
 Helper method to transition the state machine to the specified state. Does nothing if the new state is the same as the old state. Instantiates the state class the first time the state class is needed.
 
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.
 

Variables

static StateHandler _stateHandlers []
 Array of StateHandler objects that map CurrentState value to functions that transition from that state to the next based on the context.
 

Detailed Description

Implementation of the State_RemoveComments() function, along with the state machine used to filter out comments out of a piece of source code, as used in the State Pattern.

Definition in file State_RemoveComments.c.

Typedef Documentation

◆ StateContext

typedef struct StateContext StateContext

Represents the context in which the state machine runs.

◆ StateFunctionPtr

typedef CurrentState(* StateFunctionPtr) (struct StateContext *)

Alias for a function pointer that takes a StateContext object and returns a value from the CurrentState enumeration.

Definition at line 36 of file State_RemoveComments.c.

Enumeration Type Documentation

◆ _Constants

enum _Constants

Contains constants used in the state machine.

Enumerator
EOF_CHAR 

Indicates end of string.

Definition at line 41 of file State_RemoveComments.c.

◆ CurrentState

Represents the current state of the state machine.

Enumerator
State_Initial 

State before the state machine actually starts. transitions to NormalText.

State_NormalText 

" transitions to QuotedText, / transitions to StartComment, EOF_CHAR transitions to Done

State_DoubleQuotedText 

\ transitions to EscapedDoubleQuoteText, " transitions to NormalText, EOF_CHAR transitions to Done

State_SingleQuotedText 

' transitions to EscapedSingleQuoteText, \ transitions to NormalText, EOF_CHAR transitions to Done

State_EscapedDoubleQuoteText 

\ transitions to QuotedText, EOF_CHAR transitions to Done

State_EscapedSingleQuoteText 

\ transitions to SingleQuotedText, EOF_CHAR transitions to Done

State_StartComment 

/ transitions to LineComment, * transitions to BlockComment, EOF_CHAR transitions to Done, all else transitions to NormalText

State_LineComment 

\n transitions to NormalText, EOF_CHAR transitions to Done

State_BlockComment 

* transitions to EndBlockComment, EOF_CHAR transitions to Done

State_EndBlockComment 

/ transitions to NormalText, EOF_CHAR transitions to Done, all else transitions to BlockComment

State_Done 

Indicates processing is done.

State_Error 

Indicates an error occurred and the state machine needs to exit.

Definition at line 15 of file State_RemoveComments.c.

Function Documentation

◆ _CurrentStateToString()

static const char * _CurrentStateToString ( CurrentState  state)
static

Convert the CurrentState enumeration to a string for output purposes.

Parameters
stateA value from the CurrentState enumeration.
Returns
Returns a string containing the string version of the given state.

Definition at line 69 of file State_RemoveComments.c.

References State_BlockComment, State_Done, State_DoubleQuotedText, State_EndBlockComment, State_Error, State_EscapedDoubleQuoteText, State_EscapedSingleQuoteText, State_Initial, State_LineComment, State_NormalText, State_SingleQuotedText, and State_StartComment.

Referenced by _SetNextState().

◆ _GetNextCharacter()

static char _GetNextCharacter ( StateContext context)
static

◆ _GetStateFunction()

static StateFunctionPtr _GetStateFunction ( CurrentState  state)
static

Retrieve the function that is used to transition from the given state to another state.

Parameters
stateA value from the CurrentState enumeration indicating state for which to get a transition function.
Returns
Returns a StateFunctionPtr for the specified state.

Definition at line 679 of file State_RemoveComments.c.

References _stateHandlers, and StateHandler::stateHandler.

Referenced by _SetNextState().

◆ _OutputCharacter()

static void _OutputCharacter ( StateContext context,
char  character 
)
static

Save the character to the accumulation of the filtered text.

Parameters
contextThe StateContext object controlling the state machine and which contains the DynamicString object being used to accumulate the filtered text.
characterThe character to save.

Definition at line 173 of file State_RemoveComments.c.

References EOF_CHAR, StateContext::outputText, and StateContext::outputTextIndex.

Referenced by _State_DoubleQuotedText_GoNextState(), _State_EscapedDoubleQuoteText_GoNextState(), _State_EscapedSingleQuoteText_GoNextState(), _State_LineComment_GoNextState(), _State_NormalText_GoNextState(), _State_SingleQuotedText_GoNextState(), and _State_StartComment_GoNextState().

◆ _SetNextState()

static bool _SetNextState ( StateContext context,
CurrentState  newState 
)
static

Helper method to transition the state machine to the specified state. Does nothing if the new state is the same as the old state. Instantiates the state class the first time the state class is needed.

Parameters
contextThe StateContext object that controls the state machine.
newStateA value from the CurrentState enumeration indicating the state to which to transition.
Returns
Returns true if the next state was successfully set; otherwise, returns false, indicating some kind of problem.

Definition at line 713 of file State_RemoveComments.c.

References _CurrentStateToString(), _GetStateFunction(), StateContext::currentState, and StateContext::currentStateBehavior.

Referenced by State_RemoveComments().

◆ _State_BlockComment_GoNextState()

static CurrentState _State_BlockComment_GoNextState ( StateContext context)
static

Handles the state of being in a block comment.

Process the next character from the context, returning the next state the context should move to.

Transitions to the following states for the seen input:

  • * - go to State_EndBlockComment (possible end of block comment)
  • EOF_CHAR - go to State_Done (no more input)
Parameters
contextA StateContext object representing the context to use for getting the next character of input and to which to output characters.
Returns
Returns a value from the CurrentState enumeration indicating the next state to which the state machine should transition to.

Definition at line 541 of file State_RemoveComments.c.

References _GetNextCharacter(), EOF_CHAR, State_BlockComment, State_Done, State_EndBlockComment, and State_Error.

◆ _State_Done_GoNextState()

static CurrentState _State_Done_GoNextState ( StateContext context)
static

Handles the state of being done with input.

Process the next character from the context, returning the next state the context should move to.

Transitions to the following states for the seen input:

  • Always stay in State_Done
Parameters
contextA StateContext object representing the context to use for getting the next character of input and to which to output characters.
Returns
Returns a value from the CurrentState enumeration indicating the next state to which the state machine should transition to.

Definition at line 626 of file State_RemoveComments.c.

References State_Done, and State_Error.

◆ _State_DoubleQuotedText_GoNextState()

static CurrentState _State_DoubleQuotedText_GoNextState ( StateContext context)
static

Handles the state of being inside a double-quote string where filtering is essentially turned off until the end of the string is reached.

Process the next character from the context, returning the next state the context should move to.

Transitions to the following states for the seen input:

  • " - go to State_NormalText (end of a double-quoted string)
  • \ - go to State_EscapedDoubleQuoteText (start of an escaped character)
  • EOF_CHAR - go to State_Done (no more input)
Parameters
contextA StateContext object representing the context to use for getting the next character of input and to which to output characters.
Returns
Returns a value from the CurrentState enumeration indicating the next state to which the state machine should transition to.

Definition at line 259 of file State_RemoveComments.c.

References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_DoubleQuotedText, State_Error, State_EscapedDoubleQuoteText, and State_NormalText.

◆ _State_EndBlockComment_GoNextState()

static CurrentState _State_EndBlockComment_GoNextState ( StateContext context)
static

Handles the state of possibly being at the end of a block comment.

Process the next character from the context, returning the next state the context should move to.

Transitions to the following states for the seen input:

  • / - go to State_NormalText (found end of block comment)
  • {ANY} - go to State_BlockComment (still in block comment)
  • EOF_CHAR - go to State_Done (no more input)
Parameters
contextA StateContext object representing the context to use for getting the next character of input and to which to output characters.
Returns
Returns a value from the CurrentState enumeration indicating the next state to which the state machine should transition to.

Definition at line 584 of file State_RemoveComments.c.

References _GetNextCharacter(), EOF_CHAR, State_BlockComment, State_Done, State_Error, and State_NormalText.

◆ _State_EscapedDoubleQuoteText_GoNextState()

static CurrentState _State_EscapedDoubleQuoteText_GoNextState ( StateContext context)
static

Handles the state of being in an escaped character sequence inside a double-quoted string. We don't do anything with the escaped character other than output it. Handling escaped characters allows us to more accurately detect the end of the string.

Process the next character from the context, returning the next state the context should move to.

Transitions to the following states for the seen input:

  • {ANY} - go to State_DoubleQuotedText (end of escape sequence)
  • EOF_CHAR - go to State_Done (no more input)
Parameters
contextA StateContext object representing the context to use for getting the next character of input and to which to output characters.
Returns
Returns a value from the CurrentState enumeration indicating the next state to which the state machine should transition to.

Definition at line 361 of file State_RemoveComments.c.

References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_DoubleQuotedText, and State_Error.

◆ _State_EscapedSingleQuoteText_GoNextState()

static CurrentState _State_EscapedSingleQuoteText_GoNextState ( StateContext context)
static

Handles the state of being in an escaped character sequence inside a single-quoted string. We don't do anything with the escaped character other than output it. Handling escaped characters allows us to more accurately detect the end of the string.

Process the next character from the context, returning the next state the context should move to.

Transitions to the following states for the seen input:

  • {ANY} - go to State_SingleQuotedText (end of escape sequence)
  • EOF_CHAR - go to State_Done (no more input)
Parameters
contextA StateContext object representing the context to use for getting the next character of input and to which to output characters.
Returns
Returns a value from the CurrentState enumeration indicating the next state to which the state machine should transition to.

Definition at line 403 of file State_RemoveComments.c.

References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_Error, and State_SingleQuotedText.

◆ _State_LineComment_GoNextState()

static CurrentState _State_LineComment_GoNextState ( StateContext context)
static

Handles the state of being in a line comment.

Process the next character from the context, returning the next state the context should move to.

Transitions to the following states for the seen input:


  • - go to State_NormalText (a newline is the end of a line comment)
  • EOF_CHAR - go to State_Done (no more input)
Parameters
contextA StateContext object representing the context to use for getting the next character of input and to which to output characters.
Returns
Returns a value from the CurrentState enumeration indicating the next state to which the state machine should transition to.

Definition at line 497 of file State_RemoveComments.c.

References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_Error, State_LineComment, and State_NormalText.

◆ _State_NormalText_GoNextState()

static CurrentState _State_NormalText_GoNextState ( StateContext context)
static

Handles the state of normal text behavior.

Process the next character from the context, returning the next state the context should move to.

Transitions to the following states for the seen input:

  • " : go to State_DoubleQuotedText (start of a double-quoted string)
  • ' : go to State_SingleQuotedText (start of a single-quoted string)
  • / : go to State_StartComment (start of a line or block comment)
  • EOF_CHAR : go to State_Done (no more input)
Parameters
contextA StateContext object representing the context to use for getting the next character of input and to which to output characters.
Returns
Returns a value from the CurrentState enumeration indicating the next state to which the state machine should transition to.

Definition at line 204 of file State_RemoveComments.c.

References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_DoubleQuotedText, State_Error, State_NormalText, State_SingleQuotedText, and State_StartComment.

◆ _State_SingleQuotedText_GoNextState()

static CurrentState _State_SingleQuotedText_GoNextState ( StateContext context)
static

Handles the state of being inside a single-quoted string where filtering is effectively turned off until the end of the string is reached.

Process the next character from the context, returning the next state the context should move to.

Transitions to the following states for the seen input:

  • ' - go to State_NormalText (end of a single-quoted string)
  • \ - go to State_EscapedSingleQuoteText (start of an escaped character)
  • EOF_CHAR - go to State_Done (no more input)
Parameters
contextA StateContext object representing the context to use for getting the next character of input and to which to output characters.
Returns
Returns a value from the CurrentState enumeration indicating the next state to which the state machine should transition to.

Definition at line 310 of file State_RemoveComments.c.

References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_Error, State_EscapedSingleQuoteText, State_NormalText, and State_SingleQuotedText.

◆ _State_StartComment_GoNextState()

static CurrentState _State_StartComment_GoNextState ( StateContext context)
static

Handles the state of being at the possible start of a line or block comment.

Process the next character from the context, returning the next state the context should move to.

Transitions to the following states for the seen input:

  • / - go to State_LineComment (start of a line comment)
  • * - go to State_BlockComment (start of a block comment)
  • {ANY} - go to State_NormalText (not start of a comment)
  • EOF_CHAR - go to State_Done (no more input)
Parameters
contextA StateContext object representing the context to use for getting the next character of input and to which to output characters.
Returns
Returns a value from the CurrentState enumeration indicating the next state to which the state machine should transition to.

Definition at line 445 of file State_RemoveComments.c.

References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_BlockComment, State_Done, State_Error, State_LineComment, State_NormalText, and State_StartComment.

◆ State_RemoveComments()

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.

Parameters
textThe text to filter.
filteredTextA DynamicString that returns the filtered text.
Returns
Returns true if the text was filtered successfully, otherwise, returns false if an out of memory condition occurs (or a NULL argument).

Definition at line 745 of file State_RemoveComments.c.

References _SetNextState(), StateContext::currentState, StateContext::currentStateBehavior, DynamicString_Set(), StateContext::inputText, StateContext::outputText, StateContext::outputTextIndex, State_Done, State_Error, State_Initial, State_NormalText, and StateContext::textIndex.

Referenced by State_Exercise().

Variable Documentation

◆ _stateHandlers

StateHandler _stateHandlers[]
static
Initial value:
= {
{ State_Initial , NULL },
}
static CurrentState _State_DoubleQuotedText_GoNextState(StateContext *context)
Handles the state of being inside a double-quote string where filtering is essentially turned off unt...
static CurrentState _State_EscapedSingleQuoteText_GoNextState(StateContext *context)
Handles the state of being in an escaped character sequence inside a single-quoted string....
static CurrentState _State_SingleQuotedText_GoNextState(StateContext *context)
Handles the state of being inside a single-quoted string where filtering is effectively turned off un...
@ State_Done
Indicates processing is done.
@ State_StartComment
/ transitions to LineComment, * transitions to BlockComment, EOF_CHAR transitions to Done,...
@ State_NormalText
" transitions to QuotedText, / transitions to StartComment, EOF_CHAR transitions to Done
@ State_BlockComment
* transitions to EndBlockComment, EOF_CHAR transitions to Done
@ State_EscapedSingleQuoteText
\ transitions to SingleQuotedText, EOF_CHAR transitions to Done
@ State_DoubleQuotedText
\ transitions to EscapedDoubleQuoteText, " transitions to NormalText, EOF_CHAR transitions to Done
@ State_EscapedDoubleQuoteText
\ transitions to QuotedText, EOF_CHAR transitions to Done
@ State_EndBlockComment
/ transitions to NormalText, EOF_CHAR transitions to Done, all else transitions to BlockComment
@ State_SingleQuotedText
' transitions to EscapedSingleQuoteText, \ transitions to NormalText, EOF_CHAR transitions to Done
@ State_LineComment
\n transitions to NormalText, EOF_CHAR transitions to Done
@ State_Initial
State before the state machine actually starts. transitions to NormalText.
static CurrentState _State_LineComment_GoNextState(StateContext *context)
Handles the state of being in a line comment.
static CurrentState _State_EndBlockComment_GoNextState(StateContext *context)
Handles the state of possibly being at the end of a block comment.
static CurrentState _State_NormalText_GoNextState(StateContext *context)
Handles the state of normal text behavior.
static CurrentState _State_BlockComment_GoNextState(StateContext *context)
Handles the state of being in a block comment.
static CurrentState _State_StartComment_GoNextState(StateContext *context)
Handles the state of being at the possible start of a line or block comment.
static CurrentState _State_EscapedDoubleQuoteText_GoNextState(StateContext *context)
Handles the state of being in an escaped character sequence inside a double-quoted string....
static CurrentState _State_Done_GoNextState(StateContext *context)
Handles the state of being done with input.

Array of StateHandler objects that map CurrentState value to functions that transition from that state to the next based on the context.

Definition at line 658 of file State_RemoveComments.c.

Referenced by _GetStateFunction().