Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / Visual Basic

Create String Variable from Text File Source

Rate me:
Please Sign up or sign in to vote.
4.74/5 (12 votes)
2 Jun 20042 min read 88.5K   1.1K   20  
A utility to consume a text file and produce a code snippet concatenating the contents in a string variable.. and more!
//Copyright 2004 Michael McKechney (www.mckechney.com)
using System;
using System.IO;
using System.Text;
namespace StringCreator
{
	public enum FormatOption
	{
		CSharp,
		VBNet,
		VB6
	}
	/// <summary>
	/// Summary description for CreatorUtility.
	/// </summary>
	public class CreatorUtility
	{
		public CreatorUtility()
		{
			
		}


		/// <summary>
		/// Whether or not to trim spaces
		/// </summary>
		private bool preserveWhiteSpace = true;
		public bool PreserveWhiteSpace
		{
			get
			{
				return this.preserveWhiteSpace;
			}
			set
			{
				this.preserveWhiteSpace = value;
			}
		}

		
		/// <summary>
		/// Whether or not to trim out blank lines
		/// </summary>
		private bool preserveBlankLines = true;
		public bool PreserveBlankLines
		{
			get
			{
				return this.preserveBlankLines;
			}
			set
			{
				this.preserveBlankLines = value;
			}
		}


		/// <summary>
		/// Code method to use to process file
		/// </summary>
		private FormatOption outputFormat = FormatOption.CSharp;
		public FormatOption OutputFormat
		{
			get
			{
				return this.outputFormat;
			}
			set
			{
				this.outputFormat = value;
			}
		}


		/// <summary>
		/// Full name of source file
		/// </summary>
		private string fileName = string.Empty;
		public string FileName
		{
			get
			{
				return this.fileName;
			}
			set
			{
				this.fileName = value;
			}
		}
		

		/// <summary>
		/// String to add before anything else 
		/// </summary>
		private string appendBeforeText = string.Empty;
		public string AppendBeforeText
		{
			get
			{
				return this.appendBeforeText;
			}
			set
			{
				this.appendBeforeText = value;
			}

		}


		/// <summary>
		/// String to add after the end of everything else
		/// </summary>
		private string appendAfterText = string.Empty;
		public string AppendAfterText
		{
			get
			{
				return this.appendAfterText;
			}
			set
			{
				this.appendAfterText = value;
			}
		}


		/// <summary>
		/// Whether or not to initialize the string variable in the produced code
		/// </summary>
		private bool initilizeStringVariable = true;
		public bool InitilizeStringVariable
		{
			get
			{
				return this.initilizeStringVariable;
			}
			set
			{
				this.initilizeStringVariable = value;
			}
		}


		/// <summary>
		/// Whether or not to add the comment line specifying the source file name and location
		/// </summary>
		private bool addProcessedFromString = true;
		public bool AddProcessedFromString
		{
			get
			{
				return this.addProcessedFromString;
			}
			set
			{
				this.addProcessedFromString = value;
			}
		}

		
		/// <summary>
		/// String to use when/if removing the source file name
		/// </summary>
		private string substituteFileNamePattern = string.Empty;
		public string SubstituteFileNamePattern
		{
			get
			{
				return this.substituteFileNamePattern;
			}
			set
			{
				this.substituteFileNamePattern = value;
			}
		}


		/// <summary>
		/// String lines to add to beginning of the generated string variable
		/// </summary>
		private string[] prefixToVariable;
		public string[] PrefixToVariable
		{
			get
			{
				return this.prefixToVariable;
			}
			set
			{
				this.prefixToVariable = value;
			}
		}


