diff options
Diffstat (limited to 'OpenGridServices-Source/OpenGridServices.Manager/OpenGridServices.Manager/Util.cs')
-rw-r--r-- | OpenGridServices-Source/OpenGridServices.Manager/OpenGridServices.Manager/Util.cs | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/OpenGridServices-Source/OpenGridServices.Manager/OpenGridServices.Manager/Util.cs b/OpenGridServices-Source/OpenGridServices.Manager/OpenGridServices.Manager/Util.cs new file mode 100644 index 0000000..5bf7ff9 --- /dev/null +++ b/OpenGridServices-Source/OpenGridServices.Manager/OpenGridServices.Manager/Util.cs | |||
@@ -0,0 +1,133 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using libsecondlife; | ||
5 | using libsecondlife.Packets; | ||
6 | |||
7 | namespace OpenSim.Framework.Utilities | ||
8 | { | ||
9 | public class Util | ||
10 | { | ||
11 | private static Random randomClass = new Random(); | ||
12 | private static uint nextXferID = 5000; | ||
13 | private static object XferLock = new object(); | ||
14 | |||
15 | public static ulong UIntsToLong(uint X, uint Y) | ||
16 | { | ||
17 | return Helpers.UIntsToLong(X, Y); | ||
18 | } | ||
19 | |||
20 | public static Random RandomClass | ||
21 | { | ||
22 | get | ||
23 | { | ||
24 | return randomClass; | ||
25 | } | ||
26 | } | ||
27 | |||
28 | public static uint GetNextXferID() | ||
29 | { | ||
30 | uint id = 0; | ||
31 | lock(XferLock) | ||
32 | { | ||
33 | id = nextXferID; | ||
34 | nextXferID++; | ||
35 | } | ||
36 | return id; | ||
37 | } | ||
38 | |||
39 | //public static int fast_distance2d(int x, int y) | ||
40 | //{ | ||
41 | // x = System.Math.Abs(x); | ||
42 | // y = System.Math.Abs(y); | ||
43 | |||
44 | // int min = System.Math.Min(x, y); | ||
45 | |||
46 | // return (x + y - (min >> 1) - (min >> 2) + (min >> 4)); | ||
47 | //} | ||
48 | |||
49 | public static string FieldToString(byte[] bytes) | ||
50 | { | ||
51 | return FieldToString(bytes, String.Empty); | ||
52 | } | ||
53 | |||
54 | /// <summary> | ||
55 | /// Convert a variable length field (byte array) to a string, with a | ||
56 | /// field name prepended to each line of the output | ||
57 | /// </summary> | ||
58 | /// <remarks>If the byte array has unprintable characters in it, a | ||
59 | /// hex dump will be put in the string instead</remarks> | ||
60 | /// <param name="bytes">The byte array to convert to a string</param> | ||
61 | /// <param name="fieldName">A field name to prepend to each line of output</param> | ||
62 | /// <returns>An ASCII string or a string containing a hex dump, minus | ||
63 | /// the null terminator</returns> | ||
64 | public static string FieldToString(byte[] bytes, string fieldName) | ||
65 | { | ||
66 | // Check for a common case | ||
67 | if (bytes.Length == 0) return String.Empty; | ||
68 | |||
69 | StringBuilder output = new StringBuilder(); | ||
70 | bool printable = true; | ||
71 | |||
72 | for (int i = 0; i < bytes.Length; ++i) | ||
73 | { | ||
74 | // Check if there are any unprintable characters in the array | ||
75 | if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09 | ||
76 | && bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00) | ||
77 | { | ||
78 | printable = false; | ||
79 | break; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | if (printable) | ||
84 | { | ||
85 | if (fieldName.Length > 0) | ||
86 | { | ||
87 | output.Append(fieldName); | ||
88 | output.Append(": "); | ||
89 | } | ||
90 | |||
91 | if (bytes[bytes.Length - 1] == 0x00) | ||
92 | output.Append(UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1)); | ||
93 | else | ||
94 | output.Append(UTF8Encoding.UTF8.GetString(bytes)); | ||
95 | } | ||
96 | else | ||
97 | { | ||
98 | for (int i = 0; i < bytes.Length; i += 16) | ||
99 | { | ||
100 | if (i != 0) | ||
101 | output.Append(Environment.NewLine); | ||
102 | if (fieldName.Length > 0) | ||
103 | { | ||
104 | output.Append(fieldName); | ||
105 | output.Append(": "); | ||
106 | } | ||
107 | |||
108 | for (int j = 0; j < 16; j++) | ||
109 | { | ||
110 | if ((i + j) < bytes.Length) | ||
111 | output.Append(String.Format("{0:X2} ", bytes[i + j])); | ||
112 | else | ||
113 | output.Append(" "); | ||
114 | } | ||
115 | |||
116 | for (int j = 0; j < 16 && (i + j) < bytes.Length; j++) | ||
117 | { | ||
118 | if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E) | ||
119 | output.Append((char)bytes[i + j]); | ||
120 | else | ||
121 | output.Append("."); | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | |||
126 | return output.ToString(); | ||
127 | } | ||
128 | public Util() | ||
129 | { | ||
130 | |||
131 | } | ||
132 | } | ||
133 | } | ||