]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Parse.cs
created a new project MapProcessorLib to for importing map files
[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 key-value pair.
30 /// </summary>
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)
35 {
36 Match match = Regex.Match(line, @"^\s*(\w+)\s*=\s*(.+)\s*$");
37 if (match.Success)
38 {
39 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
40 return pair;
41 }
42 return null;
43 }
44
45 /// <summary>
46 /// Parses a pair of coordinates.
47 /// </summary>
48 /// <param name="atom">Text.</param>
49 /// <returns>The coordinates, or null if parsing failed.</returns>
50 public static Point? Coordinates(string atom)
51 {
52 Match match = Regex.Match(atom, @"^\s*\[(\S+?)\s*,\s*(\S+?)\]\s*$");
53 if (match.Success)
54 {
55 int? x = Integer(match.Groups[1].Value);
56 int? y = Integer(match.Groups[2].Value);
57 if (x != null && y != null)
58 {
59 return new Point(x.Value, y.Value);
60 }
61 }
62 return null;
63 }
64
65 /// <summary>
66 /// Parses a range of integers.
67 /// </summary>
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)
72 {
73 Match match = Regex.Match(atom, @"^\s*<(\S+?)\s*,\s*(\S+?)>\s*$");
74 if (match.Success)
75 {
76 int? min = Integer(match.Groups[1].Value);
77 int? max = Integer(match.Groups[2].Value);
78 if (min != null && max != null)
79 {
80 int[] range = { min.Value, max.Value };
81 return range;
82 }
83 }
84 return null;
85 }
86
87 /// <summary>
88 /// Parses a string.
89 /// </summary>
90 /// <param name="atom">Text.</param>
91 /// <returns>The string, or null if parsing failed.</returns>
92 public static string String(string atom)
93 {
94 Match match = Regex.Match(atom, @"^\s*(""?)(.*)\1\s*$");
95 if (match.Success) return match.Groups[2].Value;
96 return null;
97 }
98
99 /// <summary>
100 /// Parses a constant from an enum.
101 /// </summary>
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)
106 {
107 string constant = String(atom);
108 if (constant != null)
109 {
110 foreach (string enumConstant in System.Enum.GetNames(typeof(T)))
111 {
112 if (constant == enumConstant)
113 {
114 return (T)System.Enum.Parse(typeof(T), constant);
115 }
116 }
117 }
118 return default(T);
119 }
120
121 /// <summary>
122 /// Parses an integer.
123 /// </summary>
124 /// <param name="atom">Text.</param>
125 /// <returns>The integer, or null if parsing failed.</returns>
126 public static int? Integer(string atom)
127 {
128 try
129 {
130 int integer = Convert.ToInt32(atom.Trim());
131 return integer;
132 }
133 #pragma warning disable 0168
134 catch (System.Exception ex)
135 #pragma warning restore 0168
136 {
137 return null;
138 }
139 }
140
141 /// <summary>
142 /// Parses a boolean value.
143 /// </summary>
144 /// <param name="atom">Text.</param>
145 /// <returns>True or false, or null if parsing failed.</returns>
146 public static bool? Boolean(string atom)
147 {
148 Match match = Regex.Match(atom, @"^\s*(true|false)\s*$", RegexOptions.IgnoreCase);
149 if (match.Success)
150 {
151 if (match.Groups[1].Value[0] == 't' || match.Groups[1].Value[0] == 'T') return true;
152 else return false;
153 }
154 return null;
155 }
156
157 /// <summary>
158 /// Parses a function.
159 /// </summary>
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)
164 {
165 Match match = Regex.Match(atom, @"^\s*(\w+)\((.*)\)\s*$");
166 if (match.Success)
167 {
168 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
169 return pair;
170 }
171 return null;
172 }
173
174 /// <summary>
175 /// Parses a comma-separated list of atoms.
176 /// </summary>
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)
181 {
182 return null;
183 }
184
185 /// <summary>
186 /// Parses a whitespace-separated list of atoms.
187 /// </summary>
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)
191 {
192 List<string> list = new List<string>();
193
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)
197 {
198 Console.WriteLine("matched: " + match.Value);
199 list.Add(match.Value);
200 }
201
202 return list.ToArray();
203 }
204 }
205 }
This page took 0.042233 seconds and 4 git commands to generate.