/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                               DEMO PROGRAMS SET
                          xml_test2.cpp  -  description
                             -------------------
    begin                : October 3, 2005
    copyright            : (C) 2005-2006 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 <sptk3/sptk.h>
#include <sptk3/cxml>
#include <iostream>

using namespace std;
using namespace sptk;

CXmlNode* node;
int main(int argc,char *argv[]) {
	char buffer[1024]; 

   try {
		CXmlDoc doc;
		CDateTime start = CDateTime::Now();
		cout << "The XML document generation started:" << endl;
		cout << "Size of CXmlNode is : " << sizeof(CXmlNode) << endl;
		cout << "Size of CXmlAttributes is : " << sizeof(CXmlAttributes) << endl;
		cout << "Size of CXmlNodeList is : " << sizeof(CXmlNodeList) << endl;
		for (unsigned i = 0; i < 100000; i++) {
			CXmlNode* row = new CXmlElement(doc,"row");

			//new CXmlComment(row,"A comment");
			//new CXmlCDataSection(row,"A CData section\n- anything can go here");

			sprintf(buffer,"%i",i);
			CXmlNode* cell1 = new CXmlElement(*row,"cell");
			new CXmlText(*cell1,buffer);

			sprintf(buffer,"A pretty long string number %i",i);
			CXmlNode* cell2 = new CXmlElement(*row,"cell");
			new CXmlText(*cell2,buffer);
		}

		cout.setf(ios::fixed);
		cout.precision(2);

		CDateTime end = CDateTime::Now();
		double duration = (end-start) * 3600 * 24;
      start = end;

      cout << "The document is ready (" << doc.size() * 5 << " nodes): " << duration << " seconds" << endl;

      CBuffer *buf = new CBuffer;
		doc.save(*buf);

      end = CDateTime::Now();
		duration = (end-start) * 3600 * 24;
      start = end;
      cout << "The document is saved to buffer (" << doc.size() * 5 << " nodes): " << duration << " seconds" << endl;

      doc.load(*buf);
      end = CDateTime::Now();
		duration = (end-start) * 3600 * 24;
      start = end;
      cout << "The document is loaded from the buffer (" << doc.size() * 5 << " nodes): " << duration << " seconds" << endl;

      doc.save(*buf);

      end = CDateTime::Now();
		duration = (end-start) * 3600 * 24;
      start = end;
      cout << "The document is saved to buffer(" << doc.size() * 5 << " nodes): " << duration << " seconds" << endl;

      buf->saveToFile("1.xml");

      end = CDateTime::Now();
		duration = (end-start) * 3600 * 24;
      start = end;
      cout << "The document is saved to disk: " << duration << " seconds" << endl;

      // Releasing the buffer memory
      delete buf;

      cout << endl << "The XML document test completed. Press any key to exit.." << endl;
		getchar();
		cout << "Thank you." << endl;
/*
      CBuffer buff;
      buff.loadFromFile("/tmp/1.xml");
      doc.load(buff);
      doc.save(buff);
      buff.saveToFile("/tmp/2.xml");
*/
   }
	catch (std::exception& e) {
		puts(e.what());
		return 1;
	}
	return 0;
}
