From 2a3c79df83e800d5dfe75a1a3b140ed81da2b1d6 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Mon, 16 Jul 2007 15:40:11 +0000 Subject: changed to native line ending encoding --- .../src/Core/Utilities/CommandLineCollection.cs | 324 ++--- Prebuild/src/Core/Utilities/CurrentDirectory.cs | 178 +-- Prebuild/src/Core/Utilities/Helper.cs | 1322 ++++++++++---------- Prebuild/src/Core/Utilities/Log.cs | 558 ++++----- 4 files changed, 1191 insertions(+), 1191 deletions(-) (limited to 'Prebuild/src/Core/Utilities') diff --git a/Prebuild/src/Core/Utilities/CommandLineCollection.cs b/Prebuild/src/Core/Utilities/CommandLineCollection.cs index 496731f..62eb18b 100644 --- a/Prebuild/src/Core/Utilities/CommandLineCollection.cs +++ b/Prebuild/src/Core/Utilities/CommandLineCollection.cs @@ -1,162 +1,162 @@ -#region BSD License -/* -Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) - -Redistribution and use in source and binary forms, with or without modification, are permitted -provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this list of conditions - and the following disclaimer. -* Redistributions in binary form must reproduce the above copyright notice, this list of conditions - and the following disclaimer in the documentation and/or other materials provided with the - distribution. -* The name of the author may not be used to endorse or promote products derived from this software - without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING -IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#endregion - -#region CVS Information -/* - * $Source$ - * $Author: robloach $ - * $Date: 2006-09-26 00:30:53 +0200 (ti, 26 sep 2006) $ - * $Revision: 165 $ - */ -#endregion - -using System; -using System.Collections; -using System.Collections.Specialized; -using System.Diagnostics; - -namespace Prebuild.Core.Utilities -{ - /// - /// The CommandLine class parses and interprets the command-line arguments passed to - /// prebuild. - /// - public class CommandLineCollection - { - #region Fields - - // The raw OS arguments - private string[] m_RawArgs; - - // Command-line argument storage - private Hashtable m_Arguments; - - #endregion - - #region Constructors - - /// - /// Create a new CommandLine instance and set some internal variables. - /// - public CommandLineCollection(string[] args) - { - m_RawArgs = args; - m_Arguments = new Hashtable(); - - Parse(); - } - - #endregion - - #region Private Methods - - private void Parse() - { - if(m_RawArgs.Length < 1) - return; - - int idx = 0; - string arg = null, lastArg = null; - - while(idx 2 && arg[0] == '/') - { - arg = arg.Substring(1); - lastArg = arg; - m_Arguments[arg] = ""; - } - else - { - if(lastArg != null) - { - m_Arguments[lastArg] = arg; - lastArg = null; - } - } - - idx++; - } - } - - #endregion - - #region Public Methods - - /// - /// Wases the passed. - /// - /// The arg. - /// - public bool WasPassed(string arg) - { - return (m_Arguments.ContainsKey(arg)); - } - - #endregion - - #region Properties - - /// - /// Gets the parameter associated with the command line option - /// - /// Returns null if option was not specified, - /// null string if no parameter was specified, and the value if a parameter was specified - public string this[string index] - { - get - { - if(m_Arguments.ContainsKey(index)) - { - return (string)(m_Arguments[index]); - } - else - { - return null; - } - } - } - - #endregion - - #region IEnumerable Members - - /// - /// Returns an enumerator that can iterate through a collection. - /// - /// - /// An - /// that can be used to iterate through the collection. - /// - public IDictionaryEnumerator GetEnumerator() - { - return m_Arguments.GetEnumerator(); - } - - #endregion - } -} +#region BSD License +/* +Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) + +Redistribution and use in source and binary forms, with or without modification, are permitted +provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions + and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions + and the following disclaimer in the documentation and/or other materials provided with the + distribution. +* The name of the author may not be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#endregion + +#region CVS Information +/* + * $Source$ + * $Author: robloach $ + * $Date: 2006-09-26 00:30:53 +0200 (ti, 26 sep 2006) $ + * $Revision: 165 $ + */ +#endregion + +using System; +using System.Collections; +using System.Collections.Specialized; +using System.Diagnostics; + +namespace Prebuild.Core.Utilities +{ + /// + /// The CommandLine class parses and interprets the command-line arguments passed to + /// prebuild. + /// + public class CommandLineCollection + { + #region Fields + + // The raw OS arguments + private string[] m_RawArgs; + + // Command-line argument storage + private Hashtable m_Arguments; + + #endregion + + #region Constructors + + /// + /// Create a new CommandLine instance and set some internal variables. + /// + public CommandLineCollection(string[] args) + { + m_RawArgs = args; + m_Arguments = new Hashtable(); + + Parse(); + } + + #endregion + + #region Private Methods + + private void Parse() + { + if(m_RawArgs.Length < 1) + return; + + int idx = 0; + string arg = null, lastArg = null; + + while(idx 2 && arg[0] == '/') + { + arg = arg.Substring(1); + lastArg = arg; + m_Arguments[arg] = ""; + } + else + { + if(lastArg != null) + { + m_Arguments[lastArg] = arg; + lastArg = null; + } + } + + idx++; + } + } + + #endregion + + #region Public Methods + + /// + /// Wases the passed. + /// + /// The arg. + /// + public bool WasPassed(string arg) + { + return (m_Arguments.ContainsKey(arg)); + } + + #endregion + + #region Properties + + /// + /// Gets the parameter associated with the command line option + /// + /// Returns null if option was not specified, + /// null string if no parameter was specified, and the value if a parameter was specified + public string this[string index] + { + get + { + if(m_Arguments.ContainsKey(index)) + { + return (string)(m_Arguments[index]); + } + else + { + return null; + } + } + } + + #endregion + + #region IEnumerable Members + + /// + /// Returns an enumerator that can iterate through a collection. + /// + /// + /// An + /// that can be used to iterate through the collection. + /// + public IDictionaryEnumerator GetEnumerator() + { + return m_Arguments.GetEnumerator(); + } + + #endregion + } +} diff --git a/Prebuild/src/Core/Utilities/CurrentDirectory.cs b/Prebuild/src/Core/Utilities/CurrentDirectory.cs index a76d844..ff2d159 100644 --- a/Prebuild/src/Core/Utilities/CurrentDirectory.cs +++ b/Prebuild/src/Core/Utilities/CurrentDirectory.cs @@ -1,89 +1,89 @@ -#region BSD License -/* -Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) - -Redistribution and use in source and binary forms, with or without modification, are permitted -provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this list of conditions - and the following disclaimer. -* Redistributions in binary form must reproduce the above copyright notice, this list of conditions - and the following disclaimer in the documentation and/or other materials provided with the - distribution. -* The name of the author may not be used to endorse or promote products derived from this software - without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING -IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#endregion - -#region CVS Information -/* - * $Source$ - * $Author: jendave $ - * $Date: 2006-01-28 01:49:58 +0100 (lö, 28 jan 2006) $ - * $Revision: 71 $ - */ -#endregion - -using System; -using System.Collections; - -namespace Prebuild.Core.Utilities -{ - /// - /// - /// - public class CurrentDirectory - { - #region Fields - - private Stack m_Stack; - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - public CurrentDirectory() - { - m_Stack = new Stack(); - } - - #endregion - - #region Public Methods - - /// - /// Pushes this instance. - /// - public void Push() - { - m_Stack.Push(Environment.CurrentDirectory); - } - - /// - /// Pops this instance. - /// - public void Pop() - { - if(m_Stack.Count < 1) - { - return; - } - - string cwd = (string)m_Stack.Pop(); - Helper.SetCurrentDir(cwd); - } - - #endregion - } -} +#region BSD License +/* +Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) + +Redistribution and use in source and binary forms, with or without modification, are permitted +provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions + and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions + and the following disclaimer in the documentation and/or other materials provided with the + distribution. +* The name of the author may not be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#endregion + +#region CVS Information +/* + * $Source$ + * $Author: jendave $ + * $Date: 2006-01-28 01:49:58 +0100 (lö, 28 jan 2006) $ + * $Revision: 71 $ + */ +#endregion + +using System; +using System.Collections; + +namespace Prebuild.Core.Utilities +{ + /// + /// + /// + public class CurrentDirectory + { + #region Fields + + private Stack m_Stack; + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the class. + /// + public CurrentDirectory() + { + m_Stack = new Stack(); + } + + #endregion + + #region Public Methods + + /// + /// Pushes this instance. + /// + public void Push() + { + m_Stack.Push(Environment.CurrentDirectory); + } + + /// + /// Pops this instance. + /// + public void Pop() + { + if(m_Stack.Count < 1) + { + return; + } + + string cwd = (string)m_Stack.Pop(); + Helper.SetCurrentDir(cwd); + } + + #endregion + } +} diff --git a/Prebuild/src/Core/Utilities/Helper.cs b/Prebuild/src/Core/Utilities/Helper.cs index 33c9618..27b9ccf 100644 --- a/Prebuild/src/Core/Utilities/Helper.cs +++ b/Prebuild/src/Core/Utilities/Helper.cs @@ -1,661 +1,661 @@ -#region BSD License -/* -Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) - -Redistribution and use in source and binary forms, with or without modification, are permitted -provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this list of conditions - and the following disclaimer. -* Redistributions in binary form must reproduce the above copyright notice, this list of conditions - and the following disclaimer in the documentation and/or other materials provided with the - distribution. -* The name of the author may not be used to endorse or promote products derived from this software - without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING -IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#endregion - -#region CVS Information -/* - * $Source$ - * $Author: jendave $ - * $Date: 2007-02-13 21:58:03 +0100 (ti, 13 feb 2007) $ - * $Revision: 205 $ - */ -#endregion - -using System; -using System.Collections; -using System.Diagnostics; -using System.IO; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; -using System.Collections.Specialized; -using System.Xml; -using Prebuild.Core.Nodes; - -namespace Prebuild.Core.Utilities -{ - /// - /// - /// - public class Helper - { - #region Fields - - private static Stack dirStack; - private static Regex varRegex; - static bool checkForOSVariables; - - /// - /// - /// - public static bool CheckForOSVariables - { - get - { - return checkForOSVariables; - } - set - { - checkForOSVariables = value; - } - } - - #endregion - - #region Constructors - - /// - /// Initializes the class. - /// - static Helper() - { - dirStack = new Stack(); - //m_VarRegex = new Regex(@"\${(?[\w|_]+)}"); - } - - #endregion - - #region Properties - - /// - /// - /// - public static Stack DirStack - { - get - { - return dirStack; - } - } - - /// - /// - /// - public static Regex VarRegex - { - get - { - return varRegex; - } - set - { - varRegex = value; - } - } - - #endregion - - #region Public Methods - - #region String Parsing - #region Inner Classes and Delegates - /// - /// - /// - public delegate string StringLookup(string key); - - #endregion - - /// - /// Gets a collection of StringLocationPair objects that represent the matches - /// - /// The target. - /// The before group. - /// The after group. - /// if set to true [include delimiters in substrings]. - /// - public static StringCollection FindGroups(string target, string beforeGroup, string afterGroup, bool includeDelimitersInSubstrings) - { - if( beforeGroup == null ) - { - throw new ArgumentNullException("beforeGroup"); - } - if( afterGroup == null ) - { - throw new ArgumentNullException("afterGroup"); - } - StringCollection results = new StringCollection(); - if(target == null || target.Length == 0) - { - return results; - } - - int beforeMod = 0; - int afterMod = 0; - if(includeDelimitersInSubstrings) - { - //be sure to not exlude the delims - beforeMod = beforeGroup.Length; - afterMod = afterGroup.Length; - } - int startIndex = 0; - while((startIndex = target.IndexOf(beforeGroup,startIndex)) != -1) { - int endIndex = target.IndexOf(afterGroup,startIndex);//the index of the char after it - if(endIndex == -1) - { - break; - } - int length = endIndex - startIndex - beforeGroup.Length;//move to the first char in the string - string substring = substring = target.Substring(startIndex + beforeGroup.Length - beforeMod, - length - afterMod); - - results.Add(substring); - //results.Add(new StringLocationPair(substring,startIndex)); - startIndex = endIndex + 1; - //the Interpolate*() methods will not work if expressions are expandded inside expression due to an optimization - //so start after endIndex - - } - return results; - } - - /// - /// Replaces the groups. - /// - /// The target. - /// The before group. - /// The after group. - /// The lookup. - /// - public static string ReplaceGroups(string target, string beforeGroup, string afterGroup, StringLookup lookup) { - if( target == null ) - { - throw new ArgumentNullException("target"); - } - //int targetLength = target.Length; - StringCollection strings = FindGroups(target,beforeGroup,afterGroup,false); - if( lookup == null ) - { - throw new ArgumentNullException("lookup"); - } - foreach(string substring in strings) - { - target = target.Replace(beforeGroup + substring + afterGroup, lookup(substring) ); - } - return target; - } - - /// - /// Replaces ${var} statements in a string with the corresonding values as detirmined by the lookup delegate - /// - /// The target. - /// The lookup. - /// - public static string InterpolateForVariables(string target, StringLookup lookup) - { - return ReplaceGroups(target, "${" , "}" , lookup); - } - - /// - /// Replaces ${var} statements in a string with the corresonding environment variable with name var - /// - /// - /// - public static string InterpolateForEnvironmentVariables(string target) - { - return InterpolateForVariables(target, new StringLookup(Environment.GetEnvironmentVariable)); - } - - #endregion - - /// - /// Translates the value. - /// - /// Type of the translate. - /// The translation item. - /// - public static object TranslateValue(Type translateType, string translationItem) - { - if(translationItem == null) - { - return null; - } - - try - { - string lowerVal = translationItem.ToLower(); - if(translateType == typeof(bool)) - { - return (lowerVal == "true" || lowerVal == "1" || lowerVal == "y" || lowerVal == "yes" || lowerVal == "on"); - } - else if(translateType == typeof(int)) - { - return (Int32.Parse(translationItem)); - } - else - { - return translationItem; - } - } - catch(FormatException) - { - return null; - } - } - - /// - /// Deletes if exists. - /// - /// The file. - /// - public static bool DeleteIfExists(string file) - { - string resFile = null; - try - { - resFile = ResolvePath(file); - } - catch(ArgumentException) - { - return false; - } - - if(!File.Exists(resFile)) - { - return false; - } - - File.Delete(resFile); - return true; - } - - // This little gem was taken from the NeL source, thanks guys! - /// - /// Makes a relative path - /// - /// Path to start from - /// Path to end at - /// Path that will get from startPath to endPath - public static string MakePathRelativeTo(string startPath, string endPath) - { - string tmp = NormalizePath(startPath, '/'); - string src = NormalizePath(endPath, '/'); - string prefix = ""; - - while(true) - { - if((String.Compare(tmp, 0, src, 0, tmp.Length) == 0)) - { - string ret; - int size = tmp.Length; - if(size == src.Length) - { - return "./"; - } - if ((src.Length > tmp.Length) && src[tmp.Length-1] != '/' && src[tmp.Length-1] != '\\') - { - } - else - { - ret = prefix + endPath.Substring(size, endPath.Length - size); - ret = ret.Trim(); - if(ret[0] == '/' || ret[0] == '\\') - { - ret = "." + ret; - } - - return NormalizePath(ret); - } - - } - - if(tmp.Length < 2) - { - break; - } - - int lastPos = tmp.LastIndexOf('/', tmp.Length - 2); - int prevPos = tmp.IndexOf('/'); - - if((lastPos == prevPos) || (lastPos == -1)) - { - break; - } - - tmp = tmp.Substring(0, lastPos + 1); - prefix += "../"; - } - - return endPath; - } - - /// - /// Resolves the path. - /// - /// The path. - /// - public static string ResolvePath(string path) - { - string tmpPath = NormalizePath(path); - if(tmpPath.Length < 1) - { - tmpPath = "."; - } - - tmpPath = Path.GetFullPath(tmpPath); - if(!File.Exists(tmpPath) && !Directory.Exists(tmpPath)) - { - throw new ArgumentException("Path could not be resolved: " + tmpPath); - } - - return tmpPath; - } - - /// - /// Normalizes the path. - /// - /// The path. - /// The separator character. - /// - public static string NormalizePath(string path, char separatorCharacter) - { - if(path == null || path == "" || path.Length < 1) - { - return ""; - } - - string tmpPath = path.Replace('\\', '/'); - tmpPath = tmpPath.Replace('/', separatorCharacter); - return tmpPath; - } - - /// - /// Normalizes the path. - /// - /// The path. - /// - public static string NormalizePath(string path) - { - return NormalizePath(path, Path.DirectorySeparatorChar); - } - - /// - /// Ends the path. - /// - /// The path. - /// The separator character. - /// - public static string EndPath(string path, char separatorCharacter) - { - if(path == null || path == "" || path.Length < 1) - { - return ""; - } - - if(!path.EndsWith(separatorCharacter.ToString())) - { - return (path + separatorCharacter); - } - - return path; - } - - /// - /// Ends the path. - /// - /// The path. - /// - public static string EndPath(string path) - { - return EndPath(path, Path.DirectorySeparatorChar); - } - - /// - /// Makes the file path. - /// - /// The path. - /// The name. - /// The ext. - /// - public static string MakeFilePath(string path, string name, string ext) - { - string ret = EndPath(NormalizePath(path)); - - if( name == null ) - { - throw new ArgumentNullException("name"); - } - - ret += name; - if(!name.EndsWith("." + ext)) - { - ret += "." + ext; - } - - //foreach(char c in Path.GetInvalidPathChars()) - //{ - // ret = ret.Replace(c, '_'); - //} - - return ret; - } - - /// - /// Makes the file path. - /// - /// The path. - /// The name. - /// - public static string MakeFilePath(string path, string name) - { - string ret = EndPath(NormalizePath(path)); - - if( name == null ) - { - throw new ArgumentNullException("name"); - } - - ret += name; - - //foreach (char c in Path.GetInvalidPathChars()) - //{ - // ret = ret.Replace(c, '_'); - //} - - return ret; - } - - /// - /// - /// - /// - /// - public static string MakeReferencePath(string path) - { - string ret = EndPath(NormalizePath(path)); - - //foreach (char c in Path.GetInvalidPathChars()) - //{ - // ret = ret.Replace(c, '_'); - //} - - return ret; - } - - /// - /// Sets the current dir. - /// - /// The path. - public static void SetCurrentDir(string path) - { - if( path == null ) - { - throw new ArgumentNullException("path"); - } - if(path.Length < 1) - { - return; - } - - Environment.CurrentDirectory = path; - } - - /// - /// Checks the type. - /// - /// The type to check. - /// The attr. - /// The inter. - /// - public static object CheckType(Type typeToCheck, Type attr, Type inter) - { - if(typeToCheck == null || attr == null) - { - return null; - } - - object[] attrs = typeToCheck.GetCustomAttributes(attr, false); - if(attrs == null || attrs.Length < 1) - { - return null; - } - if( inter == null ) - { - throw new ArgumentNullException("inter"); - } - - if(typeToCheck.GetInterface(inter.FullName) == null) - { - return null; - } - - return attrs[0]; - } - - /* A bit of overhead for simple group parsing, there are problems with Regex in Mono - public static string ParseValue(string val) - { - if(val == null || val.Length < 1 || !CheckForOSVariables) - return val; - - string tmp = val; - Match m = m_VarRegex.Match(val); - while(m.Success) - { - if(m.Groups["var"] == null) - continue; - - Capture c = m.Groups["var"].Captures[0]; - if(c == null) - continue; - - string var = c.Value; - string envVal = Environment.GetEnvironmentVariable(var); - if(envVal == null) - envVal = ""; - - tmp = tmp.Replace("${" + var + "}", envVal); - m = m.NextMatch(); - } - - return tmp; - }*/ - - /// - /// Attributes the value. - /// - /// The node. - /// The attr. - /// The def. - /// - public static string AttributeValue(XmlNode node, string attr, string def) - { - if( node == null ) - { - throw new ArgumentNullException("node"); - } - if(node.Attributes[attr] == null) - { - return def; - } - string val = node.Attributes[attr].Value; - if(!CheckForOSVariables) - { - return val; - } - - return InterpolateForEnvironmentVariables(val); - } - - /// - /// Parses the boolean. - /// - /// The node. - /// The attr. - /// if set to true [default value]. - /// - public static bool ParseBoolean(XmlNode node, string attr, bool defaultValue) - { - if( node == null ) - { - throw new ArgumentNullException("node"); - } - if(node.Attributes[attr] == null) - { - return defaultValue; - } - return bool.Parse(node.Attributes[attr].Value); - } - - /// - /// Enums the attribute value. - /// - /// The node. - /// The attr. - /// Type of the enum. - /// The def. - /// - public static object EnumAttributeValue(XmlNode node, string attr, Type enumType, object def) - { - if( def == null ) - { - throw new ArgumentNullException("def"); - } - string val = AttributeValue(node, attr, def.ToString()); - return Enum.Parse(enumType, val, true); - } - - /// - /// - /// - /// - /// - /// - public static string AssemblyFullName(string assemblyName, ProjectType projectType) - { - return assemblyName + (projectType == ProjectType.Library ? ".dll" : ".exe"); - } - - #endregion - } -} +#region BSD License +/* +Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) + +Redistribution and use in source and binary forms, with or without modification, are permitted +provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions + and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions + and the following disclaimer in the documentation and/or other materials provided with the + distribution. +* The name of the author may not be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#endregion + +#region CVS Information +/* + * $Source$ + * $Author: jendave $ + * $Date: 2007-02-13 21:58:03 +0100 (ti, 13 feb 2007) $ + * $Revision: 205 $ + */ +#endregion + +using System; +using System.Collections; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; +using System.Collections.Specialized; +using System.Xml; +using Prebuild.Core.Nodes; + +namespace Prebuild.Core.Utilities +{ + /// + /// + /// + public class Helper + { + #region Fields + + private static Stack dirStack; + private static Regex varRegex; + static bool checkForOSVariables; + + /// + /// + /// + public static bool CheckForOSVariables + { + get + { + return checkForOSVariables; + } + set + { + checkForOSVariables = value; + } + } + + #endregion + + #region Constructors + + /// + /// Initializes the class. + /// + static Helper() + { + dirStack = new Stack(); + //m_VarRegex = new Regex(@"\${(?[\w|_]+)}"); + } + + #endregion + + #region Properties + + /// + /// + /// + public static Stack DirStack + { + get + { + return dirStack; + } + } + + /// + /// + /// + public static Regex VarRegex + { + get + { + return varRegex; + } + set + { + varRegex = value; + } + } + + #endregion + + #region Public Methods + + #region String Parsing + #region Inner Classes and Delegates + /// + /// + /// + public delegate string StringLookup(string key); + + #endregion + + /// + /// Gets a collection of StringLocationPair objects that represent the matches + /// + /// The target. + /// The before group. + /// The after group. + /// if set to true [include delimiters in substrings]. + /// + public static StringCollection FindGroups(string target, string beforeGroup, string afterGroup, bool includeDelimitersInSubstrings) + { + if( beforeGroup == null ) + { + throw new ArgumentNullException("beforeGroup"); + } + if( afterGroup == null ) + { + throw new ArgumentNullException("afterGroup"); + } + StringCollection results = new StringCollection(); + if(target == null || target.Length == 0) + { + return results; + } + + int beforeMod = 0; + int afterMod = 0; + if(includeDelimitersInSubstrings) + { + //be sure to not exlude the delims + beforeMod = beforeGroup.Length; + afterMod = afterGroup.Length; + } + int startIndex = 0; + while((startIndex = target.IndexOf(beforeGroup,startIndex)) != -1) { + int endIndex = target.IndexOf(afterGroup,startIndex);//the index of the char after it + if(endIndex == -1) + { + break; + } + int length = endIndex - startIndex - beforeGroup.Length;//move to the first char in the string + string substring = substring = target.Substring(startIndex + beforeGroup.Length - beforeMod, + length - afterMod); + + results.Add(substring); + //results.Add(new StringLocationPair(substring,startIndex)); + startIndex = endIndex + 1; + //the Interpolate*() methods will not work if expressions are expandded inside expression due to an optimization + //so start after endIndex + + } + return results; + } + + /// + /// Replaces the groups. + /// + /// The target. + /// The before group. + /// The after group. + /// The lookup. + /// + public static string ReplaceGroups(string target, string beforeGroup, string afterGroup, StringLookup lookup) { + if( target == null ) + { + throw new ArgumentNullException("target"); + } + //int targetLength = target.Length; + StringCollection strings = FindGroups(target,beforeGroup,afterGroup,false); + if( lookup == null ) + { + throw new ArgumentNullException("lookup"); + } + foreach(string substring in strings) + { + target = target.Replace(beforeGroup + substring + afterGroup, lookup(substring) ); + } + return target; + } + + /// + /// Replaces ${var} statements in a string with the corresonding values as detirmined by the lookup delegate + /// + /// The target. + /// The lookup. + /// + public static string InterpolateForVariables(string target, StringLookup lookup) + { + return ReplaceGroups(target, "${" , "}" , lookup); + } + + /// + /// Replaces ${var} statements in a string with the corresonding environment variable with name var + /// + /// + /// + public static string InterpolateForEnvironmentVariables(string target) + { + return InterpolateForVariables(target, new StringLookup(Environment.GetEnvironmentVariable)); + } + + #endregion + + /// + /// Translates the value. + /// + /// Type of the translate. + /// The translation item. + /// + public static object TranslateValue(Type translateType, string translationItem) + { + if(translationItem == null) + { + return null; + } + + try + { + string lowerVal = translationItem.ToLower(); + if(translateType == typeof(bool)) + { + return (lowerVal == "true" || lowerVal == "1" || lowerVal == "y" || lowerVal == "yes" || lowerVal == "on"); + } + else if(translateType == typeof(int)) + { + return (Int32.Parse(translationItem)); + } + else + { + return translationItem; + } + } + catch(FormatException) + { + return null; + } + } + + /// + /// Deletes if exists. + /// + /// The file. + /// + public static bool DeleteIfExists(string file) + { + string resFile = null; + try + { + resFile = ResolvePath(file); + } + catch(ArgumentException) + { + return false; + } + + if(!File.Exists(resFile)) + { + return false; + } + + File.Delete(resFile); + return true; + } + + // This little gem was taken from the NeL source, thanks guys! + /// + /// Makes a relative path + /// + /// Path to start from + /// Path to end at + /// Path that will get from startPath to endPath + public static string MakePathRelativeTo(string startPath, string endPath) + { + string tmp = NormalizePath(startPath, '/'); + string src = NormalizePath(endPath, '/'); + string prefix = ""; + + while(true) + { + if((String.Compare(tmp, 0, src, 0, tmp.Length) == 0)) + { + string ret; + int size = tmp.Length; + if(size == src.Length) + { + return "./"; + } + if ((src.Length > tmp.Length) && src[tmp.Length-1] != '/' && src[tmp.Length-1] != '\\') + { + } + else + { + ret = prefix + endPath.Substring(size, endPath.Length - size); + ret = ret.Trim(); + if(ret[0] == '/' || ret[0] == '\\') + { + ret = "." + ret; + } + + return NormalizePath(ret); + } + + } + + if(tmp.Length < 2) + { + break; + } + + int lastPos = tmp.LastIndexOf('/', tmp.Length - 2); + int prevPos = tmp.IndexOf('/'); + + if((lastPos == prevPos) || (lastPos == -1)) + { + break; + } + + tmp = tmp.Substring(0, lastPos + 1); + prefix += "../"; + } + + return endPath; + } + + /// + /// Resolves the path. + /// + /// The path. + /// + public static string ResolvePath(string path) + { + string tmpPath = NormalizePath(path); + if(tmpPath.Length < 1) + { + tmpPath = "."; + } + + tmpPath = Path.GetFullPath(tmpPath); + if(!File.Exists(tmpPath) && !Directory.Exists(tmpPath)) + { + throw new ArgumentException("Path could not be resolved: " + tmpPath); + } + + return tmpPath; + } + + /// + /// Normalizes the path. + /// + /// The path. + /// The separator character. + /// + public static string NormalizePath(string path, char separatorCharacter) + { + if(path == null || path == "" || path.Length < 1) + { + return ""; + } + + string tmpPath = path.Replace('\\', '/'); + tmpPath = tmpPath.Replace('/', separatorCharacter); + return tmpPath; + } + + /// + /// Normalizes the path. + /// + /// The path. + /// + public static string NormalizePath(string path) + { + return NormalizePath(path, Path.DirectorySeparatorChar); + } + + /// + /// Ends the path. + /// + /// The path. + /// The separator character. + /// + public static string EndPath(string path, char separatorCharacter) + { + if(path == null || path == "" || path.Length < 1) + { + return ""; + } + + if(!path.EndsWith(separatorCharacter.ToString())) + { + return (path + separatorCharacter); + } + + return path; + } + + /// + /// Ends the path. + /// + /// The path. + /// + public static string EndPath(string path) + { + return EndPath(path, Path.DirectorySeparatorChar); + } + + /// + /// Makes the file path. + /// + /// The path. + /// The name. + /// The ext. + /// + public static string MakeFilePath(string path, string name, string ext) + { + string ret = EndPath(NormalizePath(path)); + + if( name == null ) + { + throw new ArgumentNullException("name"); + } + + ret += name; + if(!name.EndsWith("." + ext)) + { + ret += "." + ext; + } + + //foreach(char c in Path.GetInvalidPathChars()) + //{ + // ret = ret.Replace(c, '_'); + //} + + return ret; + } + + /// + /// Makes the file path. + /// + /// The path. + /// The name. + /// + public static string MakeFilePath(string path, string name) + { + string ret = EndPath(NormalizePath(path)); + + if( name == null ) + { + throw new ArgumentNullException("name"); + } + + ret += name; + + //foreach (char c in Path.GetInvalidPathChars()) + //{ + // ret = ret.Replace(c, '_'); + //} + + return ret; + } + + /// + /// + /// + /// + /// + public static string MakeReferencePath(string path) + { + string ret = EndPath(NormalizePath(path)); + + //foreach (char c in Path.GetInvalidPathChars()) + //{ + // ret = ret.Replace(c, '_'); + //} + + return ret; + } + + /// + /// Sets the current dir. + /// + /// The path. + public static void SetCurrentDir(string path) + { + if( path == null ) + { + throw new ArgumentNullException("path"); + } + if(path.Length < 1) + { + return; + } + + Environment.CurrentDirectory = path; + } + + /// + /// Checks the type. + /// + /// The type to check. + /// The attr. + /// The inter. + /// + public static object CheckType(Type typeToCheck, Type attr, Type inter) + { + if(typeToCheck == null || attr == null) + { + return null; + } + + object[] attrs = typeToCheck.GetCustomAttributes(attr, false); + if(attrs == null || attrs.Length < 1) + { + return null; + } + if( inter == null ) + { + throw new ArgumentNullException("inter"); + } + + if(typeToCheck.GetInterface(inter.FullName) == null) + { + return null; + } + + return attrs[0]; + } + + /* A bit of overhead for simple group parsing, there are problems with Regex in Mono + public static string ParseValue(string val) + { + if(val == null || val.Length < 1 || !CheckForOSVariables) + return val; + + string tmp = val; + Match m = m_VarRegex.Match(val); + while(m.Success) + { + if(m.Groups["var"] == null) + continue; + + Capture c = m.Groups["var"].Captures[0]; + if(c == null) + continue; + + string var = c.Value; + string envVal = Environment.GetEnvironmentVariable(var); + if(envVal == null) + envVal = ""; + + tmp = tmp.Replace("${" + var + "}", envVal); + m = m.NextMatch(); + } + + return tmp; + }*/ + + /// + /// Attributes the value. + /// + /// The node. + /// The attr. + /// The def. + /// + public static string AttributeValue(XmlNode node, string attr, string def) + { + if( node == null ) + { + throw new ArgumentNullException("node"); + } + if(node.Attributes[attr] == null) + { + return def; + } + string val = node.Attributes[attr].Value; + if(!CheckForOSVariables) + { + return val; + } + + return InterpolateForEnvironmentVariables(val); + } + + /// + /// Parses the boolean. + /// + /// The node. + /// The attr. + /// if set to true [default value]. + /// + public static bool ParseBoolean(XmlNode node, string attr, bool defaultValue) + { + if( node == null ) + { + throw new ArgumentNullException("node"); + } + if(node.Attributes[attr] == null) + { + return defaultValue; + } + return bool.Parse(node.Attributes[attr].Value); + } + + /// + /// Enums the attribute value. + /// + /// The node. + /// The attr. + /// Type of the enum. + /// The def. + /// + public static object EnumAttributeValue(XmlNode node, string attr, Type enumType, object def) + { + if( def == null ) + { + throw new ArgumentNullException("def"); + } + string val = AttributeValue(node, attr, def.ToString()); + return Enum.Parse(enumType, val, true); + } + + /// + /// + /// + /// + /// + /// + public static string AssemblyFullName(string assemblyName, ProjectType projectType) + { + return assemblyName + (projectType == ProjectType.Library ? ".dll" : ".exe"); + } + + #endregion + } +} diff --git a/Prebuild/src/Core/Utilities/Log.cs b/Prebuild/src/Core/Utilities/Log.cs index da2cc96..2f26557 100644 --- a/Prebuild/src/Core/Utilities/Log.cs +++ b/Prebuild/src/Core/Utilities/Log.cs @@ -1,279 +1,279 @@ -#region BSD License -/* -Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) - -Redistribution and use in source and binary forms, with or without modification, are permitted -provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this list of conditions - and the following disclaimer. -* Redistributions in binary form must reproduce the above copyright notice, this list of conditions - and the following disclaimer in the documentation and/or other materials provided with the - distribution. -* The name of the author may not be used to endorse or promote products derived from this software - without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, -BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING -IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#endregion - -#region CVS Information -/* - * $Source$ - * $Author: jendave $ - * $Date: 2006-01-28 01:49:58 +0100 (lö, 28 jan 2006) $ - * $Revision: 71 $ - */ -#endregion - -using System; -using System.IO; - -namespace Prebuild.Core.Utilities -{ - /// - /// - /// - public enum LogType - { - /// - /// - /// - None, - /// - /// - /// - Info, - /// - /// - /// - Warning, - /// - /// - /// - Error - } - - /// - /// - /// - [Flags] - public enum LogTargets - { - /// - /// - /// - None = 0, - /// - /// - /// - Null = 1, - /// - /// - /// - File = 2, - /// - /// - /// - Console = 4 - } - - /// - /// Summary description for Log. - /// - public class Log : IDisposable - { - #region Fields - - private StreamWriter m_Writer; - private LogTargets m_Target = LogTargets.Null; - bool disposed; - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The target. - /// Name of the file. - public Log(LogTargets target, string fileName) - { - m_Target = target; - - if((m_Target & LogTargets.File) != 0) - { - m_Writer = new StreamWriter(fileName, false); - } - } - - #endregion - - #region Public Methods - - /// - /// Writes this instance. - /// - public void Write() - { - Write(string.Empty); - } - - /// - /// Writes the specified MSG. - /// - /// The MSG. - public void Write(string msg) - { - if((m_Target & LogTargets.Null) != 0) - { - return; - } - - if((m_Target & LogTargets.Console) != 0) - { - Console.WriteLine(msg); - } - if((m_Target & LogTargets.File) != 0 && m_Writer != null) - { - m_Writer.WriteLine(msg); - } - } - - /// - /// Writes the specified format. - /// - /// The format. - /// The args. - public void Write(string format, params object[] args) - { - Write(string.Format(format,args)); - } - - /// - /// Writes the specified type. - /// - /// The type. - /// The format. - /// The args. - public void Write(LogType type, string format, params object[] args) - { - if((m_Target & LogTargets.Null) != 0) - { - return; - } - - string str = ""; - switch(type) - { - case LogType.Info: - str = "[I] "; - break; - case LogType.Warning: - str = "[!] "; - break; - case LogType.Error: - str = "[X] "; - break; - } - - Write(str + format,args); - } - - /// - /// Writes the exception. - /// - /// The type. - /// The ex. - public void WriteException(LogType type, Exception ex) - { - if(ex != null) - { - Write(type, ex.Message); - //#if DEBUG - m_Writer.WriteLine("Exception @{0} stack trace [[", ex.TargetSite.Name); - m_Writer.WriteLine(ex.StackTrace); - m_Writer.WriteLine("]]"); - //#endif - } - } - - /// - /// Flushes this instance. - /// - public void Flush() - { - if(m_Writer != null) - { - m_Writer.Flush(); - } - } - - #endregion - - #region IDisposable Members - - /// - /// Performs application-defined tasks associated with freeing, releasing, or - /// resetting unmanaged resources. - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Dispose objects - /// - /// - /// If true, it will dispose close the handle - /// - /// - /// Will dispose managed and unmanaged resources. - /// - protected virtual void Dispose(bool disposing) - { - if (!this.disposed) - { - if (disposing) - { - if (m_Writer != null) - { - m_Writer.Close(); - m_Writer = null; - } - } - } - this.disposed = true; - } - - /// - /// - /// - ~Log() - { - this.Dispose(false); - } - - /// - /// Closes and destroys this object - /// - /// - /// Same as Dispose(true) - /// - public void Close() - { - Dispose(); - } - - #endregion - } -} +#region BSD License +/* +Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) + +Redistribution and use in source and binary forms, with or without modification, are permitted +provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions + and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions + and the following disclaimer in the documentation and/or other materials provided with the + distribution. +* The name of the author may not be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#endregion + +#region CVS Information +/* + * $Source$ + * $Author: jendave $ + * $Date: 2006-01-28 01:49:58 +0100 (lö, 28 jan 2006) $ + * $Revision: 71 $ + */ +#endregion + +using System; +using System.IO; + +namespace Prebuild.Core.Utilities +{ + /// + /// + /// + public enum LogType + { + /// + /// + /// + None, + /// + /// + /// + Info, + /// + /// + /// + Warning, + /// + /// + /// + Error + } + + /// + /// + /// + [Flags] + public enum LogTargets + { + /// + /// + /// + None = 0, + /// + /// + /// + Null = 1, + /// + /// + /// + File = 2, + /// + /// + /// + Console = 4 + } + + /// + /// Summary description for Log. + /// + public class Log : IDisposable + { + #region Fields + + private StreamWriter m_Writer; + private LogTargets m_Target = LogTargets.Null; + bool disposed; + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the class. + /// + /// The target. + /// Name of the file. + public Log(LogTargets target, string fileName) + { + m_Target = target; + + if((m_Target & LogTargets.File) != 0) + { + m_Writer = new StreamWriter(fileName, false); + } + } + + #endregion + + #region Public Methods + + /// + /// Writes this instance. + /// + public void Write() + { + Write(string.Empty); + } + + /// + /// Writes the specified MSG. + /// + /// The MSG. + public void Write(string msg) + { + if((m_Target & LogTargets.Null) != 0) + { + return; + } + + if((m_Target & LogTargets.Console) != 0) + { + Console.WriteLine(msg); + } + if((m_Target & LogTargets.File) != 0 && m_Writer != null) + { + m_Writer.WriteLine(msg); + } + } + + /// + /// Writes the specified format. + /// + /// The format. + /// The args. + public void Write(string format, params object[] args) + { + Write(string.Format(format,args)); + } + + /// + /// Writes the specified type. + /// + /// The type. + /// The format. + /// The args. + public void Write(LogType type, string format, params object[] args) + { + if((m_Target & LogTargets.Null) != 0) + { + return; + } + + string str = ""; + switch(type) + { + case LogType.Info: + str = "[I] "; + break; + case LogType.Warning: + str = "[!] "; + break; + case LogType.Error: + str = "[X] "; + break; + } + + Write(str + format,args); + } + + /// + /// Writes the exception. + /// + /// The type. + /// The ex. + public void WriteException(LogType type, Exception ex) + { + if(ex != null) + { + Write(type, ex.Message); + //#if DEBUG + m_Writer.WriteLine("Exception @{0} stack trace [[", ex.TargetSite.Name); + m_Writer.WriteLine(ex.StackTrace); + m_Writer.WriteLine("]]"); + //#endif + } + } + + /// + /// Flushes this instance. + /// + public void Flush() + { + if(m_Writer != null) + { + m_Writer.Flush(); + } + } + + #endregion + + #region IDisposable Members + + /// + /// Performs application-defined tasks associated with freeing, releasing, or + /// resetting unmanaged resources. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Dispose objects + /// + /// + /// If true, it will dispose close the handle + /// + /// + /// Will dispose managed and unmanaged resources. + /// + protected virtual void Dispose(bool disposing) + { + if (!this.disposed) + { + if (disposing) + { + if (m_Writer != null) + { + m_Writer.Close(); + m_Writer = null; + } + } + } + this.disposed = true; + } + + /// + /// + /// + ~Log() + { + this.Dispose(false); + } + + /// + /// Closes and destroys this object + /// + /// + /// Same as Dispose(true) + /// + public void Close() + { + Dispose(); + } + + #endregion + } +} -- cgit v1.1