		/// <summary>
		/// String lines to add to the end of the generated string variable
		/// </summary>
		private string[] appendToVariable;
		public string[] AppendToVariable
		{
			get
			{
				return this.appendToVariable;
			}
			set
			{
				this.appendToVariable = value;
			}
		}

		
		/// <summary>
		/// Worker method that handles file processing
		/// </summary>
		/// <param name="fileName">Full name of the file to use as a source</param>
		/// <returns>Generated code snippet</returns>
		public string ProcessFile(string fileName)
		{	
	
			this.fileName = fileName;
			string line;
			StringBuilder sb = new StringBuilder();

			//Add append before text
			if(this.appendBeforeText != string.Empty)
			{
				sb.Append(this.appendBeforeText+"\r\n");
			}
			
			if(this.AddProcessedFromString)
			{
				sb.Append( GetProcessedFromString());
			}

			//Initialize the variable if requested
			if(this.initilizeStringVariable)
			{
				sb.Append( InitilizeVariable());
			}

			//Process the Prefix Variable string
			if(this.prefixToVariable != null && this.prefixToVariable.Length > 0)
			{
				for(int i=0;i<this.prefixToVariable.Length;i++)
				{
					switch(outputFormat)
					{
						case FormatOption.CSharp:
							sb.Append( ProcessCSharp(this.prefixToVariable[i],preserveBlankLines,preserveWhiteSpace));
							break;
						case FormatOption.VBNet:
							sb.Append( ProcessVBNET(this.prefixToVariable[i],preserveBlankLines,preserveWhiteSpace));
							break;
						case FormatOption.VB6:
							sb.Append( ProcessVB6(this.prefixToVariable[i],preserveBlankLines,preserveWhiteSpace));
							break;
					}	
					if(this.prefixToVariable[i].Trim() != string.Empty || this.preserveBlankLines == true)
					{
						sb.Append("\r\n");
					}
				}
			}

			//Process each line in the file
			using (StreamReader sr = new StreamReader(fileName))
			{
				while( (line = sr.ReadLine()) != null)
				{
					switch(outputFormat)
					{
						case FormatOption.CSharp:
							sb.Append( ProcessCSharp(line,preserveBlankLines,preserveWhiteSpace));
							break;
						case FormatOption.VBNet:
							sb.Append( ProcessVBNET(line,preserveBlankLines,preserveWhiteSpace));
							break;
						case FormatOption.VB6:
							sb.Append( ProcessVB6(line,preserveBlankLines,preserveWhiteSpace));
							break;
					}	
					if(line.Trim() != string.Empty || this.preserveBlankLines == true)
					{
						sb.Append("\r\n");
					}
				}
			}
	

			//Process the Append Variable string
			if(this.appendToVariable != null && this.appendToVariable.Length > 0)
			{
				for(int i=0;i<this.appendToVariable.Length;i++)
				{
					switch(outputFormat)
					{
						case FormatOption.CSharp:
							sb.Append( ProcessCSharp(this.appendToVariable[i],preserveBlankLines,preserveWhiteSpace));
							break;
						case FormatOption.VBNet:
							sb.Append( ProcessVBNET(this.appendToVariable[i],preserveBlankLines,preserveWhiteSpace));
							break;
						case FormatOption.VB6:
							sb.Append( ProcessVB6(this.appendToVariable[i],preserveBlankLines,preserveWhiteSpace));
							break;
					}	
					if(this.appendToVariable[i].Trim() != string.Empty || this.preserveBlankLines == true)
					{
						sb.Append("\r\n");
					}
				}
				
			}

			//Finalize the string variable
			sb.Append( FinalizeStringVariable() );


			//Append the after text
			if(this.appendAfterText != string.Empty)
			{
				sb.Append(this.appendAfterText+"\r\n");
			}

			//Perform File Name substitution
			if(this.substituteFileNamePattern != string.Empty)
			{
				string shortfileName = Path.GetFileNameWithoutExtension(this.fileName);
				sb.Replace(shortfileName,this.substituteFileNamePattern);
			}

			return sb.ToString();
			
		}
		

