/*
* Copyright (C) 2004-2005 Jonathan Bindel, jonathanbindel@gmail.com
*
* 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.
*/
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.GZip;
using DCSharp.Security.Cryptography;
using DCSharp.Text;
using LFileDCSharp.Xml.FileList.File;
using LDirectoryDCSharp.Xml.FileList.Directory;
namespace DCSharp.Extras{
public delegate void BlankEventHandler();
public delegate void StringEventHandler(string name);
public delegate void ExceptionEventHandler(Exception ex);
public class Util
{
public static string AppName
{
get
{
return "DC#";
}
}
public static string AppVersion
{
get
{
return Assembly.GetEntryAssembly().GetName().Version.ToString();
}
}
public static string Website
{
get
{
return "http://mono.dcsharp.com/";
}
}
public static void MoveFile(string source, string dest, bool overwrite)
{
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(dest));
if(overwrite && File.Exists(dest))
{
File.Delete(dest);
}
File.Move(source, dest);
}
#region Net
public static string AddressPart(string hostname)
{
int i = hostname.IndexOf(':');
if(i > 0)
{
hostname = hostname.Substring(0, i);
}
hostname = Regex.Replace(hostname, @"\s+", "");
return hostname;
}
public static int PortPart(string hostname)
{
int i = hostname.LastIndexOf(':');
if(i > 0 && i < hostname.Length - 1)
{
string port = hostname.Substring(i + 1);
port = Regex.Match(port, @"\d+").Value;
return int.Parse(port);
}
return -1;
}
private static string localIP;
public static string GetLocalIPAddress()
{
if(localIP == null)
{
IPHostEntry iphostentry = Dns.GetHostEntry(Dns.GetHostName());
localIP = iphostentry.AddressList[0].ToString();
}
return localIP;
}
#endregion
public static bool WholeNumber(string strNumber)
{
if(strNumber.Length > 0)
{
Regex objNotWholePattern = new Regex("[^0-9]");
return !objNotWholePattern.IsMatch(strNumber);
}
return false;
}
public static bool StringMatchesKeywords(string text, string[] keywords)
{
CultureInfo culture = CultureInfo.CurrentCulture;
foreach(string keyword in keywords)
{
if(culture.CompareInfo.IndexOf(text, keyword,
CompareOptions.IgnoreCase) < 0)
{
return false;
}
}
return true;
}
#region FileList
public static void SaveNMDCList(DCSharp.Xml.FileList.FileListing list,
string filename)
{
StringBuilder text = new StringBuilder();
LDirectory[] dirs = list.Directories.ToArray();
for(int i = 0; i < dirs.Length; i++)
{
// Do not add first
if(i > 0)
{
text.Append("\r\n");
}
AppendNMDCEntry(text, dirs[i], 0);
}
byte[] bytes = Encoding.Default.GetBytes(text.ToString());
using(FileStream fs = new FileStream(filename, FileMode.Create,
FileAccess.Write, FileShare.Read))
{
NMDCHuffmanCompressor.Compress(bytes, fs);
}
}
private static void AppendNMDCEntry(StringBuilder text, LDirectory dir,
int level)
{
text.Append(new String('\t', level));
text.Append(dir.Name);
foreach(LDirectory childDir in dir.Directories)
{
text.Append("\r\n");
AppendNMDCEntry(text, childDir, level + 1);
}
foreach(LFile file in dir.Files)
{
text.Append("\r\n");
text.Append(new String('\t', level + 1));
text.Append(file.Name);
text.Append("|");
text.Append(file.Size);
}
}
#endregion
#region Compression
public enum Compression
{
BZip2,
GZip,
Huffman
}
public static void Decompress(Stream instream, Stream outstream,
Compression compression)
{
if(instream.CanRead && outstream.CanWrite)
{
if(compression == Compression.BZip2)
{
BZip2.Decompress(instream, outstream);
}
else if(compression == Compression.GZip)
{
Stream gzipStream = new GZipInputStream(instream);
WriteToStream(gzipStream, outstream);
}
else if(compression == Compression.Huffman)
{
Stream huffmanStream = new NMDCHuffmanStream(instream);
WriteToStream(huffmanStream, outstream);
}
else
{
throw new ArgumentOutOfRangeException();
}
}
else
{
throw new IOException();
}
}
public static void Compress(string inFile, string outFile,
Compression compression)
{
using (FileStream instream = new FileStream(inFile, FileMode.Open,
FileAccess.Read, FileShare.Read),
outstream = new FileStream(outFile,
FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
{
Compress(instream, outstream, compression);
}
}
public static void Compress(Stream instream, Stream outstream,
Compression compression)
{
if(instream.CanRead && outstream.CanWrite)
{
if(compression == Compression.BZip2)
{
BZip2.Compress(instream, outstream, 1);
}
else if(compression == Compression.GZip)
{
Stream gzipStream = new GZipOutputStream(outstream);
WriteToStream(instream, gzipStream);
}
else
{
throw new NotSupportedException();
}
}
else
{
throw new IOException();
}
}
private static void WriteToStream(Stream instream, Stream outstream)
{
int size;
byte[] buffer = new byte[4096];
do
{
size = instream.Read(buffer, 0, buffer.Length);
if(size > 0)
{
outstream.Write(buffer, 0, size);
}
}
while (size > 0);
instream.Close();
}
#endregion
}
}
|