自由空间
free space
<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

留言簿(22)

随笔分类

随笔档案

文章档案

相册

vckbase

搜索

最新评论

阅读排行榜

评论排行榜

 
VC知识库BLOG   首页  新随笔  联系  聚合  登录 
  随笔-81 文章-1 评论-227 Trackbacks-0

#define MAX_DATA_SIZE 100

/*
说明: 取指定分隔符中的数据,如a=b,取出a、b
buff:   数据
out_left:  输出left值
out_right:  输出right值
ch:    分隔符,不能以0x20为分隔符
返回: -1:buff为空或长度不够3位
  -2:分隔符为0x20
  1:成功
*/
int GetValue(const char *buff,char *out_left,char *out_right,char ch)
{
 int len = strlen(buff);
 bool m_equal = false;
 int i =0;
 int j =0;
 int h =0;
 int k =0;
 if(len<3)
  return -1;
 if(ch == 0x20)  //空格
  return -2;
 for(i;i<len;i++)
 {
 
  if(buff[i]==ch) //分隔符号
  {
   m_equal = true;  
   k++;
  }
  if(k>=2)
      break;
  if(m_equal)
  {
   if(buff[i]!=0x0D)
   {
    if(buff[i]== 0x3B||buff[i]==0x2F||buff[i]==0x3B||buff[i]==0x23)// '/' ';' '#' ' ' 为注释
     break;
    if(buff[i]!=0x20 && buff[i]!=ch)
    {
     
     out_right[h]=buff[i];
     h++;
    }
   }
   
  }
  else
  {
   if(buff[i]!=0x20)
   {
    out_left[j]=buff[i];
    j++;
   }
  }
 }
 out_left[j]='\0';
 out_right[h]='\0';
 return 1;
}
/*
说明: 类似于GetPrivateProfileString API函数,取
  [section]
  key=string
  key的值string
  支持 / ; # 为注释
lpAppName:   section
lpKeyName:   key
lpReturnedString: string 
nSize:    行最大字符
lpFileName:   文件名
返回: -1:fgets读失败
  -2:有两个相同的section
  -3:string为空或无section、key
  -4:nSize大于100
  1:成功
*/

int GetPrivateString(const char *lpAppName,const char *lpKeyName,char * lpReturnedString,int nSize,const char *lpFileName)
{
if(nSize>MAX_DATA_SIZE)
    return -4;
 FILE *stream;
 bool m_app = false;
 char buffer[MAX_DATA_SIZE];
 char left_value[MAX_DATA_SIZE];
 char right_value[MAX_DATA_SIZE];
 int i_sum =0; //app计数,允许只有一个app

 int result = 0;
 char tmp_app_name[MAX_DATA_SIZE];
 memset(tmp_app_name,0,sizeof(tmp_app_name));
 int len = strlen(lpAppName)-1;
 if(lpAppName[0]!=0x5B && lpAppName[len]!=0x5D) //无[],为其加上
 {
  sprintf(tmp_app_name,"%c%s%c",'[',lpAppName,']');
 }
 
 
 if( (stream = fopen( lpFileName, "r" )) != NULL )
 {
  while( !feof( stream ) )
  {
   memset(buffer,0,sizeof(buffer));
   if( fgets( buffer, nSize, stream ) == NULL)
   {
    fclose(stream);
    return -1;
   }
  
   if(memcmp(buffer,tmp_app_name,strlen(tmp_app_name))==0)
   {
    m_app = true;
    i_sum++;
   }
   if(i_sum>1)
   {
    fclose(stream);
    return -2;
   }
   if(m_app)
   {
    if(buffer[0]!=0x5B)
    {
     memset(left_value,0,sizeof(left_value));
     memset(right_value,0,sizeof(right_value));
     if(GetValue(buffer,left_value,right_value,'=')!=-1)
     {
       if(memcmp(left_value,lpKeyName,strlen(lpKeyName))==0)
      {
       if(strlen(right_value)<=0)
       {
        fclose(stream);
        return -3;
       }
       strcpy(lpReturnedString,right_value);
       fclose(stream);
       return 1;
      }
     }
    }
   }
   
  }
 
  fclose( stream );
 }
 return -3;
}

posted on 2005-04-17 12:52 自由空间 阅读(2377) 评论(8)  编辑 收藏
Comments
  • # rt
    紫水晶
    Posted @ 2005-04-18 10:25
    记一下,呵呵,rt到底什么意思啊,学用一下
  • # 砰~~~
    Jozu
    Posted @ 2005-04-18 12:17
    千万不要这么用:
    fgets( buffer, nSize, stream )
    一个典型的buffer overflow!!!
  • # re: 需要的就顶一下!GetPrivateString !如果你在win下就不需要了:)
    freedk
    Posted @ 2005-04-18 13:37
    啊!!!那用什么?jozu,快...
  • # nSize不要做gets的参数,自己用buffer大小作参数
    jozu
    Posted @ 2005-04-18 15:52
    rt
  • # re: 需要的就顶一下!GetPrivateString !如果你在win下就不需要了:)
    freedk
    Posted @ 2005-04-18 18:13
    那我判断一下nSize不就行了?:)rt是啥意思?
  • # 现在就可以了~
    jozu
    Posted @ 2005-04-19 04:18
    要避免使用用户可填的缓冲大小作为自己缓冲大小的函数,如果要用的话,一定要避免自己的缓冲越界。

    rt 就是如题的意思:)
  • # 原来是这个意思啊
    紫水晶
    Posted @ 2005-04-19 09:14
    rt
  • # re: 需要的就顶一下!GetPrivateString !如果你在win下就不需要了:) 感谢jozu提出意见:)
    freedk
    Posted @ 2005-04-19 09:14
    嗯!记住了:)
标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]