Skip to content

Commit

Permalink
refactor: make flags code reusable
Browse files Browse the repository at this point in the history
flags related code defined in lregex.c and could be used in the file.
This patch makes the code usable outside of the file.
evalFlags is renamed to flagsEval.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Sep 7, 2014
1 parent 9692dbe commit 0397609
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 63 deletions.
78 changes: 78 additions & 0 deletions flags.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
*
* Copyright (c) 2000-2003, Darren Hiebert
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
* This module contains functions to process command line options.
*/

/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */

#include <string.h>
#include <stdio.h>

#include "flags.h"
#include "vstring.h"
#include "routines.h"

#define LONG_FLAG_OPEN '{'
#define LONG_FLAG_CLOSE '}'

void flagsEval (const char* flags, flagDefinition* defs, unsigned int ndefs, void* data)
{
unsigned int i, j;

if (!flags)
return;
if (!defs)
return;

for (i = 0 ; flags [i] != '\0' ; ++i)
{
if (flags [i] == LONG_FLAG_OPEN)
{
const char* aflag = flags + i + 1;
char* needle_close_paren = strchr(aflag, LONG_FLAG_CLOSE);
const char* param;
char* needle_eqaul;

if (needle_close_paren == NULL)
{
error (WARNING, "long flags specifier opened with `%c' is not closed `%c'",
LONG_FLAG_OPEN, LONG_FLAG_CLOSE);
break;
}

*needle_close_paren = '\0';
needle_eqaul = strchr(aflag, '=');
if ((needle_eqaul == NULL || (needle_eqaul >= needle_close_paren)))
{
needle_eqaul = NULL;
param = NULL;
}
else
{
param = needle_eqaul + 1;
*needle_eqaul = '\0';
}

for ( j = 0 ; j < ndefs ; ++j )
if (defs[j].long_str && (strcmp(aflag, defs[j].long_str) == 0))
defs[j].long_proc(aflag, param, data);

if (needle_eqaul)
*needle_eqaul = '=';
*needle_close_paren = LONG_FLAG_CLOSE;

i = needle_close_paren - flags;
}
else for (j = 0 ; j < ndefs ; ++j)
if (flags[i] == defs[j].short_char)
defs[j].short_proc(flags[i], data);
}
}
24 changes: 24 additions & 0 deletions flags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
*
*
* Copyright (c) 2000-2003, Darren Hiebert
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
* Defines external interface to option processing.
*/
#ifndef _FLAGS_H
#define _FLAGS_H

typedef struct {
char short_char;
char *long_str;
void (* short_proc) (char c, void *data);
void (* long_proc) (const char* const s, const char* const param, void *data);
/* TODO: handler for help message */
} flagDefinition;

extern void flagsEval (const char* flags, flagDefinition* defs, unsigned int ndefs, void* data);

#endif /* _FLAGS_H */
63 changes: 1 addition & 62 deletions lregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "debug.h"
#include "entry.h"
#include "flags.h"
#include "parse.h"
#include "read.h"
#include "routines.h"
Expand All @@ -50,9 +51,6 @@

#define REGEX_NAME "Regex"

#define LONG_FLAG_OPEN '{'
#define LONG_FLAG_CLOSE '}'

/*
* DATA DECLARATIONS
*/
Expand Down Expand Up @@ -89,12 +87,6 @@ typedef struct {
unsigned int count;
} patternSet;

typedef struct {
char short_char;
char *long_str;
void (* short_proc) (char c, void *data);
void (* long_proc) (const char* const s, const char* const param, void *data);
} flagDefinition;
#define COUNT(D) (sizeof(D)/sizeof(D[0]))

/*
Expand All @@ -111,59 +103,6 @@ static int SetUpper = -1; /* upper language index in list */
* FUNCTION DEFINITIONS
*/

static void evalFlags (const char* flags, flagDefinition* defs, unsigned int ndefs, void* data)
{
unsigned int i, j;

if (!flags)
return;
if (!defs)
return;

for (i = 0 ; flags [i] != '\0' ; ++i)
{
if (flags [i] == LONG_FLAG_OPEN)
{
const char* aflag = flags + i + 1;
char* needle_close_paren = strchr(aflag, LONG_FLAG_CLOSE);
const char* param;
char* needle_eqaul;

if (needle_close_paren == NULL)
{
error (WARNING, "long flags specifier opened with `%c' is not closed `%c'",
LONG_FLAG_OPEN, LONG_FLAG_CLOSE);
break;
}

*needle_close_paren = '\0';
needle_eqaul = strchr(aflag, '=');
if ((needle_eqaul == NULL || (needle_eqaul >= needle_close_paren)))
{
needle_eqaul = NULL;
param = NULL;
}
else
{
param = needle_eqaul + 1;
*needle_eqaul = '\0';
}

for ( j = 0 ; j < ndefs ; ++j )
if (defs[j].long_str && (strcmp(aflag, defs[j].long_str) == 0))
defs[j].long_proc(aflag, param, data);

if (needle_eqaul)
*needle_eqaul = '=';
*needle_close_paren = LONG_FLAG_CLOSE;

i = needle_close_paren - flags;
}
else for (j = 0 ; j < ndefs ; ++j)
if (flags[i] == defs[j].short_char)
defs[j].short_proc(flags[i], data);
}
}

static void clearPatternSet (const langType language)
{
Expand Down
4 changes: 3 additions & 1 deletion source.mak
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Shared macros

HEADERS = \
args.h ctags.h debug.h entry.h general.h get.h keyword.h \
args.h ctags.h debug.h entry.h flags.h general.h get.h keyword.h \
main.h options.h parse.h parsers.h read.h routines.h sort.h \
strlist.h trash.h vstring.h

Expand All @@ -24,6 +24,7 @@ SOURCES = \
entry.c \
erlang.c \
falcon.c \
flags.c \
flex.c \
fortran.c \
get.c \
Expand Down Expand Up @@ -93,6 +94,7 @@ OBJECTS = \
entry.$(OBJEXT) \
erlang.$(OBJEXT) \
falcon.$(OBJEXT) \
flags.$(OBJEXT) \
flex.$(OBJEXT) \
fortran.$(OBJEXT) \
get.$(OBJEXT) \
Expand Down

0 comments on commit 0397609

Please sign in to comment.