-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebUtilities.cs
218 lines (187 loc) · 4.74 KB
/
WebUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System.Globalization;
using System.Net;
using System.Text;
namespace IRB.RevigoWeb
{
public static class WebUtilities
{
public static class TypeConverter
{
public static Boolean ToBoolean(object? value)
{
bool bValue = false;
if (value != null && value != DBNull.Value)
{
try
{
bValue = Convert.ToBoolean(value);
}
catch
{ }
}
return bValue;
}
public static uint ToUInt32(object? value)
{
uint uiValue = 0;
if (value != null && value != DBNull.Value)
{
try
{
uiValue = Convert.ToUInt32(value);
}
catch
{ }
}
return uiValue;
}
public static int ToInt32(object? value)
{
int iValue = 0;
if (value != null && value != DBNull.Value)
{
try
{
iValue = Convert.ToInt32(value);
}
catch
{ }
}
return iValue;
}
public static long ToInt64(object? value)
{
long lValue = 0;
if (value != null && value != DBNull.Value)
{
try
{
lValue = Convert.ToInt64(value);
}
catch
{ }
}
return lValue;
}
public static double ToDouble(object? value)
{
double dValue = 0.0;
if (value != null && value != DBNull.Value)
{
try
{
dValue = Convert.ToDouble(value);
}
catch
{ }
}
return dValue;
}
public static string? ToString(object? value)
{
string? sValue = null;
if (value != null && value != DBNull.Value)
{
try
{
sValue = Convert.ToString(value);
}
catch
{ }
}
return sValue;
}
public static string DoubleToJSON(double value)
{
if (double.IsNaN(value))
{
return "\"NaN\"";
}
return value.ToString(CultureInfo.InvariantCulture);
}
public static string StringToJSON(string? text)
{
if (string.IsNullOrEmpty(text))
return "null";
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
char ch = text[i];
if (ch < '\x20')
{
switch (ch)
{
case '\b':
result.Append("\\b");
break;
case '\f':
result.Append("\\f");
break;
case '\n':
result.Append("\\n");
break;
case '\r':
result.Append("\\r");
break;
case '\t':
result.Append("\\t");
break;
default:
result.AppendFormat("\\u{0:x4}", (int)ch);
break;
}
}
else if (ch > '\xff')
{
result.AppendFormat("\\u{0:x4}", (int)ch);
}
else
{
switch (ch)
{
case '\"':
result.Append("\\\"");
break;
case '/':
result.Append("\\/");
break;
case '\\':
result.Append("\\\\");
break;
default:
result.Append(ch);
break;
}
}
}
return result.ToString();
}
public static string JoinStringArray(List<string> lines)
{
StringBuilder sbTemp = new StringBuilder();
for (int i = 0; i < lines.Count; i++)
{
sbTemp.AppendLine(lines[i]);
}
return sbTemp.ToString();
}
public static string StringArrayToJSON(List<string> lines)
{
StringBuilder sbTemp = new StringBuilder();
sbTemp.Append("[");
for (int i = 0; i < lines.Count; i++)
{
if (i > 0)
sbTemp.Append(",");
sbTemp.AppendFormat("\"{0}\"", WebUtilities.TypeConverter.StringToJSON(lines[i]));
}
sbTemp.Append("]");
return sbTemp.ToString();
}
public static string HtmlEncode(string text)
{
return WebUtility.HtmlEncode(text.Replace('\"', '\'').Replace("\n", "\\n").Replace("\r", "\\r"));
}
}
}
}