From d049d16a6d380574615acdcffa4975bcb6fc8a44 Mon Sep 17 00:00:00 2001 From: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu, 1 Nov 2012 16:11:29 +0100 Subject: [PATCH] Added xml_open_document API call to read a XML document from file Squashed commit of the following: commit 3501a6d4a4696fe14781043109964f42ff489309 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:10:37 2012 +0100 Open XML document... commit e4f63354259055578ef8827bde25c3322c5386ec Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:10:04 2012 +0100 Open XML document... commit cf9b46c26059a668f842b472609454ab0f9b809f Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:09:56 2012 +0100 Open XML document... commit be3baa3d99609744084f0a2bfe49b5cd73f7ad87 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:09:48 2012 +0100 Open XML document... commit 9be46ba2cf68f22f3aa1befd75f6e63a3cc3972f Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:07:35 2012 +0100 Open XML document... commit 081a5997470c2e633bc1c8703c14ef7b70c55f29 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:06:58 2012 +0100 Open XML document... commit 4af333480380d603266f8df42b8c45408c05d48d Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:05:24 2012 +0100 Open XML document... commit f5c3a25541864db290895b652648485bba2e72fc Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:04:30 2012 +0100 Open XML document... commit 81ee66c39efaa110e92af713e04cb4249be536dc Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:02:53 2012 +0100 Open XML document... commit 1448aa14844bf74c3ccbcc6c4cd62476b6987f04 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:02:03 2012 +0100 Open XML document... commit dc767c8d671013b936fdcb729d1ebcad250f6dd4 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:01:14 2012 +0100 Open XML document... commit ad23dcc1278b132f01bc1a9c403fbd2261341759 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 16:00:25 2012 +0100 Open XML document... commit f7c905130de9c210f6e522c19ef1d4f29fd1f218 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 15:59:31 2012 +0100 Open XML document... commit 1cce95d4627e1c62bf67b5531a20a49588bef792 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 15:57:29 2012 +0100 Open XML document... commit 1b3fa5f68492bf5091b4cd51dec6c2817e170efa Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 15:45:03 2012 +0100 Open XML document... commit 0778105ea2b0761342b17e22b4be541a6a2ded3b Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 15:41:48 2012 +0100 Open XML document... commit 64418dcb81dcad5cb771553260c54d45eb6544c5 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 15:41:06 2012 +0100 Open XML document... commit 54061a0e3b7a7eb19c2048452a445c654f64a5f4 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 15:40:39 2012 +0100 Open XML document... commit e776734fa212642d0fe467356c66211e06c489db Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 15:39:39 2012 +0100 Open XML document... commit 81bbf1e0c342888ece7de9c921cbd1fb66de3a38 Author: ooxi <85fcd0ef4ec8@f977375cdcd6.anonbox.net> Date: Thu Nov 1 15:38:36 2012 +0100 Open XML document... --- CMakeLists.txt | 6 +++++- src/xml.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- src/xml.h | 19 +++++++++++++++++-- test/test-xml.c | 24 ++++++++++++++++++++++++ test/test.xml | 7 +++++++ 5 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 test/test.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index 77ff54c..b4a1c10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ PROJECT(xml) SET(VERSION_MAJOR "0") SET(VERSION_MINOR "1") -SET(VERSION_PATCH "3") +SET(VERSION_PATCH "4") CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0 FATAL_ERROR) @@ -30,6 +30,10 @@ ADD_EXECUTABLE(test-xml ) TARGET_LINK_LIBRARIES(test-xml xml) +FILE( COPY ${TEST_SOURCE_DIRECTORY}/test.xml + DESTINATION ${PROJECT_BINARY_DIR} +) + # Building example ADD_EXECUTABLE(example diff --git a/src/xml.c b/src/xml.c index ee13669..9e78b4b 100644 --- a/src/xml.c +++ b/src/xml.c @@ -629,8 +629,6 @@ exit_failure: /** * [PUBLIC API] - * - * */ struct xml_document* xml_parse_document(uint8_t* buffer, size_t length) { @@ -669,6 +667,53 @@ struct xml_document* xml_parse_document(uint8_t* buffer, size_t length) { +/** + * [PUBLIC API] + */ +struct xml_document* xml_open_document(FILE* source) { + + /* Prepare buffer + */ + size_t const read_chunk = 1; // TODO 4096; + + size_t document_length = 0; + size_t buffer_size = 1; // TODO 4069 + uint8_t* buffer = malloc(buffer_size * sizeof(uint8_t)); + + /* Read hole file into buffer + */ + while (!feof(source)) { + + /* Reallocate buffer + */ + if (buffer_size - document_length < read_chunk) { + buffer = realloc(buffer, buffer_size + 2 * read_chunk); + buffer_size += 2 * read_chunk; + } + + size_t read = fread( + &buffer[document_length], + sizeof(uint8_t), read_chunk, + source + ); + + document_length += read; + } + fclose(source); + + /* Try to parse buffer + */ + struct xml_document* document = xml_parse_document(buffer, document_length); + + if (!document) { + free(buffer); + return 0; + } + return document; +} + + + /** * [PUBLIC API] */ diff --git a/src/xml.h b/src/xml.h index 39643ee..4bc3064 100644 --- a/src/xml.h +++ b/src/xml.h @@ -53,14 +53,29 @@ struct xml_string; * * @warning `buffer` will be referenced by the document, you may not free it * until you free the xml_document - * @warning You have to call xml_free after you finished using the document + * @warning You have to call xml_document_free after you finished using the + * document * - * @return The parsed xml fragment iff `parsing was successful + * @return The parsed xml fragment iff parsing was successful, 0 otherwise */ struct xml_document* xml_parse_document(uint8_t* buffer, size_t length); +/** + * Tries to read an XML document from disk + * + * @param source File that will be read into an xml document. Will be closed + * + * @warning You have to call xml_document_free with free_buffer = true after you + * finished using the document + * + * @return The parsed xml fragment iff parsing was successful, 0 otherwise + */ +struct xml_document* xml_open_document(FILE* source); + + + /** * Frees all resources associated with the document. All xml_node and xml_string * references obtained through the document will be invalidated diff --git a/test/test-xml.c b/test/test-xml.c index 6c0479a..fc55573 100644 --- a/test/test-xml.c +++ b/test/test-xml.c @@ -193,6 +193,29 @@ static void test_xml_parse_document_2() { +/** + * Tests the xml_open_document functionality + */ +static void test_xml_parse_document_3() { + #define FILE_NAME "test.xml" + FILE* handle = fopen(FILE_NAME, "rb"); + assert_that(handle, "Cannot open " FILE_NAME); + + struct xml_document* document = xml_open_document(handle); + assert_that(document, "Cannot parse " FILE_NAME); + + struct xml_node* element = xml_easy_child( + xml_document_root(document), "Element", "With", 0 + ); + assert_that(element, "Cannot find Document/Element/With"); + assert_that(string_equals(xml_node_content(element), "Child"), "Content of Document/Element/With must be `Child'"); + + xml_document_free(document, true); + #undef FILE_NAME +} + + + /** @@ -202,6 +225,7 @@ int main(int argc, char** argv) { test_xml_parse_document_0(); test_xml_parse_document_1(); test_xml_parse_document_2(); + test_xml_parse_document_3(); fprintf(stdout, "All tests passed :-)\n"); exit(EXIT_SUCCESS); diff --git a/test/test.xml b/test/test.xml new file mode 100644 index 0000000..a0e1ab8 --- /dev/null +++ b/test/test.xml @@ -0,0 +1,7 @@ + + + + Child + + +