在STM32+FATFS上移植ezxml

ezXML - XML Parsing C Library
version 0.8.5

ezXML is a C library for parsing XML documents inspired by simpleXML for PHP.
As the name implies, it's easy to use. It's ideal for parsing XML configuration
files or REST web service responses. It's also fast and lightweight (less than
20k compiled). The latest verions is available here:
http://prdownloads.sf.net/ezxml/ezxml-0.8.6.tar.gz?download

  • 添加头文件
#include "ff.h"
  • ezxml_parse_fp
    原函数
// Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
// stream into memory and then parses it. For xml files, use ezxml_parse_file()
// or ezxml_parse_fd()
ezxml_t ezxml_parse_fp(FILE *fp)
{
    ezxml_root_t root;
    size_t l, len = 0;
    char *s;

    if (! (s = malloc(EZXML_BUFSIZE))) return NULL;
    do {
        len += (l = fread((s + len), 1, EZXML_BUFSIZE, fp));
        if (l == EZXML_BUFSIZE) s = realloc(s, len + EZXML_BUFSIZE);
    } while (s && l == EZXML_BUFSIZE);

    if (! s) return NULL;
    root = (ezxml_root_t)ezxml_parse_str(s, len);
    root->len = -1; // so we know to free s in ezxml_free()
    return &root->xml;
}

移植函数

ezxml_t ezxml_parse_fp(FIL *fp)
{
    ezxml_root_t root;
    size_t l, len = 0;
    char *s;

    if (! (s = malloc(EZXML_BUFSIZE))) return NULL;
    do {
        f_read(fp,(s + len),EZXML_BUFSIZE,&l);
        len += l;
        if (l == EZXML_BUFSIZE) s = realloc(s, len + EZXML_BUFSIZE);
    } while (s && l == EZXML_BUFSIZE);

    if (! s) return NULL;
    root = (ezxml_root_t)ezxml_parse_str(s, len);
    root->len = -1; // so we know to free s in ezxml_free()
    return &root->xml;
}
  • 没有用到ezxml_t ezxml_parse_fd(int fd), 删除之
  • ezxml_parse_file
    原函数
// a wrapper for ezxml_parse_fd that accepts a file name
ezxml_t ezxml_parse_file(const char *file)
{
    int fd = open(file, O_RDONLY, 0);
    ezxml_t xml = ezxml_parse_fd(fd);
    
    if (fd >= 0) close(fd);
    return xml;
}

移植函数

ezxml_t ezxml_parse_file(const char *file)
{
    FIL fp;
    FRESULT res;
    ezxml_t xml;
    res = f_open(&fp, file, FA_READ);

    xml = ezxml_parse_fp(&fp);
    
    if (res == FR_OK) f_close(&fp);
    return xml;
}
  • strdup 实现
#define strdup              ezxml_mem_strdup
/*rgw 2016.11.28*/
char *ezxml_mem_strdup(const char *s)
{
    char *result = (char *)malloc(strlen(s) + 1);
    if (result == NULL)
        return NULL;
    strcpy(result, s);
    return result;
}

你可能感兴趣的:(在STM32+FATFS上移植ezxml)