Sei sulla pagina 1di 5

/*

Author: Blake Richardson


Date: 11/20/2016
Email: blake.a.l.richardson@gmail.com
Copyright: (C) 2016 DigiPen Institute of Technology. Reproduction or disclosur
e of this file or its contents without
the prior written consent of DigiPen Institute of Technology is pro
hibited.
*/
#include "FileOperations.h"
FileReader::FileReader(std::string filename)
{
m_p_file.open(filename, std::ifstream::in);
if(!m_p_file.is_open())
std::cout << "Couldn't open file at: " << filename << std::endl;
}
FileReader::~FileReader()
{
m_p_file.close();
}
void FileReader::ParseFile(bool mode)
{
std::vector<std::string> column_names;
int line_count = 0;
int header_size = 8;
if(mode)
{
while(line_count < header_size)
{
std::string throw_away;
std::getline(m_p_file, throw_away, '\n');
line_count++;
}
}
std::string columns_in_single_line;
std::getline(m_p_file, columns_in_single_line, '\n');
line_count++;
if(mode) column_names = GetColumnNames(columns_in_single_line);
else column_names = GetColumnData(columns_in_single_line);
while(m_p_file)
{
std::string grab_from_file;
std::getline(m_p_file, grab_from_file, '\n');
if(grab_from_file == "\n" || grab_from_file == "")
break;
m_values.push_back(Tokenize(grab_from_file, column_names, true));
line_count++;
}
}

std::vector<DataBlock> FileReader::GetValues()
{
return m_values;
}
DataBlock FileReader::Tokenize(std::string& input, std::vector<std::string> colu
mns, bool mode)
{
DataBlock block;
std::vector<std::string> data;
//added a bool to make it easier to check multiple types of data file
//without writing more functions
if(mode) data = GetColumnData(input);
else data = GetRSColumnNames(input);
//add columns to the blocks for each column in the data
for(unsigned i = 0; i < columns.size(); i++)
block.AddColumn(columns[i], data[i]);
return block;
}
bool NotDelim(std::string delims, char character)
{
//make sure the character is not a delimiter
for(unsigned i = 0; i < delims.size(); i++)
{
if(delims[i] == character)
return false;
}
return true;
}
std::vector<std::string> BreakItUp(unsigned start_point, std::string delims, con
st std::string& input)
{
std::vector<std::string> return_vec;
std::string push_this_into_vector;
//put all of the characters into a new vector
for(unsigned i = start_point; i < input.size(); i++)
{
//don't put any deliminators in, and no quotation marks
if(NotDelim(delims, input[i]) && input[i] != '\"' && input[i] != '(' && inpu
t[i] != ')')
push_this_into_vector += input[i];
else if(input[i] != '\"' && input[i] != '(' && input[i] != ')') //if you fin
d a delimiter, that is the end of that word
{
return_vec.push_back(push_this_into_vector);
push_this_into_vector.clear();
}
}
//for words that are at the end but do not have delimiters
if(push_this_into_vector.size() > 0)
return_vec.push_back(push_this_into_vector);
return return_vec;

}
std::vector<std::string> FileReader::GetColumnNames(const std::string& input)
{
//because the header in the gyroscope data has some characters i don't want
//i have the "break up" start at index 2 instead of index 0
std::vector<std::string> return_vec;
std::string delims = " ";
return_vec = BreakItUp(2, delims, input);
return return_vec;
}
std::vector<std::string> FileReader::GetColumnData(const std::string& input)
{
std::vector<std::string> return_vec;
std::string delims = ",";
return_vec = BreakItUp(0, delims, input);
return return_vec;
}
std::vector<std::string> FileReader::GetRSColumnNames(const std::string& input)
{
std::vector<std::string> return_vec;
std::string delims = ":";
return_vec = BreakItUp(0, delims, input);
return return_vec;
}
void OutputToFile(std::string filename, Centroid& group)
{
std::ofstream output(filename);
std::vector<std::string> names = group.GetCenter().GetColumnNames();
for (unsigned i = 0; i < names.size(); i++)
{
if (i != names.size() - 1)
output << names[i] << ",";
else
output << names[i] << std::endl;
}
std::vector<DataBlock> blocks = group.GetAllPoints();
for (unsigned i = 0; i < blocks.size(); i++)
{
for (unsigned j = 0; j < blocks[i].GetSize(); j++)
{
if (j != blocks[i].GetSize() - 1)
output << blocks[i][j] << ",";
else
output << blocks[i][j] << std::endl;
}
}
}
void OutputToFile(std::string filename, std::vector<Centroid> centroids, std::ve
ctor<std::string> variables)
{
std::ofstream output(filename);

for (auto group : centroids)


{
std::vector<std::string> names = group.GetCenter().GetColumnName
s();
for (unsigned i = 0; i < names.size(); i++)
{
if (i != names.size() - 1)
output << names[i] << ",";
else
output << names[i] << std::endl;
}
DataBlock& centroid = group.GetCenter();
for (unsigned j = 0; j < variables.size(); j++)
{
if (j != variables.size() - 1)
output << variables[j] << "(" << centroid[variab
les[j]] << "),";
else
output << variables[j] << "(" << centroid[variab
les[j]] << ")" << std::endl;
}
std::vector<DataBlock> blocks = group.GetAllPoints();
for (unsigned i = 0; i < blocks.size(); i++)
{
for (unsigned j = 0; j < blocks[i].GetSize(); j++)
{
if (j != blocks[i].GetSize() - 1)
output << blocks[i][j] << ",";
else
output << blocks[i][j] << std::endl;
}
}
}
}
void OutputToFileWithOutCenter(std::string filename, std::vector<Centroid> centr
oids, std::vector<std::string> variables)
{
std::ofstream output(filename);
std::vector<std::string> names = centroids[0].GetCenter().GetColumnNames
();
for (unsigned i = 0; i < names.size(); i++)
{
if (i != names.size() - 1)
output << names[i] << ",";
else
output << names[i] << std::endl;
}
for (auto group : centroids)
{
std::vector<DataBlock> blocks = group.GetAllPoints();
for (unsigned i = 0; i < blocks.size(); i++)
{

for (unsigned j = 0; j < blocks[i].GetSize(); j++)


{
if (j != blocks[i].GetSize() - 1)
output << blocks[i][j] << ",";
else
output << blocks[i][j] << std::endl;
}
}
}
}
void CentroidsToOneFile(std::string filename, std::vector<Centroid> centroids, s
td::vector<std::string> variables, bool screen )
{
std::ofstream output(filename);
for (auto group : centroids)
{
DataBlock& centroid = group.GetCenter();
for (unsigned j = 0; j < variables.size(); j++)
{
if (j != variables.size() - 1)
output << variables[j] << "(" << centroid[variab
les[j]] << "),";
else
output << variables[j] << "(" << centroid[variab
les[j]] << ")" << std::endl;
if (screen)
{
if (j != variables.size() - 1)
std::cout << variables[j] << "(" << cent
roid[variables[j]] << "),";
else
std::cout << variables[j] << "(" << cent
roid[variables[j]] << ")" << std::endl;
}
}
}
}

Potrebbero piacerti anche