Add xml struct to store data in

This commit is contained in:
William Brawner 2018-02-07 21:59:00 -06:00
parent 2b334cec32
commit 3af94c0e98
3 changed files with 51 additions and 17 deletions

3
.gitignore vendored
View file

@ -1,3 +1,6 @@
.vs/
.vscode/
build/
out/
*.sln
*.swp

View file

@ -6,7 +6,7 @@ SOURCES=src/feader.c
OUTPUT=-o out/feader
all: clean setup build
all: clean setup build run
build: $(SOURCES)
@ -17,3 +17,6 @@ clean:
setup:
mkdir out
run:
./out/feader

View file

@ -1,26 +1,54 @@
#include "curl/curl.h"
#include "/usr/local/include/curl/easy.h"
#include <stdio.h>
#include "curl/easy.h"
#include <stdlib.h>
#include <string.h>
size_t my_write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);
typedef struct {
char* data;
int size;
} xml;
int main(int argc, char** argv) {
CURL *curl = curl_easy_init();
size_t my_write_callback(char *ptr, size_t size, size_t nmemb, xml *x);
size_t get_xml(xml *x, char *url);
int main(int argc, char **argv) {
xml x;
x.size = 0;
x.data = malloc(1);
x.data[0] = '\0';
int res = get_xml(&x, "http://127.0.0.1/wbrawner-jekyll/_site/feed.xml");
printf("\n%s\n", x.data);
free(x.data);
return res;
}
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, char* url) {
CURL *curl = curl_easy_init();
if (!curl) {
printf("Unable to instantiate curl object. Aborting");
return 1;
}
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1/wbrawner-jekyll/_site/feed.xml");
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, *my_write_callback);
res = curl_easy_perform(curl);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, x);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
size_t my_write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) {
printf("Got the following from the server: \n%s\n", ptr);
return 0;
}
}