Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

mlsystem.cc

Go to the documentation of this file.
00001 
00025 #include <stdio.h>
00026 #include "magic/mlsystem.h"
00027 
00028 
00029 
00031 //                                                                           //
00032 //                |      ----                                                //
00033 //                |     |          ___               ___                     //
00034 //                |     | --- |/\  ___| |/|/| |/|/|  ___| |/\                //
00035 //                |     |   \ |   (   | | | | | | | (   | |                  //
00036 //                |____ |___/ |    \__| | | | | | |  \__| |                  //
00037 //                                                                           //
00039 
00040 void LGrammar::addRule (const String& left, const String& right) {
00041     rules.add (new LRule (left, right));
00042 }
00043 
00044 void LGrammar::applyTo (String& str, int times) const {
00045     for (int t=0; t<times; t++) {
00046         String nextstr = "";
00047         // Scan through the string
00048         for (uint pos=0; pos < str.length(); pos++) {
00049             // Try to match each rule to this position
00050             bool matched = false;
00051             for (int rule=0; rule<rules.size(); rule++) {
00052                 const String& left = rules[rule].left;
00053                 if (str.mid (pos, left.length()) == left) {
00054                     // Matched. Now replace.
00055                     nextstr += rules[rule].right;
00056                     matched = true;
00057                     break;
00058                 }
00059             }
00060             
00061             // If no match, just copy the character
00062             if (!matched)
00063                 nextstr += str.mid (pos, 1);
00064         }
00065         str = nextstr;
00066     }
00067 }
00068 

Generated on Thu Feb 10 20:06:42 2005 for LibMagiC by doxygen1.2.18