		#region Helper methods for the language specific output
		/// <summary>
		/// Helper method to generate a line of C# code
		/// </summary>
		/// <param name="line">input string</param>
		/// <param name="preserveBlankLines">Whether or not to add a blank line filler if "line" is empty</param>
		/// <param name="preserveWhiteSpace">Whether or not to trim the "line" value</param>
		/// <returns>A line of C# Code</returns>
		private string ProcessCSharp(string line, bool preserveBlankLines, bool preserveWhiteSpace)
		{
			
			//Handle blank line first
			if(line.Trim() == string.Empty && preserveBlankLines)
			{
				return "sb.Append(\"\\r\\n\");";
			}
			else if(line.Trim() == string.Empty)
			{
				return string.Empty;
			}
			
			//Now handle a line with text
			line = line.Replace("\"","\\\"");
			if(preserveWhiteSpace)
			{
				return "sb.Append(\""+line+"\\r\\n\");";
			}
			else
			{
				return "sb.Append(\""+line.Trim()+"\\r\\n\");";
			}
			
		}
		/// <summary>
		/// Helper method to generate a line of VB.NET code
		/// </summary>
		/// <param name="line">input string</param>
		/// <param name="preserveBlankLines">Whether or not to add a blank line filler if "line" is empty</param>
		/// <param name="preserveWhiteSpace">Whether or not to trim the "line" value</param>
		/// <returns>A line of VB.NET Code</returns>
		private string ProcessVBNET(string line, bool preserveBlankLines, bool preserveWhiteSpace)
		{

			//Handle blank line first
			if(line.Trim() == string.Empty && preserveBlankLines)
			{
				return "sb.Append(vbCrLf);";
			}
			else if (line.Trim() == string.Empty)
			{
				return string.Empty;
			}
			
			//Now handle a line with text
			line = line.Replace("\"","\"\"");
			if(preserveWhiteSpace)
			{
				return "sb.Append(\""+line+ "\" & vbCrLf)";
			}
			else
			{
				return "sb.Append(\""+line.Trim()+ "\" & vbCrLf)";
			}		
		}
		/// <summary>
		/// Helper method to generate a line of VB 6 code
		/// </summary>
		/// <param name="line">input string</param>
		/// <param name="preserveBlankLines">Whether or not to add a blank line filler if "line" is empty</param>
		/// <param name="preserveWhiteSpace">Whether or not to trim the "line" value</param>
		/// <returns>A line of VB 6 Code</returns>
		private string ProcessVB6(string line, bool preserveBlankLines, bool preserveWhiteSpace)
		{
			//Handle blank line first
			if(line.Trim() == string.Empty && preserveBlankLines)
			{
				return "myString = myString & vbCrLf";
			}
			else if(line.Trim() == string.Empty)
			{
				return string.Empty;
			}
			
			//Now handle a line with text
			if(preserveWhiteSpace)
			{
				return "myString = myString & \""+line.Replace("\"","\"\"") + "\" & vbCrLf";
			}
			else
			{
				return "myString = myString & TRIM(\""+line.Replace("\"","\"\"") + "\") & vbCrLf";
			}				

		}

		#endregion

		#region Helper methods to get inserted text
		/// <summary>
		/// Gets code string to initilize string variable
		/// </summary>
		/// <returns>Code string in appropriate language</returns>
		private string InitilizeVariable()
		{
			System.Text.StringBuilder sb = new StringBuilder();
			switch(outputFormat)
			{
				case FormatOption.CSharp:
					sb.Append( "System.Text.StringBuilder sb = new System.Text.StringBuilder();\r\n");
					sb.Append( "String resultString;\r\n");
					break;
				case FormatOption.VBNet:
					sb.Append( "Dim sb As System.Text.StringBuilder =  New System.Text.StringBuilder() \r\n");
					sb.Append( "Dim resultString As String \r\n");
					break;
				case FormatOption.VB6:
					sb.Append("Dim myString As String\r\n");
					break;
			}		
			return sb.ToString();
		}
		/// <summary>
		/// Gets Code string to finalize the string variable
		/// </summary>
		/// <returns>Code string in appropriate language</returns>
		private string FinalizeStringVariable()
		{
			StringBuilder sb = new StringBuilder();
			switch(outputFormat)
			{
				case FormatOption.CSharp:
					return "resultString = sb.ToString();\r\n";
				case FormatOption.VBNet:
					return "resultString = sb.ToString()\r\n";
				case FormatOption.VB6:
					return "";
			}		
			return "";
		}

		/// <summary>
		/// Gets the commend string for the "Processed by" and "Source File" lines
		/// </summary>
		/// <returns>Comment string</returns>
		private string GetProcessedFromString()
		{
			switch(this.outputFormat)
			{
				case FormatOption.CSharp:
					return ("//Processed by String Creator Application by Michael W. McKechney \r\n//Source File: "+ this.fileName + "\r\n");
				case FormatOption.VBNet:
				case FormatOption.VB6:
					return ("'Processed by String Creator Application by Michael W. McKechney \r\n'Source File: "+ this.fileName + "\r\n");

			}
			return "";
		}
		#endregion
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions