Click here to Skip to main content
15,894,646 members
Articles / General Programming / String

Enhanced String Handling

Rate me:
Please Sign up or sign in to vote.
4.78/5 (16 votes)
16 Dec 2010CPOL28 min read 45.6K   338   38  
Allow for constructs within a string to be programmatically evaluated for a final string value
using System;
using System.Text.RegularExpressions;
using System.IO;
using EnhancedStringEvaluate;


namespace TestEvaluation.ProcessEvaluate
{
	/// <summary>
	/// Processes a directory config-varialbe -- 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;

			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;

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

			// Keep 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