corrade-vassal – Blame information for rev 13

Subversion Repositories:
Rev:
Rev Author Line No. Line
13 eva 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10  
11 namespace wasSharp
12 {
13 public class KeyValue
14 {
15 ///////////////////////////////////////////////////////////////////////////
16 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
17 ///////////////////////////////////////////////////////////////////////////
18 /// <summary>
19 /// Returns the value of a key from a key-value data string.
20 /// </summary>
21 /// <param name="key">the key of the value</param>
22 /// <param name="data">the key-value data segment</param>
23 /// <returns>true if the key was found in data</returns>
24 public static string Get(string key, string data)
25 {
26 return data.Split('&')
27 .AsParallel()
28 .Select(o => o.Split('=').ToList())
29 .Where(o => o.Count.Equals(2))
30 .Select(o => new
31 {
32 k = o.First(),
33 v = o.Last()
34 })
35 .Where(o => o.k.Equals(key))
36 .Select(o => o.v)
37 .FirstOrDefault();
38 }
39  
40 ///////////////////////////////////////////////////////////////////////////
41 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
42 ///////////////////////////////////////////////////////////////////////////
43 /// <summary>
44 /// Returns a key-value data string with a key set to a given value.
45 /// </summary>
46 /// <param name="key">the key of the value</param>
47 /// <param name="value">the value to set the key to</param>
48 /// <param name="data">the key-value data segment</param>
49 /// <returns>
50 /// a key-value data string or the empty string if either key or
51 /// value are empty
52 /// </returns>
53 public static string Set(string key, string value, string data)
54 {
55 HashSet<string> output = new HashSet<string>(data.Split('&')
56 .AsParallel()
57 .Select(o => o.Split('=').ToList())
58 .Where(o => o.Count.Equals(2))
59 .Select(o => new
60 {
61 k = o.First(),
62 v = !o.First().Equals(key) ? o.Last() : value
63 }).Select(o => string.Join("=", o.k, o.v)));
64 string append = string.Join("=", key, value);
65 if (!output.Contains(append))
66 {
67 output.Add(append);
68 }
69 return string.Join("&", output.ToArray());
70 }
71  
72 ///////////////////////////////////////////////////////////////////////////
73 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
74 ///////////////////////////////////////////////////////////////////////////
75 /// <summary>
76 /// Deletes a key-value pair from a string referenced by a key.
77 /// </summary>
78 /// <param name="key">the key to search for</param>
79 /// <param name="data">the key-value data segment</param>
80 /// <returns>a key-value pair string</returns>
81 public static string Delete(string key, string data)
82 {
83 return string.Join("&", data.Split('&')
84 .AsParallel()
85 .Select(o => o.Split('=').ToList())
86 .Where(o => o.Count.Equals(2))
87 .Select(o => new
88 {
89 k = o.First(),
90 v = o.Last()
91 })
92 .Where(o => !o.k.Equals(key))
93 .Select(o => string.Join("=", o.k, o.v))
94 .ToArray());
95 }
96  
97 ///////////////////////////////////////////////////////////////////////////
98 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
99 ///////////////////////////////////////////////////////////////////////////
100 /// <summary>
101 /// Decodes key-value pair data to a dictionary.
102 /// </summary>
103 /// <param name="data">the key-value pair data</param>
104 /// <returns>a dictionary containing the keys and values</returns>
105 public static Dictionary<string, string> Decode(string data)
106 {
107 return data.Split('&')
108 .AsParallel()
109 .Select(o => o.Split('=').ToList())
110 .Where(o => o.Count.Equals(2))
111 .Select(o => new
112 {
113 k = o.First(),
114 v = o.Last()
115 })
116 .GroupBy(o => o.k)
117 .ToDictionary(o => o.Key, p => p.First().v);
118 }
119  
120 ///////////////////////////////////////////////////////////////////////////
121 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
122 ///////////////////////////////////////////////////////////////////////////
123 /// <summary>
124 /// Serialises a dictionary to key-value data.
125 /// </summary>
126 /// <param name="data">a dictionary</param>
127 /// <returns>a key-value data encoded string</returns>
128 public static string Encode(Dictionary<string, string> data)
129 {
130 return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value)));
131 }
132  
133 ///////////////////////////////////////////////////////////////////////////
134 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
135 ///////////////////////////////////////////////////////////////////////////
136 /// <summary>Escapes a dictionary's keys and values for sending as POST data.</summary>
137 /// <param name="data">A dictionary containing keys and values to be escaped</param>
138 /// <param name="func">The function to use to escape the keys and values in the dictionary.</param>
139 public static Dictionary<string, string> Escape(Dictionary<string, string> data,
140 Func<string, string> func)
141 {
142 return data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value));
143 }
144 }
145 }