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 null if parsing failed.</returns>
105 public static T Constant<T>(string atom)
107 string constant = String(atom);
108 if (constant != null)
110 foreach (string enumConstant in System.Enum.GetNames(typeof(T)))
112 if (constant == enumConstant)
114 return (T)System.Enum.Parse(typeof(T), constant);
122 /// Parses an integer.
124 /// <param name="atom">Text.</param>
125 /// <returns>The integer, or null if parsing failed.</returns>
126 public static int? Integer(string atom)
130 int integer = Convert.ToInt32(atom.Trim());
133 #pragma warning disable 0168
134 catch (System.Exception ex)
135 #pragma warning restore 0168
142 /// Parses a boolean value.
144 /// <param name="atom">Text.</param>
145 /// <returns>True or false, or null if parsing failed.</returns>
146 public static bool? Boolean(string atom)
148 Match match = Regex.Match(atom, @"^\s*(true|false)\s*$", RegexOptions.IgnoreCase);
151 if (match.Groups[1].Value[0] == 't' || match.Groups[1].Value[0] == 'T') return true;
158 /// Parses a function.
160 /// <param name="atom">Text.</param>
161 /// <returns>An array two strings containing the function name and
162 /// parameter-list, in that order, or null if parsing failed.</returns>
163 public static string[] Function(string atom)
165 Match match = Regex.Match(atom, @"^\s*(\w+)\((.*)\)\s*$");
168 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
175 /// Parses a comma-separated list of atoms.
177 /// <param name="text">Text.</param>
178 /// <returns>An array of strings containing the atoms, or null
179 /// if parsing failed.</returns>
180 public static string[] ParameterList(string text)
186 /// Parses a whitespace-separated list of atoms.
188 /// <param name="text">Text.</param>
189 /// <returns>An array of atoms, or null if parsing failed.</returns>
190 public static string[] List(string text)
192 List<string> list = new List<string>();
194 MatchCollection matches = Regex.Matches(text, @"\s*("".*?"")|(\w+\(.*?\))|(\[.*?\])|(<.*?>)|(\S+)(?:\s+|$)");
195 // FIXME: This may barf all over itself if there are nested parentheses, doublequotes, brackets, etc.
196 foreach (Match match in matches)
198 Console.WriteLine("matched: " + match.Value);
199 list.Add(match.Value);
202 return list.ToArray();