Remove libcurl and libxml2 and add yxml

This commit is contained in:
William Brawner 2018-03-06 20:56:21 -06:00
parent c60c465732
commit 9f5ce5d762
7 changed files with 1278 additions and 201 deletions

View file

@ -5,37 +5,14 @@ project(
DESCRIPTION "The C library for Feader"
)
set(CMAKE_C_FLAGS, "-g -Wall")
include_directories(
"/usr/include"
"/usr/include/libxml2"
"/usr/include/x86_64-linux-gnu"
"/data/data/com.termux/files/usr/include"
"/data/data/com.termux/files/usr/include/libxml2"
"include"
)
link_directories(
"/usr/lib/x86_64-linux-gnu"
"/usr/lib"
"/data/data/com.termux/files/usr/lib"
"lib"
)
add_executable(feader-cli src/feader.c)
add_executable(
feader-cli
src/feader.c
)
add_library(
feader SHARED
src/feader.c
)
target_link_libraries(
feader-cli
curl
sqlite3
xml2
)
target_link_libraries(
feader
curl
sqlite3
xml2
)
set_target_properties(
feader
PROPERTIES

View file

@ -2,24 +2,13 @@
This repo houses the library behind Feader. The idea behind the project is to separate the logic for retrieving, parsing, and storing RSS feed elements from the display, so that interface used to access the data can be independent from the logic, therefore allowing much easier cross-platform development.
## Configuring the Environment
## External dependencies
### Ubuntu
The external dependencies are included in the `src/` directory, to allow for compilation anywhere. They were acquired from the following sources:
Install the following packages
- SQLite3: http://www.sqlite.org/download.html
- Yxml: https://g.blicky.net/yxml.git
```
cmake libcurl3-openssl-dev libxml2-dev libsqlite3-dev
```
OR run the configure script:
```
./configure
```
### Windows
WIP
## Building
@ -30,7 +19,6 @@ mkdir build
cd build
cmake ..
make
./Feader https://wbrawner.com/feed.xml
```
## Todo:

65
configure vendored
View file

@ -1,65 +0,0 @@
#!/bin/bash
source /etc/os-release
if [ "$ID" != "ubuntu" ]; then
echo "Unsupported build system. If you experience any build issues,
please try on Ubuntu, or modify the script to support your system
and submit a PR :)";
fi
CWD=$(pwd)
DEPS=$CWD/deps
TMP=$CWD/tmp
check_dependency() {
if [ ! "$(which $1)" ]; then
echo "please install $1 before continuing"
exit 1
fi
}
setup_dir() {
test -d $1 && rm -rf $1
mkdir $1
}
manual_setup() {
cd $TMP
# Set up libxml2
git clone https://github.com/gnome/libxml2 $TMP/libxml2
cd $TMP/libxml2
git reset --hard 707ad080e61014ab4a6d60dc12875e233c1f673c
virtualenv venv
source venv/bin/activate
/bin/bash $TMP/libxml2/autogen.sh --prefix=$DEPS
make install
deactivate
# Set up cURL
git clone https://github.com/curl/curl $TMP/curl
cd $TMP/curl
git reset --hard ccd1ec7aa8a0c0da9106c9a39e0d7de531d04dfb
/bin/bash $TMP/curl/buildconf
/bin/bash $TMP/curl/configure --prefix=$DEPS
make install
cd $TMP
rm -rf $TMP
}
setup_dir $DEPS
setup_dir $TMP
check_dependency cmake
check_dependency git
check_dependency virtualenv
manual_setup
cd $CWD
setup_dir build
cd build
cmake ..
echo "To build, run \"cd build && make\""

View file

@ -1,10 +1,4 @@
#include "curl/curl.h"
#include "curl/easy.h"
#include "feader.h"
#include "libxml/parser.h"
#include "libxml/tree.h"
#include "libxml/xmlreader.h"
#include "libxml/xmlstring.h"
#include <stdlib.h>
#include <string.h>
#include "sqlite/sqlite3.h"
@ -31,14 +25,6 @@ void fead_xml(char* url) {
xml *x = malloc(sizeof(xml));
get_xml_ptr(x, url);
int res = get_xml(x);
if (res == CURLE_OK) {
printf("XML data retrieved from server\n");
} else {
printf("Unable to retrieve XML for URL: %s\n", x->url);
printf("%s\n", x->errBuf);
}
parse_xml_items(x);
print_xml_elements(x);
@ -53,90 +39,58 @@ void get_xml_ptr(xml* x, char* url) {
x->url = url;
}
size_t my_write_callback(char *ptr, size_t size, size_t nmemb, xml *x) {
int byte_size = (size * nmemb);
printf("byte_size: %d\n", byte_size);
int new_size = x->size + byte_size;
printf("new_size size: %d\n", new_size);
x->data = realloc(x->data, new_size + 1);
memcpy(x->data + x->size, ptr, byte_size);
x->data[new_size] = '\0';
x->size = new_size;
printf("New xml size: %d\n", x->size);
return byte_size;
}
size_t get_xml(xml* x) {
CURL *curl = curl_easy_init();
if (!curl) {
printf("Unable to instantiate curl object. Aborting");
return 1;
}
curl_easy_setopt(curl, CURLOPT_URL, x->url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, *my_write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, x);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, x->errBuf);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_global_cleanup();
return res;
}
void parse_xml_items(xml *x) {
xmlChar* xChar = xmlCharStrndup(x->data, x->size);
x->xdp = xmlReadDoc(
xChar,
x->url,
NULL,
XML_PARSE_RECOVER
);
xmlFree(xChar);
// xmlChar* xChar = xmlCharStrndup(x->data, x->size);
// x->xdp = xmlReadDoc(
// xChar,
// x->url,
// NULL,
// XML_PARSE_RECOVER
// );
// xmlFree(xChar);
}
void print_xml_elements(xml* x) {
if (x->xdp == NULL || x->xdp->children == NULL) {
printf("Unable to parse XML\n");
}
// if (x->xdp == NULL || x->xdp->children == NULL) {
// printf("Unable to parse XML\n");
// }
xmlNode* rss = x->xdp->children;
xmlNode* channel = rss->children->next;
xmlNode* channelChild = channel->children;
int articleCount = 0;
while (channelChild != NULL) {
if (strncmp("title", channelChild->name, strlen(channelChild->name)) == 0) {
if (channelChild->children != NULL) {
printf("%s: %s\n", channelChild->name, channelChild->children->content);
}
} else if (strncmp("description", channelChild->name, strlen(channelChild->name)) == 0) {
if (channelChild->children != NULL) {
printf("%s: %s\n", channelChild->name, channelChild->children->content);
}
} else if (strncmp("item", channelChild->name, strlen(channelChild->name)) == 0) {
articleCount++;
xmlNode* itemData = channelChild->children;
printf("Article info:\n");
do {
if (strncmp("text", itemData->name, strlen(itemData->name)) != 0) {
if (strlen(itemData->children->content) > 200) {
printf("%s: %ld chars\n", itemData->name, strlen(itemData->children->content));
} else {
printf("%s: %s\n", itemData->name, itemData->children->content);
}
}
itemData = itemData->next;
} while (itemData != NULL);
printf("\n");
}
channelChild = channelChild->next;
}
// xmlNode* rss = x->xdp->children;
// xmlNode* channel = rss->children->next;
// xmlNode* channelChild = channel->children;
// int articleCount = 0;
// while (channelChild != NULL) {
// if (strncmp("title", channelChild->name, strlen(channelChild->name)) == 0) {
// if (channelChild->children != NULL) {
// printf("%s: %s\n", channelChild->name, channelChild->children->content);
// }
// } else if (strncmp("description", channelChild->name, strlen(channelChild->name)) == 0) {
// if (channelChild->children != NULL) {
// printf("%s: %s\n", channelChild->name, channelChild->children->content);
// }
// } else if (strncmp("item", channelChild->name, strlen(channelChild->name)) == 0) {
// articleCount++;
// xmlNode* itemData = channelChild->children;
// printf("Article info:\n");
// do {
// if (strncmp("text", itemData->name, strlen(itemData->name)) != 0) {
// if (strlen(itemData->children->content) > 200) {
// printf("%s: %ld chars\n", itemData->name, strlen(itemData->children->content));
// } else {
// printf("%s: %s\n", itemData->name, itemData->children->content);
// }
// }
// itemData = itemData->next;
// } while (itemData != NULL);
// printf("\n");
// }
// channelChild = channelChild->next;
// }
rss = NULL;
channel = NULL;
// rss = NULL;
// channel = NULL;
printf("Found %d articles in the feed\n", articleCount);
// printf("Found %d articles in the feed\n", articleCount);
}
void cleanup_xml(xml* x) {

View file

@ -1,4 +1,3 @@
#include "libxml/tree.h"
#include "sqlite/sqlite3.h"
#define DB_FILE "feader.db"
@ -8,7 +7,6 @@ typedef struct {
long totalSize;
char* url;
char* errBuf;
xmlDocPtr xdp;
} xml;
size_t my_write_callback(char *ptr, size_t size, size_t nmemb, xml *x);

1063
src/yxml/yxml.c Normal file

File diff suppressed because it is too large Load diff

162
src/yxml/yxml.h Normal file
View file

@ -0,0 +1,162 @@
/* Copyright (c) 2013-2014 Yoran Heling
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef YXML_H
#define YXML_H
#include <stdint.h>
#include <stddef.h>
#if defined(_MSC_VER) && !defined(__cplusplus) && !defined(inline)
#define inline __inline
#endif
/* Full API documentation for this library can be found in the "yxml.pod" file
* in the yxml git repository, or online at http://dev.yorhel.nl/yxml/man */
typedef enum {
YXML_EEOF = -5, /* Unexpected EOF */
YXML_EREF = -4, /* Invalid character or entity reference (&whatever;) */
YXML_ECLOSE = -3, /* Close tag does not match open tag (<Tag> .. </OtherTag>) */
YXML_ESTACK = -2, /* Stack overflow (too deeply nested tags or too long element/attribute name) */
YXML_ESYN = -1, /* Syntax error (unexpected byte) */
YXML_OK = 0, /* Character consumed, no new token present */
YXML_ELEMSTART = 1, /* Start of an element: '<Tag ..' */
YXML_CONTENT = 2, /* Element content */
YXML_ELEMEND = 3, /* End of an element: '.. />' or '</Tag>' */
YXML_ATTRSTART = 4, /* Attribute: 'Name=..' */
YXML_ATTRVAL = 5, /* Attribute value */
YXML_ATTREND = 6, /* End of attribute '.."' */
YXML_PISTART = 7, /* Start of a processing instruction */
YXML_PICONTENT = 8, /* Content of a PI */
YXML_PIEND = 9 /* End of a processing instruction */
} yxml_ret_t;
/* When, exactly, are tokens returned?
*
* <TagName
* '>' ELEMSTART
* '/' ELEMSTART, '>' ELEMEND
* ' ' ELEMSTART
* '>'
* '/', '>' ELEMEND
* Attr
* '=' ATTRSTART
* "X ATTRVAL
* 'Y' ATTRVAL
* 'Z' ATTRVAL
* '"' ATTREND
* '>'
* '/', '>' ELEMEND
*
* </TagName
* '>' ELEMEND
*/
typedef struct {
/* PUBLIC (read-only) */
/* Name of the current element, zero-length if not in any element. Changed
* after YXML_ELEMSTART. The pointer will remain valid up to and including
* the next non-YXML_ATTR* token, the pointed-to buffer will remain valid
* up to and including the YXML_ELEMEND for the corresponding element. */
char *elem;
/* The last read character(s) of an attribute value (YXML_ATTRVAL), element
* data (YXML_CONTENT), or processing instruction (YXML_PICONTENT). Changed
* after one of the respective YXML_ values is returned, and only valid
* until the next yxml_parse() call. Usually, this string only consists of
* a single byte, but multiple bytes are returned in the following cases:
* - "<?SomePI ?x ?>": The two characters "?x"
* - "<![CDATA[ ]x ]]>": The two characters "]x"
* - "<![CDATA[ ]]x ]]>": The three characters "]]x"
* - "&#N;" and "&#xN;", where dec(n) > 127. The referenced Unicode
* character is then encoded in multiple UTF-8 bytes.
*/
char data[8];
/* Name of the current attribute. Changed after YXML_ATTRSTART, valid up to
* and including the next YXML_ATTREND. */
char *attr;
/* Name/target of the current processing instruction, zero-length if not in
* a PI. Changed after YXML_PISTART, valid up to (but excluding)
* the next YXML_PIEND. */
char *pi;
/* Line number, byte offset within that line, and total bytes read. These
* values refer to the position _after_ the last byte given to
* yxml_parse(). These are useful for debugging and error reporting. */
uint64_t byte;
uint64_t total;
uint32_t line;
/* PRIVATE */
int state;
unsigned char *stack; /* Stack of element names + attribute/PI name, separated by \0. Also starts with a \0. */
size_t stacksize, stacklen;
unsigned reflen;
unsigned quote;
int nextstate; /* Used for '@' state remembering and for the "string" consuming state */
unsigned ignore;
unsigned char *string;
} yxml_t;
#ifdef __cplusplus
extern "C" {
#endif
void yxml_init(yxml_t *, void *, size_t);
yxml_ret_t yxml_parse(yxml_t *, int);
/* May be called after the last character has been given to yxml_parse().
* Returns YXML_OK if the XML document is valid, YXML_EEOF otherwise. Using
* this function isn't really necessary, but can be used to detect documents
* that don't end correctly. In particular, an error is returned when the XML
* document did not contain a (complete) root element, or when the document
* ended while in a comment or processing instruction. */
yxml_ret_t yxml_eof(yxml_t *);
#ifdef __cplusplus
}
#endif
/* Returns the length of the element name (x->elem), attribute name (x->attr),
* or PI name (x->pi). This function should ONLY be used directly after the
* YXML_ELEMSTART, YXML_ATTRSTART or YXML_PISTART (respectively) tokens have
* been returned by yxml_parse(), calling this at any other time may not give
* the correct results. This function should also NOT be used on strings other
* than x->elem, x->attr or x->pi. */
static inline size_t yxml_symlen(yxml_t *x, const char *s) {
return (x->stack + x->stacklen) - (const unsigned char*)s;
}
#endif
/* vim: set noet sw=4 ts=4: */