singrdk/base/Windows/substitute/Substitute.cs

76 lines
2.3 KiB
C#
Raw Normal View History

2008-03-05 09:52:00 -05:00
///////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: Substitute.cs
//
// Note:
//
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Microsoft.Singularity.Tools
{
class Substitute
{
2008-11-17 18:29:00 -05:00
static bool unescape = false;
2008-03-05 09:52:00 -05:00
private static void Apply(TextReader input,
TextWriter output,
string inPattern,
string outPattern)
{
2008-11-17 18:29:00 -05:00
if (unescape)
outPattern = Regex.Unescape(outPattern);
2008-03-05 09:52:00 -05:00
string line;
while (null != (line = input.ReadLine())) {
line = Regex.Replace(line, inPattern, outPattern);
output.WriteLine(line);
}
}
public static int Main(string[] args)
{
2008-11-17 18:29:00 -05:00
int start = 0;
int length = args.Length;
if (length > 0 && String.Equals(args[0], "-e")) {
unescape = true;
start = 1;
}
switch (args.Length-start) {
2008-03-05 09:52:00 -05:00
case 2:
2008-11-17 18:29:00 -05:00
Apply(Console.In, Console.Out, args[start+0], args[start+1]);
2008-03-05 09:52:00 -05:00
return 0;
case 3:
2008-11-17 18:29:00 -05:00
using (StreamReader sr = new StreamReader(args[start+2])) {
Apply(sr, Console.Out, args[start+0], args[start+1]);
2008-03-05 09:52:00 -05:00
}
return 0;
case 4:
2008-11-17 18:29:00 -05:00
using (StreamReader sr = new StreamReader(args[start+2])) {
2008-03-05 09:52:00 -05:00
if (sr.Peek() >= 0) {
2008-11-17 18:29:00 -05:00
using (StreamWriter sw = new StreamWriter(args[start+3],
false, sr.CurrentEncoding)) {
Apply(sr, sw, args[start+0], args[start+1]);
2008-03-05 09:52:00 -05:00
}
}
}
return 0;
default:
2008-11-17 18:29:00 -05:00
Console.WriteLine("Usage: substitute [-e] <string1> <string2> [<Input file> [<OutputFile>]]");
2008-03-05 09:52:00 -05:00
return -1;
}
}
}
}