diff options
author | Ludovic Pouzenc <lpouzenc@gmail.com> | 2013-07-11 22:22:34 +0200 |
---|---|---|
committer | Ludovic Pouzenc <lpouzenc@gmail.com> | 2013-07-11 22:22:34 +0200 |
commit | 22ef62efac76adb97bd14be2d897823a108a154d (patch) | |
tree | 8cca6496080cedc6e5f7a82fde2fb4411cb5bb23 /src/parser.c | |
parent | 4a11294dd17f3e087477057a85add7b9b6b4ea5d (diff) | |
download | mplemmings-22ef62efac76adb97bd14be2d897823a108a154d.tar.gz mplemmings-22ef62efac76adb97bd14be2d897823a108a154d.tar.bz2 mplemmings-22ef62efac76adb97bd14be2d897823a108a154d.zip |
Debut de parser pour les fichiers styles/*.ini. Part en segfault, surement boucle infinie.
Diffstat (limited to 'src/parser.c')
-rw-r--r-- | src/parser.c | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..44ab4ed --- /dev/null +++ b/src/parser.c @@ -0,0 +1,153 @@ +#include "parser.h" + +#include "minIni.h" + +/* +long ini_getl(const mTCHAR *Section, const mTCHAR *Key, long DefValue, const mTCHAR *Filename); +int ini_gets(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *DefValue, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename); + +int ini_getsection(int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename); +int ini_getkey(const mTCHAR *Section, int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename); + +typedef int (*INI_CALLBACK)(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, const void *UserData); +int ini_browse(INI_CALLBACK Callback, const void *UserData, const mTCHAR *Filename); +*/ + +typedef struct { + int need_2pass; + gameIni_t *gIni; +} parserState_t; + +int callback_pass1(const mTCHAR *section, const mTCHAR *key, const mTCHAR *value, const void *userData); +int callback_pass2(const mTCHAR *Section, const mTCHAR *key, const mTCHAR *value, const void *userData); + + +int main() { + gameIni_t gIni; + return loadIni(&gIni,"/home/lpouzenc/Bureau/USB/lem/2010-netlemmings/trunk/styles/brick/brick.ini"); +} + +int loadIni(gameIni_t *gIni, const char *filepath) { + int res; + parserState_t state; + const void *UserData=(void *)&state; + state.gIni=gIni; + + (void) SDL_memset(&state,0,sizeof(parserState_t)); + (void) SDL_memset(gIni,0,sizeof(gameIni_t)); + + res=ini_browse(callback_pass1, UserData, filepath); + if (res!=0) return res; + if (state.need_2pass) { + res=ini_browse(callback_pass2, UserData, filepath); + } +} + +uint32_t hextext2rgb(const char str[]) { + return 42; //FIXME +} + +/*#define IS(str) if (SDL_strcasecmp(Key,str)==0) +#define PREFIX(str) if (SDL_strncasecmp(Key,str)==0) +*/ +int callback_pass1(const mTCHAR *section, const mTCHAR *key, const mTCHAR *value, const void *userData) { + gameIni_t *gIni = ((parserState_t*) userData)->gIni; + + if (SDL_strcasecmp(key,"bgcolor")==0) + {gIni->style.bgColor = hextext2rgb(value); return 0;} + if (SDL_strcasecmp(key,"debrisColor")==0) + {gIni->style.debrisColor = hextext2rgb(value); return 0;} + if (SDL_strcasecmp(key,"particleColor")==0) + { + char *wordBoundary; + int i=0; + ((parserState_t*) userData)->need_2pass=1; + + while ( strlen(value)>0 && (wordBoundary=strchr(value,',')) != NULL ) { + *wordBoundary='\0'; + //gIni->style.particleColor[i++]=hextext2rgb(value); // 2nd pass + value = wordBoundary+1; + } + // Parse the last one + //gIni->style.particleColor[i++]=hextext2rgb(value); //2nd pass + + if (gIni->style.particleColor != NULL ) { + return 1; + } + + gIni->style.particleColor=SDL_calloc(i,sizeof(uint32_t)); + if ( ! gIni->style.particleColor ) { + return 2; + } + gIni->style.particleColorCount=i; + + return 0; + } + if (SDL_strcasecmp(key,"tiles")==0) { + int v = atoi(value); + if ( v<0 || v>MAX_OBJECT_TYPES) return 3; + gIni->style.tiles = v; + return 0; + } + + if (SDL_strncasecmp(key,"frames_", 7) == 0) { + int i = atoi(value+7); + if ( i<0 || i>=gIni->style.tiles) return 4; + + int v = atoi(value); + if ( v<0 || v>MAX_OBJECT_FRAMES) return 5; + + gIni->style.objects[i].frames = v; + return 0; + } + + if (SDL_strncasecmp(key,"anim_", 5) == 0) { + int i = atoi(value+5); + if ( i<0 || i>=gIni->style.tiles) return 6; + + int v = atoi(value); + if ( v<0 || v>3) return 7; + + gIni->style.objects[i].anim = v; + return 0; + } + + if (SDL_strncasecmp(key,"type_", 5) == 0) { + int i = atoi(value+5); + if ( i<0 || i>=gIni->style.tiles) return 8; + + int v = atoi(value); + if (! (v==0 || v==32 || (v>=3 && v<=8))) return 9; + + gIni->style.objects[i].type = v; + return 0; + } + + if (SDL_strncasecmp(key,"sound_", 6) == 0) { + int i = atoi(value+6); + if ( i<0 || i>=gIni->style.tiles) return 10; + + int v = atoi(value); + if ( v<0 || v>MAX_SOUNDS_COUNT) return 11; + + gIni->style.objects[i].frames = v; + return 0; + } + + if (SDL_strncasecmp(key,"frames_", 7) == 0) { + int i = atoi(value+7); + if ( i<0 || i>=gIni->style.tiles) return 4; + + int v = atoi(value); + if ( v<0 || v>MAX_OBJECT_FRAMES) return 5; + + gIni->style.objects[i].frames = v; + return 0; + } + + return -1; +} + +int callback_pass2(const mTCHAR *section, const mTCHAR *key, const mTCHAR *value, const void *userData) { + +} |