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