| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- using System;
- using System.Diagnostics;
- using System.Text;
- public class CommBufUtil
- {
- public static CommError.Type printMultiStr(ref VisualBuf buf, string str, int times)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- for (int i = 0; i < times; i++)
- {
- ret = buf.sprintf("{0}", str);
- if (ret != CommError.Type.COMM_NO_ERROR)
- {
- break;
- }
- }
- return ret;
- }
- public static CommError.Type printVariable(ref VisualBuf buf, int indent, char sep, string variable, bool withSep)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- if (withSep)
- {
- ret = buf.sprintf("{0}{1}", variable, sep);
- }
- else
- {
- ret = buf.sprintf("{0}: ", variable);
- }
- }
- return ret;
- }
- public static CommError.Type printVariable(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx, bool withSep)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- if (withSep)
- {
- ret = buf.sprintf("{0}[{1:d}]{2}", variable, arrIdx, sep);
- }
- else
- {
- ret = buf.sprintf("{0}[{1:d}]: ", variable, arrIdx);
- }
- }
- return ret;
- }
- public static CommError.Type printVariable(ref VisualBuf buf, int indent, char sep, string variable, string format, params object[] args)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}: ", variable);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf(format, args);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}", sep);
- }
- return ret;
- }
- public static CommError.Type printVariable(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx, string format, params object[] args)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}[{1:d}]: ", variable,arrIdx);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf(format, args);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}", sep);
- }
- return ret;
- }
- public static CommError.Type printArray(ref VisualBuf buf, int indent, char sep, string variable,Int64 count)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}[0:{1:d}]: ", variable, count);
- }
- return ret;
- }
- public static CommError.Type printString(ref VisualBuf buf, int indent, char sep, string variable, byte[] bStr)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- string strUni = "";
- int count = CommTypeUtil.cstrlen(bStr);
- if (ret == CommError.Type.COMM_NO_ERROR)
- {
- ret = printMultiStr(ref buf, " ", indent);
- }
- if (ret == CommError.Type.COMM_NO_ERROR)
- {
- strUni = Encoding.ASCII.GetString(bStr, 0, count);
- }
- if (ret == CommError.Type.COMM_NO_ERROR)
- {
- ret = buf.sprintf("{0}: {1}{2}", variable, strUni, sep);
- }
- return ret;
- }
- public static CommError.Type printWString(ref VisualBuf buf, int indent, char sep, string variable, Int16[] str)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- //int count = CommTypeUtil.wstrlen(str) + 1;
- ret = buf.sprintf("{0}: ",variable);
- if (ret == CommError.Type.COMM_NO_ERROR)
- {
- int len = CommTypeUtil.wstrlen(str);
- for (int i = 0; i < len; i++)
- {
- ret = buf.sprintf("0x{0:X4}", str[i]);
- if (CommError.Type.COMM_NO_ERROR != ret)
- {
- break;
- }
- }
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}", sep);
- }
- return ret;
- }
- public static CommError.Type printString(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx,byte[] bStr)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- string strUni = "";
- int count = CommTypeUtil.cstrlen(bStr);
- if (ret == CommError.Type.COMM_NO_ERROR)
- {
- ret = printMultiStr(ref buf, " ", indent);
- }
- if (ret == CommError.Type.COMM_NO_ERROR)
- {
- strUni = Encoding.ASCII.GetString(bStr, 0, count);
- }
- if (ret == CommError.Type.COMM_NO_ERROR)
- {
- ret = buf.sprintf("{0}[{1:d}]: {2}{3}", variable, arrIdx,strUni, sep);
- }
- return ret;
- }
- public static CommError.Type printWString(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx,Int16[] str)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- //int count = CommTypeUtil.wstrlen(str) + 1;
- ret = buf.sprintf("{0}[{1:d}]",variable,arrIdx);
- if (ret == CommError.Type.COMM_NO_ERROR)
- {
- int len = CommTypeUtil.wstrlen(str);
- for (int i = 0; i < len; i++)
- {
- ret = buf.sprintf("0x{0:X4}", str[i]);
- if (CommError.Type.COMM_NO_ERROR != ret)
- {
- break;
- }
- }
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}", sep);
- }
- return ret;
- }
- public static CommError.Type printTdrIP(ref VisualBuf buf, int indent, char sep, string variable, UInt32 ip)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}: ", variable);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = CommTypeUtil.IP2Str(ref buf,ip);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}",sep);
- }
- return ret;
- }
- public static CommError.Type printTdrIP(ref VisualBuf buf, int indent, char sep, string variable,int arrIdx, UInt32 ip)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}[{1:d}]: ", variable, arrIdx);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = CommTypeUtil.IP2Str(ref buf,ip);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}",sep);
- }
- return ret;
- }
- public static CommError.Type printTdrTime(ref VisualBuf buf, int indent, char sep, string variable, UInt32 time)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}: ", variable);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = CommTypeUtil.Time2Str(ref buf,time);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}",sep);
- }
- return ret;
- }
- public static CommError.Type printTdrTime(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx, UInt32 time)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}[{1:d}]: ", variable, arrIdx);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = CommTypeUtil.Time2Str(ref buf,time);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}",sep);
- }
- return ret;
- }
- public static CommError.Type printTdrDate(ref VisualBuf buf, int indent, char sep, string variable, UInt32 date)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}: ", variable);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = CommTypeUtil.CommDate2Str(ref buf,date);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}",sep);
- }
- return ret;
- }
- public static CommError.Type printTdrDate(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx, UInt32 date)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}[{1:d}]: ", variable, arrIdx);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = CommTypeUtil.CommDate2Str(ref buf,date);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}",sep);
- }
- return ret;
- }
- public static CommError.Type printTdrDateTime(ref VisualBuf buf, int indent, char sep, string variable, UInt64 datetime)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}: ", variable);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = CommTypeUtil.CommDateTime2Str(ref buf,datetime);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}",sep);
- }
- return ret;
- }
- public static CommError.Type printTdrDateTime(ref VisualBuf buf, int indent, char sep, string variable, int arrIdx, UInt64 datetime)
- {
- CommError.Type ret = CommError.Type.COMM_NO_ERROR;
- ret = printMultiStr(ref buf, " ", indent);
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}[{1:d}]: ", variable, arrIdx);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = CommTypeUtil.CommDateTime2Str(ref buf,datetime);
- }
- if (CommError.Type.COMM_NO_ERROR == ret)
- {
- ret = buf.sprintf("{0}",sep);
- }
- return ret;
- }
- }
|