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

using namespace std;

int    my_int_value;
bool   my_bool_value;
string my_string_value;

void dump_file(string filename){
	char c;
	ifstream fin(filename.c_str(), ios::in);
  
	//check file
  	if(fin.fail()){
		printf("> error opening %s\n",filename.c_str());
		exit(1);
  	}
  
	//print content:
	while(fin){
		fin.get(c);
		if (fin) cout << c;
	}

	fin.close();
}

int main(int argc, char **arv){
	string config_filename = "/tmp/tconfig.test.cfg";
	TinyConfig tconfig;

	//setup variables & init default values
	tconfig.add_variable("my_int_value", &my_int_value, 1234);
	tconfig.add_variable("my_bool_value", &my_bool_value, true);
	tconfig.add_variable("my_string_value", &my_string_value, "test string");

	//store to file:
	printf("> storing variables to config file '%s'\n",config_filename.c_str());
	tconfig.save(config_filename);

	//print file contents:
	printf("> dumping config file contents:\n============================================\n");
	dump_file(config_filename);
	printf("============================================\n");

	//create dummy config that overrides default settings:
	printf("> writing new dummy config to file...\n");
	FILE *fh = fopen(config_filename.c_str(), "w");
	fprintf(fh, "my_bool_value[bool] = 0\nmy_int_value[int] = 54321\nmy_string_value[string] = new string\n");
	fclose(fh);
	printf("> file contents:\n============================================\n");
        dump_file(config_filename);
        printf("============================================\n");


	//load config
	printf("> value of my string before: '%s'\n",my_string_value.c_str());
	printf("> loading new config from file\n");
	tconfig.load(config_filename);
	printf("> value of my string after : '%s'\n",my_string_value.c_str());
	
	
}