home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Jeux / demos / crystalPPC.lha / config.cpp < prev    next >
C/C++ Source or Header  |  1998-02-05  |  2KB  |  107 lines

  1. #include <math.h>
  2. #include <time.h>
  3. #include "system.h"
  4.  
  5. #ifndef DEF_H
  6. #include "def.h"
  7. #endif
  8.  
  9. #ifndef CONFIG_H
  10. #include "config.h"
  11. #endif
  12.  
  13. //---------------------------------------------------------------------------
  14.  
  15. Config::Config (char* filename)
  16. {
  17.   first = NULL;
  18.   FILE* fp = fopen (filename, "r");
  19.   if (!fp)
  20.   {
  21.     printf ("WARNING! Couldn't open config file '%s'. Using defaults.\n", filename);
  22.     return;
  23.   }
  24.   char buf[1000];
  25.   while (fgets (buf, 999, fp))
  26.   {
  27.     char* p;
  28.     p = strchr (buf, '\n');
  29.     if (p) *p = 0;
  30.  
  31.     p = strchr (buf, '=');
  32.     if (!p)
  33.     {
  34.       printf ("Bad line in config file:\n");
  35.       printf ("   '%s'\n", buf);
  36.       exit (0);
  37.     }
  38.     *p = 0;
  39.     add (buf, p+1);
  40.   }
  41. }
  42.  
  43. Config::~Config ()
  44. {
  45.   while (first)
  46.   {
  47.     ConfigEl* n = first->next;
  48.     delete [] first->name;
  49.     delete [] first->val;
  50.     delete first;
  51.     first = n;
  52.   }
  53. }
  54.  
  55. void Config::add (char* name, char* val)
  56. {
  57.   ConfigEl* n = new ConfigEl;
  58.   n->name = new char [strlen (name)+1];
  59.   strcpy (n->name, name);
  60.   n->val = new char [strlen (val)+1];
  61.   strcpy (n->val, val);
  62.   n->next = first;
  63.   first = n;
  64. }
  65.  
  66. Config::ConfigEl* Config::get (char* name)
  67. {
  68.   ConfigEl* c = first;
  69.   while (c)
  70.   {
  71.     if (!strcmp (c->name, name)) return c;
  72.     c = c->next;
  73.   }
  74.   return NULL;
  75. }
  76.  
  77. int Config::get_int (char* name, int def)
  78. {
  79.   ConfigEl* c = get (name);
  80.   if (!c) return def;
  81.   int rc;
  82.   sscanf (c->val, "%d", &rc);
  83.   return rc;
  84. }
  85.  
  86. char* Config::get_str (char* name, char* def)
  87. {
  88.   ConfigEl* c = get (name);
  89.   if (!c) return def;
  90.   return c->val;
  91. }
  92.  
  93. int Config::get_yesno (char* name, int def)
  94. {
  95.   ConfigEl* c = get (name);
  96.   if (!c) return def;
  97.   return !strcmp (c->val, "yes");
  98. }
  99.  
  100. //---------------------------------------------------------------------------
  101.  
  102. CrystConfig::CrystConfig (char* filename) : Config (filename)
  103. {
  104. }
  105.  
  106. //---------------------------------------------------------------------------
  107.