2

我正在编写一个用于反序列化数据的库。我有像这样的结构定义:

#ifndef SEARCHRESULTS_H
#define SEARCHRESULTS_H

typedef struct {
    char *url;
} SearchResult;

typedef struct {
    int count;
    SearchResult *searchResults;
} SearchResults;

#endif

由于 C 缺乏反射,所有函数都必须手动编写,但我相信应该有一种很好的方法来解析标题、查找struct名称和字段并生成函数(伪 C):

#include "SearchResults.h"
#include "pdata_serialization.h"

void SearchResult_parse(pdata *data, SearchResult *obj) {
    obj->url = strdup(data->values["url"]);
}

void SearchResult_free(SearchResult *obj) {
    free(obj->url);
    free(obj);
}

void SearchResults_parse(pdata *data, SearchResults *obj) {
    obj->count = data->values["count"];
    obj->searchResults = malloc(sizeof(SearchResult) * obj->count);
    for (int i = 0; i < obj->count; i++)
        SearchResult_parse(data->values["searchResults"][i], &obj->searchResults[i]);
}

void SearchResults_free(SearchResults *obj) {
    for (int i = 0; i < obj->count; i++)
        SearchResult_free(&obj->searchResults[i]);
    free(obj);
}

我不是在寻找一个完整的解决方案,而是一个理智和最小的想法和一个小例子。

4

2 回答 2

1

GNU Autogen解决了这个问题。我将标题转换为定义文件,每个结构一个:

// SearchResult.def
AutoGen Definitions struct;
struct = {
    type = "char *";
    name = "url";
};

// SearchResults.def
AutoGen Definitions struct;
struct = {
    type = "SearchResult *";
    name = "searchResults";
};

并编写了以下不太漂亮的模板:

[= AutoGen5 Template h c =][=
    (define model (string-substitute (def-file) ".def" ""))
=][=
    CASE (suffix)
=][=
    == h
=][=
    (make-header-guard "")
=]

typedef struct _[= (. model) =] {[=
    FOR struct =]
    [=
        (get "type")
    =][=
        (get "name")
    =];[=
        IF (and (*== (get "type") "*") (not (== (get "type") "char *"))) =]
    size_t [= (get "name") =]Count;[=
        ENDIF =][=
    ENDFOR struct
=]
} [= (. model) =];

#endif /* [= (. header-guard) =] */
[=
    == c
=]#include "[= (. header-file) =]"
#include "pdata_serialization.h"

void [= (. model) =]_parse(pdata *data, [= (. model) =] *obj) {[=
    FOR struct =][=
        IF (== (get "type") "char *")
    =]
    obj->[= (get "name") =] = strdup(data->values["[= (get "name") =]"]);[=
        ELIF (*== (get "type") "*")
    =]
    obj->[= (get "name") =]Count = data->values["[= (get "name") =]Count"];
    obj->[= (get "name") =] = malloc(sizeof([= (string-substitute (get "type") " *" "") =]) * obj->[= (get "name") =]Count);
    for (size_t i = 0; i < obj->[= (get "name") =]Count; i++)
        [= (string-substitute (get "type") " *" "") =]_parse(data->values["[= (get "name") =]"][i], &obj->[= (get "name") =][i]);[=
        ENDIF
    =][=
    ENDFOR struct
=]
}

void [= (. model) =]_free([= (. model) =] *obj) {[=
    FOR struct =][=
        IF (== (get "type") "char *")
    =]
    free(obj->[= (get "name") =]);[=
        ELIF (*== (get "type") "*")
    =]
    for (size_t i = 0; i < obj->[= (get "name") =]Count; i++)
        [= (string-substitute (get "type") " *" "") =]_free(&obj->[= (get "name") =][i]);
    free(obj->[= (get "name") =]);[=
        ENDIF
    =][=
    ENDFOR struct
=]
    free(obj);
}
[= ESAC =]

有用!

于 2014-06-09T12:53:01.953 回答
0

我建议将解析后的数据作为键值对存储在哈希表中,以便您查找实体。

当然,对于请求的属性不可用的情况(例如,url 属性丢失),您必须采取措施。根据您在哈希表中存储信息的方式,还存在类型安全问题。但也许值得将键值对存储为字符串,并在需要时将它们转换为数字或其他内容。

于 2013-01-28T16:19:13.300 回答