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"
#ifdef DEBUG
#define debug(x...) fprintf(stderr, x)
#else
#define debug(x...) ((void)0)
#endif
%}
%x VAL
@ -7,12 +13,12 @@
%%
<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 */ }
<*>\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 */ }
= { BEGIN(VAL); return '='; }
. { return (int)yytext[0]; }

View File

@ -4,6 +4,12 @@
#include <string.h>
#include "ini.h"
#ifdef DEBUG
#define debug(x...) fprintf(stderr, x)
#else
#define debug(x...) ((void)0)
#endif
extern FILE *yyin;
int yylex(void);
int yyparse (void);
@ -70,7 +76,7 @@ SECTION:
prop *p = $4;
prop *q;
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);
free(p->name);
free(p->value);

View File

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