工具箱解析用C iCalendar档案 [英] Toolset to parse iCalendar file in C

查看:226
本文介绍了工具箱解析用C iCalendar档案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析ICS文件在C和将逐行这样做。每行可以有很大的差异其格式,但普遍认为的标准。

I need to parse an ics file in C and will be doing it line by line. Each line can vary a lot by their format but generally holds a standard.

下面是一些规则,我注意到:

Here are some rules I have noticed:


  • 有一个属性名

  • 可选参数,每个以分号开始

    • 能有CSV太

    • 可以用双引号值,其中像逗号,冒号半和冒号万一事情将需要在此忽略

    下面是一个例子集成电路组件,将需要分析出:

    Here is an example ics component that would need to be parsed out:

    UID:uid1@example.com
    DTSTAMP:19970714T170000Z
    ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
    CATEGORIES:Project Report, XYZ, Weekly Meeting
    DTSTART:19970714T170000Z
    DTEND:19970715T035959Z
    SUMMARY:Bastille Day Party
    

    您将在搞什么注意到 MAILTO 有一个如下的。只有第一个冒号会被解析,并且冒号后剩下的就是属性值。

    You'll notice in things like the MAILTO there is a following :. Only the first colon would be parsed, and the rest after that colon is the property value.

    使用类似的strtok()似乎基本是足够的这个问题。

    Using something like strtok() seems to basic to be adequate for this problem.

    应该像普通的前pression被用来解决这个问题?寻找到它,我看到了一个正则表达式的解决方案的例子在C#中被做了这个StackOverflow的答案

    Should something like regular expression be used to solve this problem? Looking into it, I see an example of a regex solution being done in C# on this stackoverflow answer.

    推荐答案

    您可以用这个做

    #include <stdlib.h>
    #include <string.h>
    
    int
    main(void)
    {
        FILE *ics;
        char line[100];
    
        ics = fopen("example.ics", "r");
        if (ics == NULL)
            return -1;
        while (fgets(line, sizeof(line), ics) != NULL)
        {
            char *separator;
            char *key;
            char *tail;
            char *value;
    
            if ((tail = strchr(line, '\n')) != NULL)
                *tail = '\0'; // Remove the trailing '\n'
            separator = strpbrk(line, ":;");
            if (separator == NULL)
                continue;
            *separator = '\0';
    
            key = line; // Maybe you want to strip surrounding white spaces
            value = separator + 1; // Maybe you want to strip surrounding white spaces
    
            fprintf(stdout, "%s --> %s\n", key, value);
        }
        fclose(ics);
    
        return 0;
    }
    

    使用这个常规的前pression是喜欢杀苍蝇用火箭筒。

    Using a regular expression for this is like killing a fly with a bazooka.

    这篇关于工具箱解析用C iCalendar档案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