deviceParser.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "main.h"
  2. #include "./deviceParser.h"
  3. #include "../../xml/obj_xml.h"
  4. typedef void (*DEVICETAGCALLBACK)(DeviceParser* /*self*/, Device* /*device*/, const wchar_t* /*value*/);
  5. typedef struct DEVICETAG
  6. {
  7. const wchar_t *name;
  8. BOOL multiEntry;
  9. DEVICETAGCALLBACK callback;
  10. } DEVICETAG;
  11. static void
  12. DeviceParser_ConnectionCb(DeviceParser *self, Device *device, const wchar_t *value)
  13. {
  14. char *connectionAnsi;
  15. connectionAnsi = (NULL != value) ?
  16. String_ToAnsi(CP_UTF8, 0, value, -1, NULL, NULL) :
  17. NULL;
  18. device->SetConnection(connectionAnsi);
  19. AnsiString_Free(connectionAnsi);
  20. }
  21. static void
  22. DeviceParser_DisplayNameCb(DeviceParser *self, Device *device, const wchar_t *value)
  23. {
  24. device->SetDisplayName(value);
  25. }
  26. static void
  27. DeviceParser_IconCb(DeviceParser *self, Device *device, const wchar_t *value)
  28. {
  29. device->AddIcon(value, self->iconSize.cx, self->iconSize.cy);
  30. }
  31. static void
  32. DeviceParser_TotalSpaceCb(DeviceParser *self, Device *device, const wchar_t *value)
  33. {
  34. size_t size;
  35. if (NULL == value)
  36. size = -1;
  37. else
  38. {
  39. LONGLONG lval;
  40. if (FALSE == StrToInt64Ex(value, STIF_DEFAULT, &lval))
  41. return;
  42. size = (size_t)lval;
  43. }
  44. device->SetTotalSpace(size);
  45. }
  46. static void
  47. DeviceParser_UsedSpaceCb(DeviceParser *self, Device *device, const wchar_t *value)
  48. {
  49. size_t size;
  50. if (NULL == value)
  51. size = -1;
  52. else
  53. {
  54. LONGLONG lval;
  55. if (FALSE == StrToInt64Ex(value, STIF_DEFAULT, &lval))
  56. return;
  57. size = (size_t)lval;
  58. }
  59. device->SetUsedSpace(size);
  60. }
  61. static void
  62. DeviceParser_HiddenCb(DeviceParser *self, Device *device, const wchar_t *value)
  63. {
  64. int hidden;
  65. if (FALSE != StrToIntEx(value, STIF_DEFAULT, &hidden))
  66. device->SetHidden(0 != hidden);
  67. }
  68. static void
  69. DeviceParser_CommandCb(DeviceParser *self, Device *device, const wchar_t *value)
  70. {
  71. char *command;
  72. if (FALSE != IS_STRING_EMPTY(value))
  73. return;
  74. command = String_ToAnsi(CP_UTF8, 0, value, -1, NULL, NULL);
  75. if (NULL != command)
  76. {
  77. device->AddCommand(command, self->commandFlags);
  78. AnsiString_Free(command);
  79. }
  80. }
  81. static void
  82. DeviceParser_ModelCb(DeviceParser *self, Device *device, const wchar_t *value)
  83. {
  84. device->SetModel(value);
  85. }
  86. static void
  87. DeviceParser_StatusCb(DeviceParser *self, Device *device, const wchar_t *value)
  88. {
  89. device->SetStatus(value);
  90. }
  91. static const DEVICETAG knownTags[DEVICE_TAG_MAX] =
  92. {
  93. {L"connection", FALSE, DeviceParser_ConnectionCb},
  94. {L"displayName", FALSE, DeviceParser_DisplayNameCb},
  95. {L"icon", TRUE, DeviceParser_IconCb},
  96. {L"totalSpace", FALSE, DeviceParser_TotalSpaceCb},
  97. {L"usedSpace", FALSE, DeviceParser_UsedSpaceCb},
  98. {L"hidden", FALSE, DeviceParser_HiddenCb},
  99. {L"command", TRUE, DeviceParser_CommandCb},
  100. {L"model", FALSE, DeviceParser_ModelCb},
  101. {L"status", FALSE, DeviceParser_StatusCb},
  102. };
  103. DeviceParser::DeviceParser()
  104. : device(NULL)
  105. {
  106. }
  107. DeviceParser::~DeviceParser()
  108. {
  109. if (NULL != device)
  110. device->Release();
  111. }
  112. BOOL DeviceParser::Begin(obj_xml *reader, ifc_xmlreaderparams *params)
  113. {
  114. const wchar_t *value;
  115. char *nameAnsi, *typeAnsi;
  116. if (NULL != device)
  117. return FALSE;
  118. if (NULL == reader || NULL == params)
  119. return FALSE;
  120. value = params->getItemValue(L"name");
  121. nameAnsi = (NULL != value) ? String_ToAnsi(CP_UTF8, 0, value, -1, NULL, NULL) : NULL;
  122. value = params->getItemValue(L"type");
  123. typeAnsi = (NULL != value) ? String_ToAnsi(CP_UTF8, 0, value, -1, NULL, NULL) : NULL;
  124. if (NULL == nameAnsi ||
  125. NULL == typeAnsi ||
  126. FAILED(Device::CreateInstance(nameAnsi, typeAnsi, NULL, &device)))
  127. {
  128. device = NULL;
  129. }
  130. AnsiString_Free(nameAnsi);
  131. AnsiString_Free(typeAnsi);
  132. if (NULL == device)
  133. return FALSE;
  134. reader->xmlreader_registerCallback(L"testprovider\fdevices\fdevice\fconnection", this);
  135. reader->xmlreader_registerCallback(L"testprovider\fdevices\fdevice\fdisplayName", this);
  136. reader->xmlreader_registerCallback(L"testprovider\fdevices\fdevice\ficon", this);
  137. reader->xmlreader_registerCallback(L"testprovider\fdevices\fdevice\ftotalSpace", this);
  138. reader->xmlreader_registerCallback(L"testprovider\fdevices\fdevice\fusedSpace", this);
  139. reader->xmlreader_registerCallback(L"testprovider\fdevices\fdevice\fhidden", this);
  140. reader->xmlreader_registerCallback(L"testprovider\fdevices\fdevice\fcommand", this);
  141. reader->xmlreader_registerCallback(L"testprovider\fdevices\fdevice\fmodel", this);
  142. reader->xmlreader_registerCallback(L"testprovider\fdevices\fdevice\fstatus", this);
  143. ZeroMemory(hitList, sizeof(hitList));
  144. return TRUE;
  145. }
  146. BOOL DeviceParser::End(obj_xml *reader, Device **result)
  147. {
  148. if (NULL != reader)
  149. reader->xmlreader_unregisterCallback(this);
  150. if (NULL == device)
  151. return FALSE;
  152. if (NULL != result)
  153. {
  154. *result = device;
  155. device->AddRef();
  156. }
  157. device->Release();
  158. device = NULL;
  159. return TRUE;
  160. }
  161. void DeviceParser::Event_XmlStartElement(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  162. {
  163. elementString.Clear();
  164. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, L"icon", -1, xmltag, -1))
  165. {
  166. const wchar_t *sVal;
  167. int iVal;
  168. sVal = params->getItemValue(L"width");
  169. if (NULL == sVal ||
  170. FALSE == StrToIntEx(sVal, STIF_DEFAULT, &iVal))
  171. {
  172. iVal = 0;
  173. }
  174. iconSize.cx = iVal;
  175. sVal = params->getItemValue(L"height");
  176. if (NULL == sVal ||
  177. FALSE == StrToIntEx(sVal, STIF_DEFAULT, &iVal))
  178. {
  179. iVal = 0;
  180. }
  181. iconSize.cy = iVal;
  182. }
  183. else if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, L"command", -1, xmltag, -1))
  184. {
  185. const wchar_t *sVal;
  186. int iVal;
  187. commandFlags = DeviceCommandFlag_None;
  188. sVal = params->getItemValue(L"primary");
  189. if (FALSE == IS_STRING_EMPTY(sVal) &&
  190. FALSE != StrToIntEx(sVal, STIF_DEFAULT, &iVal) &&
  191. 0 != iVal)
  192. {
  193. commandFlags |= DeviceCommandFlag_Primary;
  194. }
  195. sVal = params->getItemValue(L"group");
  196. if (FALSE == IS_STRING_EMPTY(sVal) &&
  197. FALSE != StrToIntEx(sVal, STIF_DEFAULT, &iVal) &&
  198. 0 != iVal)
  199. {
  200. commandFlags |= DeviceCommandFlag_Group;
  201. }
  202. sVal = params->getItemValue(L"disabled");
  203. if (FALSE == IS_STRING_EMPTY(sVal) &&
  204. FALSE != StrToIntEx(sVal, STIF_DEFAULT, &iVal) &&
  205. 0 != iVal)
  206. {
  207. commandFlags |= DeviceCommandFlag_Disabled;
  208. }
  209. sVal = params->getItemValue(L"hidden");
  210. if (FALSE == IS_STRING_EMPTY(sVal) &&
  211. FALSE != StrToIntEx(sVal, STIF_DEFAULT, &iVal) &&
  212. 0 != iVal)
  213. {
  214. commandFlags |= DeviceCommandFlag_Hidden;
  215. }
  216. }
  217. }
  218. void DeviceParser::Event_XmlEndElement(const wchar_t *xmlpath, const wchar_t *xmltag)
  219. {
  220. if (NULL == device)
  221. return;
  222. for (size_t i = 0; i < DEVICE_TAG_MAX; i++)
  223. {
  224. if (FALSE == hitList[i] &&
  225. CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, knownTags[i].name, -1, xmltag, -1))
  226. {
  227. knownTags[i].callback(this, device, elementString.Get());
  228. if (FALSE == knownTags[i].multiEntry)
  229. hitList[i] = TRUE;
  230. break;
  231. }
  232. }
  233. }
  234. void DeviceParser::Event_XmlCharData(const wchar_t *xmlpath, const wchar_t *xmltag, const wchar_t *value)
  235. {
  236. elementString.Append(value);
  237. }
  238. void DeviceParser::Event_XmlError(int linenum, int errcode, const wchar_t *errstr)
  239. {
  240. elementString.Clear();
  241. }
  242. #define CBCLASS DeviceParser
  243. START_DISPATCH;
  244. VCB(ONSTARTELEMENT, Event_XmlStartElement)
  245. VCB(ONENDELEMENT, Event_XmlEndElement)
  246. VCB(ONCHARDATA, Event_XmlCharData)
  247. VCB(ONERROR, Event_XmlError)
  248. END_DISPATCH;
  249. #undef CBCLASS