/** * @brief Try to parse provided text as BBCode descriptor. * @param lineText String to parse. Possible BB descriptor lines: * ~~~~ * 1. printf("aZ...igored...//@BB Hello World! * 2. puts("mm...igored...//@BB Measured value is {0}. * 3. ...igored...@BB[tm] Time is {0}:{1} {2:(0),(1)AM,(2)PM} and today is {3:Mon,Tue,Wed,Thu,Fri,Sat,Sun}. * ~~~~ * @param sourceFile the file name where this line is taken. * @param sourceFileLine line number of this text. * @return BBCodeObject if descriptor found, null otherwise. */ public static BBCodeObject Parse(string lineText, string sourceFile, int sourceFileLine) { BBCodeObject bbCode = null; Match match; // Regexp matcher for printf and puts style descriptors. match = Regex.Match(lineText, @"(?:printf\(""|puts\("")([a-zA-Z]{2}).*//@BB[\s]+(.*)[\s]*$"); // If above match fails, try to match //@BB[..] style descriptors. if (!match.Success) { match = Regex.Match(lineText, @"@BB\[([a-zA-Z]{2})\][\s]+(.*)[\s]*$"); } // Check for success. if (match.Success) { // Get the shortcode and format string. string shortCode = match.Groups[1].Captures[0].Value; string formatString = match.Groups[2].Captures[0].Value; // Create a BBCodeObject. bbCode = new BBCodeObject(shortCode, formatString, sourceFile, sourceFileLine); } // Return. return(bbCode); }
/** * @brief Scans a project location of file and fills the BBCodeBase. * @param fileName is path to a directory or a single file. * @param bbCodeBase clears all entries in this object and adds the new entries. * @return true on successful scan. */ private static bool ScanSingleFile(string fileName, ref BBCodeBase bbCodeBase) { try { // Open the file as text. StreamReader sr = File.OpenText(fileName); // Iterate all lines until the end. string lineText; int lineNumber = 0; while (!sr.EndOfStream) { // Read single line. lineText = sr.ReadLine(); lineNumber++; // Try to parse the line with descriptor parser. Result is not null if successful. BBCodeObject bbCode = BBCodeDescriptorParser.Parse(lineText, fileName, lineNumber); // Add to the database (only if not null). bbCodeBase.AddBBCode(bbCode); } // Close the file. sr.Close(); // Everything is OK. return(true); } catch { } // Problem occured. return(false); }
private Dictionary <string, BBCodeObject> dataBase = new Dictionary <string, BBCodeObject>(); ///< Keeps all identified BBCode descriptors and searching by shortCode possible. /** * @brief Method to add new BBCodeObject into the dictionary. * @param bbCode The new object to add (if it is not null). * @return false if bbCode is null or duplicate is found with different format. true otherwise. */ public bool AddBBCode(BBCodeObject bbCode) { // Return false if bbCode is null. if (bbCode == null) { return(false); } // Search the shortCode in the map. if (dataBase.ContainsKey(bbCode.shortCode)) { // If the shortCode is already in the dictionary, compare the formatStrings. if (dataBase[bbCode.shortCode].formatString != bbCode.formatString) { // If format strings mismatch, give a warning and keep the first added one. Console.WriteLine("WARNING: BB[" + bbCode.shortCode + "] defined in " + dataBase[bbCode.shortCode].sourceFile + ":" + dataBase[bbCode.shortCode].sourceFileLine + " has redefined in " + bbCode.sourceFile + ":" + bbCode.sourceFileLine); return(false); } } else { // Add the bbCode to the dictionary. dataBase[bbCode.shortCode] = bbCode; } // Return true. return(true); }