|
36 | 36 | #include <stdarg.h>
|
37 | 37 | #include <string.h>
|
38 | 38 | #include <ctype.h>
|
| 39 | +#include <assert.h> |
39 | 40 |
|
40 | 41 | /* Ted Campbell, Aug 14 2007 */
|
41 | 42 | #if !defined(WIN32) && !defined(_WIN32)
|
@@ -787,24 +788,20 @@ ezxml_t ezxml_parse_str(char *s, size_t len) {
|
787 | 788 | /* Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire */
|
788 | 789 | /* stream into memory and then parses it. For xml files, use ezxml_parse_file() */
|
789 | 790 | /* or ezxml_parse_fd() */
|
790 |
| -ezxml_t ezxml_parse_fp(FILE * fp) { |
| 791 | +ezxml_t ezxml_parse_fp(FILE * fp, size_t size) { |
791 | 792 | ezxml_root_t root;
|
792 |
| - size_t l, len = 0; |
793 |
| - char *s; |
| 793 | + char *s; //The buffer |
794 | 794 |
|
795 | 795 | /* Jason Luu, Aug 29, 2007. Removed assignment in conditional statement */
|
796 |
| - s = (char*)malloc(EZXML_BUFSIZE); |
| 796 | + s = (char*)malloc(size); |
797 | 797 | if (!s)
|
798 | 798 | return NULL;
|
799 |
| - do { |
800 |
| - len += (l = fread((s + len), 1, EZXML_BUFSIZE, fp)); |
801 |
| - if (l == EZXML_BUFSIZE) |
802 |
| - s = (char*)realloc(s, len + EZXML_BUFSIZE); |
803 |
| - } while (s && l == EZXML_BUFSIZE); |
804 | 799 |
|
805 |
| - if (!s) |
806 |
| - return NULL; |
807 |
| - root = (ezxml_root_t) ezxml_parse_str(s, len); |
| 800 | + size_t bytes_read = fread(s, 1, size, fp); |
| 801 | + |
| 802 | + assert(bytes_read == size); |
| 803 | + |
| 804 | + root = (ezxml_root_t) ezxml_parse_str(s, bytes_read); |
808 | 805 | /* Ted Campbell, Aug 14, 2007. Added explicit cast. */
|
809 | 806 | root->len = (size_t) (-1); /* so we know to free s in ezxml_free() */
|
810 | 807 | return &root->xml;
|
@@ -844,11 +841,18 @@ ezxml_t ezxml_parse_fd(int fd) {
|
844 | 841 |
|
845 | 842 | /* a wrapper for ezxml_parse_fd that accepts a file name */
|
846 | 843 | ezxml_t ezxml_parse_file(const char *file) {
|
847 |
| - int fd = open(file, O_RDONLY, 0); |
848 |
| - ezxml_t xml = ezxml_parse_fd(fd); |
| 844 | + ezxml_t xml; |
| 845 | + |
| 846 | + FILE* fp = fopen(file, "r"); |
| 847 | + if (fp) { |
| 848 | + //Determine the file size |
| 849 | + struct stat st; |
| 850 | + stat(file, &st); |
| 851 | + |
849 | 852 |
|
850 |
| - if (fd >= 0) |
851 |
| - close(fd); |
| 853 | + xml = ezxml_parse_fp(fp, st.st_size); |
| 854 | + fclose(fp); |
| 855 | + } |
852 | 856 | return xml;
|
853 | 857 | }
|
854 | 858 |
|
|
0 commit comments