Click here to Skip to main content
Click here to Skip to main content

RTools.Util - C# Library

, 26 Oct 2004
Rate this:
Please Sign up or sign in to vote.
A C# Library for tokenizing, argument parsing, and file finding.

Introduction

RTools.Util is a small C# library containing some utility classes. The primary classes are:

  • StreamTokenizer: A stream tokenizer like Java's which can be useful for parsing text. This takes text (from a file, string, etc) in a stream and produces a list of Token objects. The Token objects are subclassed to provide type information. Some Token subclasses are WordToken, CharToken, WhitespaceToken, QuoteToken, CommentToken, FloatToken, etc.
  • Opts: A command-line option parsing class similar to the perl/unix getopts.
  • Finder: A small "find" utility class for producing lists of files under a particular directory. I created this to use in zipping up this package.
  • HighResClock: A wrapper for Kernel32.dll's high resolution clock API. This allows you time information with resolution greater than DateTime.Now's, which is machine dependent but typically 10 or 15 ms. The effective resolution of HighResClock on a P4 1.4GHz is about 10us.
  • SoftwarePackage: This class provides some utility methods to do things list installed software packages, etc. This is Windows-only.

Using the code

I've included an Ndoc-generated .chm file in the source download. It contains a lot more information. The information and samples here are a subset of what's in the .chm.

Here are a couple example uses of StreamTokenizer which illustrate the basics:

// one Token at a time
StreamTokenizer tokenizer = new StreamTokenizer();
tokenizer.TextReader = File.OpenText(fileName);
tokenizer.GrabWhitespace = true;
tokenizer.Verbosity = VerbosityLevel.Debug; // just for debugging
Token token;
while (NextToken(out token))
{
     if (token == '{') // do something
     ...
}

This use of StreamTokenizer shows all-at-once tokenization, which trades off performance (tokenize everything even if you aren't going to use all the tokens, and memory) for ease of programming:

// all at once Tokenize
StreamTokenizer tokenizer = new StreamTokenizer();
tokenizer.Settings.ParseNumbers = true;
ArrayList tokens = new ArrayList();
if (!tokenizer.TokenizeString("some string", tokens)) 
{ 
    // error handling
}
foreach (Token t in tokens) Console.WriteLine("t = {0}", t);

Here's an example use of Opts:

Opts opts = new Opts();
opts.ParseSpec = "file=s, type:s, v, d");
 - or -
opts.UsageSpec = "-file fileName -type [typeName] [-v|-d]";
if (!opts.Parse(args, out errorMessage)) { // display error message and usage }
if (opts.Options.ContainsKey("v")) // -v was specified
if (!opts.Options.ContainsKey("file")) { error... // need -file specified }
Console.WriteLine("-file specified is {0}", opts.Options["file"]);

Finder has a Main() which I use for a simple command-line find. Here's an example use of Finder's other methods:

// setup args
string dirName = ".";
ArrayList regexps = new ArrayList();
regexps.Add("*.cs");
regexps.Add("*.csproj");
SortedList list = new SortedList();

// do the find
Finder.FindFiles(dirName, regexps, ref list, false);
Finder.AddParents(list);

// display the results
foreach(DictionaryEntry entry in list)
{
    string s = (string)entry.Key;
    Console.WriteLine(s);
}

Points of Interest

There's some chance that the version on www.rseghers.name/codeAndUtils.htm is more up to date.

I had high hopes of keeping StreamTokenizer's tokenization state machine very clean, but the number (floating point) parse forced me into some complexity. I'd be happy to hear better strategies.

It might be better to use a parser-generator (I see that Mono has one, and there's another article on CodeProject that used it) for parsing. The main advantage of StreamTokenizer , I believe, is that there's a smaller learning curve to use it.

I haven't included an AssemblyInfo.cs file that doesn't reference my private key (.snk) file. I am not including my private key file, and therefore the project I've included won't build as is. You have to remove the reference to my .snk file from AssemblyInfo.cs to get it to build. Sorry, I don't like the idea of having multiple .csproj and AssemblyInfo.cs files, and haven't found a more elegant solution.

History

  • Nov 5 2002 - First CodeProject submission.
  • Nov 16 2002 - First CodeProject download.
  • Jan 27 2003 - Update.
  • Oct 22 2004 - Update library to latest version (see changelog.txt in the download). Update this web page accordingly.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Ryan Seghers

United States United States
No Biography provided

Comments and Discussions

 
GeneralHighResClock.Now may need adjustment Pinmemberwlgrinfeld@yahoo.com13-Sep-06 4:31 
GeneralRe: HighResClock.Now may need adjustment PinmemberRyan Seghers27-Sep-06 13:12 
Questionfile open/close problem? PinmemberK-Bob30-Aug-06 4:49 
AnswerRe: file open/close problem? PinmemberRyan Seghers27-Sep-06 13:02 
GeneralUsing Other packages in C# PinmemberR.Premkumar1-Feb-06 19:45 
GeneralAnother option if you're doing command line parsing PinmemberAndrew S. Townley2-Dec-04 0:34 
Generalthere have no words in "RTools.Utils.chm" file Pinmemberpowerfulchen21-Dec-03 19:03 
GeneralRe: there have no words in "RTools.Utils.chm" file PinmemberFrankie Leung28-Oct-04 19:12 
Generalthanks for great job! Pinmemberobjet2117-Mar-03 16:17 
GeneralJust what I needed Pinmemberdog_spawn10-Mar-03 14:18 
GeneralThanks again Pinmemberdog_spawn22-Aug-03 8:22 
GeneralFailure to compile... PinmemberRSYOUNG16-Feb-03 4:57 
GeneralRe: Failure to compile... PinmemberRyan Seghers16-Feb-03 17:42 
GeneralRe: Failure to compile... PinmemberRSYOUNG17-Feb-03 3:45 
GeneralGreat job! PinmemberLuis Alonso Ramos27-Jan-03 19:15 
Looks very interesting to me, I'll take a look at it tomorrow, it may help for a school assignment (I need to open and display PGM (portable graymaps) images).
 
I'll let you know my comments on it.
 
Now I'm going to sleep...
 
-- LuisR
 
──────────────
  Luis Alonso Ramos
  Chihuahua, Mexico
  www.luisalonsoramos.com
 
"Do not worry about your difficulties in mathematics, I assure you that mine are greater." -- Albert Einstein

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 27 Oct 2004
Article Copyright 2002 by Ryan Seghers
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid