2 using System.Collections.Generic;
5 using System.Text.RegularExpressions;
6 using Microsoft.Xna.Framework;
11 /// Class with handy static methods taking strings and returning objects
12 /// parsed from those strings.
17 /// Parses a section header of an INI file.
19 /// <param name="line">Text.</param>
20 /// <returns>The section header, or null if parsing failed.</returns>
21 public static string IniSectionHeader(string line)
23 Match match = Regex.Match(line, @"^\s*\[(\w+)\]\s*$");
24 if (match.Success) return match.Groups[1].Value;
29 /// Parses a key-value pair.
31 /// <param name="line">Text.</param>
32 /// <returns>An array of two strings containg the key and value,
33 /// in that order, or null if parsing failed.</returns>
34 public static string[] KeyValuePair(string line)
36 Match match = Regex.Match(line, @"^\s*(\w+)\s*=\s*(.+)\s*$");
39 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
46 /// Parses a pair of coordinates.
48 /// <param name="atom">Text.</param>
49 /// <returns>The coordinates, or null if parsing failed.</returns>
50 public static Point? Coordinates(string atom)
52 Match match = Regex.Match(atom, @"^\s*\[(\S+?)\s*,\s*(\S+?)\]\s*$");
55 int? x = Integer(match.Groups[1].Value);
56 int? y = Integer(match.Groups[2].Value);
57 if (x != null && y != null)
59 return new Point(x.Value, y.Value);
66 /// Parses a range of integers.
68 /// <param name="atom">Text.</param>
69 /// <returns>An array of two integers containing the min and max,
70 /// in that order, or null if parsing failed.</returns>
71 public static int[] Range(string atom)
73 Match match = Regex.Match(atom, @"^\s*<(\S+?)\s*,\s*(\S+?)>\s*$");
76 int? min = Integer(match.Groups[1].Value);
77 int? max = Integer(match.Groups[2].Value);
78 if (min != null && max != null)
80 int[] range = { min.Value, max.Value };
90 /// <param name="atom">Text.</param>
91 /// <returns>The string, or null if parsing failed.</returns>
92 public static string String(string atom)
94 Match match = Regex.Match(atom, @"^\s*(""?)(.*)\1\s*$");
95 if (match.Success) return match.Groups[2].Value;
100 /// Parses a constant from an enum.
102 /// <typeparam name="T">An enumeration.</typeparam>
103 /// <param name="atom">Text.</param>
104 /// <returns>The constant, or default(T) if parsing failed.</returns>
105 public static T Constant<T>(string atom)
109 return (T)System.Enum.Parse(typeof(T), String(atom), true);
111 #pragma warning disable 0168
112 catch (System.Exception ex)
113 #pragma warning restore 0168
120 /// Parses an integer.
122 /// <param name="atom">Text.</param>
123 /// <returns>The integer, or null if parsing failed.</returns>
124 public static int? Integer(string atom)
128 int integer = Convert.ToInt32(atom.Trim());
131 #pragma warning disable 0168
132 catch (System.Exception ex)
133 #pragma warning restore 0168
140 /// Parses a boolean value.
142 /// <param name="atom">Text.</param>
143 /// <returns>True or false, or null if parsing failed.</returns>
144 public static bool? Boolean(string atom)
146 Match match = Regex.Match(atom, @"^\s*(true|false)\s*$", RegexOptions.IgnoreCase);
149 if (match.Groups[1].Value[0] == 't' || match.Groups[1].Value[0] == 'T') return true;
156 /// Parses a function.
158 /// <param name="atom">Text.</param>
159 /// <returns>An array two strings containing the function name and
160 /// parameter-list, in that order, or null if parsing failed.</returns>
161 public static string[] Function(string atom)
163 Match match = Regex.Match(atom, @"^\s*(\w+)\((.*)\)\s*$");
166 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
173 /// Parses a whitespace-separated list of atoms.
175 /// <param name="text">Text.</param>
176 /// <returns>An array of atoms, or null if parsing failed.</returns>
177 public static string[] List(string text)
179 List<string> list = new List<string>();
181 MatchCollection matches = Regex.Matches(text, @"\s*("".*?"")|(\w+\(.*?\))|(\[.*?\])|(<.*?>)|(\S+)(?:\s+|$)");
182 // FIXME: This may barf all over itself if there are nested parentheses, doublequotes, brackets, etc.
183 foreach (Match match in matches)
185 Console.WriteLine("matched: " + match.Value);
186 list.Add(match.Value);
189 return list.ToArray();