/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                               DEMO PROGRAMS SET
                          registry.cpp  -  description
                             -------------------
    begin                : January 3, 2003
    copyright            : (C) 2003-2005 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef __BORLANDC__
#include <vcl.h>
#pragma hdrstop
#endif

#include <stdio.h>
#include <stdlib.h>

#include <sptk3/CRegistry.h>
#include <sptk3/CException.h>

using namespace std;

void printRegistry(CRegistryMode mode) {
   // Open user settings, file is located in user home directory
   CRegistry   mySettings("mySettings.ini","sptk_test",mode);
   try {
      mySettings.load();
      printf("---> Reading %s\n",mySettings.fileName().c_str());

      mySettings.topic("Window");
      printf("Window position: %s, %s\n",mySettings["x-position"].c_str(),mySettings["y-position"].c_str());

      mySettings.topic("Colors");
      printf("Window header: %s, background is %s, foreground is %s\n",
         mySettings["header"].c_str(),
         mySettings["header"]["background"].c_str(),
         mySettings["header"]["foreground"].c_str());
      printf("Window background: %s\n",mySettings["background"].c_str());
   }
   catch (exception& e) {
      puts(e.what());
      // Registry file not found, trying to create an empty one.
      mySettings.topic("Window");
      mySettings["header"] = "Test header";
      mySettings.save();
   }
}

void updateRegistry(CRegistryMode mode) {
   // Open user settings, file is located in user home directory
   CRegistry   mySettings("mySettings.ini","sptk_test",mode);
   try {
      printf("<--- Updating %s\n",mySettings.fileName().c_str());

      mySettings.topic("Window");
      mySettings["x-position"] = 100;
      mySettings["y-position"] = 150;

      mySettings.topic("Colors");
      // Using extended attributes.
      // Extended attributes are only possible to save if the output format is XML.
      // Otherwise - they are ignored ad lost.
      mySettings["header"] = "Test Colors";
      mySettings["header"]["background"] = "BLUE";
      mySettings["header"]["foreground"] = "WHITE";
      mySettings["background"] = "0xC0D0FF";

      mySettings.save();
   }
   catch (exception& e) {
      puts(e.what());
   }
}

int main() {
   puts("-------- Test for the USER (stored in homedir)  registry. -----------\n");

   // Print the original registry
   // The registry file is located in the user home directory
   printRegistry(USER_REGISTRY);

   // Define the registry values and save the registry 
   updateRegistry(USER_REGISTRY);

   // Print the changed registry
   printRegistry(USER_REGISTRY);

   return 0;
}
//---------------------------------------------------------------------------
