xmlopen.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*====================================================================*
  2. *
  3. * NODE * xmlopen (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 XMLOPEN_SOURCE
  19. #define XMLOPEN_SOURCE
  20. #include <unistd.h>
  21. #include <memory.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include "../nodes/node.h"
  25. #include "../tools/memory.h"
  26. #include "../tools/files.h"
  27. #include "../tools/error.h"
  28. NODE * xmlopen (char const * filename)
  29. {
  30. ssize_t length;
  31. NODE * node = NEW (NODE);
  32. signed fd = open (filename, O_BINARY|O_RDONLY);
  33. if (fd == -1)
  34. {
  35. error (1, errno, FILE_CANTOPEN, filename);
  36. }
  37. length = lseek (fd, 0, SEEK_END);
  38. if (length == -1)
  39. {
  40. error (1, errno, FILE_CANTSEEK, filename);
  41. }
  42. if (lseek (fd, 0, SEEK_SET) == -1)
  43. {
  44. error (1, errno, FILE_CANTHOME, filename);
  45. }
  46. memset (node, 0, sizeof (NODE));
  47. node->text = STR (length);
  48. if (read (fd, node->text, length) < length)
  49. {
  50. error (1, errno, FILE_CANTREAD, filename);
  51. }
  52. node->text [length] = (char)(0);
  53. close (fd);
  54. xmlscan (node);
  55. return (node);
  56. }
  57. #endif