]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Parse.cs
Improved projectiles
[chaz/carfire] / CarFire / CarFire / CarFire / Parse.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Text.RegularExpressions;
6 using Microsoft.Xna.Framework;
7
8 namespace CarFire
9 {
10 /// <summary>
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.
14 /// </summary>
15 public class Parse
16 {
17 /// <summary>
18 /// Parses a section header of an INI file.
19 /// </summary>
20 /// <param name="line">Text.</param>
21 /// <returns>The section header, or null if parsing failed.</returns>
22 public static string IniSectionHeader(string line)
23 {
24 Match match = Regex.Match(line, @"^\s*\[(\w+)\]\s*$");
25 if (match.Success) return match.Groups[1].Value;
26 return null;
27 }
28
29 /// <summary>
30 /// Parses a comment of an INI file.
31 /// </summary>
32 /// <param name="line">Text.</param>
33 /// <returns>The comment, or null if parsing failed.</returns>
34 public static string IniComment(string line)
35 {
36 Match match = Regex.Match(line, @"^;\s*(.*)\s*$");
37 if (match.Success) return match.Groups[1].Value;
38 return null;
39 }
40
41 /// <summary>
42 /// Parses a key-value pair.
43 /// </summary>
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)
48 {
49 Match match = Regex.Match(line, @"^\s*(\w+)\s*=\s*(.+)\s*$");
50 if (match.Success)
51 {
52 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
53 return pair;
54 }
55 return null;
56 }
57
58 /// <summary>
59 /// Parses a pair of coordinates.
60 /// </summary>
61 /// <param name="atom">Text.</param>
62 /// <returns>The coordinates, or null if parsing failed.</returns>
63 public static Point? Coordinates(string atom)
64 {
65 Match match = Regex.Match(atom, @"^\s*\[(\S+?)\s*,\s*(\S+?)\]\s*$");
66 if (match.Success)
67 {
68 int? x = Integer(match.Groups[1].Value);
69 int? y = Integer(match.Groups[2].Value);
70 if (x != null && y != null)
71 {
72 return new Point(x.Value, y.Value);
73 }
74 }
75 return null;
76 }
77
78 /// <summary>
79 /// Parses a range of integers.
80 /// </summary>
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)
85 {
86 Match match = Regex.Match(atom, @"^\s*<(\S+?)\s*,\s*(\S+?)>\s*$");
87 if (match.Success)
88 {
89 int? min = Integer(match.Groups[1].Value);
90 int? max = Integer(match.Groups[2].Value);
91 if (min != null && max != null)
92 {
93 int[] range = { min.Value, max.Value };
94 return range;
95 }
96 }
97 return null;
98 }
99
100 /// <summary>
101 /// Parses a string.
102 /// </summary>
103 /// <param name="atom">Text.</param>
104 /// <returns>The string, or null if parsing failed.</returns>
105 public static string String(string atom)
106 {
107 Match match = Regex.Match(atom, @"^\s*(""?)(.*)\1\s*$");
108 if (match.Success) return match.Groups[2].Value;
109 return null;
110 }
111
112 /// <summary>
113 /// Parses a single character.
114 /// </summary>
115 /// <param name="atom">Text.</param>
116 /// <returns>The character, or null if parsing failed.</returns>
117 public static char? Char(string atom)
118 {
119 string str = String(atom);
120 if (str != null && str.Length == 1) return str[0];
121 return null;
122 }
123
124 /// <summary>
125 /// Parses a constant from an enum.
126 /// </summary>
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)
131 {
132 try
133 {
134 return (T)System.Enum.Parse(typeof(T), String(atom));
135 }
136 #pragma warning disable 0168
137 catch (System.Exception ex)
138 #pragma warning restore 0168
139 {
140 return default(T);
141 }
142 }
143
144 /// <summary>
145 /// Parses an integer.
146 /// </summary>
147 /// <param name="atom">Text.</param>
148 /// <returns>The integer, or null if parsing failed.</returns>
149 public static int? Integer(string atom)
150 {
151 try
152 {
153 int integer = Convert.ToInt32(atom.Trim());
154 return integer;
155 }
156 #pragma warning disable 0168
157 catch (System.Exception ex)
158 #pragma warning restore 0168
159 {
160 return null;
161 }
162 }
163
164 /// <summary>
165 /// Parses a boolean value.
166 /// </summary>
167 /// <param name="atom">Text.</param>
168 /// <returns>True or false, or null if parsing failed.</returns>
169 public static bool? Boolean(string atom)
170 {
171 Match match = Regex.Match(atom, @"^\s*(true|false)\s*$", RegexOptions.IgnoreCase);
172 if (match.Success)
173 {
174 if (match.Groups[1].Value[0] == 't' || match.Groups[1].Value[0] == 'T') return true;
175 else return false;
176 }
177 return null;
178 }
179
180 /// <summary>
181 /// Parses a function.
182 /// </summary>
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)
187 {
188 Match match = Regex.Match(atom, @"^\s*(\w+)\((.*)\)\s*$");
189 if (match.Success)
190 {
191 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
192 return pair;
193 }
194 return null;
195 }
196
197 /// <summary>
198 /// Parses a whitespace-separated list of atoms.
199 /// </summary>
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)
203 {
204 List<string> list = new List<string>();
205
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)
209 {
210 list.Add(match.Value);
211 }
212
213 return list.ToArray();
214 }
215 }
216 }
This page took 0.042861 seconds and 4 git commands to generate.