Click here to Skip to main content
15,895,833 members
Articles / Security / Encryption

Enhanced String Handling II

Rate me:
Please Sign up or sign in to vote.
3.76/5 (11 votes)
16 Dec 2010CPOL2 min read 36.3K   213   15  
Overcoming limitations of: Enhanced String Handling
using System;
using System.Text.RegularExpressions;
using System.IO;
using EnhancedStringEvaluate;


namespace TestEvaluation.ProcessEvaluate
{
	/// <summary>
	/// Processes a directory config-variable -- CurrentDir, {CurrentDir::} or {CurrentDir::-n} construct.
	/// </summary>
	public sealed class ProcessCurrentDir : IProcessEvaluate
	{
		private readonly Regex _reCurrDir;

		public ProcessCurrentDir() : this(DelimitersAndSeparator.DefaultDelimitedString) { }
		public ProcessCurrentDir(IDelimitersAndSeparator delim)
		{
			//string pattern = @"({)\s*CurrentDir\s*::\s*(-\s*[0-9]+)?(})";
			string pattern = string.Format(@"({0})\s*CurrentDir\s*::\s*(?<prevCount>-\s*[0-9]+)?({1})", delim.OpenDelimEquivalent, delim.CloseDelimEquivalent);
			RegexOptions reo = RegexOptions.Singleline | RegexOptions.IgnoreCase;
			_reCurrDir = new Regex(pattern, reo);

			CurrentDir = null;
		}

		/// <summary>
		/// Tracks the directory path we are "currently" in.
		/// </summary>
		public string CurrentDir { get; set; }

		#region IProcessEvaluate Members

		public void Evaluate(object src, EnhancedStringEventArgs ea)
		{
			// Initialize return code
			ea.IsHandled = false;

			// Evaluate Task 1
			string text = ea.EhancedPairElem.Value;
			if (string.IsNullOrWhiteSpace(text)) return;
			bool rc = _reCurrDir.IsMatch(text);
			if (!rc) return;

			// Get the replacement current directory
			string replacement = _reCurrDir.Replace(text, CurrentDirReplace);
			if (replacement == text) return;

			// Evaluate Task 2: Announce that we succeeded to find {CurrentDir} and replaced it
			ea.IsHandled = true;

			// Evaluate Task 3: Keep the new value
			ea.EhancedPairElem.Value = replacement;
			return;
		}

		#endregion

		private string CurrentDirReplace(Match m)
		{
			string prevCount = m.Groups["prevCount"].Value;
			if (string.IsNullOrEmpty(prevCount))
				return CurrentDir;

			int cnt;
			bool rc = int.TryParse(prevCount, out cnt);
			if (!rc) return string.Format(@"--\\no path at the given count: {0}\\--", prevCount);

			if (cnt == 0) return CurrentDir;

			string prevDir = CurrentDir;
			for (int i = 0; i > cnt; --i)
			{
				prevDir = Path.GetDirectoryName(prevDir);
				if (prevDir == null) return string.Format(@"--\\no path at given count: {0}\\--", prevCount);
			}

			return prevDir;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
avifarah@gmail.com

Comments and Discussions