xmlread.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*====================================================================*
  2. *
  3. * NODE * xmlread (NODE * node, char const * filename);
  4. *
  5. * node.h
  6. *
  7. * open an XML file and return the parse tree root;
  8. *
  9. * the entire file is read into a buffer associated with the text
  10. * member in the root node; the buffer is then split into strings
  11. * referenced by child nodes, forming a hierarchial string vector;
  12. *
  13. * Motley Tools by Charles Maier;
  14. * Copyright (c) 2001-2006 by Charles Maier Associates;
  15. * Licensed under the Internet Software Consortium License;
  16. *
  17. *--------------------------------------------------------------------*/
  18. #ifndef XMLREAD_SOURCE
  19. #define XMLREAD_SOURCE
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <sys/stat.h>
  23. #include <memory.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include "../tools/error.h"
  27. #include "../tools/files.h"
  28. #include "../nodes/node.h"
  29. signed xmlread (NODE * node, char const * filename)
  30. {
  31. struct stat stat;
  32. signed fd;
  33. memset (node, 0, sizeof (NODE));
  34. if (lstat (filename, &stat))
  35. {
  36. error (1, errno, FILE_CANTSTAT, filename);
  37. }
  38. if (!(node->text = malloc (stat.st_size + 1)))
  39. {
  40. error (1, errno, FILE_CANTLOAD, filename);
  41. }
  42. if ((fd = open (filename, O_BINARY|O_RDONLY)) == -1)
  43. {
  44. error (1, errno, FILE_CANTOPEN, filename);
  45. }
  46. if (read (fd, node->text, stat.st_size) != stat.st_size)
  47. {
  48. error (1, errno, FILE_CANTREAD, filename);
  49. }
  50. node->text [stat.st_size] = (char)(0);
  51. close (fd);
  52. return (0);
  53. }
  54. #endif