Skip to content

Instantly share code, notes, and snippets.

@mendesbarreto
Created November 25, 2014 14:04
Show Gist options
  • Select an option

  • Save mendesbarreto/db88e1dd248826894fa4 to your computer and use it in GitHub Desktop.

Select an option

Save mendesbarreto/db88e1dd248826894fa4 to your computer and use it in GitHub Desktop.
CVS Reader in C++
#ifndef __CVS__HELPER__H
#define __CVS__HELPER__H
#include <exception>
#include <string>
#include <iostream>
#include <sstream>
class CSVRow
{
private:
std::string m_data;
std::stringstream m_dataStream;
public:
CSVRow();
~CSVRow();
inline std::string ReadItem()
{
std::string L_item;
std::getline(m_dataStream, L_item, ',');
return L_item;
}
inline std::string GetData(){ return m_data; }
inline void SetData(std::string str)
{
m_data.clear();
m_data.append(str.c_str());
m_dataStream.str(m_data);
}
};
class CSVReader
{
protected:
std::istringstream m_fileStream;
int32 m_fileSize;
int32 m_rowNumber;
char* m_pContent;
public:
CSVReader(void);
void SetContent(std::string &contentFile);
bool ReadRow(CSVRow& row);
int32 GetFileSize()
{
return m_fileSize;
}
~CSVReader();
};
#ifdef IW_SDK
#include "s3e.h"
#define READ "rb"
#define WRITE "wb"
class CSVReaderS3e : public CSVReader
{
private:
s3eFile* m_pFile;
public:
CSVReaderS3e(char* fileName);
~CSVReaderS3e();
};
#endif // IW_SDK
#endif // __CVS__HELPER__H
#include <string>
#include <iostream>
#include <assert.h>
#include <sstream>
#include "CSVHelper.h"
void CSVReader::SetContent(std::string &contentFile)
{
m_fileStream.str(contentFile);
}
CSVReader::~CSVReader()
{
delete m_pContent;
}
bool CSVReader::ReadRow(CSVRow& row)
{
std::string line;
//std::getline(m_fileStream, line, '\n');
//getline(stringStream, line, m_rowNumber);
//if (line == "0")
if (std::getline(m_fileStream, line, '\n'))
{
row.SetData(line);
return true;
}
return false;
}
CSVReader::CSVReader(void)
{
}
CSVRow::CSVRow()
{
}
CSVRow::~CSVRow()
{
}
#ifdef IW_SDK
#include "IwDebug.h"
CSVReaderS3e::~CSVReaderS3e()
{
if (m_pFile != 0)
s3eFileClose(m_pFile);
}
CSVReaderS3e::CSVReaderS3e(char* fileName)
{
IwTrace(CSV_READER, (("The follow file will be load: %s", fileName)));
m_pFile = s3eFileOpen(fileName, READ);
assert(m_pFile != NULL);
m_fileSize = s3eFileGetSize(m_pFile);
m_pContent = (char*)s3eMallocBase(m_fileSize + 1);
memset(m_pContent, 0, m_fileSize + 1);
m_rowNumber = 0;
if (s3eFileRead(m_pContent, m_fileSize, 1, m_pFile))
{
std::string content = m_pContent;
SetContent(content);
}
}
#endif // IW_SDK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment