// NAnt - A .NET build tool
// Copyright (C) 2001-2004 Gerry Shaw
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Hani Atassi (haniatassi@users.sourceforge.net)
using System;
using System.ComponentModel;
namespace NAnt.VisualCpp.Util {
///
/// Defines how to deal with backslashes in values of command line
/// arguments.
///
public enum BackslashProcessingMethod {
///
/// Does not perform any processing on backslashes.
///
None = 0,
///
/// Duplicates the trailing backslash.
///
Duplicate = 1,
///
/// Fixes the trailing backslash by replaces trailing double backslashes
/// with only one backslash and removing single trailing backslashes.
///
Fix = 2,
///
/// Removes all the trailing backslashes.
///
Clean = 3
}
///
/// Groups a set of useful manipulation methods for
/// command-line arguments.
///
public class ArgumentUtils {
///
/// Performs backslash processing on the specified value using a given
/// method.
///
/// The to process.
/// The to use.
///
/// with backslashes processed using the given
/// .
///
public static string ProcessTrailingBackslash(string value, BackslashProcessingMethod processingMethod) {
string processedValue = null;
switch (processingMethod) {
case BackslashProcessingMethod.None:
processedValue = value;
break;
case BackslashProcessingMethod.Duplicate:
processedValue = DuplicateTrailingBackslash(value);
break;
case BackslashProcessingMethod.Fix:
processedValue = FixTrailingBackslash(value);
break;
case BackslashProcessingMethod.Clean:
processedValue = CleanTrailingBackslash(value);
break;
default:
throw new InvalidEnumArgumentException("processingMethod",
(int) processingMethod, typeof(BackslashProcessingMethod));
}
return processedValue;
}
///
/// Duplicates the trailing backslash.
///
/// The input string to check and duplicate the trailing backslash if necessary.
/// The result string after being processed.
///
/// Also duplicates trailing backslash in quoted value.
///
public static string DuplicateTrailingBackslash(string value) {
if (value == null) {
throw new ArgumentNullException("value");
}
if (value.Length == 0) {
return value;
}
bool isQuoted = value.Length > 2 && value.StartsWith("\"") && value.EndsWith("\"");
int lastIndex = (isQuoted ? value.Length - 2 : value.Length - 1);
if (value[lastIndex] == '\\') {
return value.Insert(lastIndex, @"\");
}
return value;
}
///
/// Fixes the trailing backslash. This function replaces the trailing double backslashes with
/// only one backslash. It also, removes the single trailing backslash.
///
/// The input string.
/// The result string after being processed.
public static string FixTrailingBackslash(string value) {
if (value == null) {
throw new ArgumentNullException("value");
}
if (value.Length == 0) {
return value;
}
if (value.EndsWith(@"\\")) {
return value.Remove(value.Length - 2, 2) + @"\";
} else if (value.EndsWith(@"\")) {
return value.Remove(value.Length - 1, 1);
} else {
return value;
}
}
///
/// Removes all the trailing backslashes from the input.
///
/// The input string.
/// The result string without trailing backslashes.
public static string CleanTrailingBackslash(string value) {
if (value == null) {
throw new ArgumentNullException("value");
}
return value.TrimEnd('\\');
}
///
/// Quotes an argument value and processes backslashes using a given
/// .
///
/// The argument value to quote.
/// The to use.
///
/// The quoted argument value.
///
public static string QuoteArgumentValue(string value, BackslashProcessingMethod processingMethod) {
// duplicate trailing backslashes (even if value is quoted)
string quotedValue = ArgumentUtils.ProcessTrailingBackslash(value, processingMethod);
// determine if value is already quoted
bool isQuoted = value.StartsWith("\"") && value.EndsWith("\"");
if (!isQuoted) {
quotedValue = "\"" + quotedValue + "\"";
}
return quotedValue;
}
}
}