- 论坛徽章:
- 0
|
有关在UNIX下用C写一个读配置文件的问题
- // -------------------------------------------------------------------------------------------
- // 从一个串 读入一个单词(用引号括起来的或是只由字母数字组成的串),
- // 如果没有读入则返回NULL,如果已读入则返回下一个分析的位置,
- // 现在使用的只是读配置文件
- // -------------------------------------------------------------------------------------------
- const char *GetAWord (const char *pool, char *Word, int StrLen)
- {
- int poolptr = 0, wordptr = 0;
- char c, *dest;
- if (!pool)
- return NULL;
- while (isspace (pool[poolptr]))
- poolptr++;
- c = pool[poolptr];
- if (!c)
- return NULL;
- if (c == '\'' || c == '"')
- {
- dest = strchr (pool + poolptr + 1, c);
- if (!dest)
- return NULL;
- wordptr = dest - (pool + poolptr);
- if (wordptr >; StrLen - 1)
- return NULL;
- strncpy (Word, pool + poolptr + 1, wordptr - 1);
- Word[wordptr - 1] = 0;
- return dest + 1;
- }
- while (isalnum (pool[poolptr]))
- {
- if (wordptr >; StrLen - 1)
- return NULL;
- Word[wordptr++] = pool[poolptr++];
- }
- Word[wordptr] = 0;
- if (wordptr)
- return pool + poolptr;
- return NULL;
- }
- 以上是函数说明
- 以前写得不是很好
复制代码 |
|