Csv_robust typing

#include<string>
#include<iostream>
#include<stdio.h>


using namespace std;


#define LogOut printf
//this class don't use the container to buffer the file data,because it's too big sometimes.
class Csv
{
public:
Csv();
Csv(const char *FileAddr, int FileSize);


bool CheckFile(const char *FileAddr, int FileSize);
//bool ReadLine(const char* Adrr, string &str);
bool ReadLine();
bool ReadLine(const char* Addr, string &Str);
bool ReadField(int Row, int Col, string &Str);
bool ReadField(int Row, int Col, int &Val);
bool ReadField(int Row, int Col, float &Val);


int FindCols(int Col, const string &Str); //find the row index of Str in the specific columnn 
int FindCols(int Col, int Val);
int FindCols(int Col, float Val);


int CntLine(const char *path);
string CntCol(string line);
protected:
const char *m_FileAddr;
int m_FileSize;
bool isFileValid;
const char* m_Pos; //current position of the csv file
private:
bool isGraphic(char ch);
string m_Line;
string m_Field;
};


Csv::Csv()
{
m_FileAddr = NULL;
m_FileSize = 0;
m_Pos = NULL;
isFileValid = false;
}


Csv::Csv(const char *FileAddr, int FileSize)
{
if(CheckFile(FileAddr, FileSize))
{
m_FileAddr = FileAddr;
m_FileSize = FileSize;
m_Pos = m_FileAddr;
isFileValid = true;
}
}






//read data from the flash to string object which storages the data to Heap,not the stack of the Task
const short LineSizeLimit = 100;
bool Csv::isGraphic(char ch)
{
if(ch < 0x20 || ch > 0x7e)
return false;
else
return true;
}




bool Csv::ReadLine(const char* Addr, string &Str)//just read one line data,and make difference to the csv object
{
const char *pos = Addr;


while((*pos != '\r' && *(pos + 1) != '\n') && pos < Addr + LineSizeLimit && isGraphic(*pos))
{
pos++;
}
if(pos >= Addr + LineSizeLimit || !(isGraphic(*pos) || *pos == '\r' || *pos == '\n'))
{
Str.clear();
return false;
}
Str.assign(Addr, pos - Addr);
return true;
}


bool Csv::ReadLine()//read a new line to the m_line from m_pos,this function for read each line of the file 
{
const char *pos = m_Pos;


while((*pos != '\r' && *(pos + 1) != '\n') && pos < m_Pos + LineSizeLimit && isGraphic(*pos))
{
pos++;
}
if(pos >= m_Pos + LineSizeLimit || !(isGraphic(*pos) || *pos == '\r' || *pos == '\n'))
{
m_Line.clear();
return false;
}
m_Line.assign(m_Pos, pos - m_Pos);
m_Pos = pos + 2; //move to next line addr of the file
return true;
}


bool Csv::CheckFile(const char *FileAddr, int FileSize)
{
string str;
if(!FileAddr || !FileSize || !ReadLine(FileAddr, str)) //just check the first line
{
LogOut("Warnning: Csv::Csv() input argurment invalid!\n");
m_FileAddr = NULL;
m_FileSize = 0;
isFileValid = false;
return false;
}
m_FileAddr = FileAddr;
m_FileSize = FileSize;
m_Pos = m_FileAddr;
return true;
}

bool SimulateFlash(const char* FileName[], int Num, unsigned char** Addr, int* Size)
{
const int BufSize = 1024 * 8;
static unsigned char FileBuf[BufSize] = {0};
FILE** fp = new FILE*[Num];
unsigned char *BaseAddr = FileBuf;
unsigned char *pos = FileBuf;

for(int i = 0; i < Num; i++) {
char ch;
if((fp[i] = fopen(FileName[i], "r")) == NULL) {
LogOut("Can't open file:%s\n", FileName[i]);
perror(NULL);
continue;
}
while((ch = fgetc(fp[i])) != EOF && pos < FileBuf + BufSize)
*pos++ = ch;
if(pos >= FileBuf + BufSize) {
LogOut("FileBuf has not enough space to contain the File!\n");
delete[] fp;
return false;
}
*(Size + i) = pos - BaseAddr;
*(Addr + i) = BaseAddr;
BaseAddr = pos;
}
delete[] fp;
return true;
}




int main()
{
const char *FileName[] = {"Big.csv", "Middle.csv", "Small.csv"};
int FileSize[sizeof(FileName)/sizeof(FileName[0])];
unsigned char* Addr[sizeof(FileName)/sizeof(FileName[0])];
SimulateFlash(FileName, sizeof(FileName)/sizeof(FileName[0]), Addr, FileSize);


const char* BigWave = (const char*)Addr[0];
const char* MiddleWave = (const char*)Addr[1];
const char* SmallWave = (const char*)Addr[2];
int BigWaveSize = FileSize[0];
int MiddleWaveSize = FileSize[1];
int SmallWaveSize = FileSize[2];


Csv csv;
string str;
bool rtn = csv.ReadLine(BigWave, str);
cout << str << endl;


// for(int i = 0; i < BigWaveSize; i++)
// {
// cout << *(BigWave + i);
// }
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值