]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Parse.cs
169cf30a7b8340b59af9aa61aae521c9e7c04ad8
[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 default(T) if parsing failed.</returns>
105 public static T Constant<T>(string atom)
106 {
107 try
108 {
109 return (T)System.Enum.Parse(typeof(T), String(atom), true);
110 }
111 #pragma warning disable 0168
112 catch (System.Exception ex)
113 #pragma warning restore 0168
114 {
115 return default(T);
116 }
117 }
118
119 /// <summary>
120 /// Parses an integer.
121 /// </summary>
122 /// <param name="atom">Text.</param>
123 /// <returns>The integer, or null if parsing failed.</returns>
124 public static int? Integer(string atom)
125 {
126 try
127 {
128 int integer = Convert.ToInt32(atom.Trim());
129 return integer;
130 }
131 #pragma warning disable 0168
132 catch (System.Exception ex)
133 #pragma warning restore 0168
134 {
135 return null;
136 }
137 }
138
139 /// <summary>
140 /// Parses a boolean value.
141 /// </summary>
142 /// <param name="atom">Text.</param>
143 /// <returns>True or false, or null if parsing failed.</returns>
144 public static bool? Boolean(string atom)
145 {
146 Match match = Regex.Match(atom, @"^\s*(true|false)\s*$", RegexOptions.IgnoreCase);
147 if (match.Success)
148 {
149 if (match.Groups[1].Value[0] == 't' || match.Groups[1].Value[0] == 'T') return true;
150 else return false;
151 }
152 return null;
153 }
154
155 /// <summary>
156 /// Parses a function.
157 /// </summary>
158 /// <param name="atom">Text.</param>
159 /// <returns>An array two strings containing the function name and
160 /// parameter-list, in that order, or null if parsing failed.</returns>
161 public static string[] Function(string atom)
162 {
163 Match match = Regex.Match(atom, @"^\s*(\w+)\((.*)\)\s*$");
164 if (match.Success)
165 {
166 string[] pair = { match.Groups[1].Value, match.Groups[2].Value };
167 return pair;
168 }
169 return null;
170 }
171
172 /// <summary>
173 /// Parses a whitespace-separated list of atoms.
174 /// </summary>
175 /// <param name="text">Text.</param>
176 /// <returns>An array of atoms, or null if parsing failed.</returns>
177 public static string[] List(string text)
178 {
179 List<string> list = new List<string>();
180
181 MatchCollection matches = Regex.Matches(text, @"\s*("".*?"")|(\w+\(.*?\))|(\[.*?\])|(<.*?>)|(\S+)(?:\s+|$)");
182 // FIXME: This may barf all over itself if there are nested parentheses, doublequotes, brackets, etc.
183 foreach (Match match in matches)
184 {
185 Console.WriteLine("matched: " + match.Value);
186 list.Add(match.Value);
187 }
188
189 return list.ToArray();
190 }
191 }
192 }
This page took 0.035463 seconds and 3 git commands to generate.