testSuiteLoader.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "main.h"
  2. #include "./testSuiteLoader.h"
  3. #include "../../xml/obj_xml.h"
  4. #include <api/service/waservicefactory.h>
  5. #include <strsafe.h>
  6. typedef void (CALLBACK *LOADERTAGCALLBACK)(TestSuiteLoader* /*loader*/, const wchar_t* /*value*/);
  7. typedef struct LOADERTAG
  8. {
  9. const wchar_t *name;
  10. LOADERTAGCALLBACK callback;
  11. } LOADERTAG;
  12. static void CALLBACK
  13. LoaderTag_ImageBase(TestSuiteLoader *loader, const wchar_t *value)
  14. {
  15. String_Free(loader->imageBase);
  16. loader->imageBase = String_Duplicate(value);
  17. }
  18. static void CALLBACK
  19. LoaderTag_Connect(TestSuiteLoader *loader, const wchar_t *value)
  20. {
  21. if (IS_STRING_EMPTY(value))
  22. return;
  23. const wchar_t *block, *cursor;
  24. char *name;
  25. size_t length;
  26. block = value;
  27. cursor = block;
  28. for(;;)
  29. {
  30. if (L'\0' == *cursor ||
  31. L';' == *cursor ||
  32. L',' == *cursor)
  33. {
  34. if (block < cursor)
  35. {
  36. length = cursor - block;
  37. name = String_ToAnsi(CP_UTF8, 0, block, (int)length, NULL, NULL);
  38. if (NULL != name)
  39. loader->connectList.push_back(name);
  40. }
  41. if (L'\0' == *cursor)
  42. break;
  43. block = cursor + 1;
  44. }
  45. cursor++;
  46. }
  47. //loader->SetImageBase(value);
  48. }
  49. static const LOADERTAG knownTags[LOADER_TAG_MAX] =
  50. {
  51. {L"imageBase", LoaderTag_ImageBase},
  52. {L"connect", LoaderTag_Connect},
  53. };
  54. TestSuiteLoader::TestSuiteLoader()
  55. : imageBase(NULL)
  56. {
  57. }
  58. TestSuiteLoader::~TestSuiteLoader()
  59. {
  60. size_t index;
  61. index = connectList.size();
  62. while(index--)
  63. {
  64. AnsiString_Free(connectList[index]);
  65. }
  66. String_Free(imageBase);
  67. }
  68. BOOL TestSuiteLoader::Load(const wchar_t *path, TestSuite *testSuite)
  69. {
  70. BOOL result;
  71. HANDLE fileHandle;
  72. obj_xml *reader;
  73. if (NULL == testSuite)
  74. return FALSE;
  75. if (NULL == path || L'\0' == *path)
  76. return FALSE;
  77. fileHandle = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  78. if (INVALID_HANDLE_VALUE == fileHandle)
  79. return FALSE;
  80. result = FALSE;
  81. if (NULL != WASABI_API_SVC)
  82. {
  83. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(obj_xmlGUID);
  84. reader = (NULL != sf) ? (obj_xml*)sf->getInterface() : NULL;
  85. if (NULL != reader)
  86. {
  87. if (OBJ_XML_SUCCESS == reader->xmlreader_open())
  88. {
  89. reader->xmlreader_registerCallback(L"testprovider\fimageBase", this);
  90. reader->xmlreader_registerCallback(L"testprovider\fconnect", this);
  91. ZeroMemory(hitList, sizeof(hitList));
  92. deviceParser.Begin(reader, testSuite);
  93. typeParser.Begin(reader, testSuite);
  94. connectionParser.Begin(reader, testSuite);
  95. commandParser.Begin(reader, testSuite);
  96. result = FeedFile(reader, fileHandle, 8192);
  97. deviceParser.End();
  98. typeParser.End();
  99. connectionParser.End();
  100. commandParser.End();
  101. reader->xmlreader_close();
  102. testSuite->SetIconBase(imageBase);
  103. testSuite->SetConnectList(connectList.begin(), connectList.size());
  104. }
  105. sf->releaseInterface(reader);
  106. }
  107. }
  108. CloseHandle(fileHandle);
  109. return result;
  110. }
  111. BOOL TestSuiteLoader::FeedFile(obj_xml *reader, HANDLE fileHandle, DWORD bufferSize)
  112. {
  113. BOOL result;
  114. DWORD read;
  115. BYTE *buffer;
  116. int readerCode;
  117. if (NULL == reader ||
  118. INVALID_HANDLE_VALUE == fileHandle ||
  119. 0 == bufferSize)
  120. {
  121. return FALSE;
  122. }
  123. buffer = (BYTE*)malloc(bufferSize);
  124. if (NULL == buffer)
  125. return FALSE;
  126. readerCode = OBJ_XML_SUCCESS;
  127. result = TRUE;
  128. for(;;)
  129. {
  130. if (FALSE == ReadFile(fileHandle, buffer, bufferSize, &read, NULL) || 0 == read)
  131. {
  132. result = FALSE;
  133. if (0 == read && OBJ_XML_SUCCESS == readerCode)
  134. reader->xmlreader_feed(0, 0);
  135. break;
  136. }
  137. readerCode = reader->xmlreader_feed(buffer, read);
  138. if (OBJ_XML_SUCCESS != readerCode)
  139. {
  140. result = FALSE;
  141. break;
  142. }
  143. }
  144. free(buffer);
  145. return result;
  146. }
  147. void TestSuiteLoader::Event_XmlStartElement(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  148. {
  149. elementString.Clear();
  150. }
  151. void TestSuiteLoader::Event_XmlEndElement(const wchar_t *xmlpath, const wchar_t *xmltag)
  152. {
  153. for (size_t i = 0; i < LOADER_TAG_MAX; i++)
  154. {
  155. if (FALSE == hitList[i] &&
  156. CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, knownTags[i].name, -1, xmltag, -1))
  157. {
  158. knownTags[i].callback(this, elementString.Get());
  159. hitList[i] = TRUE;
  160. break;
  161. }
  162. }
  163. }
  164. void TestSuiteLoader::Event_XmlCharData(const wchar_t *xmlpath, const wchar_t *xmltag, const wchar_t *value)
  165. {
  166. elementString.Append(value);
  167. }
  168. void TestSuiteLoader::Event_XmlError(int linenum, int errcode, const wchar_t *errstr)
  169. {
  170. elementString.Clear();
  171. }
  172. #define CBCLASS TestSuiteLoader
  173. START_DISPATCH;
  174. VCB(ONSTARTELEMENT, Event_XmlStartElement)
  175. VCB(ONENDELEMENT, Event_XmlEndElement)
  176. VCB(ONCHARDATA, Event_XmlCharData)
  177. VCB(ONERROR, Event_XmlError)
  178. END_DISPATCH;
  179. #undef CBCLASS