linux版本中间件

Util.cpp 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "Util.h"
  2. #include <chrono>
  3. #include <sstream>
  4. #include "tinyxml.h"
  5. /*
  6. <?xml version="1.0" encoding="utf-8"?> <result>
  7. <interpretation grammar="session:hello" confidence="1">
  8. <instance>
  9. <result>也不准确。</result>
  10. <beginTime>2420</beginTime>
  11. <endTime>3540</endTime>
  12. <taskId>fba26d3861574ac4b55c01f4881c5bae</taskId>
  13. <waveformUri>b3f91c673b9146b7-7.wav</waveformUri>
  14. </instance>
  15. <input mode="speech">也不准确。</input>
  16. </interpretation>
  17. </result>
  18. */
  19. bool parseASRXml(const char* desc, std::string& result, std::string& error)
  20. {
  21. try{
  22. TiXmlDocument doc;
  23. doc.Parse(desc);
  24. auto pRoot = doc.RootElement();
  25. if (pRoot != nullptr && pRoot->ValueStr() == "result") {
  26. auto pElement = pRoot->FirstChildElement("interpretation");
  27. if (pElement) {
  28. auto pInput = pElement->FirstChildElement("input");
  29. if (pInput) {
  30. auto pText = pInput->GetText();
  31. result = pText != nullptr ? pText : "";
  32. return true;
  33. }
  34. }
  35. }
  36. }
  37. catch (const std::exception& e){
  38. error = e.what();
  39. }
  40. return false;
  41. }
  42. int64_t GetUnixTime()
  43. {
  44. int64_t times = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  45. return times;
  46. }
  47. std::string std::to_string(const char * src)
  48. {
  49. if (src == nullptr)return "";
  50. std::stringstream ss;
  51. ss << src;
  52. std::string str;
  53. ss >> str;
  54. return str;
  55. }