模仿TParameters字符串解析(更正)

 // test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include <string>
#include <vector>
using namespace std;

typedef struct _Param
{
  string name;
  string value;
}Param;

vector<Param> ParamList;
inline string getParam(const char *param_name)
{
 string a;
   for(unsigned int i=0; i <ParamList.size(); i++)
   {
  a=ParamList[i].name;
  if(ParamList[i].name==string(param_name))
   return "'"+ParamList[i].value+"'";
   }
  return "";
}

string ParseSql(char *Str)
{
 string ret_str="";
 char buffer[8192];
 bool begin_parse=false;
 unsigned int len= strlen(Str);
 unsigned int pos=0;
 unsigned int b_pos=0;
 unsigned int e_pos=0;
    //正确的字符串为    b_pos~e_pos
    //要替换的字符串为  e_pos~b_pos
 //当前位置 pos
 char begin_char =':';
 char end_char1  =',';
 char end_char2  =' ';
 char *ptr=Str;
 if(len<1)
  return "";
 while(pos <len)
 {
       if(*Str ==begin_char) {
          begin_parse =true;
          b_pos  =pos;
          memset(buffer,0,sizeof(buffer));
    if(e_pos>=1)
    {
     memcpy(buffer, ptr+e_pos-1, b_pos-e_pos+1);  //加减1 表示要多拷贝上一个字符 end_char
        memset(buffer+b_pos-e_pos+1,'/0',1);
    }
    else
    {
              memcpy(buffer, ptr+e_pos, b_pos-e_pos);  
        memset(buffer+b_pos-e_pos,'/0',1);
    }
          ret_str += buffer;
    ++b_pos;  //begin_char实际位置
    } //拷贝正确的字符串

    if(begin_parse&&(*Str ==end_char1 || *Str==end_char2) ) {
          e_pos =pos;
    begin_parse =false;
    memset(buffer,0,sizeof(buffer));
          memcpy(buffer, ptr+b_pos, e_pos-b_pos );
       memset(buffer+e_pos-b_pos, '/0',1);
    ret_str += getParam(buffer);
    ++e_pos;  //end_char实际位置
    }//替换字符串
    //如果解析到最后一个字符,发现都是不需要解析的字符串,则加到最后面
    if( (!begin_parse) && (pos==len-1))
    {
     memcpy(buffer, ptr+e_pos-1, pos-e_pos+1+1);   //前面1为复制空格,后面1为复制当前字符
     memset(buffer+pos-e_pos+1+1,'/0',1);
     ret_str += buffer;
       }
    ++Str;   
       ++pos;
 }
 //如最后没有空格,则补空格
 if( (*(ptr+len-1)!=end_char1)&& (*(ptr+len-1)!=end_char2))
 {
          e_pos =pos;
    memset(buffer,0,sizeof(buffer));
          memcpy(buffer, ptr+b_pos, e_pos-b_pos );
       memset(buffer+e_pos-b_pos, '/0',1);
    ret_str += getParam(buffer);
 }
 
 return ret_str;
}
int main(int argc, char* argv[])
{
 printf("!/n");
    Param  p;

 p.name="a";
 p.value="1"; 
 ParamList.push_back(p);

 p.name="b";
 p.value="2"; 
 ParamList.push_back(p);
 p.name="c";
 p.value="3"; 
 ParamList.push_back(p);
 p.name="d";
 p.value="4"; 
 ParamList.push_back(p);
 p.name="e";
 p.value="5"; 
 ParamList.push_back(p);
 p.name="f";
 p.value="6"; 
 ParamList.push_back(p);
   
 string str;
 char *pstr="insert into table (a,b,c) vlues(:a,:b,:c )";
 char *pstr1="a=:a b=:b,c=:c,d=:d e=:e ";
 char *pstr2="a=:a,b=:b,c=:c,d=:d,e=:e ";
 char *pstr3="a=:a b=:b c=:c d=:d e=:e";

 printf("解析前:/n%s/n",pstr);
 str =ParseSql(pstr); 
 printf("解析后:/n%s/n",str.c_str());

 printf("解析前:/n%s/n",pstr1);
 str =ParseSql(pstr1); 
 printf("解析后:/n%s/n",str.c_str());

 printf("解析前:/n%s/n",pstr2);
 str =ParseSql(pstr2); 
 printf("解析后:/n%s/n",str.c_str());

 printf("解析前:/n%s/n",pstr3);
 str =ParseSql(pstr3); 
 printf("解析后:/n%s/n",str.c_str());
 return 0;
}

 

运行结果如下:

!
解析前:
insert into table (a,b,c) vlues(:a,:b,:c )
解析后:
insert into table (a,b,c) vlues('1','2','3' )
解析前:
a=:a b=:b,c=:c,d=:d e=:e
解析后:
a='1' b='2',c='3',d='4' e='5'
解析前:
a=:a,b=:b,c=:c,d=:d,e=:e
解析后:
a='1',b='2',c='3',d='4',e='5'
解析前:
a=:a b=:b c=:c d=:d e=:e
解析后:
a='1' b='2' c='3' d='4' e='5'
Press any key to continue


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值