Util.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. #include "Util.h"
  2. #include <algorithm>
  3. #include <functional>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <limits>
  8. #include "Poco/Exception.h"
  9. #include "Poco/MD5Engine.h"
  10. #include "Poco/File.h"
  11. #include <fstream>
  12. #include "ErrorCode.h"
  13. #include "Poco/DOM/Document.h"
  14. #include "Poco/DOM/Text.h"
  15. #include "Poco/DOM/Element.h"
  16. #include "Poco/DOM/ProcessingInstruction.h"
  17. #include "Poco/DOM/DOMWriter.h"
  18. #include "Poco/DOM/DOMParser.h"
  19. #include "Poco/DOM/NodeIterator.h"
  20. #include "Poco/SAX/InputSource.h"
  21. #include "Poco/DOM/Node.h"
  22. #include "Poco/DOM/NodeFilter.h"
  23. #include "Poco/XML/XMLWriter.h"
  24. #include "Poco/AutoPtr.h"
  25. #include "Poco/TextConverter.h"
  26. #include "Poco/TextEncoding.h"
  27. #include "Poco/GBKEncoding.h"
  28. #include "Poco/KOI8REncoding.h"
  29. #include "Poco/UTF8Encoding.h"
  30. #include "Poco/TextIterator.h"
  31. #include "Poco/StreamCopier.h"
  32. #include "PhmsLogger.h"
  33. #include "Poco/Buffer.h"
  34. #include "Poco/Util/Application.h"
  35. #include "Poco/Path.h"
  36. #include "Poco/Util/IniFileConfiguration.h"
  37. #include <iomanip>
  38. #include "json/json.h"
  39. #include "json/value.h"
  40. using Poco::MD5Engine;
  41. using Poco::Exception;
  42. using Poco::DigestEngine;
  43. using Poco::File;
  44. using Poco::XML::Document;
  45. using Poco::XML::Text;
  46. using Poco::XML::Element;
  47. using Poco::XML::ProcessingInstruction;
  48. using Poco::XML::DOMWriter;
  49. using Poco::XML::DOMParser;
  50. using Poco::XML::NodeIterator;
  51. using Poco::XML::InputSource;
  52. using Poco::XML::Node;
  53. using Poco::XML::NodeFilter;
  54. using Poco::XML::XMLWriter;
  55. using Poco::AutoPtr;
  56. using Poco::TextConverter;
  57. using Poco::TextEncoding;
  58. using Poco::GBKEncoding;
  59. using Poco::KOI8REncoding;
  60. using Poco::UTF8Encoding;
  61. using Poco::StreamCopier;
  62. using Poco::Util::Application;
  63. using Poco::Path;
  64. using Poco::TextIterator;
  65. using Poco::Util::IniFileConfiguration;
  66. using namespace std;
  67. string CUtil::m_stringAppDir="";
  68. /**********************************************************
  69. *
  70. *功能:去前空格
  71. *
  72. *str:源字符串
  73. *
  74. *返回值:去除前空格后的字符串
  75. *
  76. ***********************************************************/
  77. string& CUtil::Ltrim(string &str)
  78. {
  79. str.erase(str.begin(), std::find_if(str.begin(), str.end(),
  80. std::not1(std::ptr_fun(::isspace))));
  81. return str;
  82. }
  83. /**********************************************************
  84. *
  85. *功能:去后空格
  86. *
  87. *str:源字符串
  88. *
  89. *返回值:去除后空格后的字符串
  90. *
  91. ***********************************************************/
  92. string& CUtil::Rtrim(string &str)
  93. {
  94. str.erase(std::find_if(str.rbegin(), str.rend(),
  95. std::not1(std::ptr_fun(::isspace))).base(),
  96. str.end());
  97. return str;
  98. }
  99. /**********************************************************
  100. *
  101. *功能:去前后空格
  102. *
  103. *str:源字符串
  104. *
  105. *返回值:去除前后空格后的字符串
  106. *
  107. ***********************************************************/
  108. string& CUtil::Trim(string &str)
  109. {
  110. return Rtrim(Ltrim(str));
  111. }
  112. /**********************************************************
  113. *
  114. *功能:double转string
  115. *
  116. *dValut:源double数值
  117. *decplaces:欲保留的小数点后位数(包括小数点)
  118. *
  119. *返回值:转换后的string
  120. *
  121. ***********************************************************/
  122. #define DECIMAL_POINT "."
  123. string CUtil::DoubleToString(long double dValut, int decplaces)
  124. {
  125. if((long)(dValut*100)%100 == 0)
  126. {
  127. return CUtil::ULongToString(dValut);
  128. }
  129. if(dValut == 0.0)
  130. {
  131. return "";
  132. }
  133. ostringstream out;
  134. //out.ios
  135. //int prec=numeric_limits<double>::digits10;//使用最大精度18
  136. //out.precision(prec);//覆盖默认精度
  137. out<<setiosflags(ios::fixed)<<setprecision(2)<<dValut;//四舍五入并保留小数点后两位
  138. string str= out.str(); //从流中取出字符串
  139. size_t n=str.find(DECIMAL_POINT);
  140. if ((n!=string::npos) && (str.size()> n+decplaces)) //后面至少还有decplaces位吗?
  141. {
  142. str[n+decplaces]='\0';//覆盖第一个多余的数,得到小数点后decplaces-1位
  143. }
  144. string stringTemp = str.c_str();
  145. str.swap(stringTemp);//自交换删除nul之后的多余字符,因为string对象的数据可以包含nul字符;而字符串的实际长度由size()的返回值决定。
  146. return str;
  147. }
  148. /**********************************************************
  149. *
  150. *功能:string转unsigned long
  151. *
  152. *nValue:源string值
  153. *
  154. *返回值:转换后的unsigned long
  155. *
  156. ***********************************************************/
  157. unsigned long CUtil::StringToULong(string stringValue)
  158. {
  159. unsigned long nValue;
  160. istringstream iStringStream(stringValue);
  161. iStringStream>>nValue;
  162. return nValue;
  163. }
  164. /**********************************************************
  165. *
  166. *功能:unsigned long转string
  167. *
  168. *nValue:源unsigned long数值
  169. *
  170. *返回值:转换后的string
  171. *
  172. ***********************************************************/
  173. string CUtil::ULongToString(unsigned long nValue)
  174. {
  175. string stringDst;
  176. ostringstream oStringStream;
  177. oStringStream<<nValue;
  178. stringDst = oStringStream.str();
  179. return stringDst;
  180. }
  181. /**********************************************************
  182. *
  183. *功能:unsigned int转string
  184. *
  185. *nValue:源unsigned int数值
  186. *
  187. *返回值:转换后的string
  188. *
  189. ***********************************************************/
  190. string CUtil::UIntToString(unsigned int nValue)
  191. {
  192. string stringDst;
  193. ostringstream oStringStream;
  194. oStringStream<<nValue;
  195. stringDst = oStringStream.str();
  196. return stringDst;
  197. }
  198. string CUtil::Int64ToString(Poco::Int64 nValue)
  199. {
  200. string stringDst;
  201. ostringstream oStringStream;
  202. oStringStream<<nValue;
  203. stringDst = oStringStream.str();
  204. return stringDst;
  205. }
  206. /**********************************************************
  207. *
  208. *功能:求Md5码
  209. *
  210. *bufSrc:源buf
  211. *nLength:buf长度
  212. *
  213. *返回值:计算出的md5码
  214. *
  215. ***********************************************************/
  216. string CUtil::GetMd5(const char* bufSrc, int nLength)
  217. {
  218. string stringMd5;
  219. MD5Engine engine;
  220. engine.update(bufSrc, nLength);
  221. stringMd5 = DigestEngine::digestToHex(engine.digest());
  222. return stringMd5;
  223. }
  224. /**********************************************************
  225. *
  226. *功能:求Md5码
  227. *
  228. *stringFilePath:源文件路径
  229. *stringMd5:输出参数,计算出的md5码
  230. *
  231. *返回值:正确或错误码
  232. *
  233. ***********************************************************/
  234. int CUtil::GetMd5(string stringFilePath, string& stringMd5)
  235. {
  236. //计算md5
  237. char* bufSrc = NULL;
  238. unsigned long nLength = 0;
  239. int nRet = PHMS_SUCCESSFUL_RESULT;
  240. nRet = CUtil::GetFileSize(stringFilePath, nLength);
  241. if(nRet != PHMS_SUCCESSFUL_RESULT)
  242. {
  243. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  244. return nRet;
  245. }
  246. bufSrc = new char[nLength+1];
  247. memset(bufSrc, 0, nLength+1);
  248. ifstream iFStream;
  249. try
  250. {
  251. iFStream.exceptions(ios::eofbit|ios::failbit|ios::badbit);
  252. iFStream.open(stringFilePath.c_str(), ios_base::binary);
  253. iFStream.read(bufSrc, nLength);
  254. iFStream.close();
  255. }
  256. catch (ios_base::failure& e)
  257. {
  258. //写日志
  259. delete bufSrc;
  260. CPhmsLogger::GetPhmsLogger()->WriteLog(e.what(), __FUNCTION__, __FILE__, __LINE__);
  261. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_FILE_READ_FAIL), __FUNCTION__, __FILE__, __LINE__);
  262. return COMMON_FILE_READ_FAIL;
  263. }
  264. stringMd5 = CUtil::GetMd5(bufSrc, nLength);
  265. delete bufSrc;
  266. return PHMS_SUCCESSFUL_RESULT;
  267. }
  268. /**********************************************************
  269. *
  270. *功能:获得文件长度
  271. *
  272. *stringFilePath:源文件路径
  273. *nFileSize:输出参数,文件长度
  274. *
  275. *返回值:文件长度
  276. *
  277. ***********************************************************/
  278. int CUtil::GetFileSize(string stringFilePath, unsigned long& nFileSize)
  279. {
  280. ifstream ifLocalTargetFile;
  281. unsigned long nPos=0;
  282. try
  283. {
  284. ifLocalTargetFile.exceptions(ios::badbit);
  285. ifLocalTargetFile.open(stringFilePath.c_str(), ios::in|ios::binary);
  286. nPos = ifLocalTargetFile.tellg();
  287. ifLocalTargetFile.seekg(0,ios::end);
  288. nFileSize = ifLocalTargetFile.tellg();
  289. ifLocalTargetFile.seekg(nPos);
  290. ifLocalTargetFile.close();
  291. }
  292. catch(const ios::failure& error)
  293. {
  294. //写日志
  295. nFileSize = 0;
  296. CPhmsLogger::GetPhmsLogger()->WriteLog(error.what(), __FUNCTION__, __FILE__, __LINE__);
  297. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_FILE_READ_FAIL), __FUNCTION__, __FILE__, __LINE__);
  298. return COMMON_FILE_READ_FAIL;
  299. }
  300. return PHMS_SUCCESSFUL_RESULT;
  301. }
  302. /**********************************************************
  303. *
  304. *功能:获得文件长度
  305. *
  306. *stringFilePath:源文件路径
  307. *
  308. *返回值:文件长度
  309. *
  310. ***********************************************************/
  311. unsigned long CUtil::GetFileSize(string stringFilePath)
  312. {
  313. File fileTarget(stringFilePath, "gbk");
  314. if(fileTarget.exists())
  315. return (unsigned long)fileTarget.getSize();
  316. else
  317. return 0;
  318. }
  319. //该函数只适合于只有一个Text Node子节点的Element Node
  320. int CUtil::GetXmlFieldVaule(int nSaveType, string stringXmlContent, string stringXmlPath, string stringField, string& stringVaule)
  321. {
  322. DOMParser parser;//创建一个分析者
  323. AutoPtr<Document> pDoc = NULL;//开始分析,并返回指向分析后数据的指针
  324. string stringContent;
  325. string stringUtf8NodeVaule;
  326. string stringWorkingDir = CUtil::GetCurrentAppDir();
  327. string stringIniFilePath = stringWorkingDir+"PhmsConfig.ini";
  328. AutoPtr<IniFileConfiguration> iniFile = new IniFileConfiguration(stringIniFilePath, "gbk");
  329. string stringEncoding = iniFile->getString("OTHER.Encoding", "GBK");
  330. TextEncoding* pEnvEncoding = TextEncoding::find(stringEncoding);
  331. TextEncoding* pUtf8Encoding = TextEncoding::find("UTF-8");
  332. TextConverter converter(*pUtf8Encoding, *pEnvEncoding);
  333. //int nError = 0;
  334. try
  335. {
  336. if(nSaveType == 1)
  337. {
  338. //xml内容保存到了stringContent
  339. stringContent = stringXmlContent;
  340. parser.setEncoding(pEnvEncoding->canonicalName());
  341. pDoc = parser.parseString(stringContent);//开始分析,并返回指向分析后数据的指针
  342. }
  343. else
  344. {
  345. if(nSaveType == 2)
  346. {
  347. //xml内容保存到了stringFilePath
  348. ifstream iFStream;
  349. ostringstream oStringStream;
  350. iFStream.open(stringXmlPath.c_str());
  351. StreamCopier::copyStream(iFStream, oStringStream);
  352. iFStream.close();
  353. stringContent = oStringStream.str();
  354. parser.setEncoding(pEnvEncoding->canonicalName());
  355. pDoc = parser.parseString(stringContent);//开始分析,并返回指向分析后数据的指针
  356. }
  357. else
  358. {
  359. return PHMS_SUCCESSFUL_RESULT;
  360. }
  361. }
  362. NodeIterator it(pDoc, NodeFilter::SHOW_ALL);//创建一个节点迭代器,默认迭代显示全部node
  363. Node* pNode = it.nextNode();//此时指向第一个
  364. while (pNode)
  365. {
  366. if(pNode->nodeName() == stringField)
  367. {
  368. if(pNode->hasChildNodes())
  369. {
  370. pNode = pNode->firstChild();
  371. if(pNode->nodeType() == Node::TEXT_NODE)
  372. {
  373. stringUtf8NodeVaule = pNode->nodeValue();
  374. converter.convert(stringUtf8NodeVaule, stringVaule);
  375. break;
  376. }
  377. else
  378. {
  379. stringVaule = "";
  380. break;
  381. }
  382. }
  383. else
  384. {
  385. stringVaule = "";
  386. break;
  387. }
  388. }
  389. //std::cout<<pNode->nodeName()<<" "<<pNode->nodeName().size()<<" "<<pNode->nodeValue()<<" "<<pNode->nodeValue().size()<<endl;
  390. pNode = it.nextNode();//指向下一个node
  391. }
  392. if(!pNode)
  393. {
  394. //写日志
  395. //返回xml中没有sid
  396. stringVaule = "";
  397. return PHMS_SUCCESSFUL_RESULT;
  398. }
  399. }
  400. catch(Exception& e)
  401. {
  402. //写日志
  403. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  404. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_PARSE_XML_FAIL), __FUNCTION__, __FILE__, __LINE__);
  405. return COMMON_PARSE_XML_FAIL;
  406. }
  407. return PHMS_SUCCESSFUL_RESULT;
  408. }
  409. int CUtil::GetOrReplaceImagePathVector(int nSaveType, string stringXmlContent, string stringXmlPath, vector<string>& vImagePath)
  410. {
  411. DOMParser parser;//创建一个分析者
  412. AutoPtr<Document> pDoc = NULL;//开始分析,并返回指向分析后数据的指针
  413. string stringContent;
  414. string stringUtf8NodeVaule;
  415. string stringWorkingDir = CUtil::GetCurrentAppDir();
  416. string stringIniFilePath = stringWorkingDir+"PhmsConfig.ini";
  417. AutoPtr<IniFileConfiguration> iniFile = new IniFileConfiguration(stringIniFilePath, "gbk");
  418. string stringEncoding = iniFile->getString("OTHER.Encoding", "GBK");
  419. TextEncoding* pEnvEncoding = TextEncoding::find(stringEncoding);
  420. TextEncoding* pUtf8Encoding = TextEncoding::find("UTF-8");
  421. TextConverter converter(*pUtf8Encoding, *pEnvEncoding);
  422. //int nError = 0;
  423. try
  424. {
  425. if(nSaveType == 1)
  426. {
  427. //xml内容保存到了stringContent
  428. stringContent = stringXmlContent;
  429. parser.setEncoding(pEnvEncoding->canonicalName());
  430. pDoc = parser.parseString(stringContent);//开始分析,并返回指向分析后数据的指针
  431. }
  432. else
  433. {
  434. if(nSaveType == 2)
  435. {
  436. //xml内容保存到了stringFilePath
  437. ifstream iFStream;
  438. ostringstream oStringStream;
  439. iFStream.open(stringXmlPath.c_str());
  440. StreamCopier::copyStream(iFStream, oStringStream);
  441. iFStream.close();
  442. stringContent = oStringStream.str();
  443. parser.setEncoding(pEnvEncoding->canonicalName());
  444. pDoc = parser.parseString(stringContent);//开始分析,并返回指向分析后数据的指针
  445. }
  446. else
  447. {
  448. return PHMS_SUCCESSFUL_RESULT;
  449. }
  450. }
  451. NodeIterator it(pDoc, NodeFilter::SHOW_ALL);//创建一个节点迭代器,默认迭代显示全部node
  452. Node* pNode = it.nextNode();//此时指向第一个
  453. Node* pChildNode = NULL;
  454. Node* pGrandChildNode = NULL;
  455. while (pNode)
  456. {
  457. if(pNode->nodeName() == "image")
  458. {
  459. pChildNode = pNode->firstChild();
  460. do
  461. {
  462. if(pChildNode->nodeName() != "path")
  463. {
  464. pChildNode = pChildNode->nextSibling();
  465. continue;
  466. }
  467. pGrandChildNode = pChildNode->firstChild();
  468. if(pGrandChildNode && pGrandChildNode->nodeType()==Node::TEXT_NODE)
  469. {
  470. if(pGrandChildNode->nodeValue().size() != 0)
  471. {
  472. vImagePath.push_back(pGrandChildNode->nodeValue());
  473. }
  474. else
  475. {
  476. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_PARSE_XML_FAIL), __FUNCTION__, __FILE__, __LINE__);
  477. return COMMON_PICTURE_CONSULTATION_ERROR;
  478. }
  479. }
  480. break;
  481. }while(pChildNode && pChildNode->nodeType()==Node::ELEMENT_NODE);
  482. }
  483. //std::cout<<pNode->nodeName()<<" "<<pNode->nodeName().size()<<" "<<pNode->nodeValue()<<" "<<pNode->nodeValue().size()<<endl;
  484. pNode = it.nextNode();//指向下一个node
  485. }
  486. }
  487. catch(Exception& e)
  488. {
  489. //写日志
  490. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  491. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_PARSE_XML_FAIL), __FUNCTION__, __FILE__, __LINE__);
  492. return COMMON_PARSE_XML_FAIL;
  493. }
  494. return PHMS_SUCCESSFUL_RESULT;
  495. }
  496. int CUtil::GetMapFromRequestXml(string stringXmlContent, string stringKey, map<string, string>& mapParam)
  497. {//未设置字符编码
  498. DOMParser parser;//创建一个分析者
  499. AutoPtr<Document> pDoc = NULL;//开始分析,并返回指向分析后数据的指针
  500. string stringNodeKey;
  501. string stringNodeVaule;
  502. //GBKEncoding encodingGbk;
  503. //int nError = 0;
  504. try
  505. {
  506. //parser.setEncoding(encodingGbk.canonicalName());
  507. pDoc = parser.parseString(stringXmlContent);//开始分析,并返回指向分析后数据的指针
  508. NodeIterator it(pDoc, NodeFilter::SHOW_ALL);//创建一个节点迭代器,默认迭代显示全部node
  509. Node* pNode = it.nextNode();//此时指向第一个
  510. while (pNode)
  511. {
  512. if(pNode->nodeName() == stringKey)
  513. {
  514. if(pNode->hasChildNodes())
  515. {
  516. pNode = pNode->firstChild();
  517. while(pNode)
  518. {
  519. if(pNode->hasChildNodes())
  520. {
  521. if(pNode->firstChild()->nodeType()==Node::TEXT_NODE)
  522. {
  523. stringNodeKey = pNode->nodeName();
  524. stringNodeVaule = pNode->firstChild()->nodeValue();
  525. //string stringGBKNodeValue;
  526. //CUtil::UTF8ToGBK(stringNodeVaule, stringGBKNodeValue);
  527. mapParam[stringNodeKey] = stringNodeVaule/*stringGBKNodeValue*/;
  528. }
  529. }
  530. else
  531. {
  532. if(pNode->nodeType() == Node::ELEMENT_NODE)
  533. {
  534. stringNodeKey = pNode->nodeName();
  535. stringNodeVaule = "";
  536. mapParam[stringNodeKey] = stringNodeVaule;
  537. }
  538. }
  539. pNode = pNode->nextSibling();
  540. }
  541. }
  542. break;
  543. }
  544. pNode = it.nextNode();//指向下一个node
  545. }
  546. if(!pNode)
  547. {
  548. //写日志
  549. //返回xml中没有sid
  550. return PHMS_SUCCESSFUL_RESULT;
  551. }
  552. }
  553. catch(Exception& e)
  554. {
  555. //写日志
  556. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  557. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_PARSE_XML_FAIL), __FUNCTION__, __FILE__, __LINE__);
  558. return COMMON_PARSE_XML_FAIL;
  559. }
  560. return PHMS_SUCCESSFUL_RESULT;
  561. }
  562. string CUtil::GetErrorMsg(int nError)
  563. {
  564. string stringErrorMsg;
  565. switch(nError)
  566. {
  567. case COMMON_PARAM_VALIDATE_FAIL:
  568. stringErrorMsg = "COMMON_PARAM_VALIDATE_FAIL";
  569. break;
  570. case COMMON_NO_IP:
  571. stringErrorMsg = "COMMON_NO_IP";
  572. break;
  573. case COMMON_NO_PORT:
  574. stringErrorMsg = "COMMON_NO_PORT";
  575. break;
  576. case COMMON_DNS_FAIL:
  577. stringErrorMsg = "COMMON_DNS_FAIL";
  578. break;
  579. case COMMON_CONNECT_FAIL:
  580. stringErrorMsg = "COMMON_CONNECT_FAIL";
  581. break;
  582. case COMMON_SEND_FAIL:
  583. stringErrorMsg = "COMMON_SEND_FAIL";
  584. break;
  585. case COMMON_SEND_TIMEOUT:
  586. stringErrorMsg = "COMMON_SEND_TIMEOUT";
  587. break;
  588. case COMMON_RECV_FAIL:
  589. stringErrorMsg = "COMMON_RECV_FAIL";
  590. break;
  591. case COMMON_RECV_TIMEOUT:
  592. stringErrorMsg = "COMMON_RECV_TIMEOUT";
  593. break;
  594. case COMMON_OFF_NETWORK:
  595. stringErrorMsg = "COMMON_OFF_NETWORK";
  596. break;
  597. case COMMON_FILE_OPEN_FAIL:
  598. stringErrorMsg = "COMMON_FILE_OPEN_FAIL";
  599. break;
  600. case COMMON_FILE_WRITE_FAIL:
  601. stringErrorMsg = "COMMON_FILE_WRITE_FAIL";
  602. break;
  603. case COMMON_FILE_READ_FAIL:
  604. stringErrorMsg = "COMMON_FILE_READ_FAIL";
  605. break;
  606. case COMMON_CREATE_DIR_FAIL:
  607. stringErrorMsg = "COMMON_CREATE_DIR_FAIL";
  608. break;
  609. case COMMON_MEMORY_FAIL:
  610. stringErrorMsg = "COMMON_MEMORY_FAIL";
  611. break;
  612. case COMMON_BUF_SMALL:
  613. stringErrorMsg = "COMMON_BUF_SMALL";
  614. break;
  615. case COMMON_DISK_FULL:
  616. stringErrorMsg = "COMMON_DISK_FULL";
  617. break;
  618. case COMMON_CALCULATE_MD5_FAIL:
  619. stringErrorMsg = "COMMON_CALCULATE_MD5_FAIL";
  620. break;
  621. case COMMON_VALIDATE_MD5_FAIL:
  622. stringErrorMsg = "COMMON_VALIDATE_MD5_FAIL";
  623. break;
  624. case COMMON_BINARY_TO_STRING:
  625. stringErrorMsg = "COMMON_BINARY_TO_STRING";
  626. break;
  627. case COMMON_RECV_CONTENT_FAIL:
  628. stringErrorMsg = "COMMON_RECV_CONTENT_FAIL";
  629. break;
  630. case COMMON_SSL_CONFLICT_ERROR:
  631. stringErrorMsg = "COMMON_SSL_CONFLICT_ERROR";
  632. break;
  633. case COMMON_OUTPUT_STREAM_FAIL:
  634. stringErrorMsg = "COMMON_OUTPUT_STREAM_FAIL";
  635. break;
  636. case COMMON_INPUT_STREAM_FAIL:
  637. stringErrorMsg = "COMMON_INPUT_STREAM_FAIL";
  638. break;
  639. case COMMON_NET_CONFIG_ERROR:
  640. stringErrorMsg = "COMMON_NET_CONFIG_ERROR";
  641. break;
  642. case COMMON_RESPONSE_SIGN_VALIDATE_FAIL:
  643. stringErrorMsg = "COMMON_RESPONSE_SIGN_VALIDATE_FAIL";
  644. break;
  645. case COMMON_NO_SAVE_SESSIONID:
  646. stringErrorMsg = "COMMON_NO_SAVE_SESSIONID";
  647. break;
  648. case COMMON_PARSE_XML_FAIL:
  649. stringErrorMsg = "COMMON_PARSE_XML_FAIL";
  650. break;
  651. case COMMON_NO_SID:
  652. stringErrorMsg = "COMMON_NO_SID";
  653. break;
  654. case COMMON_WRITE_XML_ERROR:
  655. stringErrorMsg = "COMMON_WRITE_XML_ERROR";
  656. break;
  657. case COMMON_UPLOAD_INI_SIZE_ERROR:
  658. stringErrorMsg = "COMMON_UPLOAD_INI_SIZE_ERROR";
  659. break;
  660. case COMMON_DOWNLOAD_THREAD_COUNT_TOO_LARGE:
  661. stringErrorMsg = "COMMON_DOWNLOAD_THREAD_COUNT_TOO_LARGE";
  662. break;
  663. case COMMON_LOCAL_FILE_MORE_THAN_SERVER_FILE:
  664. stringErrorMsg = "COMMON_LOCAL_FILE_MORE_THAN_SERVER_FILE";
  665. break;
  666. case COMMON_LOCAL_TEMP_TOO_LARGE:
  667. stringErrorMsg = "COMMON_LOCAL_TEMP_TOO_LARGE";
  668. break;
  669. case COMMON_DOWNLOAD_SEGMENT_FAIL_FOR_FILESIEZE_CHANGE:
  670. stringErrorMsg = "COMMON_DOWNLOAD_SEGMENT_FAIL_FOR_FILESIEZE_CHANGE";
  671. break;
  672. case COMMON_COMBILE_FILE_FAIL:
  673. stringErrorMsg = "COMMON_COMBILE_FILE_FAIL";
  674. break;
  675. case COMMON_INVALID_HANDLE:
  676. stringErrorMsg = "COMMON_INVALID_HANDLE";
  677. break;
  678. case COMMON_SESSION_BUF_TOO_SMALL:
  679. stringErrorMsg = "COMMON_SESSION_BUF_TOO_SMALL";
  680. break;
  681. case COMMON_PHMS_REQUEST_VALIDATE_FAIL:
  682. stringErrorMsg = "COMMON_PHMS_REQUEST_VALIDATE_FAIL";
  683. break;
  684. case COMMON_NO_URL:
  685. stringErrorMsg = "COMMON_NO_URL";
  686. break;
  687. case COMMON_PROXY_INFO_ERROR:
  688. stringErrorMsg = "COMMON_PROXY_INFO_ERROR";
  689. break;
  690. case COMMON_RESPONSE_FORMAT_ERROR:
  691. stringErrorMsg = "COMMON_RESPONSE_FORMAT_ERROR";
  692. break;
  693. case COMMON_CONVERT_ENCODE_ERROR:
  694. stringErrorMsg = "COMMON_CONVERT_ENCODE_ERROR";
  695. break;
  696. case COMMON_UPLOAD_LESS_3_BYTES:
  697. stringErrorMsg = "COMMON_UPLOAD_LESS_3_BYTES";
  698. break;
  699. case COMMON_NO_NET_CONFIG_FILE:
  700. stringErrorMsg = "COMMON_NO_NET_CONFIG_FILE";
  701. break;
  702. case COMMON_NET_CONFIG_READ_ERROR:
  703. stringErrorMsg = "COMMON_NET_CONFIG_READ_ERROR";
  704. break;
  705. case COMMON_NO_VALID_SESSIONID:
  706. stringErrorMsg = "COMMON_NO_VALID_SESSIONID";
  707. break;
  708. case COMMON_HANDLE_THREAD_MISMATCH:
  709. stringErrorMsg = "COMMON_HANDLE_THREAD_MISMATCH";
  710. break;
  711. case COMMON_GET_CASE_STATE_ERROR:
  712. stringErrorMsg = "COMMON_GET_CASE_STATE_ERROR";
  713. break;
  714. case COMMON_GENERATE_LOG_FILE_ERROR:
  715. stringErrorMsg = "COMMON_GENERATE_LOG_FILE_ERROR";
  716. break;
  717. case COMMON_DOWNLOAD_FILE_SIZE_0:
  718. stringErrorMsg = "COMMON_DOWNLOAD_FILE_SIZE_0";
  719. break;
  720. case COMMON_SAME_PUSH_IS_RUN:
  721. stringErrorMsg = "COMMON_SAME_PUSH_IS_RUN";
  722. break;
  723. case COMMON_SESSION_INVALID:
  724. stringErrorMsg = "COMMON_SESSION_INVALID";
  725. break;
  726. case COMMON_LOGIN_ANOTHER_PLACE:
  727. stringErrorMsg = "COMMON_LOGIN_ANOTHER_PLACE";
  728. break;
  729. case COMMON_SERVER_NO_SUPPORT:
  730. stringErrorMsg = "COMMON_SERVER_NO_SUPPORT";
  731. break;
  732. case COMMON_INVALID_CHARSET:
  733. stringErrorMsg = "COMMON_INVALID_CHARSET";
  734. break;
  735. case COMMON_NO_SET_TERMINAL_TYPE:
  736. stringErrorMsg = "COMMON_NO_SET_TERMINAL_TYPE";
  737. break;
  738. case COMMON_URL_NOT_FOUND:
  739. stringErrorMsg = "COMMON_URL_NOT_FOUND";
  740. break;
  741. case COMMON_GET_CASE_SIZE_ERROR:
  742. stringErrorMsg = "COMMON_GET_CASE_SIZE_ERROR";
  743. break;
  744. case COMMON_COMBILE_FILE_FAIL_BUT_CAN_RETRY:
  745. stringErrorMsg = "COMMON_COMBILE_FILE_FAIL_BUT_CAN_RETRY";
  746. break;
  747. case COMMON_INVALID_THREAD_ID:
  748. stringErrorMsg = "COMMON_INVALID_THREAD_ID";
  749. break;
  750. case COMMON_ILLEGAL_GBK_ENCODING:
  751. stringErrorMsg = "COMMON_ILLEGAL_GBK_ENCODING";
  752. break;
  753. case COMMON_PARAM_SIZE_0:
  754. stringErrorMsg = "COMMON_PARAM_SIZE_0";
  755. break;
  756. case COMMON_PICTURE_CONSULTATION_ERROR:
  757. stringErrorMsg = "COMMON_PICTURE_CONSULTATION_ERROR";
  758. break;
  759. case COMMON_XIKANG_URL_OPENID_NULL:
  760. stringErrorMsg = "COMMON_XIKANG_URL_OPENID_NULL";
  761. break;
  762. case COMMON_GET_TOKEN_FAIL:
  763. stringErrorMsg = "COMMON_GET_TOKEN_FAIL";
  764. break;
  765. case COMMON_INVALID_URL:
  766. stringErrorMsg = "COMMON_INVALID_URL";
  767. break;
  768. default:
  769. stringErrorMsg = "SERVER_RETURN_INTERFACE_ERROR";
  770. break;
  771. }
  772. return stringErrorMsg;
  773. }
  774. int CUtil::GBKToUTF8(string stringGbk, string& stringUtf8)
  775. {
  776. GBKEncoding encodingGBK;
  777. UTF8Encoding encodingUtf8;
  778. TextConverter converter(encodingGBK, encodingUtf8);
  779. converter.convert(stringGbk, stringUtf8);
  780. return PHMS_SUCCESSFUL_RESULT;
  781. }
  782. int CUtil::UTF8ToGBK(string stringUtf8, string& stringGbk)
  783. {
  784. GBKEncoding encodingGBK;
  785. UTF8Encoding encodingUtf8;
  786. TextConverter converter(encodingUtf8, encodingGBK);
  787. converter.convert(stringUtf8, stringGbk);
  788. return PHMS_SUCCESSFUL_RESULT;
  789. }
  790. int CUtil::EnvToUtf8(string stringEnv, string& stringUtf8)
  791. {
  792. string stringWorkingDir = CUtil::GetCurrentAppDir();
  793. string stringIniFilePath = stringWorkingDir+"PhmsConfig.ini";
  794. AutoPtr<IniFileConfiguration> iniFile = new IniFileConfiguration(stringIniFilePath, "gbk");
  795. string stringEncoding = iniFile->getString("OTHER.Encoding", "GBK");
  796. TextEncoding* pEnvEncoding = TextEncoding::find(stringEncoding);
  797. TextEncoding* pUtf8Encoding = TextEncoding::find("UTF-8");
  798. TextConverter converter(*pEnvEncoding, *pUtf8Encoding);
  799. converter.convert(stringEnv, stringUtf8);
  800. return PHMS_SUCCESSFUL_RESULT;
  801. }
  802. string CUtil::EnvToUtf8(string stringEnv)
  803. {
  804. string stringUtf8;
  805. string stringWorkingDir = CUtil::GetCurrentAppDir();
  806. string stringIniFilePath = stringWorkingDir+"PhmsConfig.ini";
  807. AutoPtr<IniFileConfiguration> iniFile = new IniFileConfiguration(stringIniFilePath, "gbk");
  808. string stringEncoding = iniFile->getString("OTHER.Encoding", "GBK");
  809. TextEncoding* pEnvEncoding = TextEncoding::find(stringEncoding);
  810. TextEncoding* pUtf8Encoding = TextEncoding::find("UTF-8");
  811. TextConverter converter(*pEnvEncoding, *pUtf8Encoding);
  812. converter.convert(stringEnv, stringUtf8);
  813. return stringUtf8;
  814. }
  815. int CUtil::UTF8ToEnv(string stringUtf8, string& stringEnv)
  816. {
  817. string stringWorkingDir = CUtil::GetCurrentAppDir();
  818. string stringIniFilePath = stringWorkingDir+"PhmsConfig.ini";
  819. AutoPtr<IniFileConfiguration> iniFile = new IniFileConfiguration(stringIniFilePath, "gbk");
  820. string stringEncoding = iniFile->getString("OTHER.Encoding", "GBK");
  821. TextEncoding* pEnvEncoding = TextEncoding::find(stringEncoding);
  822. TextEncoding* pUtf8Encoding = TextEncoding::find("UTF-8");
  823. TextConverter converter(*pUtf8Encoding, *pEnvEncoding);
  824. converter.convert(stringUtf8, stringEnv);
  825. return PHMS_SUCCESSFUL_RESULT;
  826. }
  827. string CUtil::GetCurrentAppDir()
  828. {
  829. if(m_stringAppDir.size() != 0)
  830. {
  831. return m_stringAppDir;
  832. }
  833. string stringPathEnv;
  834. string stringPathUtf8;
  835. stringPathUtf8 = Path::current();
  836. CUtil::UTF8ToGBK(stringPathUtf8, stringPathEnv);
  837. #if(POCO_OS == POCO_OS_MAC_OS_X)
  838. return stringPathEnv + "Documents/";
  839. #else
  840. return stringPathEnv;
  841. #endif
  842. }
  843. bool CUtil::IsLegalGBK(string stringSrc)
  844. {
  845. GBKEncoding inEncoding;
  846. TextIterator it(stringSrc, inEncoding);
  847. TextIterator end(stringSrc);
  848. for(; it!=end; it++)
  849. {
  850. if(*it == -1)
  851. return false;
  852. }
  853. return true;
  854. }
  855. bool CUtil::IsLegalKOI8R(string stringSrc)
  856. {
  857. KOI8REncoding inEncoding;
  858. TextIterator it(stringSrc, inEncoding);
  859. TextIterator end(stringSrc);
  860. for(; it!=end; it++)
  861. {
  862. if(*it == -1)
  863. return false;
  864. }
  865. return true;
  866. }
  867. bool CUtil::IsLegalUTF8(string stringSrc)
  868. {
  869. UTF8Encoding inEncoding;
  870. TextIterator it(stringSrc, inEncoding);
  871. TextIterator end(stringSrc);
  872. for(; it!=end; it++)
  873. {
  874. if(*it == -1)
  875. return false;
  876. }
  877. return true;
  878. }
  879. void CUtil::SetAppDir(string stringWorkDir)
  880. {
  881. if(m_stringAppDir.size()==0)
  882. {
  883. m_stringAppDir = stringWorkDir;
  884. }
  885. }
  886. void CUtil::ReplaceStringInFile(string stringPath, string stringSrc, string stringDst)
  887. {
  888. string stringContent;
  889. ifstream iFStream;
  890. ostringstream oStringStream;
  891. iFStream.open(stringPath.c_str());
  892. StreamCopier::copyStream(iFStream, oStringStream);
  893. iFStream.close();
  894. stringContent = oStringStream.str();
  895. string::size_type nPos = stringContent.find(stringSrc);
  896. if(nPos != string::npos)
  897. {
  898. stringContent.replace(nPos, stringSrc.size(), stringDst);
  899. }
  900. ofstream oFStream;
  901. istringstream iStringStream(stringContent);
  902. oFStream.open(stringPath.c_str(), ios_base::out|ios_base::trunc);
  903. StreamCopier::copyStream(iStringStream, oFStream);
  904. oFStream.close();
  905. return;
  906. }
  907. void CUtil::ReplaceString(string& stringContent, string stringSrc, string stringDst)
  908. {
  909. if(stringSrc == stringDst)
  910. {
  911. return;
  912. }
  913. string::size_type nPos = stringContent.find(stringSrc);
  914. while(nPos != string::npos)
  915. {
  916. stringContent.replace(nPos, stringSrc.size(), stringDst);
  917. nPos = stringContent.find(stringSrc);
  918. }
  919. }
  920. string CUtil::GetFileNameFromPath(string stringPath)
  921. {
  922. string stringFileName = stringPath;
  923. if(stringPath.size() == 0)
  924. {
  925. return stringFileName;
  926. }
  927. string::size_type nPosSlash = stringPath.rfind("\\");
  928. if(nPosSlash == string::npos)
  929. {
  930. nPosSlash = stringPath.rfind("/");
  931. if(nPosSlash == string::npos)
  932. {
  933. return stringFileName;
  934. }
  935. }
  936. stringFileName = stringPath.substr(nPosSlash+1, stringPath.size()-nPosSlash-1);
  937. return stringFileName;
  938. }
  939. void CUtil::ParseJsonObjectWithoutArray(int nSaveType, string stringJsonContent, string stringJsonPath, map<string, string>& mapResult)
  940. {
  941. string stringContent;
  942. if(nSaveType == 1)
  943. {
  944. //xml内容保存到了stringContent
  945. stringContent = stringJsonContent;
  946. }
  947. else
  948. {
  949. if(nSaveType == 2)
  950. {
  951. //xml内容保存到了stringFilePath
  952. ifstream iFStream;
  953. ostringstream oStringStream;
  954. iFStream.open(stringJsonPath.c_str());
  955. StreamCopier::copyStream(iFStream, oStringStream);
  956. iFStream.close();
  957. stringContent = oStringStream.str();
  958. }
  959. else
  960. {
  961. return;
  962. }
  963. }
  964. try
  965. {
  966. Json::Reader jsonReader;
  967. Json::Value jsonRoot;
  968. //处理两层json对象到map
  969. if(jsonReader.parse(stringContent, jsonRoot, false))
  970. {
  971. Json::Value::iterator iter = jsonRoot.begin();
  972. for(iter; iter!=jsonRoot.end(); iter++)
  973. {
  974. if(jsonRoot[iter.memberName()].isString())
  975. {
  976. mapResult[iter.memberName()] = jsonRoot[iter.memberName()].asString();
  977. }
  978. if(jsonRoot[iter.memberName()].isObject())
  979. {
  980. Json::Value jsonObject = jsonRoot[iter.memberName()];
  981. Json::Value::iterator iter2 = jsonObject.begin();
  982. for(iter2; iter2!=jsonObject.end(); iter2++)
  983. {
  984. if(jsonObject[iter2.memberName()].isString())
  985. {
  986. mapResult[iter2.memberName()] = jsonObject[iter2.memberName()].asString();
  987. }
  988. }
  989. }
  990. }
  991. }
  992. }
  993. catch(...)
  994. {
  995. CPhmsLogger::GetPhmsLogger()->WriteLog("Json Handle Exception", __FUNCTION__, __FILE__, __LINE__);
  996. }
  997. return;
  998. }
  999. string CUtil::MapToJson(map<string, string> mapParam)
  1000. {
  1001. Json::Value jRoot;
  1002. Json::FastWriter jWriter;
  1003. map<string, string>::iterator iter = mapParam.begin();
  1004. for(iter; iter!=mapParam.end(); iter++)
  1005. {
  1006. jRoot[iter->first] = iter->second;
  1007. }
  1008. string stringJson = jWriter.write(jRoot);
  1009. return stringJson;
  1010. }
  1011. int CUtil::ParseStandardQueryString(string stringQuery, string& stringId, string& stringIdType)
  1012. {
  1013. if(stringQuery.size() != 0)
  1014. {
  1015. string stringFirstLetter = stringQuery.substr(0, 1);
  1016. string stringRealId = stringQuery.substr(1);
  1017. if(stringFirstLetter=="S" || stringFirstLetter=="s")
  1018. {
  1019. stringIdType = "4";
  1020. }
  1021. else if(stringFirstLetter=="M" || stringFirstLetter=="m")
  1022. {
  1023. stringIdType = "1";
  1024. }
  1025. else if(stringFirstLetter=="Z" || stringFirstLetter=="z")
  1026. {
  1027. stringIdType = "2";
  1028. }
  1029. else if(stringFirstLetter=="T" || stringFirstLetter=="t")
  1030. {
  1031. stringIdType = "3";
  1032. }
  1033. else if(stringFirstLetter=="W" || stringFirstLetter=="w")
  1034. {
  1035. stringIdType = "0";
  1036. }
  1037. else if(stringFirstLetter=="K" || stringFirstLetter=="k")
  1038. {
  1039. stringIdType = "5";
  1040. }
  1041. else
  1042. {
  1043. return COMMON_PARAM_VALIDATE_FAIL;
  1044. }
  1045. stringId = stringRealId;
  1046. if(stringId.size() == 0)
  1047. {
  1048. return COMMON_PARAM_VALIDATE_FAIL;
  1049. }
  1050. return PHMS_SUCCESSFUL_RESULT;
  1051. }
  1052. return COMMON_PARAM_VALIDATE_FAIL;
  1053. }