]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/Parse.cs
git-svn-id: https://bd85.net/svn/cs3505_group@168 92bb83a3-7c8f-8a45-bc97-515c4e399668
[chaz/carfire] / CarFire / CarFire / CarFire / Parse.cs
index e1a482eb6a08c55398650186bdefd3f45d9fd527..ca98353067cf4f55e0ad499aa235082d1e4e32a7 100644 (file)
@@ -9,7 +9,8 @@ namespace CarFire
 {\r
     /// <summary>\r
     /// Class with handy static methods taking strings and returning objects\r
-    /// parsed from those strings.\r
+    /// parsed from those strings.  For all of these functions, white space is\r
+    /// generally ignored, but superfluous characters are not allowed.\r
     /// </summary>\r
     public class Parse\r
     {\r
@@ -25,6 +26,18 @@ namespace CarFire
             return null;\r
         }\r
 \r
+        /// <summary>\r
+        /// Parses a comment of an INI file.\r
+        /// </summary>\r
+        /// <param name="line">Text.</param>\r
+        /// <returns>The comment, or null if parsing failed.</returns>\r
+        public static string IniComment(string line)\r
+        {\r
+            Match match = Regex.Match(line, @"^;\s*(.*)\s*$");\r
+            if (match.Success) return match.Groups[1].Value;\r
+            return null;\r
+        }\r
+\r
         /// <summary>\r
         /// Parses a key-value pair.\r
         /// </summary>\r
@@ -96,26 +109,36 @@ namespace CarFire
             return null;\r
         }\r
 \r
+        /// <summary>\r
+        /// Parses a single character.\r
+        /// </summary>\r
+        /// <param name="atom">Text.</param>\r
+        /// <returns>The character, or null if parsing failed.</returns>\r
+        public static char? Char(string atom)\r
+        {\r
+            string str = String(atom);\r
+            if (str != null && str.Length == 1) return str[0];\r
+            return null;\r
+        }\r
+\r
         /// <summary>\r
         /// Parses a constant from an enum.\r
         /// </summary>\r
         /// <typeparam name="T">An enumeration.</typeparam>\r
         /// <param name="atom">Text.</param>\r
         /// <returns>The constant, or null if parsing failed.</returns>\r
-        public static T Constant<T>(string atom)\r
+        public static T? Constant<T>(string atom) where T : struct\r
         {\r
-            string constant = String(atom);\r
-            if (constant != null)\r
+            try\r
             {\r
-                foreach (string enumConstant in System.Enum.GetNames(typeof(T)))\r
-                {\r
-                    if (constant == enumConstant)\r
-                    {\r
-                        return (T)System.Enum.Parse(typeof(T), constant);\r
-                    }\r
-                }\r
+                return (T)System.Enum.Parse(typeof(T), String(atom));\r
+            }\r
+#pragma warning disable 0168\r
+            catch (System.Exception ex)\r
+#pragma warning restore 0168\r
+            {\r
+                return null;\r
             }\r
-            return default(T);\r
         }\r
 \r
         /// <summary>\r
@@ -158,7 +181,7 @@ namespace CarFire
         /// Parses a function.\r
         /// </summary>\r
         /// <param name="atom">Text.</param>\r
-        /// <returns>An array two strings containing the function name and\r
+        /// <returns>An array with two strings containing the function name and\r
         /// parameter-list, in that order, or null if parsing failed.</returns>\r
         public static string[] Function(string atom)\r
         {\r
@@ -171,17 +194,6 @@ namespace CarFire
             return null;\r
         }\r
 \r
-        /// <summary>\r
-        /// Parses a comma-separated list of atoms.\r
-        /// </summary>\r
-        /// <param name="text">Text.</param>\r
-        /// <returns>An array of strings containing the atoms, or null\r
-        /// if parsing failed.</returns>\r
-        public static string[] ParameterList(string text)\r
-        {\r
-            return null;\r
-        }\r
-\r
         /// <summary>\r
         /// Parses a whitespace-separated list of atoms.\r
         /// </summary>\r
@@ -195,7 +207,6 @@ namespace CarFire
             // FIXME: This may barf all over itself if there are nested parentheses, doublequotes, brackets, etc.\r
             foreach (Match match in matches)\r
             {\r
-                Console.WriteLine("matched: " + match.Value);\r
                 list.Add(match.Value);\r
             }\r
 \r
This page took 0.023642 seconds and 4 git commands to generate.