惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Last Watchdog
The Last Watchdog
AI
AI
Recent Announcements
Recent Announcements
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
J
Java Code Geeks
TaoSecurity Blog
TaoSecurity Blog
L
LangChain Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Microsoft Security Blog
Microsoft Security Blog
量子位
T
Threatpost
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - Franky
博客园 - 聂微东
L
LINUX DO - 最新话题
Security Archives - TechRepublic
Security Archives - TechRepublic
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
C
Check Point Blog
宝玉的分享
宝玉的分享
G
Google Developers Blog
Spread Privacy
Spread Privacy
Cloudbric
Cloudbric
SecWiki News
SecWiki News
有赞技术团队
有赞技术团队
www.infosecurity-magazine.com
www.infosecurity-magazine.com
W
WeLiveSecurity
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
V
Vulnerabilities – Threatpost
Cyberwarzone
Cyberwarzone
A
Arctic Wolf
P
Privacy & Cybersecurity Law Blog
P
Palo Alto Networks Blog
H
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
A
About on SuperTechFans
N
Netflix TechBlog - Medium
罗磊的独立博客
月光博客
月光博客

博客园 - 飘啊飘

一个精简的vi源码(2000行) dtruss 粗糙的翻译 gdb常用命令[转] gdb中信号的处理[转] 在linux中通过进程名获得进程id busybox0.60.3源码学习开始 pygame做的贪吃蛇 python中使用struct模块处理二进制数据 使用PYGAME开发的坦克游戏[代码][思路] 一篇很好的讲/etc/inittab的文章[转] 哲学家吃空心粉问题 《代码整洁之道》笔记之函数 使用面向对象概念优化条件判断语句的一个小应用 python生成文件树的代码 深入理解软件包的配置、编译与安装[转] pygame学习之对象移动 pywin32重启电脑 - 飘啊飘 - 博客园 解决LINUX和WINDOWS时间不一置 UNIX基础知识--《APUE》第一章笔记
通过状态机实现的一个配置读取函数
飘啊飘 · 2010-11-10 · via 博客园 - 飘啊飘

完整代码:

#include <stdio.h>
#include 
<string.h>
#include 
<stdlib.h>
#include 
<stdbool.h>char IS_CHAR(char x)
{
    
return (x >= 'A' && x <= 'Z'|| (x >= 'a' && x <= 'z')? true:false;
}
char *strtrim(char *s)
{
    
char *= NULL,*right = s,*left = s;while(isspace(*left)) left++;
    p 
= left;
    
while(*right != '\0') right++;
    
for(right--;isspace(*right);right--);
    
*(right+1= '\0';
    strcpy(s,p);
    
return s;
}
void parse_body(const char *s,
                
char *section,
                
char *key,
                
char *value)
{
    typedef 
enum _body_state body_state;
    
enum _body_state
    {
        STATE_NONE 
= 0,
        STATE_SECTION,
        STATE_KEY,
        STATE_SPLIT,
        STATE_IN_SECTION,
        STATE_VALUE,
        STATE_COMMENT
    };
char *p_section_start;
    
char *p_key_start;
    
char *p_value_start;char *= NULL;

    body_state state 

= STATE_NONE;
    
char *tmp = (char*)malloc(strlen(s) + 1);
    memset(tmp,
'\0',strlen(s) + 1);
    strncpy(tmp,s,strlen(s));
for(p = tmp;
        
*!= '\0';
        p
++)
    {
        
switch(state)
        {
        
case STATE_NONE:
            
if(IS_CHAR(*p))
            {
                state 
= STATE_SECTION;
                p_section_start 
= p;
            }
            
if((*p) == '#')
            {
                state 
= STATE_COMMENT;
            }
            
break;
        
case STATE_COMMENT:
            
if(*== '\n')
            {
                state 
= STATE_NONE;
            }
break;
        
case STATE_SECTION:
            
if((*p) == '{')
            {
                
*= '\0';
                state 
= STATE_IN_SECTION;

            }

break;
        
case STATE_KEY:
            
if(*== '=')
            {
                
*= '\0';
                p_value_start 
= p+1;
                state 
= STATE_VALUE;
            }
            
break;
        
case STATE_VALUE:
            
if(*== ';')
            {
                
*= '\0';

                strtrim(p_section_start);
                strtrim(p_key_start);
                strtrim(p_value_start);

if(strcmp(p_section_start,section) == 0 &&
                   strcmp(p_key_start ,key) 
== 0)
                {
                    strcpy(value,p_value_start);
                }
                state 
= STATE_IN_SECTION;
            }
            
break;
        
case STATE_SPLIT:
            
if(IS_CHAR(*p))
            {
                state 
= STATE_VALUE;
            }
            
break;
        
case STATE_IN_SECTION:
            
if(IS_CHAR(*p))
            {
                p_key_start 
= p;
                state 
= STATE_KEY;
            }
            
if((*p) == '}')
            {
                state 
= STATE_NONE;
            }
            
break;
        }
    }

    free(tmp);
}

int main()
{
    
char value[16= {0};
    FILE 
*= NULL;
    
char *buff = NULL;
    fpos_t pos 
= 0;
    f 
= fopen("d:\\my.conf","r");
    fseek(f,
0,SEEK_END);
    fgetpos(f,
&pos);
    buff 
= (char*)malloc(pos);
    memset(buff,
0,pos);
    fseek(f,
0,SEEK_SET);
    fread(buff,pos,
1,f);

    parse_body(buff,

"people","name",value);
    printf(
"value:%s",value);

    free(buff);

return 0;
}

my.conf:

#test title
#
this is a comment
#

people
{
    name

=ligang;
    age
=20;
    sex
=1;
}

box
{
    widget

=20;
    height
=30;
    color
=blue;
}

seller
{
    n73 

=    590;
    N900 
=      1500;
}

学习了李先静老师的系统程序员成长计划,自己实现了个简单的状态机,感觉真是威力无穷啊。。

 配合词法树就可以实现词法解析处理了,嘿嘿,年底也可以写个简单的解释语言玩玩咯