using System; using System.IO; using System.IO.Packaging; using System.Text; namespace RevitLess { /// /// Utility for manipulating string objects. /// public sealed class StringUtility { // added to suppress the default public constructor private StringUtility() { } /// /// Adds a trailing backslash (\) to the supplied /// path if it does not exist. /// /// The path to check. /// /// The supplied path is returned with a trailing /// backslash (\) if is does not exist. If path is /// an empty string, an empty string is returned. /// public static string AddTrailingBackslash(string path) { // check for null argument if(path == null) { throw new ArgumentNullException("path"); } // check for empty string if(path.Length == 0) { return path; } // check for existing backslash if(path.EndsWith(@"\") == false) { return string.Format(@"{0}\", path); } // there is already a trailing backslash return path; } /// /// Counts the number of specified characters in the specified string. /// /// The string to parse. /// The character to count. /// This method returns the number of specified characters /// in the specified string. Zero (0) is returned if the specified /// string does not contain any of the specified characters. public static int CharacterCount(string value, char characterToCount) { int count = 0; for(int i = 0; i < value.Length; i++) { if(value[i].Equals(characterToCount)) { count += 1; } } return count; } /// /// /// /// The original string the substring will /// be taken from. /// The starting index (zero based) in the /// original string to begin the search for the delimiter character. /// The number of characters from the starting index /// to search for the delimiter. /// The delimiter used to find the end of the substring. /// The number of occurances of the delimiter to use to /// find the end of the substring. /// This method returns the portion of the original string that contains /// count occurrances of the specified delimiter. The search starts at the character /// in the startingIndex position and continues for length characters. /// /// The string returned by this method includes the nth occurance of the /// sprcified delimiter.
/// Given the following string (originalString)...
/// P:\Projects\546002\Central Campus\Sheets\Buildings 3-4-5\HVAC\
/// SubString(originalString, 3, 31, '\', 1) returns "Projects\"
/// SubString(originalString, 3, 31, '\', 2) returns "Projects\546002\"
/// SubString(originalString, 3, 31, '\', 3) returns "Projects\546002\Central Campus\"
/// SubString(originalString, 3, 31, '\', 7) returns "Projects\546002\Central Campus\Sheets\Buildings 3-4-5\HVAC\"
/// SubString(originalString, 3, 31, '_', 3) returns the original string
///
public static string Substring(string value, int startIndex, int length, char delimiter, int count) { if(count == 0) { return value; } if(CharacterCount(value, delimiter) < count) { return value; } int internalCount = 0; for(int i = startIndex; i < length; i++) { if(value[i].Equals(delimiter)) { internalCount += 1; } if(internalCount == count) { return value.Substring(startIndex, i + 1); } } return value; } /// /// Finds a substring in the entire specified original string using the /// specified count occurrance of the specified delimiter. /// /// The original string the substring will /// be taken from. /// The delimiter used to find the end of the substring. /// The number of occurances of the delimiter to use to /// find the end of the substring. /// This method returns the portion of the original string that contains /// count occurrances of the specified delimiter. /// /// The string returned by this method includes the nth occurance of the /// sprcified delimiter.
/// Given the following string (originalString)...
/// P:\Projects\546002\Central Campus\Sheets\Buildings 3-4-5\HVAC\
/// SubString(originalString, '\', 2) returns "P:\Projects\"
/// SubString(originalString, '\', 4) returns "P:\Projects\546002\Central Campus\"
/// SubString(originalString, '\', 7) returns "P:\Projects\546002\Central Campus\Sheets\Buildings 3-4-5\HVAC\"
/// SubString(originalString, '\', 9) returns the original string
/// SubString(originalString, '_', 9) returns the original string
///
public static string Substring(string value, char delimiter, int count) { int startIndex = 0; int length = value.Length - 1; return Substring(value, startIndex, length, delimiter, count); } /// /// Purges unprintable characters from the supplied string. /// /// The string value that will be purged. /// The supplied value is returned as an ASCII string with all /// characters less than Decimnal 32 (SPACE) and greater than Decimal 126 /// (~) removed. /// The characters out of range are not replaced with a printable /// character. They are permanently removed. public static string PurgeUnprintableCharacters(string oldValue) { StringBuilder sb = new StringBuilder(); char[] oldValueArray = oldValue.ToCharArray(); foreach(char letter in oldValueArray) { int decimalValue = (int)letter; if((decimalValue >= 32) && (decimalValue <= 126)) { sb.Append(letter); } } oldValue = sb.ToString(); sb.Length = 0; sb.Capacity = 0; sb = null; return oldValue; } /// /// Converts the byte data from a StreamInfo Package stream to /// HEX string data. /// /// The StreamInfo Package the data is read from. /// A HEX string of the byte data is returned. /// The underlying stream is opened in Read Only mode using a /// Stream object. public static string ConvertStreamBytesToHex(StreamInfo streamInfo) { StringBuilder sb = new StringBuilder(); string convertedValue = string.Empty; try { using(Stream streamReader = streamInfo.GetStream(FileMode.Open, FileAccess.Read)) { int currentRead; while((currentRead = streamReader.ReadByte()) >= 0) { byte currentByte = (byte)currentRead; sb.AppendFormat("{0:X2}", currentByte); } convertedValue = sb.ToString(); return convertedValue; } } catch (Exception ex) { throw ex; } finally { sb.Length = 0; sb.Capacity = 0; sb = null; } } /// /// Converts the byte data from a StreamInfo Package stream to /// ASCII string data. /// /// The StreamInfo Package the data is read from. /// A ASCII string of the byte data is returned. /// The underlying stream is opened in Read Only mode using a /// Stream object. public static string ConvertStreamBytesToASCII(StreamInfo streamInfo) { byte[] streamData = null; try { using(Stream streamReader = streamInfo.GetStream(FileMode.Open, FileAccess.Read)) { streamData = new byte[streamReader.Length]; streamReader.Read(streamData, 0, streamData.Length); return ASCIIEncoding.ASCII.GetString(streamData); } } catch(Exception ex) { throw ex; } finally { streamData = null; } } /// /// Converts the byte data from a StreamInfo Package stream to /// Unicode string data. /// /// The StreamInfo Package the data is read from. /// A Unicode string of the byte data is returned. /// The underlying stream is opened in Read Only mode using a /// Stream object. public static string ConvertStreamBytesToUnicode(StreamInfo streamInfo) { byte[] streamData = null; try { using(Stream streamReader = streamInfo.GetStream(FileMode.Open, FileAccess.Read)) { streamData = new byte[streamReader.Length]; streamReader.Read(streamData, 0, streamData.Length); return ASCIIEncoding.Unicode.GetString(streamData); } } catch(Exception ex) { throw ex; } finally { streamData = null; } } } }