CommBufUtil.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text;
  4. public class CommBufUtil
  5. {
  6. public static CommError.Type printMultiStr(ref VisualBuf buf, string str, int times)
  7. {
  8. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  9. for (int i = 0; i < times; i++)
  10. {
  11. ret = buf.sprintf("{0}", str);
  12. if (ret != CommError.Type.COMM_NO_ERROR)
  13. {
  14. break;
  15. }
  16. }
  17. return ret;
  18. }
  19. public static CommError.Type printVariable(ref VisualBuf buf, int indent, char sep, string variable, bool withSep)
  20. {
  21. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  22. ret = printMultiStr(ref buf, " ", indent);
  23. if (CommError.Type.COMM_NO_ERROR == ret)
  24. {
  25. if (withSep)
  26. {
  27. ret = buf.sprintf("{0}{1}", variable, sep);
  28. }
  29. else
  30. {
  31. ret = buf.sprintf("{0}: ", variable);
  32. }
  33. }
  34. return ret;
  35. }
  36. public static CommError.Type printVariable(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx, bool withSep)
  37. {
  38. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  39. ret = printMultiStr(ref buf, " ", indent);
  40. if (CommError.Type.COMM_NO_ERROR == ret)
  41. {
  42. if (withSep)
  43. {
  44. ret = buf.sprintf("{0}[{1:d}]{2}", variable, arrIdx, sep);
  45. }
  46. else
  47. {
  48. ret = buf.sprintf("{0}[{1:d}]: ", variable, arrIdx);
  49. }
  50. }
  51. return ret;
  52. }
  53. public static CommError.Type printVariable(ref VisualBuf buf, int indent, char sep, string variable, string format, params object[] args)
  54. {
  55. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  56. ret = printMultiStr(ref buf, " ", indent);
  57. if (CommError.Type.COMM_NO_ERROR == ret)
  58. {
  59. ret = buf.sprintf("{0}: ", variable);
  60. }
  61. if (CommError.Type.COMM_NO_ERROR == ret)
  62. {
  63. ret = buf.sprintf(format, args);
  64. }
  65. if (CommError.Type.COMM_NO_ERROR == ret)
  66. {
  67. ret = buf.sprintf("{0}", sep);
  68. }
  69. return ret;
  70. }
  71. public static CommError.Type printVariable(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx, string format, params object[] args)
  72. {
  73. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  74. ret = printMultiStr(ref buf, " ", indent);
  75. if (CommError.Type.COMM_NO_ERROR == ret)
  76. {
  77. ret = buf.sprintf("{0}[{1:d}]: ", variable,arrIdx);
  78. }
  79. if (CommError.Type.COMM_NO_ERROR == ret)
  80. {
  81. ret = buf.sprintf(format, args);
  82. }
  83. if (CommError.Type.COMM_NO_ERROR == ret)
  84. {
  85. ret = buf.sprintf("{0}", sep);
  86. }
  87. return ret;
  88. }
  89. public static CommError.Type printArray(ref VisualBuf buf, int indent, char sep, string variable,Int64 count)
  90. {
  91. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  92. ret = printMultiStr(ref buf, " ", indent);
  93. if (CommError.Type.COMM_NO_ERROR == ret)
  94. {
  95. ret = buf.sprintf("{0}[0:{1:d}]: ", variable, count);
  96. }
  97. return ret;
  98. }
  99. public static CommError.Type printString(ref VisualBuf buf, int indent, char sep, string variable, byte[] bStr)
  100. {
  101. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  102. string strUni = "";
  103. int count = CommTypeUtil.cstrlen(bStr);
  104. if (ret == CommError.Type.COMM_NO_ERROR)
  105. {
  106. ret = printMultiStr(ref buf, " ", indent);
  107. }
  108. if (ret == CommError.Type.COMM_NO_ERROR)
  109. {
  110. strUni = Encoding.ASCII.GetString(bStr, 0, count);
  111. }
  112. if (ret == CommError.Type.COMM_NO_ERROR)
  113. {
  114. ret = buf.sprintf("{0}: {1}{2}", variable, strUni, sep);
  115. }
  116. return ret;
  117. }
  118. public static CommError.Type printWString(ref VisualBuf buf, int indent, char sep, string variable, Int16[] str)
  119. {
  120. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  121. //int count = CommTypeUtil.wstrlen(str) + 1;
  122. ret = buf.sprintf("{0}: ",variable);
  123. if (ret == CommError.Type.COMM_NO_ERROR)
  124. {
  125. int len = CommTypeUtil.wstrlen(str);
  126. for (int i = 0; i < len; i++)
  127. {
  128. ret = buf.sprintf("0x{0:X4}", str[i]);
  129. if (CommError.Type.COMM_NO_ERROR != ret)
  130. {
  131. break;
  132. }
  133. }
  134. }
  135. if (CommError.Type.COMM_NO_ERROR == ret)
  136. {
  137. ret = buf.sprintf("{0}", sep);
  138. }
  139. return ret;
  140. }
  141. public static CommError.Type printString(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx,byte[] bStr)
  142. {
  143. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  144. string strUni = "";
  145. int count = CommTypeUtil.cstrlen(bStr);
  146. if (ret == CommError.Type.COMM_NO_ERROR)
  147. {
  148. ret = printMultiStr(ref buf, " ", indent);
  149. }
  150. if (ret == CommError.Type.COMM_NO_ERROR)
  151. {
  152. strUni = Encoding.ASCII.GetString(bStr, 0, count);
  153. }
  154. if (ret == CommError.Type.COMM_NO_ERROR)
  155. {
  156. ret = buf.sprintf("{0}[{1:d}]: {2}{3}", variable, arrIdx,strUni, sep);
  157. }
  158. return ret;
  159. }
  160. public static CommError.Type printWString(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx,Int16[] str)
  161. {
  162. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  163. //int count = CommTypeUtil.wstrlen(str) + 1;
  164. ret = buf.sprintf("{0}[{1:d}]",variable,arrIdx);
  165. if (ret == CommError.Type.COMM_NO_ERROR)
  166. {
  167. int len = CommTypeUtil.wstrlen(str);
  168. for (int i = 0; i < len; i++)
  169. {
  170. ret = buf.sprintf("0x{0:X4}", str[i]);
  171. if (CommError.Type.COMM_NO_ERROR != ret)
  172. {
  173. break;
  174. }
  175. }
  176. }
  177. if (CommError.Type.COMM_NO_ERROR == ret)
  178. {
  179. ret = buf.sprintf("{0}", sep);
  180. }
  181. return ret;
  182. }
  183. public static CommError.Type printTdrIP(ref VisualBuf buf, int indent, char sep, string variable, UInt32 ip)
  184. {
  185. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  186. ret = printMultiStr(ref buf, " ", indent);
  187. if (CommError.Type.COMM_NO_ERROR == ret)
  188. {
  189. ret = buf.sprintf("{0}: ", variable);
  190. }
  191. if (CommError.Type.COMM_NO_ERROR == ret)
  192. {
  193. ret = CommTypeUtil.IP2Str(ref buf,ip);
  194. }
  195. if (CommError.Type.COMM_NO_ERROR == ret)
  196. {
  197. ret = buf.sprintf("{0}",sep);
  198. }
  199. return ret;
  200. }
  201. public static CommError.Type printTdrIP(ref VisualBuf buf, int indent, char sep, string variable,int arrIdx, UInt32 ip)
  202. {
  203. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  204. ret = printMultiStr(ref buf, " ", indent);
  205. if (CommError.Type.COMM_NO_ERROR == ret)
  206. {
  207. ret = buf.sprintf("{0}[{1:d}]: ", variable, arrIdx);
  208. }
  209. if (CommError.Type.COMM_NO_ERROR == ret)
  210. {
  211. ret = CommTypeUtil.IP2Str(ref buf,ip);
  212. }
  213. if (CommError.Type.COMM_NO_ERROR == ret)
  214. {
  215. ret = buf.sprintf("{0}",sep);
  216. }
  217. return ret;
  218. }
  219. public static CommError.Type printTdrTime(ref VisualBuf buf, int indent, char sep, string variable, UInt32 time)
  220. {
  221. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  222. ret = printMultiStr(ref buf, " ", indent);
  223. if (CommError.Type.COMM_NO_ERROR == ret)
  224. {
  225. ret = buf.sprintf("{0}: ", variable);
  226. }
  227. if (CommError.Type.COMM_NO_ERROR == ret)
  228. {
  229. ret = CommTypeUtil.Time2Str(ref buf,time);
  230. }
  231. if (CommError.Type.COMM_NO_ERROR == ret)
  232. {
  233. ret = buf.sprintf("{0}",sep);
  234. }
  235. return ret;
  236. }
  237. public static CommError.Type printTdrTime(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx, UInt32 time)
  238. {
  239. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  240. ret = printMultiStr(ref buf, " ", indent);
  241. if (CommError.Type.COMM_NO_ERROR == ret)
  242. {
  243. ret = buf.sprintf("{0}[{1:d}]: ", variable, arrIdx);
  244. }
  245. if (CommError.Type.COMM_NO_ERROR == ret)
  246. {
  247. ret = CommTypeUtil.Time2Str(ref buf,time);
  248. }
  249. if (CommError.Type.COMM_NO_ERROR == ret)
  250. {
  251. ret = buf.sprintf("{0}",sep);
  252. }
  253. return ret;
  254. }
  255. public static CommError.Type printTdrDate(ref VisualBuf buf, int indent, char sep, string variable, UInt32 date)
  256. {
  257. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  258. ret = printMultiStr(ref buf, " ", indent);
  259. if (CommError.Type.COMM_NO_ERROR == ret)
  260. {
  261. ret = buf.sprintf("{0}: ", variable);
  262. }
  263. if (CommError.Type.COMM_NO_ERROR == ret)
  264. {
  265. ret = CommTypeUtil.CommDate2Str(ref buf,date);
  266. }
  267. if (CommError.Type.COMM_NO_ERROR == ret)
  268. {
  269. ret = buf.sprintf("{0}",sep);
  270. }
  271. return ret;
  272. }
  273. public static CommError.Type printTdrDate(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx, UInt32 date)
  274. {
  275. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  276. ret = printMultiStr(ref buf, " ", indent);
  277. if (CommError.Type.COMM_NO_ERROR == ret)
  278. {
  279. ret = buf.sprintf("{0}[{1:d}]: ", variable, arrIdx);
  280. }
  281. if (CommError.Type.COMM_NO_ERROR == ret)
  282. {
  283. ret = CommTypeUtil.CommDate2Str(ref buf,date);
  284. }
  285. if (CommError.Type.COMM_NO_ERROR == ret)
  286. {
  287. ret = buf.sprintf("{0}",sep);
  288. }
  289. return ret;
  290. }
  291. public static CommError.Type printTdrDateTime(ref VisualBuf buf, int indent, char sep, string variable, UInt64 datetime)
  292. {
  293. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  294. ret = printMultiStr(ref buf, " ", indent);
  295. if (CommError.Type.COMM_NO_ERROR == ret)
  296. {
  297. ret = buf.sprintf("{0}: ", variable);
  298. }
  299. if (CommError.Type.COMM_NO_ERROR == ret)
  300. {
  301. ret = CommTypeUtil.CommDateTime2Str(ref buf,datetime);
  302. }
  303. if (CommError.Type.COMM_NO_ERROR == ret)
  304. {
  305. ret = buf.sprintf("{0}",sep);
  306. }
  307. return ret;
  308. }
  309. public static CommError.Type printTdrDateTime(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx, UInt64 datetime)
  310. {
  311. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  312. ret = printMultiStr(ref buf, " ", indent);
  313. if (CommError.Type.COMM_NO_ERROR == ret)
  314. {
  315. ret = buf.sprintf("{0}[{1:d}]: ", variable, arrIdx);
  316. }
  317. if (CommError.Type.COMM_NO_ERROR == ret)
  318. {
  319. ret = CommTypeUtil.CommDateTime2Str(ref buf,datetime);
  320. }
  321. if (CommError.Type.COMM_NO_ERROR == ret)
  322. {
  323. ret = buf.sprintf("{0}",sep);
  324. }
  325. return ret;
  326. }
  327. }