#include "csv_xml.h"

///////////////////////////////////////////////////////////

int csv_xml::line_get(ifstream * ifFile, string * strLine) 
{
	*strLine = "";
	char tempChar;
	while ((tempChar = (*ifFile).get()) != '\n')
	{
		if (tempChar == EOF) 
		{
			return 0;
		}
		
		*strLine += tempChar;
	}
	

	return 1;
}

///////////////////////////////////////////////////////////

int csv_xml::get_headings(ifstream &f)
{

	string line;
	string elem_text;
	int flag = 0;
	signed int str_pos, str_pos_last;
	int pos_diff = 0;
	unsigned int len = 0;
	int end_line = 0;
	signed int in_line_quote;				  
	
	line_get(&f, &line);
			
		str_pos = 0; //str_pos--;   //When first incremented, goes to 0 position
		str_pos_last = 0;
		in_line_quote = str_pos;
		pos_diff = 1;
		len = line.length();
		end_line = 0;

	
		while (str_pos < len && end_line == 0)
		{
			
			str_pos = (line.find(",", str_pos_last));
						
			if (str_pos == -1) {
				end_line = 1; // If find() returns -1, (means last data for this line) finish up
			}
			
			pos_diff = (str_pos_last+1) - str_pos_last;  //Kind of stupid way to do this instead of just = 1, but the logic is behind it.
			
			if (line.substr(str_pos_last, pos_diff) == "\"") 
			{ 
				if ((line.find("\"", str_pos)) == (len - 1)) {
					end_line = -1;
					str_pos = -1;
				}				
				
				if (str_pos == -1) // To make sure it goes the last loop
				{
					pos_diff = (len - 1) - str_pos_last;
					str_pos = (line.find("\"", str_pos_last + 1));
				} 
				else 
				{
					str_pos = (line.find("\",", str_pos_last + 1));
				}

				pos_diff = str_pos - (str_pos_last + 1);
				elem_text = line.substr(str_pos_last + 1, pos_diff);
				if (!replace_char(elem_text, elem_text.length(), ' ', '_'))
				{
					return 0;
				}

				headings.push_back(elem_text);
				str_pos_last = str_pos + 2;  //+2 is to account for the (",) characters
											
			} 
			else
			{
				
				if (str_pos == -1) // To make sure it goes the last loop
				{
					pos_diff = len - str_pos_last;
				} else {
					pos_diff = str_pos - str_pos_last;
				}

				elem_text = line.substr(str_pos_last, pos_diff);
				if (!replace_char(elem_text, elem_text.length(), ' ', '_'))
				{
					return 0;
				}
				headings.push_back(elem_text);
				str_pos_last = str_pos + 1;
			}
			
			
		}
		
	return 1;

}

/////////////////////////////////////////////////////

int csv_xml::parse_file(string file_in, string file_out, int getH) 
{
	flag = 0;
	pos_diff = 0;
	len = 0;
	end_line = 0;
	
	f.open(file_in.c_str());
	f2.open(file_out.c_str());

	if (f.fail())
	{
		printf("There was a problem openining the file (%s).  It either does not exist, or it is password protected.\n"
			"Please contact the administrator of this software for additional assistance\n", file_in.c_str());
		exit(1);
	}
	f2 << "<records>"<<endl;

	if (getH == 1) 
	{
		if (!get_headings(f))
		{
			return 0;
		}
	}

	while (line_get(&f, &line)) 
	{
		f2 << "\t<record>"<< endl;
		str_pos = 0; //str_pos--;   //When first incremented, goes to 0 position
		str_pos_last = 0;
		in_line_quote = str_pos;
		pos_diff = 1;
		len = line.length();
		end_line = 0;
		v_count = 0;
	
		while (str_pos < len && end_line == 0)
		{
			
			
			str_pos = (line.find(",", str_pos_last));

			if (getH != 1) 
			{
				f2 << "\t\t" << "<" << "rec" << ">" << endl;
			} else {
				f2 << "\t\t" << "<" << headings[v_count] << ">" << endl;
			}
			
			if (str_pos == -1) {
				end_line = 1; // If find() returns -1, (means last data for this line) finish up
			}
			
			pos_diff = (str_pos_last+1) - str_pos_last;  //Kind of stupid way to do this instead of just = 1, but the logic is behind it.
			
			if (line.substr(str_pos_last, pos_diff) == "\"") 
			{ 
				if ((line.find("\"", str_pos)) == (len - 1)) {
					end_line = -1;
					str_pos = -1;
				}				
				
				if (str_pos == -1) // To make sure it goes the last loop
				{
					pos_diff = (len - 1) - str_pos_last;
					str_pos = (line.find("\"", line.length() - 1)); //Changed to line.length() - 1 to accommodate all line endings with or without \n
					
				} 
				else 
				{
					str_pos = (line.find("\",", str_pos_last + 1));
				}

				pos_diff = str_pos - (str_pos_last + 1);
				elem_text = line.substr(str_pos_last + 1, pos_diff);
				str_pos_last = str_pos + 2;  //+2 is to account for the (",) characters
				f2 << "\t\t\t" << elem_text << endl;								
			} 
			else
			{
				
				if (str_pos == -1) // To make sure it goes the last loop
				{
					pos_diff = len - str_pos_last;
				} else {
					pos_diff = str_pos - str_pos_last;
				}

				elem_text = line.substr(str_pos_last, pos_diff);
				f2 << "\t\t\t" << elem_text << endl;
				str_pos_last = str_pos + 1;
			}
			
			if (getH != 1) 
			{
				f2 << "\t\t" << "</" << "rec" << ">" << endl;
			} else {
				f2 << "\t\t" << "</" << headings[v_count] << ">" << endl;
			}

			v_count++;
		}

			
		f2 << "\t</record>" << endl;

	}

	f2 << "</records>" << endl;
		
	return 1;
}

/////////////////////////////////////////////////////////

int csv_xml::replace_char(string & temp_String, int str_Length, char ch_What, char ch_With)
{
	int i;
	char *temp_alloc_String;
	temp_alloc_String = (char*)malloc(temp_String.length() + 1);
	strcpy(temp_alloc_String, temp_String.c_str());
	if (temp_alloc_String == NULL) 
	{
		csv_xml::xml_err_msg += "Unable to allocate memory\n";
		return 0;
	}
		
	for(i = 0; i < temp_String.length(); i++)
	{
		if (temp_alloc_String[i] == ch_What)
		{
			temp_alloc_String[i] = ch_With;
		}
	}
	
	temp_String = temp_alloc_String;
		
    free((void*)temp_alloc_String);
	
	return 1;
   

}

//////////////////////////////////////
