1
0

LoadXML.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <precomp.h>
  2. #include "LoadXML.h"
  3. #include <bfc/file/wildcharsenum.h>
  4. #include <wchar.h>
  5. void LoadXmlServiceProvider(obj_xml *parser, const wchar_t *filename)
  6. {
  7. /* TODO:
  8. svc = XmlProviderEnum(filename).getNext();
  9. pbuf = svc->getXmlData(filename, incpath, &p);
  10. SvcEnum::release(svc);
  11. svc = NULL;
  12. */
  13. }
  14. // when filename begins with "buf:"
  15. void LoadXmlBuffer(obj_xml *parser, const wchar_t *filename)
  16. {
  17. filename += 4;
  18. if (parser->xmlreader_feed((void *)filename, wcslen(filename)*sizeof(wchar_t)) == API_XML_SUCCESS)
  19. parser->xmlreader_feed(0, 0);
  20. }
  21. bool LoadXmlFilename(obj_xml *parser, const wchar_t *filename)
  22. {
  23. OSFILETYPE file = WFOPEN(filename, WF_READONLY_BINARY, NO_FILEREADERS);
  24. // HANDLE file = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
  25. // if (file == INVALID_HANDLE_VALUE)
  26. if (file == OPEN_FAILED)
  27. return false;
  28. size_t bytesRead=0;
  29. do
  30. {
  31. char data[4096] = {0};
  32. bytesRead = FREAD(data, 1, sizeof(data), file);
  33. // if (ReadFile(file, data, 1024, &bytesRead, NULL) && bytesRead)
  34. if (bytesRead)
  35. {
  36. if (parser->xmlreader_feed(data, bytesRead)!=API_XML_SUCCESS)
  37. {
  38. // CloseHandle(file);
  39. FCLOSE(file);
  40. return false;
  41. }
  42. }
  43. else
  44. {
  45. if (parser->xmlreader_feed(0, 0) != API_XML_SUCCESS)
  46. {
  47. FCLOSE(file);
  48. // CloseHandle(file);
  49. return false;
  50. }
  51. bytesRead=0;
  52. }
  53. } while (bytesRead);
  54. FCLOSE(file);
  55. // CloseHandle(file);
  56. return true;
  57. }
  58. /*** TODO:
  59. ** move this to a separate file
  60. ** deal with wildcard filenames (e.g. *.xml)
  61. */
  62. void LoadXmlFile(obj_xml *parser, const wchar_t *filename)
  63. {
  64. if (!WCSNICMP(filename, L"buf:", 4))
  65. {
  66. LoadXmlBuffer(parser, filename);
  67. return ;
  68. }
  69. WildcharsEnumerator e(filename);
  70. if (e.getNumFiles() > 0) // if we're including multiple files
  71. {
  72. for (int i = 0;i < e.getNumFiles();i++)
  73. {
  74. if (i) // don't reset the first time around
  75. parser->xmlreader_reset();
  76. LoadXmlFilename(parser, e.enumFile(i));
  77. }
  78. }
  79. else if (!LoadXmlFilename(parser, filename))
  80. {
  81. LoadXmlServiceProvider(parser, filename);
  82. return ;
  83. }
  84. }