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 comment of an INI file.
31 /// <param name="line">Text.</param>
32 /// <returns>The comment.</returns>
33 public static string IniComment(string line)
35 Match match = Regex.Match(line, @"^;\s*(.*)\s*$");
36 if (match.Success) return match.Groups[1].Value;
41 /// Parses a key-value pair.
43 /// <param name="line">Text.</param>
44 /// <returns>An array of two strings containg the key and value,
45 /// in that order, or null if parsing failed.</returns>
46 public static string[] KeyValuePair(string line)
48 Match match = Regex.Match(line, @"^\s*(\w+)\s*=\s*(.+)\s*$");
51 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
58 /// Parses a pair of coordinates.
60 /// <param name="atom">Text.</param>
61 /// <returns>The coordinates, or null if parsing failed.</returns>
62 public static Point? Coordinates(string atom)
64 Match match = Regex.Match(atom, @"^\s*\[(\S+?)\s*,\s*(\S+?)\]\s*$");
67 int? x = Integer(match.Groups[1].Value);
68 int? y = Integer(match.Groups[2].Value);
69 if (x != null && y != null)
71 return new Point(x.Value, y.Value);
78 /// Parses a range of integers.
80 /// <param name="atom">Text.</param>
81 /// <returns>An array of two integers containing the min and max,
82 /// in that order, or null if parsing failed.</returns>
83 public static int[] Range(string atom)
85 Match match = Regex.Match(atom, @"^\s*<(\S+?)\s*,\s*(\S+?)>\s*$");
88 int? min = Integer(match.Groups[1].Value);
89 int? max = Integer(match.Groups[2].Value);
90 if (min != null && max != null)
92 int[] range = { min.Value, max.Value };
102 /// <param name="atom">Text.</param>
103 /// <returns>The string, or null if parsing failed.</returns>
104 public static string String(string atom)
106 Match match = Regex.Match(atom, @"^\s*(""?)(.*)\1\s*$");
107 if (match.Success) return match.Groups[2].Value;
112 /// Parses a constant from an enum.
114 /// <typeparam name="T">An enumeration.</typeparam>
115 /// <param name="atom">Text.</param>
116 /// <returns>The constant, or default(T) if parsing failed.</returns>
117 public static T Constant<T>(string atom)
121 return (T)System.Enum.Parse(typeof(T), String(atom), true);
123 #pragma warning disable 0168
124 catch (System.Exception ex)
125 #pragma warning restore 0168
132 /// Parses an integer.
134 /// <param name="atom">Text.</param>
135 /// <returns>The integer, or null if parsing failed.</returns>
136 public static int? Integer(string atom)
140 int integer = Convert.ToInt32(atom.Trim());
143 #pragma warning disable 0168
144 catch (System.Exception ex)
145 #pragma warning restore 0168
152 /// Parses a boolean value.
154 /// <param name="atom">Text.</param>
155 /// <returns>True or false, or null if parsing failed.</returns>
156 public static bool? Boolean(string atom)
158 Match match = Regex.Match(atom, @"^\s*(true|false)\s*$", RegexOptions.IgnoreCase);
161 if (match.Groups[1].Value[0] == 't' || match.Groups[1].Value[0] == 'T') return true;
168 /// Parses a function.
170 /// <param name="atom">Text.</param>
171 /// <returns>An array two strings containing the function name and
172 /// parameter-list, in that order, or null if parsing failed.</returns>
173 public static string[] Function(string atom)
175 Match match = Regex.Match(atom, @"^\s*(\w+)\((.*)\)\s*$");
178 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
185 /// Parses a whitespace-separated list of atoms.
187 /// <param name="text">Text.</param>
188 /// <returns>An array of atoms, or null if parsing failed.</returns>
189 public static string[] List(string text)
191 List<string> list = new List<string>();
193 MatchCollection matches = Regex.Matches(text, @"\s*("".*?"")|(\w+\(.*?\))|(\[.*?\])|(<.*?>)|(\S+)(?:\s+|$)");
194 // FIXME: This may barf all over itself if there are nested parentheses, doublequotes, brackets, etc.
195 foreach (Match match in matches)
197 list.Add(match.Value);
200 return list.ToArray();