Chinaunix

标题: 取字符串值的一个小函数 [打印本页]

作者: darkce    时间: 2010-02-03 13:01
标题: 取字符串值的一个小函数
配置文件里取值的一个小函数
经常需要读取像
title = xxxxx
ip = 192.168.13.65
size = 1000;
类似于这样的文件。

所以有了个函数。学艺不精, 也请坛上的各高手多多指教。
示例:
int main(int argc, char **argv)
{
        char *src1 = "title = abcdesf";
        char *src2 = "number = 100";

        int a;
        char b[1024];
       
        fsStr(b, src1, '=', 1);
        fsStr(&a, src2, '=', 0);
}



int fsStr(void *dst,  const char *src, char c, int cmd)
{
        if (src == NULL)
                return 0;

        char *pl;
        pl = strchr(src, c);
        if (pl == NULL)
                return 0;

        while(*(++pl) == ' ') { ; }
        if (cmd == 0)
                *(int*)dst = atoi(pl);

        if (cmd == 1)
                sscanf(pl, "%[^\n]", (char*)dst);

        return 0;
}
作者: yulihua49    时间: 2010-02-03 13:21
配置文件里取值的一个小函数
经常需要读取像
title = xxxxx
ip = 192.168.13.65
size = 1000;
类似于这 ...
darkce 发表于 2010-02-03 13:01

  1. /*function:iscc 判断是否是汉字*/
  2. int iscc(unsigned char ch)
  3. {
  4.         return (ch >= 0x81 && ch < 0xff);
  5. }
  6. /*function:cc1 取汉字的一个字节*/
  7. static int cc1(unsigned char *bp,unsigned char *bufp)
  8. {
  9.         register unsigned char *p;
  10.         register int i = 0;
  11.         for(p = bufp; iscc(*p); p--)
  12.         {
  13.                 i++;
  14.                 if(p <= bp)
  15.                         break;
  16.         }
  17.         return (i & 1);
  18. }
  19. /*function:firstcc 汉字的第一个字节*/
  20. int firstcc(unsigned char *bp,unsigned char *bufp)
  21. {
  22.         if(!bufp || !(*bufp) || (bufp < bp) || !iscc(*bufp))
  23.                 return 0;
  24.         return (cc1(bp, bufp));
  25. }
  26. /*function:secondcc 汉字的第二个字节*/
  27. int secondcc(unsigned char *bp,unsigned char *bufp)
  28. {
  29.         if(!firstcc(bp, bufp-1))
  30.                 return 0;
  31.         if(*bufp == 0x7f)
  32.                 return 0;
  33.         if((*bufp >= 0x40) && (*bufp <= 0xfe))
  34.                 return 1;
  35.         return 0;
  36. }

  37. /************************************************************************/
  38. /*function:stptok 分解字符串为一组标记串                                        */
  39. /*description:根据分隔符'tok',从'src'分解到'trg'中,如果'tok'不设置,则*/
  40. /*根据'src'与len的最小值,将'src'拷贝到'trg'中。可以解决汉字中出现的与  */
  41. /*'tok'相同的分隔符被分解。                                                                                             */
  42. /************************************************************************/
  43. char *stptok(char *src,char *trg,int len,char *tok)
  44. {
  45.         register unsigned char *p;
  46.         if(trg) *trg = 0;
  47.         p = (unsigned char *)src;
  48.         if(!p || !(*p)) return src ;
  49.         if(tok && *tok) {
  50.                 while(*p) {
  51.         if(tok && *tok) {
  52.                 while(*p) {
  53.                         if(strchr(tok, *p)) {
  54.                                 if((p > (unsigned char *)src) && firstcc((unsigned char *)src, p-1)) {
  55.                                         p++;
  56.                                         continue;
  57.                                 }
  58.                                 break;
  59.                         }
  60.                         p++;
  61.                 }
  62.         } else {
  63.                 int l=strlen(src);
  64.                 if((len > 0) && (len < l))
  65.                         p = src + len;
  66.                 else p = src + l;
  67.         }
  68.         if(!trg || (len <= 1))
  69.                 return p;

  70.         while(*src && --len) {
  71.                 if((unsigned char *)src == p)
  72.                         break;
  73.                 *trg++ = *src++;
  74.         }
  75.         *trg = 0;

  76.         return src;
  77. }

复制代码
使用方法:

char buf[]="ABC= DEF + X";
char *p;
         p=buf;
         while (*p) {
          p=stptok(p,0,0,"+-*/%=");
         switch(*p) {
         case '=':
        case '+':
         case '-':
        case '*':
         case '/':
        case '%':
        default:
                  break;
        }
        p++;
      }
作者: fcoolx    时间: 2010-02-03 13:56
有开源可以参考
http://www.compuphase.com/minini.htm




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2