optionally use debugging statements

This commit is contained in:
mtheall 2012-06-04 15:09:42 -05:00
parent 35a6061307
commit 3729e541e5
3 changed files with 26 additions and 8 deletions

10
lex.l
View File

@ -1,5 +1,11 @@
%{ %{
#include "parse.h" #include "parse.h"
#ifdef DEBUG
#define debug(x...) fprintf(stderr, x)
#else
#define debug(x...) ((void)0)
#endif
%} %}
%x VAL %x VAL
@ -7,12 +13,12 @@
%% %%
<VAL>{ <VAL>{
[^ \t\n][^\n]* { BEGIN(INITIAL); printf("value %s\n", yytext); yylval.str = strdup(yytext); return VALUE; } [^ \t\n][^\n]* { BEGIN(INITIAL); debug("value %s\n", yytext); yylval.str = strdup(yytext); return VALUE; }
} }
<*>[ \t]* { /* ignore whitespace */ } <*>[ \t]* { /* ignore whitespace */ }
<*>\n { /* eat newlines */ } <*>\n { /* eat newlines */ }
[a-zA-Z0-9_+-]+ { printf("id %s\n", yytext); yylval.str = strdup(yytext); return ID; } [a-zA-Z0-9_+-]+ { debug("id %s\n", yytext); yylval.str = strdup(yytext); return ID; }
;.* { /* ignore comments */ } ;.* { /* ignore comments */ }
= { BEGIN(VAL); return '='; } = { BEGIN(VAL); return '='; }
. { return (int)yytext[0]; } . { return (int)yytext[0]; }

View File

@ -4,6 +4,12 @@
#include <string.h> #include <string.h>
#include "ini.h" #include "ini.h"
#ifdef DEBUG
#define debug(x...) fprintf(stderr, x)
#else
#define debug(x...) ((void)0)
#endif
extern FILE *yyin; extern FILE *yyin;
int yylex(void); int yylex(void);
int yyparse (void); int yyparse (void);
@ -70,7 +76,7 @@ SECTION:
prop *p = $4; prop *p = $4;
prop *q; prop *q;
while(p != NULL) { while(p != NULL) {
printf("Inserting [%s] %s = %s\n", section, p->name, p->value); debug("Inserting [%s] %s = %s\n", section, p->name, p->value);
IniSetValue(ini, section, p->name, p->value); IniSetValue(ini, section, p->name, p->value);
free(p->name); free(p->name);
free(p->value); free(p->value);

View File

@ -3,6 +3,12 @@
#include <string.h> #include <string.h>
#include "ini.h" #include "ini.h"
#ifdef DEBUG
#define debug(x...) fprintf(stderr, x)
#else
#define debug(x...) ((void)0)
#endif
struct IniProperty { struct IniProperty {
char *name; char *name;
char *value; char *value;
@ -19,7 +25,7 @@ typedef struct IniProperty IniProperty;
typedef struct IniSection IniSection; typedef struct IniSection IniSection;
static inline IniProperty* __IniAllocProperty(const char *name, const char *value) { static inline IniProperty* __IniAllocProperty(const char *name, const char *value) {
printf("Create property %s = %s\n", name, value); debug("Create property %s = %s\n", name, value);
IniProperty *p = malloc(sizeof(IniProperty)); IniProperty *p = malloc(sizeof(IniProperty));
memset(p, 0, sizeof(IniProperty)); memset(p, 0, sizeof(IniProperty));
if(p) { if(p) {
@ -61,7 +67,7 @@ static inline IniProperty* __IniGetProperty(IniSection *s, const char *property)
} }
static inline IniSection* __IniAllocSection(const char *name, const char *property, const char *value) { static inline IniSection* __IniAllocSection(const char *name, const char *property, const char *value) {
printf("Creating section [%s] %s = %s\n", name, property, value); debug("Creating section [%s] %s = %s\n", name, property, value);
IniSection *s = malloc(sizeof(IniSection)); IniSection *s = malloc(sizeof(IniSection));
memset(s, 0, sizeof(IniSection)); memset(s, 0, sizeof(IniSection));
if(s) { if(s) {
@ -158,7 +164,7 @@ FEOS_EXPORT int IniSetValue(Ini ini, const char *section, const char *property,
/* special case: initialized but no entries */ /* special case: initialized but no entries */
if(s->name == NULL) { if(s->name == NULL) {
printf("Create section [%s]\n", section); debug("Create section [%s]\n", section);
s->name = strdup(section); s->name = strdup(section);
if(s->name == NULL) if(s->name == NULL)
return -1; return -1;
@ -184,7 +190,7 @@ FEOS_EXPORT int IniSetValue(Ini ini, const char *section, const char *property,
return 0; return 0;
} }
else else
printf("Found section [%s]\n", section); debug("Found section [%s]\n", section);
/* find property */ /* find property */
p = __IniGetProperty(s, property); p = __IniGetProperty(s, property);
@ -198,7 +204,7 @@ FEOS_EXPORT int IniSetValue(Ini ini, const char *section, const char *property,
return 0; return 0;
} }
else else
printf("Found property [%s] %s\n", section, property); debug("Found property [%s] %s\n", section, property);
/* update value */ /* update value */
tmp = p->value; tmp = p->value;