#ifndef FILEUTILS_H
#define FILEUTILS_H

#include "Param.h"

#include <libxml/parser.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

#include <fstream>
#include <string>
#include <vector>

#include "mathUtils.h"
using namespace std;

#define LOG_ERROR_INDEX 0

//holds functions for loading from zip files, xml handlers and (later error functions)
uint8_t writeToLog (const string &, int numTabs, int logIndex);
uint8_t writeToErrorLog (const string &, int numTabs);

ofstream * saveToRecentSlotFstream (); //deletes the temporary "recent" file and returns a pointer to it.
void closeRecentSlotFstream (ofstream *); //closes the "recent" file

xmlTextWriterPtr saveToRecentSlotXML (); //deletes the temporary "recent" file and returns a pointer to it.
void closeRecentSlotXML (xmlTextWriterPtr &); //closes the "recent" file

void deleteFile (uint8_t fileIndex); //deletes a file
void renameFile (uint8_t fileIndex); //renames the recent file

bool loadXMLFile (xmlDocPtr & doc, const string & filename);

bool xPathExists (xmlDocPtr & doc, xmlXPathContextPtr & xpathCtx, const string & xpath1, const string & xpath2, const string & xpath3);
xmlChar * getFirstElementUsingXPath (xmlDocPtr &, xmlXPathContextPtr &, const string &, const string &, const string &);
xmlXPathObjectPtr getElementsUsingXPath (xmlDocPtr &, xmlXPathContextPtr &, const string &, const string &, const string &);

//Tries to open the file. Tries to verify the last argument.
bool prepareXMLFile (xmlDocPtr &, xmlXPathContextPtr &, const string &, const string &);

bool loadIntFromXMLAndPutIntoHash (xmlDocPtr &, xmlXPathContextPtr &, const string &, const string &, Param & hash, int defaultValue=0);
bool loadIntFromXMLAndPutIntoHash (xmlDocPtr & doc, xmlXPathContextPtr & xpathCtx, Param & outputHash, Param & hash);
#endif
