# NewGamePhysics.Utilities.Scanf Class Reference

C# sscanf simulator. Original idea from Scanner.cs by Geoffrey Slinker. More...

List of all members.

## Public Member Functions
| Scanf ()
| Constructor for Scanf object. Initializes the pattern tables.
object[] | Scan (string text, string format)
| Parses a text using a scanf-like format representation into an array of objects.
## Static Public Attributes
static bool | EnableTrace = false
| Enables trace logging when set to true. Used for debugging.
## Protected Attributes
readonly Hashtable | patterns
| List of scanf patterns with corresponding Regexp strings.
readonly Hashtable | types
| List of types for each scanf pattern.

## Detailed Description

C# sscanf simulator. Original idea from Scanner.cs by Geoffrey Slinker.

Definition at line 44 of file Scanf.cs.

## Constructor & Destructor Documentation

NewGamePhysics.Utilities.Scanf.Scanf | ( |  | ) |

Constructor for Scanf object. Initializes the pattern tables.

Definition at line 123 of file Scanf.cs.

    00124         {
    00125             // New format-to-regexp pattern table
    00126             patterns = new Hashtable();
    00127
    00128             // base-types
    00129             patterns.Add("%d", integerPattern);
    00130             patterns.Add("%i", integerPattern);
    00131             patterns.Add("%o", octalIntegerPattern);
    00132             patterns.Add("%u", integerPattern);
    00133             patterns.Add("%x", hexadecimalIntegerPattern);
    00134             patterns.Add("%X", hexadecimalIntegerPattern);
    00135             patterns.Add("%f", floatingPointPattern);
    00136             patterns.Add("%g", floatingPointPattern);
    00137             patterns.Add("%G", floatingPointPattern);
    00138             patterns.Add("%s", stringPattern);
    00139             patterns.Add("%c", characterPattern);
    00140
    00141             // long-types
    00142             patterns.Add("%lf", floatingPointPattern);
    00143             patterns.Add("%lg", floatingPointPattern);
    00144             patterns.Add("%lG", floatingPointPattern);
    00145             patterns.Add("%ld", integerPattern);
    00146             patterns.Add("%li", integerPattern);
    00147             patterns.Add("%lo", octalIntegerPattern);
    00148             patterns.Add("%lu", integerPattern);
    00149             patterns.Add("%lx", hexadecimalIntegerPattern);
    00150             patterns.Add("%lX", hexadecimalIntegerPattern);
    00151
    00152             // short-types
    00153             patterns.Add("%hd", integerPattern);
    00154             patterns.Add("%hi", integerPattern);
    00155             patterns.Add("%ho", octalIntegerPattern);
    00156             patterns.Add("%hu", integerPattern);
    00157             patterns.Add("%hx", hexadecimalIntegerPattern);
    00158             patterns.Add("%hX", hexadecimalIntegerPattern);
    00159
    00160             // Skips
    00161             patterns.Add(@"%\*d", integerPattern);
    00162             patterns.Add(@"%\*i", integerPattern);
    00163             patterns.Add(@"%\*o", octalIntegerPattern);
    00164             patterns.Add(@"%\*u", integerPattern);
    00165             patterns.Add(@"%\*x", hexadecimalIntegerPattern);
    00166             patterns.Add(@"%\*X", hexadecimalIntegerPattern);
    00167             patterns.Add(@"%\*f", floatingPointPattern);
    00168             patterns.Add(@"%\*g", floatingPointPattern);
    00169             patterns.Add(@"%\*G", floatingPointPattern);
    00170             patterns.Add(@"%\*s", stringPattern);
    00171             patterns.Add(@"%\*c", characterPattern);
    00172
    00173             patterns.Add(@"%\*lf", floatingPointPattern);
    00174             patterns.Add(@"%\*lg", floatingPointPattern);
    00175             patterns.Add(@"%\*lG", floatingPointPattern);
    00176             patterns.Add(@"%\*ld", integerPattern);
    00177             patterns.Add(@"%\*li", integerPattern);
    00178             patterns.Add(@"%\*lo", octalIntegerPattern);
    00179             patterns.Add(@"%\*lu", integerPattern);
    00180             patterns.Add(@"%\*lx", hexadecimalIntegerPattern);
    00181             patterns.Add(@"%\*lX", hexadecimalIntegerPattern);
    00182
    00183             patterns.Add(@"%\*hd", integerPattern);
    00184             patterns.Add(@"%\*hi", integerPattern);
    00185             patterns.Add(@"%\*ho", octalIntegerPattern);
    00186             patterns.Add(@"%\*hu", integerPattern);
    00187             patterns.Add(@"%\*hx", hexadecimalIntegerPattern);
    00188             patterns.Add(@"%\*hX", hexadecimalIntegerPattern);
    00189
    00190             // New format-to-parse type table
    00191             types = new Hashtable();
    00192
    00193             types.Add("%d", ParseType.Int32);
    00194             types.Add("%i", ParseType.Int32);
    00195             types.Add("%o", ParseType.UInt32Oct);
    00196             types.Add("%u", ParseType.UInt32);
    00197             types.Add("%x", ParseType.UInt32Hex);
    00198             types.Add("%X", ParseType.UInt32Hex);
    00199             types.Add("%f", ParseType.Single);
    00200             types.Add("%g", ParseType.Single);
    00201             types.Add("%G", ParseType.Single);
    00202             types.Add("%s", ParseType.String);
    00203             types.Add("%c", ParseType.Char);
    00204
    00205             types.Add("%lf", ParseType.Double);
    00206             types.Add("%lg", ParseType.Double);
    00207             types.Add("%lG", ParseType.Double);
    00208             types.Add("%ld", ParseType.Int64);
    00209             types.Add("%li", ParseType.Int64);
    00210             types.Add("%lo", ParseType.UInt64Oct);
    00211             types.Add("%lu", ParseType.UInt64);
    00212             types.Add("%lx", ParseType.UInt64Hex);
    00213             types.Add("%lX", ParseType.UInt64Hex);
    00214
    00215             types.Add("%hd", ParseType.Int16);
    00216             types.Add("%hi", ParseType.Int16);
    00217             types.Add("%ho", ParseType.UInt16Oct);
    00218             types.Add("%hu", ParseType.UInt16);
    00219             types.Add("%hx", ParseType.UInt16Hex);
    00220             types.Add("%hX", ParseType.UInt16Hex);
    00221
    00222             // New match evaluator
    00223             matchEvaluator = new MatchEvaluator(ReplaceFormatCode);
    00224         }

