/// <summary> /// Replaces the token from the buffer between the provided tokens /// </summary> /// <param name="buffer">Buffer to update</param> /// <param name="rpBetweenToken">replacement token</param> public virtual void ReplaceBetweenTokens(ref string buffer, ReplaceBetweenPairToken rpBetweenToken) { Utilities.ArgumentNotNull("rpBetweenToken", rpBetweenToken); Utilities.ArgumentNotNull("buffer", buffer); string regularExp = rpBetweenToken.TokenStart + "[^" + rpBetweenToken.TokenIdentifier + "]*" + rpBetweenToken.TokenEnd; buffer = System.Text.RegularExpressions.Regex.Replace(buffer, regularExp, rpBetweenToken.TokenReplacement); }
/// <summary> /// Replaces the token from the buffer between the provided tokens /// </summary> /// <param name="buffer">Buffer to update</param> /// <param name="rpBetweenToken">replacement token</param> public virtual void ReplaceBetweenTokens(ref string buffer, ReplaceBetweenPairToken rpBetweenToken) { if (rpBetweenToken == null) { throw new ArgumentNullException("rpBetweenToken"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } string regularExp = rpBetweenToken.TokenStart + "[^" + rpBetweenToken.TokenIdentifier + "]*" + rpBetweenToken.TokenEnd; buffer = System.Text.RegularExpressions.Regex.Replace(buffer, regularExp, rpBetweenToken.TokenReplacement); }
/// <summary> /// Replaces the token from the buffer between the provided tokens /// </summary> /// <param name="buffer">Buffer to update</param> /// <param name="rpBetweenToken">replacement token</param> public virtual void ReplaceBetweenTokens(ref string buffer, ReplaceBetweenPairToken rpBetweenToken) { if (rpBetweenToken == null) { throw new ArgumentNullException("rpBetweenToken"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } string regularExp = rpBetweenToken.TokenStart + "[^" + rpBetweenToken.TokenIdentifier + "]*" + rpBetweenToken.TokenEnd; buffer = System.Text.RegularExpressions.Regex.Replace(buffer, regularExp, rpBetweenToken.TokenReplacement); }
/// <summary> /// Replaces the token from the buffer between the provided tokens /// </summary> /// <param name="buffer">Buffer to update</param> /// <param name="rpBetweenToken">replacement token</param> public virtual void ReplaceBetweenTokens(ref string buffer, ReplaceBetweenPairToken rpBetweenToken) { string regularExp = rpBetweenToken.TokenStart + "[^" + rpBetweenToken.TokenIdentifier + "]*" + rpBetweenToken.TokenEnd; buffer = System.Text.RegularExpressions.Regex.Replace(buffer, regularExp, rpBetweenToken.TokenReplacement); }
public virtual void UntokenFile(string source, string destination) { if (string.IsNullOrEmpty(source)) { throw new ArgumentNullException("source"); } if (string.IsNullOrEmpty(destination)) { throw new ArgumentNullException("destination"); } // Make sure that the destination folder exists. string destinationFolder = Path.GetDirectoryName(destination); if (!Directory.Exists(destinationFolder)) { Directory.CreateDirectory(destinationFolder); } //Open the file. Check to see if the File is binary or text. // NOTE: This is not correct because GetBinaryType will return true // only if the file is executable, not if it is a dll, a library or // any other type of binary file. uint binaryType; if (!NativeMethods.GetBinaryType(source, out binaryType)) { Encoding encoding = Encoding.Default; string buffer = null; // Create the reader to get the text. Note that we will default to ASCII as // encoding if the file does not contains a different signature. using (StreamReader reader = new StreamReader(source, Encoding.ASCII, true)) { // Get the content of the file. buffer = reader.ReadToEnd(); // Detect the encoding of the source file. Note that we // can get the encoding only after a read operation is // performed on the file. encoding = reader.CurrentEncoding; } foreach (object pair in tokenlist) { DeleteToken deleteToken = pair as DeleteToken; if (deleteToken != null) { DeleteTokens(ref buffer, deleteToken); continue; } ReplaceBetweenPairToken replaceBetween = pair as ReplaceBetweenPairToken; if (replaceBetween != null) { ReplaceBetweenTokens(ref buffer, replaceBetween); continue; } ReplacePairToken replacePair = pair as ReplacePairToken; if (replacePair != null) { ReplaceTokens(ref buffer, replacePair); continue; } } File.WriteAllText(destination, buffer, encoding); } else { File.Copy(source, destination); } }
/// <summary> /// Replaces the token from the buffer between the provided tokens /// </summary> /// <param name="buffer">Buffer to update</param> /// <param name="rpBetweenToken">replacement token</param> public virtual void ReplaceBetweenTokens(ref string buffer, ReplaceBetweenPairToken rpBetweenToken) { string regularExp = rpBetweenToken.TokenStart + "[^" + rpBetweenToken.TokenIdentifier + "]*" + rpBetweenToken.TokenEnd; buffer = System.Text.RegularExpressions.Regex.Replace(buffer, regularExp, rpBetweenToken.TokenReplacement); }