//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// PLEASE DO NOT EDIT WITHOUT THE EXPLICIT CONSENT OF THE AUTHOR! \\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
using System;
using System.IO;
/**
* This class defines some simple static functions that manipulate
* file names. It has no constructor since all its methods are
* static and accessible through the class name.
*
* @version Last modified on Tue May 17 15:44:37 2005 by hak
* @author Hassan Aït-Kaci
* @copyright © 2000 ILOG, S.A.
*/
namespace Ilog.Language.Tools
{
public sealed class FileTools
{
static private char _separator = Path.DirectorySeparatorChar;
public static void SetSeparator (String s)
{
if (s.Length == 0)
throw new ApplicationException("*** Empty file separator");
if (s.Length > 1)
Console.Error.WriteLine("*** WARNING: bad file separator: \""+
s+"\" - taking only first char: '"+
s[0]+"\'");
_separator = s[0];
}
public static void SetSeparator (char c)
{
_separator = c;
}
/**
* Returns the position of the dot marking the file's extension,
* or -1 if there is none. Note that if name = foo.bar/baz,
* this returns -1.
*/
public static int DotPosition (String name)
{
int dotPos = name.LastIndexOf('.');
if (dotPos < 0) return dotPos;
int sepPos = name.LastIndexOf(_separator);
if (sepPos < dotPos) return dotPos;
return -1;
}
/**
* Returns true iff the specified name has an extension dot
* (though not necessarily an extension).
*/
public static bool HasDot (String name)
{
return DotPosition(name) > 0;
}
/**
* Returns the name.ext form of the specified file name.
*/
public static String SimpleName (String name)
{
if (!HasDot(name))
return Prefix(name);
return Prefix(name) + "." + Suffix(name);
}
/**
* Returns the name.ext form of the specified file.
*/
public static String SimpleName (FileStream file)
{
return SimpleName(file.Name);
}
/**
* Returns the relative prefix of the specified file name.
*/
public static String Prefix (String name)
{
int start = name.LastIndexOf(_separator) + 1;
int end = DotPosition(name);
if (end < 0)
return name.Substring(start);
return name.Substring(start,end-start);
}
/**
* Returns the absolute prefix of the specified file name.
*/
public static String FullPrefix (String name)
{
int end = DotPosition(name);
if (end < 0) return name;
return name.Substring(0,end);
}
/**
* Returns the name prefix of the specified file.
*/
public static String Prefix (FileStream file)
{
return Prefix(file.Name);
}
/**
* Returns the extension of the specified file name.
*/
public static String Extension (String name)
{
return Suffix(name);
}
/**
* Returns the suffix of the specified file name.
*/
public static String Suffix (String name)
{
int dot = DotPosition(name);
if (dot < 0) return "";
return name.Substring(dot+1);
}
/**
* Returns the suffix of the specified file name, or the name as is
* (if it contains no dot).
*/
public static String SuffixIfDot (String name)
{
int dot = DotPosition(name);
if (dot < 0) return name;
return name.Substring(dot+1);
}
/**
* Returns the suffix of the specified file.
*/
public static String Suffix (FileStream file)
{
return Suffix(file.Name);
}
/**
* Returns the directory part of the specified file name.
*/
public static String Dir (String name)
{
int end = name.LastIndexOf(_separator);
if (end < 0)
return "";
return name.Substring(0,end);
}
/**
* Returns the directory part of the specified file.
*/
public static String Dir (FileStream file)
{
return Dir(file.Name);
}
}
}