File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / confuse / examples / wincfgtest.c
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:49:17 2021 UTC (3 years, 3 months ago) by misho
CVS tags: MAIN, HEAD
Initial revision

    1: /* Simple Windows program that calls LoadLibrary to load the libConfuse DLL
    2:  * into memory.
    3:  */
    4: 
    5: #include <stdio.h>
    6: #include <stdlib.h>
    7: #include <windows.h>
    8: 
    9: #include "confuse.h"
   10: 
   11: #ifdef __BORLANDC__
   12: # define DLLSYM(sym) "_" ## sym /* duh! */
   13: #else

   14: # define DLLSYM(sym) sym
   15: #endif

   16: 
   17: int cb_message(cfg_t *cfg, cfg_opt_t *opt, int argc, const char **argv)
   18: {
   19:     if(argc != 1)
   20:         MessageBox(0, "message() requires only 1 argument", "message() function", MB_OK);
   21:     else
   22:         MessageBox(0, argv[0], "message() function", MB_OK);
   23:     return 0;
   24: }
   25: 
   26: static void display_last_error(void)
   27: {
   28:     LPVOID lpMsgBuf;
   29:     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
   30:        			  FORMAT_MESSAGE_FROM_SYSTEM |
   31:        			  FORMAT_MESSAGE_IGNORE_INSERTS,
   32:        			  NULL,
   33:        			  GetLastError(),
   34:        			  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
   35:        			  (LPTSTR) &lpMsgBuf,
   36:        			  0,
   37:        			  NULL);
   38: 
   39:     MessageBox(NULL, (LPCTSTR)lpMsgBuf, "libConfuse error", MB_OK | MB_ICONINFORMATION );
   40:     LocalFree( lpMsgBuf );
   41: }
   42: 
   43: typedef cfg_t *(*CFG_INIT_FPTR)(cfg_opt_t *opts, cfg_flag_t flags);
   44: typedef void (*CFG_FREE_FPTR)(cfg_t *cfg);
   45: typedef int (*CFG_PARSE_FPTR)(cfg_t *cfg, const char *filename);
   46: typedef cfg_bool_t (*CFG_GETBOOL_FPTR)(cfg_t *cfg, const char *name);
   47: typedef long int (*CFG_GETINT_FPTR)(cfg_t *cfg, const char *name);
   48: typedef char * (*CFG_GETSTR_FPTR)(cfg_t *cfg, const char *name);
   49: typedef double (*CFG_GETFLOAT_FPTR)(cfg_t *cfg, const char *name);
   50: 
   51: int main(void)
   52: {
   53:     cfg_opt_t opts[] = {
   54:         CFG_BOOL("bool", cfg_false, 0),
   55:         CFG_STR("string", "default test string", 0),
   56:         CFG_INT("number", 17, 0),
   57:         CFG_FLOAT("float", 6.789, 0),
   58:         CFG_FUNC("message", &cb_message),
   59:         CFG_END()
   60:     };
   61:     HINSTANCE hinstLib;
   62:     char buf[1024];
   63: 
   64:     CFG_INIT_FPTR cfg_init;
   65:     CFG_PARSE_FPTR cfg_parse;
   66:     CFG_FREE_FPTR cfg_free;
   67:     CFG_GETBOOL_FPTR cfg_getbool;
   68:     CFG_GETINT_FPTR cfg_getint;
   69:     CFG_GETSTR_FPTR cfg_getstr;
   70:     CFG_GETFLOAT_FPTR cfg_getfloat;
   71: 
   72:     /* Get a handle to the DLL module. */
   73:     hinstLib = LoadLibrary("libConfuse");
   74: 
   75:     /* If the handle is valid, try to get the function address. */
   76:     if(hinstLib != NULL) {
   77:         cfg_init = (CFG_INIT_FPTR)GetProcAddress(hinstLib, DLLSYM("cfg_init"));
   78:         cfg_parse = (CFG_PARSE_FPTR)GetProcAddress(hinstLib, DLLSYM("cfg_parse"));
   79:         cfg_free = (CFG_FREE_FPTR)GetProcAddress(hinstLib, DLLSYM("cfg_free"));
   80:         cfg_getbool = (CFG_GETBOOL_FPTR)GetProcAddress(hinstLib, DLLSYM("cfg_getbool"));
   81:         cfg_getint = (CFG_GETINT_FPTR)GetProcAddress(hinstLib, DLLSYM("cfg_getint"));
   82:         cfg_getstr = (CFG_GETSTR_FPTR)GetProcAddress(hinstLib, DLLSYM("cfg_getstr"));
   83:         cfg_getfloat = (CFG_GETFLOAT_FPTR)GetProcAddress(hinstLib, DLLSYM("cfg_getfloat"));
   84: 
   85:         if(cfg_init) { /* assume the other functions also are valid */
   86:             cfg_t *cfg = cfg_init(opts, 0);
   87:             if(cfg_parse(cfg, "wincfgtest.conf") == CFG_FILE_ERROR)
   88:                 perror("wincfgtest.conf");
   89: 
   90:            	sprintf(buf, "bool:    %s\nstring:  %s\nnumber:  %ld\nfloat:   %f\n",
   91:             	cfg_getbool(cfg, "bool") ? "true" : "false", cfg_getstr(cfg, "string"),
   92:              	cfg_getint(cfg, "number"), cfg_getfloat(cfg, "float"));
   93:            	MessageBox(NULL, buf, "libConfuse", MB_OK);
   94: 
   95:             cfg_free(cfg);
   96:         } else
   97:         	display_last_error();
   98: 
   99:         FreeLibrary(hinstLib);
  100:     } else
  101:     	display_last_error();
  102: 
  103:     return 0;
  104: }
  105: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>