## Member Function Documentation

object [] NewGamePhysics.Utilities.Scanf.Scan | ( | string | text,
|  | string | format |
| ) |  |  |

Parses a text using a scanf-like format representation into an array of objects.

Parameters:

  | text | Test to parse.
  | format | Format string to use for parsing.

Returns:
  Array of objects from parse. One element for each recognized pattern. null elements for skipped patterns.

Definition at line 241 of file Scanf.cs.

    00242         {
    00243             // Check if we need to update our regular expression for the format
    00244             if ((String.IsNullOrEmpty(lastFormat)) ||
    00245                 (lastFormat != format))
    00246             {
    00247                 // Convert format string into pattern
    00248                 string formatPattern = format;
    00249
    00250                 // Multiple whitespaces reduce to single whitespace match
    00251                 formatPattern = Regex.Replace(formatPattern, @"\s+", @"\s+");
    00252
    00253                 // Type names are individualized with a counter
    00254                 // The count is updated inside the match evaluator.
    00255                 typeCount = 0;
    00256
    00257                 // Maybe rebuild format pattern
    00258                 foreach (string formatCode in patterns.Keys)
    00259                 {
    00260                     formatPattern = Regex.Replace(
    00261                         formatPattern,
    00262                         formatCode,
    00263                         matchEvaluator);
    00264                 }
    00265                 Trace("scanf_trace: format_pattern: ", formatPattern);
    00266
    00267                 // New regular expression for format
    00268                 conversionRegex = new Regex(formatPattern);
    00269
    00270                 // Cache format for reuse tracking
    00271                 lastFormat = format;
    00272             }
    00273
    00274             // Get the array of group names
    00275             string[] groupNames = conversionRegex.GetGroupNames();
    00276
    00277             // Initalize return array
    00278             List<object> targets = new List<object>();
    00279
    00280             if (groupNames.Length > 0)
    00281             {
    00282                 // Match pattern
    00283                 MatchCollection matchCollection = conversionRegex.Matches(text);
    00284                 foreach (Match match in matchCollection)
    00285                 {
    00286                     if (match.Success)
    00287                     {
    00288                         foreach (string groupName in groupNames)
    00289                         {
    00290                             if (groupName.Contains(seperator[0]))
    00291                             {
    00292                                 // Part until "_" is the type
    00293                                 string typeName = groupName.Remove(groupName.LastIndexOfAny(seperator));
    00294                                 ParseType parseType = (ParseType)Enum.Parse(typeof(ParseType), typeName);
    00295
    00296                                 // Get the value to convert
    00297                                 string valueText = match.Groups[groupName].Value;
    00298
    00299                                 // Try to parse string based on value
    00300                                 object target = null;
    00301                                 try
    00302                                 {
    00303                                     target = Parse(parseType, valueText);
    00304                                 }
    00305                                 catch (Exception e)
    00306                                 {
    00307                                     Trace("scanf_trace: exception:", e.ToString());
    00308                                 }
    00309                                 finally
    00310                                 {
    00311                                     targets.Add(target);
    00312                                 }
    00313
    00314                                 // Terminate early if we have the number of types
    00315                                 // found in the earlier scan
    00316                                 if (targets.Count == typeCount)
    00317                                 {
    00318                                     return targets.ToArray();
    00319                                 }
    00320                             }
    00321                         }
    00322                     }
    00323                 }
    00324             }
    00325
    00326             // Return what we've got
    00327             return targets.ToArray();
    00328         }

## Member Data Documentation

bool NewGamePhysics.Utilities.Scanf.EnableTrace = false`[static]`

Enables trace logging when set to true. Used for debugging.

Definition at line 95 of file Scanf.cs.

readonly Hashtable NewGamePhysics.Utilities.Scanf.patterns`[protected]`

List of scanf patterns with corresponding Regexp strings.

Definition at line 84 of file Scanf.cs.

readonly Hashtable NewGamePhysics.Utilities.Scanf.types`[protected]`

List of types for each scanf pattern.

Definition at line 89 of file Scanf.cs.

The documentation for this class was generated from the following file:

- C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/Utilities/Scanf.cs