コード例 #1
0
ファイル: xml.c プロジェクト: FauxFaux/despotify
bool xml_parse_confirm(struct playlist* pl,
                       unsigned char* xml,
                       int len)
{
    ezxml_t top = ezxml_parse_str(xml, len);

    bool confirm = !strncmp(top->name, "confirm", 7);

    if (confirm) {
        xml_parse_version(pl, top, "version", -1);
    }

    ezxml_free(top);
    return confirm;
}
コード例 #2
0
ファイル: xml.c プロジェクト: FauxFaux/despotify
struct playlist* xml_parse_playlist(struct playlist* pl,
                                    unsigned char* xml,
                                    int len,
                                    bool list_of_lists)
{
    ezxml_t top = ezxml_parse_str(xml, len);
    ezxml_t tmpx = ezxml_get(top, "next-change",0, "change", 0, "ops", 0,
                             "add", 0, "items", -1);
    char* items = NULL;
    if (tmpx)
        items = tmpx->txt;

    while (items && *items && isspace(*items))
        items++;

    if (list_of_lists) {
        /* create list of playlists */
        struct playlist* prev = NULL;
        struct playlist* p = pl;

        for (char* id = strtok(items, ",\n"); id; id = strtok(NULL, ",\n"))
        {
            if (prev) {
                p = calloc(1, sizeof(struct playlist));
                prev->next = p;
            }
            DSFYstrncpy(p->playlist_id, id, sizeof p->playlist_id);
            prev = p;
        }
    }
    else {
        /* create list of tracks */
        struct track* prev = NULL;
        struct track* root = NULL;
        struct track* t = NULL;

        int track_count = 0;
        for (char* id = strtok(items, ",\n"); id; id = strtok(NULL, ",\n"))
        {
            t = calloc(1, sizeof(struct track));
            if (prev)
                prev->next = t;
            else
                root = t;
            DSFYstrncpy(t->track_id, id, sizeof t->track_id);
            prev = t;
            track_count++;
        }
        pl->tracks = root;
        pl->num_tracks = track_count; // FIXME: <version> parsing overwrites track_count
    }

    xmlstrncpy(pl->author, sizeof pl->author, top,
               "next-change",0, "change", 0, "user", -1);
    xmlstrncpy(pl->name, sizeof pl->name, top,
               "next-change",0, "change", 0, "ops",0, "name", -1);
    xml_parse_version(pl, top, "next-change", 0, "version", -1);

    ezxml_free(top);
    return pl;
}
コード例 #3
0
ファイル: xml_comm.c プロジェクト: jinrenlab/tanlz
/******************************************************************************
 **函数名称: xml_parse
 **功    能: 解析XML文件缓存
 **输入参数:
 **     xml: XML树
 **     stack: XML栈
 **     str: XML字串
 **输出参数:
 **返    回: 0: 成功  !0: 失败
 **实现描述: 
 **     1. 解析版本信息
 **     2. 解析XML BODY
 **注意事项: 
 **作    者: # Qifeng.zou # 2013.02.05 #
 ******************************************************************************/
int xml_parse(xml_tree_t *xml, Stack_t *stack, const char *str, size_t len)
{
    xml_parse_t parse;

    parse.str = str;
    parse.ptr = str;
    parse.len = len; /* 未知 */

    while (!XmlIsStrEndChar(*(parse.ptr))
            && ((size_t)(parse.ptr - parse.str) < parse.len))
    {
        while (XmlIsIgnoreChar(*(parse.ptr))) parse.ptr++;    /* 跳过无意义的字符 */

        switch(*(parse.ptr))
        {
            case XML_BEGIN_FLAG:
            {
                switch(*(parse.ptr+1))
                {
                    case XML_VERS_FLAG:  /* "<?" 版本开始 */ 
                    {
                        /* 版本信息不用加载到XML树中 */
                        if (xml_parse_version(xml, &parse)) {
                            log_error(xml->log, "XML format is wrong![%-.32s] [%ld]",
                                parse.ptr, parse.ptr-parse.str);
                            return XML_ERR_FORMAT;
                        }
                        break;
                    }
                    case XML_NOTE_FLAG:   /* "<!--" 注释信息 */
                    {
                        /* 注释信息不用加载到XML树中 */
                        if (xml_parse_note(xml, &parse)) {
                            log_error(xml->log, "XML format is wrong![%-.32s]", parse.ptr);
                            return XML_ERR_FORMAT;
                        }
                        break;
                    }
                    case XML_END_FLAG:   /* "</" 节点结束 */
                    {
                        if (xml_parse_end(xml, stack, &parse)) {
                            log_error(xml->log, "XML format is wrong![%-.32s] [%ld]",
                                parse.ptr, parse.ptr-parse.str);
                            return XML_ERR_FORMAT;
                        }
                        break;
                    }
                    default:    /* "<XYZ" 节点开始 */
                    {
                        if (xml_parse_mark(xml, stack, &parse)) {
                            log_error(xml->log, "Parse XML failed! [%-.32s] [%ld]",
                                parse.ptr, parse.ptr-parse.str);
                            return XML_ERR_FORMAT;
                        }
                        break;
                    }
                }
                break;
            }
            case STR_END_FLAG:  /* 字串结束'\0' */
            {
                if (stack_empty(stack)) {
                    log_trace(xml->log, "Parse xml success!");
                    return XML_OK;
                }
                log_error(xml->log, "Invalid format! [%-.32s] [%ld]", parse.ptr, parse.ptr-parse.str);
                return XML_ERR_FORMAT;
            }
            default:            /* 非法字符 */
            {
                log_error(xml->log, "Invalid format! [%-.32s] [%ld]", parse.ptr, parse.ptr-parse.str);
                return XML_ERR_FORMAT;
            }
        }
    }

    if (!stack_empty(stack)) {
        log_error(xml->log, "Invalid format! [%-.32s]", parse.ptr);
        return XML_ERR_FORMAT;
    }
    
    return XML_OK;
}