Skip to content

Commit 9ed4987

Browse files
Add source
1 parent 7279a89 commit 9ed4987

14 files changed

+1893
-0
lines changed

App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
5+
</startup>
6+
</configuration>

BooleanStringStyle.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
3+
namespace Rampastring.Tools
4+
{
5+
6+
/// <summary>
7+
/// Defines how boolean values are converted to strings when a <see cref="IniFile"/> is written.
8+
/// </summary>
9+
[Flags]
10+
public enum BooleanStringStyle
11+
{
12+
/// <summary>
13+
/// Write boolean values as "True" and "False".
14+
/// </summary>
15+
TRUEFALSE = 0,
16+
17+
/// <summary>
18+
/// Write boolean values as "Yes" and "No".
19+
/// </summary>
20+
YESNO = 1,
21+
22+
/// <summary>
23+
/// Write boolean values as "true" and "false".
24+
/// </summary>
25+
TRUEFALSE_LOWERCASE = 2,
26+
27+
/// <summary>
28+
/// Write boolean values as "yes" and "no".
29+
/// </summary>
30+
YESNO_LOWERCASE = 3,
31+
32+
/// <summary>
33+
/// Write boolean values as "1" and "0".
34+
/// </summary>
35+
ONEZERO = 4
36+
}
37+
38+
}

Conversions.cs

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
using System;
2+
using System.Globalization;
3+
4+
namespace Rampastring.Tools
5+
{
6+
7+
/// <summary>
8+
/// Provides static methods for converting data types.
9+
/// </summary>
10+
public static class Conversions
11+
{
12+
/// <summary>
13+
/// Converts a string to a boolean.
14+
/// </summary>
15+
/// <param name="str">The string to convert.</param>
16+
/// <param name="defaultValue">The default value to return if the conversion fails.</param>
17+
/// <returns>A boolean based on the given string.</returns>
18+
public static bool BooleanFromString(string str, bool defaultValue)
19+
{
20+
if (string.IsNullOrEmpty(str))
21+
return defaultValue;
22+
23+
char firstChar = str.ToLower(CultureInfo.InvariantCulture)[0];
24+
25+
switch (firstChar)
26+
{
27+
case 't':
28+
case 'y':
29+
case '1':
30+
case 'a':
31+
case 'e':
32+
return true;
33+
case 'n':
34+
case 'f':
35+
case '0':
36+
return false;
37+
default:
38+
return defaultValue;
39+
}
40+
}
41+
42+
/// <summary>
43+
/// Converts a boolean to a string with the specified style.
44+
/// </summary>
45+
/// <param name="boolean">The boolean.</param>
46+
/// <param name="stringStyle">The style of the boolean string.</param>
47+
/// <returns>A string that represents the boolean with the specified style.</returns>
48+
public static string BooleanToString(bool boolean, BooleanStringStyle stringStyle)
49+
{
50+
string trueString;
51+
string falseString;
52+
53+
switch (stringStyle)
54+
{
55+
case BooleanStringStyle.TRUEFALSE_LOWERCASE:
56+
trueString = "true";
57+
falseString = "false";
58+
break;
59+
case BooleanStringStyle.YESNO:
60+
trueString = "Yes";
61+
falseString = "No";
62+
break;
63+
case BooleanStringStyle.YESNO_LOWERCASE:
64+
trueString = "yes";
65+
falseString = "no";
66+
break;
67+
case BooleanStringStyle.ONEZERO:
68+
trueString = "1";
69+
falseString = "0";
70+
break;
71+
default:
72+
case BooleanStringStyle.TRUEFALSE:
73+
trueString = "True";
74+
falseString = "False";
75+
break;
76+
}
77+
78+
return boolean ? trueString : falseString;
79+
}
80+
81+
/// <summary>
82+
/// Converts a string with the English number format to a float.
83+
/// </summary>
84+
/// <param name="str">The string to convert.</param>
85+
/// <param name="defaultValue">The default value to return if the conversion fails.</param>
86+
/// <returns>A float based on the given string.</returns>
87+
public static float FloatFromString(string str, float defaultValue)
88+
{
89+
if (string.IsNullOrEmpty(str))
90+
return defaultValue;
91+
92+
try
93+
{
94+
return Convert.ToSingle(str, CultureInfo.GetCultureInfo("en-US").NumberFormat);
95+
}
96+
catch
97+
{
98+
return defaultValue;
99+
}
100+
}
101+
102+
/// <summary>
103+
/// Converts a string with the English number format to a double.
104+
/// </summary>
105+
/// <param name="str">The string to convert.</param>
106+
/// <param name="defaultValue">The default value to return if the conversion fails.</param>
107+
/// <returns>A double based on the given string.</returns>
108+
public static double DoubleFromString(string str, double defaultValue)
109+
{
110+
if (string.IsNullOrEmpty(str))
111+
return defaultValue;
112+
113+
try
114+
{
115+
return Convert.ToDouble(str, CultureInfo.GetCultureInfo("en-US").NumberFormat);
116+
}
117+
catch
118+
{
119+
return defaultValue;
120+
}
121+
}
122+
123+
/// <summary>
124+
/// Converts a string with the English number format to an integer.
125+
/// </summary>
126+
/// <param name="str">The string to convert.</param>
127+
/// <param name="defaultValue">The default value to return if the conversion fails.</param>
128+
/// <returns>An integer based on the given string.</returns>
129+
public static int IntFromString(string str, int defaultValue)
130+
{
131+
// In theory the "if" here is useless, but having it here
132+
// makes the code run 100+ times faster for null / empty strings.
133+
if (string.IsNullOrEmpty(str))
134+
return defaultValue;
135+
136+
try
137+
{
138+
return int.Parse(str, CultureInfo.InvariantCulture);
139+
}
140+
catch
141+
{
142+
return defaultValue;
143+
}
144+
}
145+
146+
public static int[] IntArrayFromStringArray(string[] array)
147+
{
148+
int[] intArray = new int[array.Length];
149+
for (int i = 0; i < array.Length; i++)
150+
intArray[i] = int.Parse(array[i], CultureInfo.InvariantCulture);
151+
152+
return intArray;
153+
}
154+
155+
/// <summary>
156+
/// Converts an array of booleans into an array of bytes,
157+
/// packing 8 boolean values into a single byte.
158+
/// </summary>
159+
/// <param name="boolArray">The boolean array.</param>
160+
/// <returns>The generated array of bytes.</returns>
161+
public static byte[] BoolArrayIntoBytes(bool[] boolArray)
162+
{
163+
// Slight modification of Marc Gravell's code at
164+
// http://stackoverflow.com/questions/713057/convert-bool-to-byte
165+
166+
int byteCount = boolArray.Length / 8;
167+
if ((boolArray.Length % 8) != 0)
168+
byteCount++;
169+
170+
byte[] bytes = new byte[byteCount];
171+
int optionIndex = 0;
172+
int byteIndex = 0;
173+
174+
for (int i = 0; i < boolArray.Length; i++)
175+
{
176+
if (boolArray[i])
177+
{
178+
bytes[byteIndex] |= (byte)(((byte)1) << optionIndex);
179+
}
180+
181+
optionIndex++;
182+
183+
if (optionIndex == 8)
184+
{
185+
optionIndex = 0;
186+
byteIndex++;
187+
}
188+
}
189+
190+
return bytes;
191+
}
192+
193+
public static bool[] BytesIntoBoolArray(byte[] byteArray)
194+
{
195+
int booleanCount = byteArray.Length * 8;
196+
bool[] boolArray = new bool[booleanCount];
197+
198+
// Worth reading:
199+
// http://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work
200+
201+
for (int i = 0; i < byteArray.Length; i++)
202+
{
203+
byte b = byteArray[i];
204+
bool[] booleans = ByteToBoolArray(b);
205+
206+
for (int j = 0; j < booleans.Length; j++)
207+
{
208+
boolArray[i * 8 + j] = booleans[j];
209+
}
210+
}
211+
212+
return boolArray;
213+
}
214+
215+
/// <summary>
216+
/// Converts a byte to an array of 8 booleans.
217+
/// </summary>
218+
/// <param name="b">The byte.</param>
219+
/// <returns>An array of 8 booleans.</returns>
220+
public static bool[] ByteToBoolArray(byte b)
221+
{
222+
// prepare the return result
223+
bool[] result = new bool[8];
224+
225+
// check each bit in the byte. if 1 set to true, if 0 set to false
226+
for (int i = 0; i < 8; i++)
227+
result[i] = (b & (1 << i)) == 0 ? false : true;
228+
229+
// reverse the array
230+
// Array.Reverse(result);
231+
232+
return result;
233+
}
234+
}
235+
236+
}

