X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FMono.C5%2FUserGuideExamples%2FFileindex.cs;h=e95313c4f9fab5f7ed326156708ffc28340d1809;hb=39fac8ab1c15fb397ad8e29cc300c5185bdc7dfc;hp=e609031ce8707b50b64d3197d2f845a1ae7c7c4f;hpb=75df74a96c33af7a99e16e4281272f1b67334a48;p=mono.git diff --git a/mcs/class/Mono.C5/UserGuideExamples/Fileindex.cs b/mcs/class/Mono.C5/UserGuideExamples/Fileindex.cs index e609031ce87..e95313c4f9f 100644 --- a/mcs/class/Mono.C5/UserGuideExamples/Fileindex.cs +++ b/mcs/class/Mono.C5/UserGuideExamples/Fileindex.cs @@ -1,82 +1,82 @@ -/* - Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ - -// C5 example: File index: read a text file, build and print a list of -// words and the line numbers (without duplicates) on which they occur. - -// Compile with -// csc /r:C5.dll Fileindex.cs - -using System; // Console -using System.IO; // StreamReader, TextReader -using System.Text.RegularExpressions; // Regex -using C5; // IDictionary, TreeDictionary, TreeSet - -namespace FileIndex -{ - class Fileindex - { - static void Main(String[] args) - { - if (args.Length != 1) - Console.WriteLine("Usage: Fileindex \n"); - else - { - IDictionary> index = IndexFile(args[0]); - PrintIndex(index); - } - } - - static IDictionary> IndexFile(String filename) - { - IDictionary> index = new TreeDictionary>(); - Regex delim = new Regex("[^a-zA-Z0-9]+"); - using (TextReader rd = new StreamReader(filename)) - { - int lineno = 0; - for (String line = rd.ReadLine(); line != null; line = rd.ReadLine()) - { - String[] res = delim.Split(line); - lineno++; - foreach (String s in res) - if (s != "") - { - if (!index.Contains(s)) - index[s] = new TreeSet(); - index[s].Add(lineno); - } - } - } - return index; - } - - static void PrintIndex(IDictionary> index) - { - foreach (String word in index.Keys) - { - Console.Write("{0}: ", word); - foreach (int ln in index[word]) - Console.Write("{0} ", ln); - Console.WriteLine(); - } - } - } +/* + Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +// C5 example: File index: read a text file, build and print a list of +// words and the line numbers (without duplicates) on which they occur. + +// Compile with +// csc /r:C5.dll Fileindex.cs + +using System; // Console +using System.IO; // StreamReader, TextReader +using System.Text.RegularExpressions; // Regex +using C5; // IDictionary, TreeDictionary, TreeSet + +namespace FileIndex +{ + class Fileindex + { + static void Main(String[] args) + { + if (args.Length != 1) + Console.WriteLine("Usage: Fileindex \n"); + else + { + IDictionary> index = IndexFile(args[0]); + PrintIndex(index); + } + } + + static IDictionary> IndexFile(String filename) + { + IDictionary> index = new TreeDictionary>(); + Regex delim = new Regex("[^a-zA-Z0-9]+"); + using (TextReader rd = new StreamReader(filename)) + { + int lineno = 0; + for (String line = rd.ReadLine(); line != null; line = rd.ReadLine()) + { + String[] res = delim.Split(line); + lineno++; + foreach (String s in res) + if (s != "") + { + if (!index.Contains(s)) + index[s] = new TreeSet(); + index[s].Add(lineno); + } + } + } + return index; + } + + static void PrintIndex(IDictionary> index) + { + foreach (String word in index.Keys) + { + Console.Write("{0}: ", word); + foreach (int ln in index[word]) + Console.Write("{0} ", ln); + Console.WriteLine(); + } + } + } } \ No newline at end of file