Util.cs :  » Business-Application » DC » DCSharp » Extras » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » DC 
DC » DCSharp » Extras » Util.cs
/* 
 * 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
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.