从char*以CSV的格式读入到vector容器里。
vector<char*> vecShr;
void getstrforcsv(char* input_buff
{
for(int k = 0 ; k < vecShr.size() ; k++ )
{
char* p_string = vecShr.at(k);
delete[] p_string;
}
vecShr.clear() ;
bool bFlagQuote = false;
unsigned int unLinePos = 0;
unsigned int unLenth = 0;
unsigned int i = 0;
unsigned int j = 0;
char* p_string = NULL;
char* p_tmp_string = NULL;
bool bFlagFirstPair = false;
bool bFlagBreak = false;
int nStrLen;
unLenth = strlen(input_buff);
if(unLenth > 1024)
{
unLenth = 1024;
}
p_tmp_string = new char[unLenth + 1];
if(NULL == p_tmp_string)
{
return;
}
for(j = 0; j < unLenth; j++)
{
unLinePos = 0;
bFlagFirstPair = false;
bFlagQuote = false;
memset(p_tmp_string, 0, unLenth + 1);
for(i = 0; i< unLenth; i++)
{
bFlagBreak = false;
// 判断Data是否被Double Quote包围
if((0 == unLinePos) && (input_buff[j] == '"'))
{
bFlagFirstPair = true;
bFlagQuote = true;
p_tmp_string[i++] = input_buff[j++];
}
if(bFlagQuote)
{
//Double Quote包围
int nQuoueNum = 0;
switch (input_buff[j])
{
case '\0':
bFlagBreak = true;
break;
case ',':
if(bFlagFirstPair)
{
p_tmp_string[i] = input_buff[j++];
}else
{
bFlagBreak = true;
}
break;
case 0x0A:
case 0x0D:
if(bFlagFirstPair)
{
p_tmp_string[i] = input_buff[j++];
}else
{
if((input_buff[j+1] == 0x0A) || (input_buff[j+1] ==0x0D))
{
j++;
}
bFlagBreak = true;
}
break;
case '"':
nQuoueNum = 1;
p_tmp_string[i] = input_buff[j];
while(input_buff[++j] == '"')
{
nQuoueNum++;
//if((nQuoueNum>3) && ((nQuoueNum - 3) & 0x01)) //<-- //{//<--多个双引号2个变1个
p_tmp_string[++i] = input_buff[j];
//}//<--
}
if(nQuoueNum & 0x01)//nQuoueNum%2
{
bFlagFirstPair = false;
}
break;
default:
p_tmp_string[i] = input_buff[j++];
}
}else
{
//Double Quote没包围
switch (input_buff[j])
{
case ',':
case '\0':
bFlagBreak = true;
break;
case 0x0A:
case 0x0D:
if((input_buff[j+1] == 0x0A) || (input_buff[j+1] ==0x0D))
{
j++;
}
bFlagBreak = true;
break;
default:
p_tmp_string[i] = input_buff[j++];
}
}
if(bFlagBreak)
{
break;
}
unLinePos++;
}
//
p_tmp_string[i] = '\0';
nStrLen = strlen(p_tmp_string) + 1;
if(nStrLen > 1)
{
p_string = new char[nStrLen];
if(NULL == p_string)
{
delete[] p_tmp_string;
return;
}else
{
strcpy_s(p_string, nStrLen, p_tmp_string);
vecShr.push_back(p_string);
}
}
}
delete [] p_tmp_string ;
return;
}