code_junkie

How can I read an XML file into a buffer in C?

Nov 13th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. struct stat file_status;
  2. char *buf = NULL;
  3. FILE * pFile;
  4.  
  5. stat("tmp.xml", &file_status);
  6. buf = (char*)malloc(file_status.st_size);
  7. pFile = fopen ("tmp.xml","r");
  8. fread (buf,1,file_status.st_size,pFile);
  9.  
  10. fclose(pFile);
  11.  
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14.  
  15. long fsize(FILE * file)
  16. {
  17. if(fseek(file, 0, SEEK_END))
  18. return -1;
  19.  
  20. long size = ftell(file);
  21. if(size < 0)
  22. return -1;
  23.  
  24. if(fseek(file, 0, SEEK_SET))
  25. return -1;
  26.  
  27. return size;
  28. }
  29.  
  30. size_t fget_contents(char ** str, const char * name, _Bool * error)
  31. {
  32. FILE * file = NULL;
  33. size_t read = 0;
  34. *str = NULL;
  35. if(error) *error = 1;
  36.  
  37. do
  38. {
  39. file = fopen(name, "rb");
  40. if(!file) break;
  41.  
  42. long size = fsize(file);
  43. if(size < 0) break;
  44.  
  45. if(error) *error = 0;
  46.  
  47. *str = malloc((size_t)size + 1);
  48. if(!*str) break;
  49.  
  50. read = fread(*str, 1, (size_t)size, file);
  51. (*str)[read] = 0;
  52. *str = realloc(*str, read + 1);
  53.  
  54. if(error) *error = (size != (long)read);
  55. }
  56. while(0);
  57.  
  58. if(file) fclose(file);
  59. return read;
  60. }
  61.  
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64.  
  65.  
  66. int main (int argc, char *argv[]) {
  67. char *buffer; /* holds the file contents. */
  68. size_t i; /* indexing into buffer. */
  69. size_t buffer_size; /* size of the buffer. */
  70. char *temp; /* for realloc(). */
  71. char c; /* for reading from the input. */
  72. FILE *input; /* our input stream. */
  73.  
  74.  
  75. if (argc == 1) {
  76. fprintf(stderr, "Needs a filename argument.n");
  77. exit(EXIT_FAILURE);
  78. }
  79. else if (argc > 2) {
  80. fprintf(stderr, "Well, you passed in a few filenames, but I'm only using %sn", argv[1]);
  81. }
  82.  
  83. if ((input = fopen(argv[1], "r")) == NULL) {
  84. fprintf(stderr, "Error opening input file %sn", argv[1]);
  85. exit(EXIT_FAILURE);
  86. }
  87.  
  88. /* Initial allocation of buffer */
  89. i = 0;
  90. buffer_size = BUFSIZ;
  91. if ((buffer = malloc(buffer_size)) == NULL) {
  92. fprintf(stderr, "Error allocating memory (before reading file).n");
  93. fclose(input);
  94. }
  95.  
  96. while ((c = fgetc(input)) != EOF) {
  97. /* Enlarge buffer if necessary. */
  98. if (i == buffer_size) {
  99. buffer_size += BUFSIZ;
  100. if ((temp = realloc(buffer, buffer_size)) == NULL) {
  101. fprintf(stderr, "Ran out of core while reading file.n");
  102. fclose(input);
  103. free(buffer);
  104. exit(EXIT_FAILURE);
  105. }
  106. buffer = temp;
  107. }
  108.  
  109. /* Add input char to the buffer. */
  110. buffer[i++] = c;
  111. }
  112.  
  113. /* Test if loop terminated from error. */
  114. if (ferror(input)) {
  115. fprintf(stderr, "There was a file input error.n");
  116. free(buffer);
  117. fclose(input);
  118. exit(EXIT_FAILURE);
  119. }
  120.  
  121. /* Make the buffer a bona-fide string. */
  122. if (i == buffer_size) {
  123. buffer_size += 1;
  124. if ((temp = realloc(buffer, buffer_size)) == NULL) {
  125. fprintf(stderr, "Ran out of core (and only needed one more byte too ;_;).n");
  126. fclose(input);
  127. free(buffer);
  128. exit(EXIT_FAILURE);
  129. }
  130. buffer = temp;
  131. }
  132. buffer[i] = '';
  133.  
  134. puts(buffer);
  135.  
  136. /* Clean up. */
  137. free(buffer);
  138. fclose(input);
  139.  
  140. return 0;
  141. }
  142.  
  143. #include <stdio.h>
  144. #include <string.h>
  145. #include <libxml/parser.h>
  146.  
  147. int main(int argc, char **argv) {
  148. xmlDoc *document;
  149. xmlNode *root, *first_child, *node;
  150. char *filename;
  151.  
  152. if (argc < 2) {
  153. fprintf(stderr, "Usage: %s filename.xmln", argv[0]);
  154. return 1;
  155. }
  156. filename = argv[1];
  157.  
  158. document = xmlReadFile(filename, NULL, 0);
  159. root = xmlDocGetRootElement(document);
  160. fprintf(stdout, "Root is <%s> (%i)n", root->name, root->type);
  161. first_child = root->children;
  162. for (node = first_child; node; node = node->next) {
  163. fprintf(stdout, "t Child is <%s> (%i)n", node->name, node->type);
  164. }
  165. fprintf(stdout, "...n");
  166. return 0;
  167. }
  168.  
  169. % gcc -o read-xml $(xml2-config --cflags) -Wall $(xml2-config --libs) read-xml.c
  170.  
  171. #ifndef MMAP_H
  172. #define MMAP_H
  173.  
  174. #include <sys/types.h>
  175.  
  176. struct region_t {
  177. void *head;
  178. off_t size;
  179. };
  180.  
  181. #define OUT_OF_BOUNDS(reg, p)
  182. (((void *)(p) < (reg)->head) || ((void *)(p) >= ((reg)->head)+(reg)->size))
  183.  
  184. #define REG_SHOW(reg)
  185. printf("h: %p, s: %ld (e: %p)n", reg->head, reg->size, reg->head+reg->size);
  186.  
  187. struct region_t *do_mmap(const char *fn);
  188. #endif
  189.  
  190. #include <stdlib.h>
  191.  
  192. #include <sys/types.h> /* open lseek */
  193. #include <sys/stat.h> /* open */
  194. #include <fcntl.h> /* open */
  195. #include <unistd.h> /* lseek */
  196. #include <sys/mman.h> /* mmap */
  197.  
  198. #include "mmap.h"
  199.  
  200. struct region_t *do_mmap(const char *fn)
  201. {
  202. struct region_t *R = calloc(1, sizeof(struct region_t));
  203.  
  204. if(R != NULL) {
  205. int fd;
  206.  
  207. fd = open(fn, O_RDONLY);
  208. if(fd != -1) {
  209. R->size = lseek(fd, 0, SEEK_END);
  210. if(R->size != -1) {
  211. R->head = mmap(NULL, R->size, PROT_READ, MAP_PRIVATE, fd, 0);
  212. if(R->head) {
  213. close(fd); /* don't need file-destructor anymore. */
  214. return R;
  215. }
  216. /* no clean up of borked (mmap,) */
  217. }
  218. close(fd); /* clean up of borked (lseek, mmap,) */
  219. }
  220. free(R); /* clean up of borked (open, lseek, mmap,) */
  221. }
  222. return NULL;
  223. }
Add Comment
Please, Sign In to add comment