//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// PLEASE DO NOT EDIT WITHOUT THE EXPLICIT CONSENT OF THE AUTHOR! \\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
using System;
using System.IO;
using System.Text;
using System.Collections;
using Ilog.Language.Util;
namespace Ilog.Language.Tools
{
/**
* This class implements a grab-bag of useful methods. If you need
* something that is not defined where you'd expect it although it is
* useful, can be reused many times, and is generic enough to warrant
* being used in arbitrary contexts, chances are it's in here - and if
* it isn't, it should be!
*
* @version Last modified on Tue May 31 16:58:49 2005 by hak
* @author Hassan Aït-Kaci
* @copyright © 2000 ILOG, S.A.
*/
public class Misc
{
/**
* Asks the user for a Y/N answer and returns it boolean
* interpretation - the default answer is yes.
*/
public static bool AskYesNo (string s)
{
return AskYesNo(s,true);
}
/**
* Asks the user for a Y/N answer with the specified default
* answer.
*/
public static bool AskYesNo (string s, bool defans)
{
Console.Write(s+"? (y/n) ["+(defans?"y":"n")+"] > ");
try
{
switch (Console.Read())
{
case '\n':
return defans;
case 'y': case 'Y':
while (Console.Read() != '\n');
return true;
case 'n': case 'N':
while (Console.Read() != '\n');
return false;
default:
while (Console.Read() != '\n');
break;
}
}
catch (IOException e)
{
Console.Error.WriteLine("*** IO Exception when asking: \""+s+"\"");
Console.Error.WriteLine(e.StackTrace);
}
Console.WriteLine("Please answer yes or no!...");
return AskYesNo(s,defans);
}
/**
* Prompts the user with the specified string on stdout, then
* returns the string entered up to carriage return on stdin.
*/
public static string Prompt (string s)
{
return Prompt(s,Console.Out);
}
/**
* Prompts the user with the specified string on the specified
* PrintStream, then returns the string entered up to carriage
* return on stdin.
*/
public static string Prompt (string s, TextWriter output)
{
output.Write(s+" ");
StringBuilder ans = new StringBuilder();
int ch;
try
{
while ((ch=Console.In.Read()) != CC.EOL)
ans.Append((char)ch);
}
catch (IOException e)
{
output.WriteLine("*** IO Exception when prompting: \""+s+"\"");
Console.Error.WriteLine(e);
}
return ans.ToString();
}
/**
* Return a string representation of the given list as a bracketed
* comma-separated list of its elements.
*/
public static string ListToString (IList list)
{
return ListToString(list,"[",", ","]");
}
/**
* Return a string representation of the given list using the
* specified start, separator, and end strings.
*/
public static string ListToString (IList list, string start,
string separator, string end)
{
StringBuilder s = new StringBuilder(start);
string sep = "";
foreach (object elt in list)
{
s.Append(sep).Append(elt);
sep = separator;
}
return s.Append(end).ToString();
}
/**
* Returns the string obtained from the specified string as an
* identical one except that special characters are rendered as
* their escape sequences.
*/
public static string Stringify (string s)
{
return Stringify(s,'"','\\');
}
/**
* Returns the string obtained from the specified string as an
* identical one except that special characters are rendered as
* their escape sequences. The last two arguments are the quote
* and escape characters.
*/
public static string Stringify (string s, char quote, char escape)
{
StringBuilder buf = new StringBuilder();
int length = s.Length;
for (int i=0; iquote and escape are
* the quote and escape characters, respectively.
*/
public static string Quotify (string s, char quote, char escape)
{
StringBuilder buf = new StringBuilder();
int length = s.Length;
for (int i=0; in
* using as many zeros on the left, using at least width
* digits in total.
*/
public static string ZeroPaddedString (int n, int width)
{
if (n < 0)
return "-"+ZeroPaddedString(-n,width);
return n.ToString().PadLeft(width,'0');
}
/**
* Returns the number of decimal digits necessary to write the
* given long integer n.
*/
public static int NumWidth (long n)
{
if (n < 0)
return 1 + NumWidth(-n);
int w = 1;
while ((n /= 10) != 0)
w++;
return w;
}
/**
* Returns the substring of the given string that starts
* at the first letter occurrence in the string. If none
* returns the full string.
*/
public static string LetterSubstring (string s)
{
int i = 0;
while (i': return ">";
case '&': return "&";
}
return c.ToString();
}
/**
* Returns a legible, possibly truncated, "view" of a Listable
* object as a string. Each element is displayed in a line offset
* by a margin. The name is used as information displayed at the
* top in the margin. The offset is used to set the margin. The
* width indicates when to truncate the view with an ellipsis
* mark.
*/
public static string View (Listable listable, string name, int off, int width)
{
if (listable == null)
return null;
int marginWidth = off + name.Length + 5; // 5 for " ==> "
StringBuilder margin = new StringBuilder(marginWidth);
for (int i=marginWidth; i-->0;)
margin.Append(' ');
StringBuilder sep = new StringBuilder(width);
for (int i=width; i-->0;)
sep.Append('-');
StringBuilder buf
= new StringBuilder(listable.Count*(marginWidth+width+1)).Append('\n');
buf.Append(margin).Append(sep).Append('\n')
.Append(margin.ToString().Substring(0,off))
.Append(name).Append(" ==> ");
IEnumerator e = listable.GetEnumerator();
if (e.MoveNext())
buf.Append(Etc(width,e.Current));
buf.Append('\n');
while (e.MoveNext())
buf.Append(margin).Append(Etc(width,e.Current)).Append('\n');
buf.Append(margin).Append(sep).Append('\n');
return buf.ToString();
}
/**
* Returns a possibly truncated string form no longer than the
* specified width for the specified object (including trailing
* " ..." if the string form is longer than the specified width).
*/
public static string Etc (int width, object obj)
{
if (obj == null)
return null;
String s = Stringify(obj.ToString(),'\\','\\'); // leave double quotes unescaped
return s.Length <= width ? s : s.Substring(0,Math.Min(width-4,s.Length)) + " ...";
}
/**
* Returns an explicit string for the specified locatable.
*/
public static string LocationString (Locatable extent)
{
if (extent != null)
{
Location start = extent.GetStart();
Location end = extent.GetEnd();
StringBuilder s = new StringBuilder();
if (start == null)
if (end == null)
return "";
else
s.Append(LocationString(end));
else
if (end == null || start.Equals(end))
s.Append(LocationString(start));
else
if (start.GetFile() == end.GetFile())
if (start.GetLine() == end.GetLine())
s.Append(start.GetFile())
.Append(" (")
.Append("line ")
.Append(start.GetLine())
.Append(", ")
.Append("columns ")
.Append(start.GetColumn())
.Append("...")
.Append(end.GetColumn())
.Append(")");
else
s.Append(LocationString(start))
.Append("...")
.Append(" (")
.Append("line ")
.Append(end.GetLine())
.Append(", ")
.Append("column ")
.Append(end.GetColumn())
.Append(")");
else
s.Append(LocationString(start))
.Append("...")
.Append(LocationString(end));
return s.ToString();
}
return "";
}
/**
* Returns an explicit string for the specified location.
*/
public static string LocationString (Location location)
{
StringBuilder s = new StringBuilder();
s.Append(location.GetFile())
.Append(" (")
.Append("line ")
.Append(location.GetLine())
.Append(", ")
.Append("column ")
.Append(location.GetColumn())
.Append(")");
return s.ToString();
}
/**
* A Java-compatible substring method. It returns the substring of
* s starting at index start and ending one position before end.
*/
public static string Substring (string s, int start, int end)
{
return s.Substring(start,end-start);
}
/**
* Returns a printable form of the given character code.
*/
public static string Pform (int c)
{
switch (c)
{
case CC.EOF: return "EOF"; // End of file
case CC.EOI: return "EOI"; // End of inclusion
case CC.SOI: return "SOI"; // Start of inclusion
case CC.WRD: return "WRD"; // Word
case CC.NUM: return "NUM"; // Number
case CC.NTG: return "NTG"; // Nothing
case CC.SPL: return "SPL"; // Special
case CC.BIP: return "BIP"; // Beep
case CC.EOL: return "\\n"; // End of line
case CC.TAB: return "\\t"; // Tab
case CC.CRT: return "\\r"; // Carriage return
case CC.BSP: return "\\b"; // Backspace
case CC.FFD: return "\\f"; // Form feed
case CC.BSL: return "\\"; // Backslash
case CC.SQT: return "'"; // Single quote
case CC.DQT: return "\""; // Double quote
case CC.BQT: return "`"; // Back quote
default: return ((char)c).ToString();
}
}
}
}