As mentioned in my previous post, I’ve been putting together the code samples for C# in Depth. Now, these are spread across several projects in a few solutions. They’re referred to in the book as things like “Listing 6.2” but I’ve given the files “real” names in the projects. When you run any project with multiple executable listings in it, the project offers you options of which listing to run, showing both the type name and the listing, which is embedded using System.ComponentModel.DescriptionAttribute
, e.g. [Description("Listing 12.4")]
. A few listings have a description which is more than just “Listing x.y” – for instance, “Listing 6.1/6.2/6.3”. These are displayed with no problems.
Now, the problem for the reader would be finding a particular listing – it’s not always obvious from the code what the type name should be, particularly when there are many variations on a theme. Clearly some sort of map is required. Ideally it should be a file looking something like this:
Chapter 1: 1.1: Foo.cs 1.2: Bar.cs 1.3/1.4: Baz.cs Chapter 2: 2.1: Gronkle.cs
It’s easy enough to work out the directory for any particular file – the projects are helpfully named “Chapter3” and the like. So, the next thing is to create this file. I really didn’t want to do that by hand. After all, there are about 150 listings in the book – and I’ve already done the work of attributing them all. Ah… we could do it programmatically. Sounds like a bit of a slog…
… but it’s a problem which is ideally suited to LINQ. It’s also fairly ideally suited to regular expressions, much as I hate to admit it. The regular expression in question is reasonably complex, but thanks to Jesse Houwing’s advice on adding comments to regular expressions, the results aren’t too bad. Here’s the finished code – which of course is part of the downloadable source code itself.
using System; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace Chapter11.Queries { /// <summary> /// The listings are scattered around .cs files within various directories. /// This class uses LINQ to find all classes with a suitable Description /// attribute, groups them by chapters and orders them by chapter and listing number. /// </summary> class DisplayListingsMap { static readonly Regex ListingPattern = new Regex( @"# First match the start of the attribute, up to the bit we're interested in \[Description\(\""Listing\ # The 'text' group is the whole of the description after Listing (?<text> # The 'chapter' group is the first set of digits in the description, before a dot (?<chapter>\d+)\. # The chapter group is the second set of digits in the description (?<listing>\d+) # After that we don't care - stop the 'text' group at the double quote [^\""]*) # Now match the end of the attribute \""\)\]", RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace); static void Main() { DirectoryInfo directory = new DirectoryInfo(@"..\..\..\.."); var query = from file in directory.GetFiles("*.cs", SearchOption.AllDirectories) let match = ListingPattern.Match(File.ReadAllText(file.FullName)) where match.Success let Details = new { Text = match.Groups["text"].Value, Chapter = int.Parse(match.Groups["chapter"].Value), Listing = int.Parse(match.Groups["listing"].Value) } orderby Details.Chapter, Details.Listing group new { File = file, Description=Details.Text } by Details.Chapter; foreach (var chapter in query) { Console.WriteLine("Chapter {0}", chapter.Key); foreach (var listing in chapter) { Console.WriteLine("{0}: {1}", listing.Description, listing.File.Name); } Console.WriteLine(); } } } } |
Isn’t it cool? The regex works out the listing number (first x.y part only) and sorts on that, grouping by chapter – then we just display the results. There are other ways of skinning the same cat – such as grouping and then ordering “inside” and “outside” a chapter separately – but they’ll all boil down to the same sort of thing.
Jon,
Agreed, it is *very* cool, mostly because of the first two letters of the acronym, which stand for “Language Integrated”. That, to me, is what makes it all sing.