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
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Text;
namespace Janrain.OpenId
{
public class KVUtil
{
#region Constructor(s)
public KVUtil()
{
}
#endregion
#region Methods
private static void Error(string message, bool strict)
{
if (strict)
throw new ArgumentException(message);
}
public static byte[] SeqToKV(NameValueCollection seq, IList<string> keyOrder, bool strict)
{
MemoryStream ms = new MemoryStream();
if (keyOrder == null) throw new ArgumentNullException("keyOrder");
if (seq.Count != keyOrder.Count) throw new ArgumentException(Strings.KeysListAndDictionaryDoNotMatch);
foreach (string key in keyOrder)
{
string val = seq[key];
if (key.IndexOf('\n') >= 0)
throw new ArgumentException("Invalid input for SeqToKV: key contains newline");
if (key.Trim().Length != key.Length)
Error("Key has whitespace at beginning or end: " + key, strict);
if (val.Trim().Length != val.Length)
Error("Value has whitespace at beginning or end: " + val, strict);
byte[] line = Encoding.UTF8.GetBytes(key + ":" + val + "\n");
ms.Write(line, 0, line.Length);
}
return ms.ToArray();
}
public static byte[] DictToKV(IDictionary dict) {
string[] keyOrder = new string[dict.Count];
dict.Keys.CopyTo(keyOrder, 0);
return DictToKV(dict, keyOrder);
}
public static byte[] DictToKV(IDictionary dict, IList<string> keyOrder)
{
NameValueCollection data = new NameValueCollection(dict.Count);
foreach (DictionaryEntry pair in dict)
{
data.Add(pair.Key.ToString(), pair.Value.ToString());
}
return SeqToKV(data, keyOrder, false);
}
public static IDictionary KVToDict(byte[] data)
{
StringReader reader = new StringReader(UTF8Encoding.UTF8.GetString(data));
Dictionary<string,string> dict = new Dictionary<string,string>();
int line_num = 0;
string line = reader.ReadLine();
while (line != null)
{
line_num++;
if (line.Trim().Length > 0)
{
string[] parts = line.Split(new char[] { ':' }, 2);
if (parts.Length != 2)
{
Error("Line " + line_num.ToString() + " does not contain a color.", false);
}
else
{
dict[parts[0].Trim()] = parts[1].Trim();
}
}
line = reader.ReadLine();
}
return dict;
}
#endregion
}
}
|