2007-07-16 15:40:11 +00:00
|
|
|
/*
|
2007-09-10 08:14:38 +00:00
|
|
|
* Copyright (c) Contributors, http://opensimulator.org/
|
2007-07-16 15:40:11 +00:00
|
|
|
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* * Neither the name of the OpenSim Project nor the
|
|
|
|
* names of its contributors may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
2007-12-10 05:25:16 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
2007-07-16 15:40:11 +00:00
|
|
|
* 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 CONTRIBUTORS 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.
|
|
|
|
*
|
|
|
|
*/
|
2007-10-30 09:05:31 +00:00
|
|
|
|
2007-07-16 15:40:11 +00:00
|
|
|
using System;
|
2007-08-15 15:24:37 +00:00
|
|
|
using System.Collections.Generic;
|
2007-10-30 09:05:31 +00:00
|
|
|
using System.Data;
|
2007-08-03 18:01:55 +00:00
|
|
|
using System.IO;
|
2007-07-18 21:55:24 +00:00
|
|
|
using System.Net;
|
2007-10-30 09:05:31 +00:00
|
|
|
using System.Net.Sockets;
|
|
|
|
using System.Security.Cryptography;
|
2007-07-16 15:40:11 +00:00
|
|
|
using System.Text;
|
|
|
|
using libsecondlife;
|
2007-08-10 17:22:54 +00:00
|
|
|
using Nini.Config;
|
|
|
|
|
2007-10-29 21:46:25 +00:00
|
|
|
namespace OpenSim.Framework
|
2007-07-16 15:40:11 +00:00
|
|
|
{
|
|
|
|
public class Util
|
|
|
|
{
|
|
|
|
private static Random randomClass = new Random();
|
|
|
|
private static uint nextXferID = 5000;
|
|
|
|
private static object XferLock = new object();
|
2007-08-15 15:24:37 +00:00
|
|
|
private static Dictionary<LLUUID, string> capsURLS = new Dictionary<LLUUID, string>();
|
2007-07-16 15:40:11 +00:00
|
|
|
|
2007-12-28 05:25:21 +00:00
|
|
|
#region Vector Equasions
|
2008-02-08 17:54:30 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get the distance between two 3d vectors
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="a">A 3d vector</param>
|
|
|
|
/// <param name="b">A 3d vector</param>
|
|
|
|
/// <returns>The distance between the two vectors</returns>
|
2007-12-27 21:41:48 +00:00
|
|
|
public static double GetDistanceTo(LLVector3 a, LLVector3 b)
|
|
|
|
{
|
|
|
|
float dx = a.X - b.X;
|
|
|
|
float dy = a.Y - b.Y;
|
|
|
|
float dz = a.Z - b.Z;
|
|
|
|
return Math.Sqrt(dx*dx + dy*dy + dz*dz);
|
|
|
|
}
|
2008-02-08 17:54:30 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get the magnitude of a 3d vector
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="a">A 3d vector</param>
|
|
|
|
/// <returns>The magnitude of the vector</returns>
|
2007-12-28 05:25:21 +00:00
|
|
|
public static double GetMagnitude(LLVector3 a) {
|
|
|
|
return Math.Sqrt((a.X * a.X) + (a.Y * a.Y) + (a.Z * a.Z));
|
|
|
|
}
|
2008-02-08 17:54:30 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get a normalized form of a 3d vector
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="a">A 3d vector</param>
|
|
|
|
/// <returns>A new vector which is normalized form of the vector</returns>
|
|
|
|
/// <remarks>The vector paramater cannot be <0,0,0></remarks>
|
|
|
|
public static LLVector3 GetNormalizedVector(LLVector3 a)
|
2007-12-28 05:25:21 +00:00
|
|
|
{
|
2008-02-08 17:54:30 +00:00
|
|
|
if (IsZeroVector(a))
|
|
|
|
throw new ArgumentException("Vector paramater cannot be a zero vector.");
|
|
|
|
|
2007-12-28 05:25:21 +00:00
|
|
|
float Mag = (float)GetMagnitude(a);
|
|
|
|
return new LLVector3(a.X / Mag, a.Y / Mag, a.Z / Mag);
|
2008-02-08 17:54:30 +00:00
|
|
|
}
|
2007-12-28 05:25:21 +00:00
|
|
|
|
2008-02-08 17:54:30 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns if a vector is a zero vector (has all zero components)
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static bool IsZeroVector( LLVector3 v )
|
|
|
|
{
|
|
|
|
if( v.X == 0 && v.Y == 0 && v.Z == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2007-12-28 05:25:21 +00:00
|
|
|
}
|
|
|
|
# endregion
|
2007-10-19 19:20:18 +00:00
|
|
|
|
2007-07-16 15:40:11 +00:00
|
|
|
public static ulong UIntsToLong(uint X, uint Y)
|
|
|
|
{
|
|
|
|
return Helpers.UIntsToLong(X, Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Random RandomClass
|
|
|
|
{
|
2007-10-30 09:05:31 +00:00
|
|
|
get { return randomClass; }
|
2007-07-16 15:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static uint GetNextXferID()
|
|
|
|
{
|
|
|
|
uint id = 0;
|
2007-10-30 09:05:31 +00:00
|
|
|
lock (XferLock)
|
2007-07-16 15:40:11 +00:00
|
|
|
{
|
|
|
|
id = nextXferID;
|
|
|
|
nextXferID++;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2007-08-15 15:24:37 +00:00
|
|
|
public Util()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-29 07:14:04 +00:00
|
|
|
public static string GetFileName(string file)
|
|
|
|
{
|
|
|
|
// Return just the filename on UNIX platforms
|
|
|
|
// TODO: this should be customisable with a prefix, but that's something to do later.
|
2007-10-30 09:05:31 +00:00
|
|
|
if (Environment.OSVersion.Platform == PlatformID.Unix)
|
2007-07-29 07:14:04 +00:00
|
|
|
{
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return %APPDATA%/OpenSim/file for 2K/XP/NT/2K3/VISTA
|
|
|
|
// TODO: Switch this to System.Enviroment.SpecialFolders.ApplicationData
|
2007-10-30 09:05:31 +00:00
|
|
|
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
|
2007-07-29 07:14:04 +00:00
|
|
|
{
|
2007-10-30 09:05:31 +00:00
|
|
|
if (!Directory.Exists("%APPDATA%\\OpenSim\\"))
|
2007-07-29 07:14:04 +00:00
|
|
|
{
|
2007-10-30 09:05:31 +00:00
|
|
|
Directory.CreateDirectory("%APPDATA%\\OpenSim");
|
2007-07-29 07:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "%APPDATA%\\OpenSim\\" + file;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Catch all - covers older windows versions
|
|
|
|
// (but those probably wont work anyway)
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool IsEnvironmentSupported(ref string reason)
|
|
|
|
{
|
|
|
|
// Must have .NET 2.0 (Generics / libsl)
|
2007-10-30 09:05:31 +00:00
|
|
|
if (Environment.Version.Major < 2)
|
2007-07-29 07:14:04 +00:00
|
|
|
{
|
|
|
|
reason = ".NET 1.0/1.1 lacks components that is used by OpenSim";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Windows 95/98/ME are unsupported
|
2007-10-30 09:05:31 +00:00
|
|
|
if (Environment.OSVersion.Platform == PlatformID.Win32Windows &&
|
|
|
|
Environment.OSVersion.Platform != PlatformID.Win32NT)
|
2007-07-29 07:14:04 +00:00
|
|
|
{
|
|
|
|
reason = "Windows 95/98/ME will not run OpenSim";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Windows 2000 / Pre-SP2 XP
|
2007-10-30 09:05:31 +00:00
|
|
|
if (Environment.OSVersion.Version.Major == 5 && (
|
|
|
|
Environment.OSVersion.Version.Minor == 0))
|
2007-07-29 07:14:04 +00:00
|
|
|
{
|
|
|
|
reason = "Please update to Windows XP Service Pack 2 or Server2003";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-16 15:40:11 +00:00
|
|
|
public static int UnixTimeSinceEpoch()
|
|
|
|
{
|
|
|
|
TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
|
2007-10-30 09:05:31 +00:00
|
|
|
int timestamp = (int) t.TotalSeconds;
|
2007-07-16 15:40:11 +00:00
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string Md5Hash(string pass)
|
|
|
|
{
|
|
|
|
MD5 md5 = MD5CryptoServiceProvider.Create();
|
|
|
|
byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(pass));
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (int i = 0; i < dataMd5.Length; i++)
|
|
|
|
sb.AppendFormat("{0:x2}", dataMd5[i]);
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string GetRandomCapsPath()
|
|
|
|
{
|
|
|
|
LLUUID caps = LLUUID.Random();
|
2007-12-20 05:43:02 +00:00
|
|
|
string capsPath = caps.ToString();
|
2007-07-16 15:40:11 +00:00
|
|
|
capsPath = capsPath.Remove(capsPath.Length - 4, 4);
|
|
|
|
return capsPath;
|
|
|
|
}
|
|
|
|
|
2007-09-08 07:50:31 +00:00
|
|
|
public static int fast_distance2d(int x, int y)
|
|
|
|
{
|
2007-10-30 09:05:31 +00:00
|
|
|
x = Math.Abs(x);
|
|
|
|
y = Math.Abs(y);
|
2007-07-16 15:40:11 +00:00
|
|
|
|
2007-10-30 09:05:31 +00:00
|
|
|
int min = Math.Min(x, y);
|
2007-07-16 15:40:11 +00:00
|
|
|
|
2007-09-08 07:50:31 +00:00
|
|
|
return (x + y - (min >> 1) - (min >> 2) + (min >> 4));
|
|
|
|
}
|
2007-07-16 15:40:11 +00:00
|
|
|
|
|
|
|
public static string FieldToString(byte[] bytes)
|
|
|
|
{
|
|
|
|
return FieldToString(bytes, String.Empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Convert a variable length field (byte array) to a string, with a
|
|
|
|
/// field name prepended to each line of the output
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>If the byte array has unprintable characters in it, a
|
|
|
|
/// hex dump will be put in the string instead</remarks>
|
|
|
|
/// <param name="bytes">The byte array to convert to a string</param>
|
|
|
|
/// <param name="fieldName">A field name to prepend to each line of output</param>
|
|
|
|
/// <returns>An ASCII string or a string containing a hex dump, minus
|
|
|
|
/// the null terminator</returns>
|
|
|
|
public static string FieldToString(byte[] bytes, string fieldName)
|
|
|
|
{
|
|
|
|
// Check for a common case
|
|
|
|
if (bytes.Length == 0) return String.Empty;
|
|
|
|
|
|
|
|
StringBuilder output = new StringBuilder();
|
|
|
|
bool printable = true;
|
|
|
|
|
|
|
|
for (int i = 0; i < bytes.Length; ++i)
|
|
|
|
{
|
|
|
|
// Check if there are any unprintable characters in the array
|
|
|
|
if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09
|
|
|
|
&& bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00)
|
|
|
|
{
|
|
|
|
printable = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (printable)
|
|
|
|
{
|
|
|
|
if (fieldName.Length > 0)
|
|
|
|
{
|
|
|
|
output.Append(fieldName);
|
|
|
|
output.Append(": ");
|
|
|
|
}
|
|
|
|
|
2008-01-10 06:49:29 +00:00
|
|
|
output.Append(CleanString(UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1)));
|
2007-07-16 15:40:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < bytes.Length; i += 16)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
output.Append(Environment.NewLine);
|
|
|
|
if (fieldName.Length > 0)
|
|
|
|
{
|
|
|
|
output.Append(fieldName);
|
|
|
|
output.Append(": ");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < 16; j++)
|
|
|
|
{
|
|
|
|
if ((i + j) < bytes.Length)
|
|
|
|
output.Append(String.Format("{0:X2} ", bytes[i + j]));
|
|
|
|
else
|
|
|
|
output.Append(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < 16 && (i + j) < bytes.Length; j++)
|
|
|
|
{
|
|
|
|
if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E)
|
2007-10-30 09:05:31 +00:00
|
|
|
output.Append((char) bytes[i + j]);
|
2007-07-16 15:40:11 +00:00
|
|
|
else
|
|
|
|
output.Append(".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.ToString();
|
|
|
|
}
|
2007-07-18 21:55:24 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a IP address from a specified DNS, favouring IPv4 addresses.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dnsAddress">DNS Hostname</param>
|
|
|
|
/// <returns>An IP address, or null</returns>
|
|
|
|
public static IPAddress GetHostFromDNS(string dnsAddress)
|
|
|
|
{
|
2007-09-12 05:37:46 +00:00
|
|
|
// Is it already a valid IP? No need to look it up.
|
|
|
|
IPAddress ipa;
|
|
|
|
if (IPAddress.TryParse(dnsAddress, out ipa))
|
|
|
|
return ipa;
|
|
|
|
|
|
|
|
// Not an IP, lookup required
|
2007-07-18 21:55:24 +00:00
|
|
|
IPAddress[] hosts = Dns.GetHostEntry(dnsAddress).AddressList;
|
|
|
|
|
|
|
|
foreach (IPAddress host in hosts)
|
|
|
|
{
|
2007-10-30 09:05:31 +00:00
|
|
|
if (host.AddressFamily == AddressFamily.InterNetwork)
|
2007-07-18 21:55:24 +00:00
|
|
|
{
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hosts.Length > 0)
|
|
|
|
return hosts[0];
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2007-07-30 21:12:29 +00:00
|
|
|
public static IPAddress GetLocalHost()
|
|
|
|
{
|
|
|
|
string dnsAddress = "localhost";
|
|
|
|
|
|
|
|
IPAddress[] hosts = Dns.GetHostEntry(dnsAddress).AddressList;
|
|
|
|
|
|
|
|
foreach (IPAddress host in hosts)
|
|
|
|
{
|
2007-10-30 09:05:31 +00:00
|
|
|
if (!IPAddress.IsLoopback(host) && host.AddressFamily == AddressFamily.InterNetwork)
|
2007-07-30 21:12:29 +00:00
|
|
|
{
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hosts.Length > 0)
|
|
|
|
return hosts[0];
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2007-08-03 18:01:55 +00:00
|
|
|
//
|
|
|
|
// directory locations
|
|
|
|
//
|
2007-10-19 19:20:18 +00:00
|
|
|
|
2007-08-03 18:01:55 +00:00
|
|
|
public static string homeDir()
|
|
|
|
{
|
|
|
|
string temp;
|
|
|
|
// string personal=(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
|
|
|
|
// temp = Path.Combine(personal,".OpenSim");
|
2007-10-30 09:05:31 +00:00
|
|
|
temp = ".";
|
2007-08-03 18:01:55 +00:00
|
|
|
return temp;
|
|
|
|
}
|
2007-12-29 19:01:55 +00:00
|
|
|
|
|
|
|
public static string assetsDir()
|
|
|
|
{
|
2007-12-31 23:20:49 +00:00
|
|
|
return Path.Combine(configDir(), "assets");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string inventoryDir()
|
|
|
|
{
|
|
|
|
return Path.Combine(configDir(), "inventory");
|
2007-12-29 19:01:55 +00:00
|
|
|
}
|
2007-08-03 18:01:55 +00:00
|
|
|
|
|
|
|
public static string configDir()
|
|
|
|
{
|
|
|
|
string temp;
|
|
|
|
temp = ".";
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string dataDir()
|
|
|
|
{
|
|
|
|
string temp;
|
|
|
|
temp = ".";
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string logDir()
|
|
|
|
{
|
|
|
|
string temp;
|
|
|
|
temp = ".";
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2007-08-15 15:24:37 +00:00
|
|
|
public static string GetCapsURL(LLUUID userID)
|
2007-07-16 15:40:11 +00:00
|
|
|
{
|
2007-08-15 15:24:37 +00:00
|
|
|
if (capsURLS.ContainsKey(userID))
|
|
|
|
{
|
|
|
|
return capsURLS[userID];
|
|
|
|
}
|
2008-01-15 02:09:55 +00:00
|
|
|
return String.Empty;
|
2007-08-15 15:24:37 +00:00
|
|
|
}
|
2007-07-16 15:40:11 +00:00
|
|
|
|
2007-08-15 15:24:37 +00:00
|
|
|
public static void SetCapsURL(LLUUID userID, string url)
|
|
|
|
{
|
|
|
|
if (capsURLS.ContainsKey(userID))
|
|
|
|
{
|
|
|
|
capsURLS[userID] = url;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
capsURLS.Add(userID, url);
|
|
|
|
}
|
2007-07-16 15:40:11 +00:00
|
|
|
}
|
2007-08-10 17:22:54 +00:00
|
|
|
|
|
|
|
// Nini (config) related Methods
|
2007-10-30 09:05:31 +00:00
|
|
|
public static IConfigSource ConvertDataRowToXMLConfig(DataRow row, string fileName)
|
2007-08-10 17:22:54 +00:00
|
|
|
{
|
2007-10-30 09:05:31 +00:00
|
|
|
if (!File.Exists(fileName))
|
2007-08-10 17:22:54 +00:00
|
|
|
{
|
|
|
|
//create new file
|
|
|
|
}
|
|
|
|
XmlConfigSource config = new XmlConfigSource(fileName);
|
|
|
|
AddDataRowToConfig(config, row);
|
|
|
|
config.Save();
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2007-10-30 09:05:31 +00:00
|
|
|
public static void AddDataRowToConfig(IConfigSource config, DataRow row)
|
2007-08-10 17:22:54 +00:00
|
|
|
{
|
2007-10-30 09:05:31 +00:00
|
|
|
config.Configs.Add((string) row[0]);
|
2007-08-10 17:22:54 +00:00
|
|
|
for (int i = 0; i < row.Table.Columns.Count; i++)
|
|
|
|
{
|
2007-10-30 09:05:31 +00:00
|
|
|
config.Configs[(string) row[0]].Set(row.Table.Columns[i].ColumnName, row[i]);
|
2007-08-10 17:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-19 08:44:25 +00:00
|
|
|
|
|
|
|
public static float Clip(float x, float min, float max)
|
|
|
|
{
|
|
|
|
return Math.Min(Math.Max(x, min), max);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int Clip(int x, int min, int max)
|
|
|
|
{
|
|
|
|
return Math.Min(Math.Max(x, min), max);
|
|
|
|
}
|
2007-12-27 21:41:48 +00:00
|
|
|
|
2007-12-20 18:43:39 +00:00
|
|
|
/// <summary>
|
2007-12-20 19:13:34 +00:00
|
|
|
/// Convert an LLUUID to a raw uuid string. Right now this is a string without hyphens.
|
2007-12-20 18:43:39 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="lluuid"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static String ToRawUuidString(LLUUID lluuid)
|
|
|
|
{
|
|
|
|
return lluuid.UUID.ToString("n");
|
|
|
|
}
|
2007-12-28 23:19:03 +00:00
|
|
|
|
2008-01-10 06:49:29 +00:00
|
|
|
public static string CleanString(string input)
|
|
|
|
{
|
|
|
|
if(input.Length == 0)
|
|
|
|
return input;
|
2007-12-28 23:19:03 +00:00
|
|
|
|
2008-01-10 06:49:29 +00:00
|
|
|
int clip=input.Length;
|
2007-12-28 23:19:03 +00:00
|
|
|
|
2008-01-10 06:49:29 +00:00
|
|
|
// Test for ++ string terminator
|
|
|
|
int pos=input.IndexOf("\0");
|
|
|
|
if(pos != -1 && pos < clip)
|
|
|
|
clip=pos;
|
2007-12-28 23:19:03 +00:00
|
|
|
|
2008-01-10 06:49:29 +00:00
|
|
|
// Test for CR
|
|
|
|
pos=input.IndexOf("\r");
|
|
|
|
if(pos != -1 && pos < clip)
|
|
|
|
clip=pos;
|
2007-12-28 23:19:03 +00:00
|
|
|
|
2008-01-10 06:49:29 +00:00
|
|
|
// Test for LF
|
|
|
|
pos=input.IndexOf("\n");
|
|
|
|
if(pos != -1 && pos < clip)
|
|
|
|
clip=pos;
|
2007-12-28 23:19:03 +00:00
|
|
|
|
2008-01-10 06:49:29 +00:00
|
|
|
// Truncate string before first end-of-line character found
|
|
|
|
return input.Substring(0, clip);
|
|
|
|
}
|
2008-01-23 23:32:19 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// returns the contents of /etc/issue on Unix Systems
|
|
|
|
/// Use this for where it's absolutely necessary to implement platform specific stuff
|
|
|
|
/// ( like the ODE library :P
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static string ReadEtcIssue()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
StreamReader sr = new StreamReader("/etc/issue.net");
|
|
|
|
string issue = sr.ReadToEnd();
|
|
|
|
sr.Close();
|
|
|
|
return issue;
|
|
|
|
}
|
|
|
|
catch (System.Exception)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2007-07-16 15:40:11 +00:00
|
|
|
}
|
2007-12-28 23:19:03 +00:00
|
|
|
}
|