/*

TEST PROGRAM FOR THE reload(src,prog) API CALL...

Author: Matthew William Coan
Date: Wed Apr  2 16:45:30 EDT 2014

*/

#include <iostream>
#include <fstream>

#include <cstring>

#include "reload.h"

using namespace std;
using namespace Reload;

class ReloadTest {
   string prog;
   string src;
public:
   ReloadTest(const string & prog, 
              const string & src);
   void run();
};


ReloadTest::ReloadTest(const string & prog, 
                       const string & src)
{
   this->prog = prog;
   this->src = src;
}

void ReloadTest::run() {
   string key,data;
   while(true) {
      cout << "key: ";
      cin >> key;
      bool found = false;
      const char * array[] = {
#include "data.h"
         NULL,
      };
      for(size_t i = 0; array[i] != NULL; i += 2) {
         if(array[i] == key) {
            found = true;
            data = array[i+1];
            break;
         }
      }
      if(found) {
         cout << "found data: " << data << endl;
      }
      else {
         cout << "data: ";
         cin >> data;
         ofstream fout("data.h", ios::app);
         if(fout) {
            fout << "\"" << key << "\"," << endl;
            fout << "\"" << data << "\"," << endl << flush;
            fout.close();
            reload(src, prog);
         }
      }
   }
}

int
main(int argc, char ** argv, char ** envp)
{
   ReloadTest(argv[0], "reload.cpp").run();
   return 0;
}
