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. For all of these functions, white space is
13 /// generally ignored, but superfluous characters are not allowed.
18 /// Parses a section header of an INI file.
20 /// <param name="line">Text.</param>
21 /// <returns>The section header, or null if parsing failed.</returns>
22 public static string IniSectionHeader(string line)
24 Match match = Regex.Match(line, @"^\s*\[(\w+)\]\s*$");
25 if (match.Success) return match.Groups[1].Value;
30 /// Parses a comment of an INI file.
32 /// <param name="line">Text.</param>
33 /// <returns>The comment, or null if parsing failed.</returns>
34 public static string IniComment(string line)
36 Match match = Regex.Match(line, @"^;\s*(.*)\s*$");
37 if (match.Success) return match.Groups[1].Value;
42 /// Parses a key-value pair.
44 /// <param name="line">Text.</param>
45 /// <returns>An array of two strings containg the key and value,
46 /// in that order, or null if parsing failed.</returns>
47 public static string[] KeyValuePair(string line)
49 Match match = Regex.Match(line, @"^\s*(\w+)\s*=\s*(.+)\s*$");
52 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
59 /// Parses a pair of coordinates.
61 /// <param name="atom">Text.</param>
62 /// <returns>The coordinates, or null if parsing failed.</returns>
63 public static Point? Coordinates(string atom)
65 Match match = Regex.Match(atom, @"^\s*\[(\S+?)\s*,\s*(\S+?)\]\s*$");
68 int? x = Integer(match.Groups[1].Value);
69 int? y = Integer(match.Groups[2].Value);
70 if (x != null && y != null)
72 return new Point(x.Value, y.Value);
79 /// Parses a range of integers.
81 /// <param name="atom">Text.</param>
82 /// <returns>An array of two integers containing the min and max,
83 /// in that order, or null if parsing failed.</returns>
84 public static int[] Range(string atom)
86 Match match = Regex.Match(atom, @"^\s*<(\S+?)\s*,\s*(\S+?)>\s*$");
89 int? min = Integer(match.Groups[1].Value);
90 int? max = Integer(match.Groups[2].Value);
91 if (min != null && max != null)
93 int[] range = { min.Value, max.Value };
103 /// <param name="atom">Text.</param>
104 /// <returns>The string, or null if parsing failed.</returns>
105 public static string String(string atom)
107 Match match = Regex.Match(atom, @"^\s*(""?)(.*)\1\s*$");
108 if (match.Success) return match.Groups[2].Value;
113 /// Parses a single character.
115 /// <param name="atom">Text.</param>
116 /// <returns>The character, or null if parsing failed.</returns>
117 public static char? Char(string atom)
119 string str = String(atom);
120 if (str != null && str.Length == 1) return str[0];
125 /// Parses a constant from an enum.
127 /// <typeparam name="T">An enumeration.</typeparam>
128 /// <param name="atom">Text.</param>
129 /// <returns>The constant, or default(T) if parsing failed.</returns>
130 public static T Constant<T>(string atom)
134 return (T)System.Enum.Parse(typeof(T), String(atom));
136 #pragma warning disable 0168
137 catch (System.Exception ex)
138 #pragma warning restore 0168
145 /// Parses an integer.
147 /// <param name="atom">Text.</param>
148 /// <returns>The integer, or null if parsing failed.</returns>
149 public static int? Integer(string atom)
153 int integer = Convert.ToInt32(atom.Trim());
156 #pragma warning disable 0168
157 catch (System.Exception ex)
158 #pragma warning restore 0168
165 /// Parses a boolean value.
167 /// <param name="atom">Text.</param>
168 /// <returns>True or false, or null if parsing failed.</returns>
169 public static bool? Boolean(string atom)
171 Match match = Regex.Match(atom, @"^\s*(true|false)\s*$", RegexOptions.IgnoreCase);
174 if (match.Groups[1].Value[0] == 't' || match.Groups[1].Value[0] == 'T') return true;
181 /// Parses a function.
183 /// <param name="atom">Text.</param>
184 /// <returns>An array two strings containing the function name and
185 /// parameter-list, in that order, or null if parsing failed.</returns>
186 public static string[] Function(string atom)
188 Match match = Regex.Match(atom, @"^\s*(\w+)\((.*)\)\s*$");
191 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
198 /// Parses a whitespace-separated list of atoms.
200 /// <param name="text">Text.</param>
201 /// <returns>An array of atoms, or null if parsing failed.</returns>
202 public static string[] List(string text)
204 List<string> list = new List<string>();
206 MatchCollection matches = Regex.Matches(text, @"\s*("".*?"")|(\w+\(.*?\))|(\[.*?\])|(<.*?>)|(\S+)(?:\s+|$)");
207 // FIXME: This may barf all over itself if there are nested parentheses, doublequotes, brackets, etc.
208 foreach (Match match in matches)
210 list.Add(match.Value);
213 return list.ToArray();