#include "CivCsvReader.h"

CivCsvReader::CivCsvReader(const char* path)
{
	_csvInput.open(path);
}

void CivCsvReader::readOnLoad()
{
    string line;
    // ¶ÁÈ¡Í·²¿
    int index = 0;
    while (getline(_csvInput, line))
    {
        istringstream sin(line);
        vector<string> fields;
        string field;
        while (getline(sin, field, ','))
        {
            if (index == 0)
                mHeaders.push_back(field);
            else
                fields.push_back(field);
        }
        if (index > 0)
            mVecVec.push_back(fields);

        index++;
    }
}


vector<string> CivCsvReader::getHeaders()
{
    return mHeaders;
}

void CivCsvReader::readByColumn(int column, vector<string>& vec)
{
    for (int i = 0; i < mVecVec.size(); i++)
    {
        vector<string> temp =mVecVec[i];
        if (column > temp.size())
            return;

        vec.push_back(temp[column]);
    }

}

void CivCsvReader::readByRow(int row, vector<string>& vec)
{
    if (mVecVec.size() < row)
        return;

    vec = mVecVec[row];
}

int CivCsvReader::getRows()
{
    return mVecVec.size();
}

int CivCsvReader::getColumns()
{
    if (mVecVec.size() <= 0)
        return 0;

    return mVecVec[0].size();
}
