- 论坛徽章:
- 15
|
配置文件里取值的一个小函数
经常需要读取像
title = xxxxx
ip = 192.168.13.65
size = 1000;
类似于这 ...
darkce 发表于 2010-02-03 13:01 ![]()
- /*function:iscc 判断是否是汉字*/
- int iscc(unsigned char ch)
- {
- return (ch >= 0x81 && ch < 0xff);
- }
- /*function:cc1 取汉字的一个字节*/
- static int cc1(unsigned char *bp,unsigned char *bufp)
- {
- register unsigned char *p;
- register int i = 0;
- for(p = bufp; iscc(*p); p--)
- {
- i++;
- if(p <= bp)
- break;
- }
- return (i & 1);
- }
- /*function:firstcc 汉字的第一个字节*/
- int firstcc(unsigned char *bp,unsigned char *bufp)
- {
- if(!bufp || !(*bufp) || (bufp < bp) || !iscc(*bufp))
- return 0;
- return (cc1(bp, bufp));
- }
- /*function:secondcc 汉字的第二个字节*/
- int secondcc(unsigned char *bp,unsigned char *bufp)
- {
- if(!firstcc(bp, bufp-1))
- return 0;
- if(*bufp == 0x7f)
- return 0;
- if((*bufp >= 0x40) && (*bufp <= 0xfe))
- return 1;
- return 0;
- }
- /************************************************************************/
- /*function:stptok 分解字符串为一组标记串 */
- /*description:根据分隔符'tok',从'src'分解到'trg'中,如果'tok'不设置,则*/
- /*根据'src'与len的最小值,将'src'拷贝到'trg'中。可以解决汉字中出现的与 */
- /*'tok'相同的分隔符被分解。 */
- /************************************************************************/
- char *stptok(char *src,char *trg,int len,char *tok)
- {
- register unsigned char *p;
- if(trg) *trg = 0;
- p = (unsigned char *)src;
- if(!p || !(*p)) return src ;
- if(tok && *tok) {
- while(*p) {
- if(tok && *tok) {
- while(*p) {
- if(strchr(tok, *p)) {
- if((p > (unsigned char *)src) && firstcc((unsigned char *)src, p-1)) {
- p++;
- continue;
- }
- break;
- }
- p++;
- }
- } else {
- int l=strlen(src);
- if((len > 0) && (len < l))
- p = src + len;
- else p = src + l;
- }
- if(!trg || (len <= 1))
- return p;
- while(*src && --len) {
- if((unsigned char *)src == p)
- break;
- *trg++ = *src++;
- }
- *trg = 0;
- return src;
- }
复制代码 使用方法:
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++;
} |
|