ExtendedStringBuilder.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System.Runtime.Serialization;
2+
using System.Text;
3+
4+
namespace Rampastring.Tools
5+
{
6+
7+
/// <summary>
8+
/// A StringBuilder that can automatically add a separator between
9+
/// appended strings.
10+
/// </summary>
11+
public class ExtendedStringBuilder : ISerializable
12+
{
13+
public ExtendedStringBuilder()
14+
{
15+
stringBuilder = new StringBuilder();
16+
}
17+
18+
public ExtendedStringBuilder(string value, bool useSeparator)
19+
{
20+
stringBuilder = new StringBuilder(value);
21+
UseSeparator = useSeparator;
22+
}
23+
24+
public ExtendedStringBuilder(string value, bool useSeparator, char separator)
25+
{
26+
stringBuilder = new StringBuilder(value);
27+
UseSeparator = useSeparator;
28+
Separator = separator;
29+
}
30+
31+
public ExtendedStringBuilder(bool useSeparator, char separator)
32+
{
33+
stringBuilder = new StringBuilder();
34+
UseSeparator = useSeparator;
35+
Separator = separator;
36+
}
37+
38+
private readonly StringBuilder stringBuilder;
39+
40+
public char Separator { get; set; }
41+
public bool UseSeparator { get; set; }
42+
43+
public int Length { get { return stringBuilder.Length; } }
44+
45+
public void Append(int value)
46+
{
47+
Append(value.ToString());
48+
}
49+
50+
public void Append(object value)
51+
{
52+
Append(value.ToString());
53+
}
54+
55+
public void Append(string value)
56+
{
57+
stringBuilder.Append(value);
58+
if (UseSeparator)
59+
stringBuilder.Append(Separator);
60+
}
61+
62+
public void Remove(int startIndex, int length)
63+
{
64+
stringBuilder.Remove(startIndex, length);
65+
}
66+
67+
public override string ToString()
68+
{
69+
if (UseSeparator)
70+
stringBuilder.Remove(stringBuilder.Length - 1, 1);
71+
72+
return stringBuilder.ToString();
73+
}
74+
75+
public void GetObjectData(SerializationInfo info, StreamingContext context)
76+
{
77+
((ISerializable)stringBuilder).GetObjectData(info, context);
78+
}
79+
}
80+
81+
}

0 commit comments

Comments
 (0)