| 0: | // FSTypeUtility.cs | |
| 1: | // Copyright (C) 2001 Mike Krueger | |
| 2: | // Copyright (C) 2001 Andrea Paatz | |
| 3: | // | |
| 4: | // This program is free software; you can redistribute it and/or modify | |
| 5: | // it under the terms of the GNU General Public License as published by | |
| 6: | // the Free Software Foundation; either version 2 of the License, or | |
| 7: | // (at your option) any later version. | |
| 8: | // | |
| 9: | // This program is distributed in the hope that it will be useful, | |
| 10: | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11: | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12: | // GNU General Public License for more details. | |
| 13: | // | |
| 14: | // You should have received a copy of the GNU General Public License | |
| 15: | // along with this program; if not, write to the Free Software | |
| 16: | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 17: | ||
| 18: | using System; | |
| 19: | using System.IO; | |
| 20: | using System.Runtime.InteropServices; | |
| 21: | using System.Collections; | |
| 22: | using System.Collections.Specialized; | |
| 23: | using System.Drawing; | |
| 24: | using System.Reflection; | |
| 25: | using System.Resources; | |
| 26: | using System.Diagnostics; | |
| 27: | using System.Windows.Forms; | |
| 28: | ||
| 29: | using SharpDevelop.Internal.Modules; | |
| 30: | using SharpDevelop.Tool.Data; | |
| 31: | ||
| 32: | namespace SharpDevelop.Tool.Function { | |
| 33: | ||
| 34: | public delegate void FileSearchDelegate(string filename); | |
| 35: | ||
| 36: | public class FileUtility | |
| 37: | { | |
| 38: | static ImageList imagelist = new ImageList(); | |
| 39: | static Hashtable hashtable = new Hashtable(); | |
| 40: | ||
| 41: | /// this part comes from Andrea Paatz | |
| 42: | readonly static char[] seperators = {Path.DirectorySeparatorChar, Path.VolumeSeparatorChar}; | |
| 43: | ||
| 44: | public static string AbsoluteToRelativePath(string baseDirectoryPath, string absPath) | |
| 45: | { | |
| 46: | string[] bPath = baseDirectoryPath.Split(seperators); | |
| 47: | string[] aPath = absPath.Split(seperators); | |
| 48: | int indx = 0; | |
| 49: | for(; indx < Math.Min(bPath.Length, aPath.Length); ++indx){ | |
| 50: | if(!bPath[indx].Equals(aPath[indx])) | |
| 51: | break; | |
| 52: | } | |
| 53: | ||
| 54: | if (indx == 0) | |
| 55: | return absPath; | |
| 56: | ||
| 57: | string erg = ""; | |
| 58: | ||
| 59: | if(indx == bPath.Length) | |
| 60: | erg += "." + Path.DirectorySeparatorChar; | |
| 61: | else | |
| 62: | for (int i = indx; i < bPath.Length; ++i) { | |
| 63: | erg += ".." + Path.DirectorySeparatorChar; | |
| 64: | } | |
| 65: | ||
| 66: | erg += String.Join(Path.DirectorySeparatorChar.ToString(), aPath, indx, aPath.Length-indx); | |
| 67: | ||
| 68: | return erg; | |
| 69: | } | |
| 70: | ||
| 71: | public static string RelativeToAbsolutePath(string baseDirectoryPath, string relPath) | |
| 72: | { | |
| 73: | if (seperators[0] != seperators[1] && relPath.IndexOf(seperators[1]) != -1) | |
| 74: | return relPath; | |
| 75: | string[] bPath = baseDirectoryPath.Split(seperators[0]); | |
| 76: | string[] rPath = relPath.Split(seperators[0]); | |
| 77: | int indx = 0; | |
| 78: | ||
| 79: | for (; indx < rPath.Length; ++indx) { | |
| 80: | if (!rPath[indx].Equals("..")) | |
| 81: | break; | |
| 82: | } | |
| 83: | ||
| 84: | if (indx == 0) | |
| 85: | return baseDirectoryPath + seperators[0] + String.Join(Path.DirectorySeparatorChar.ToString(), rPath, 1, rPath.Length-1); | |
| 86: | ||
| 87: | string erg = String.Join(Path.DirectorySeparatorChar.ToString(), bPath, 0, bPath.Length-indx); | |
| 88: | ||
| 89: | erg += seperators[0] + String.Join(Path.DirectorySeparatorChar.ToString(), rPath, indx, rPath.Length-indx); | |
| 90: | ||
| 91: | return erg; | |
| 92: | } | |
| 93: | /// EOC A.Paatz | |
| 94: | ||
| 95: | public static void SearchDirectory(string directory, string filemask, FileSearchDelegate fs) | |
| 96: | { | |
| 97: | string[] file = Directory.GetFiles(directory, filemask); | |
| 98: | foreach (string f in file) | |
| 99: | fs(f); | |
| 100: | ||
| 101: | string[] dir = Directory.GetDirectories(directory); | |
| 102: | foreach (string d in dir) | |
| 103: | SearchDirectory(d, filemask, fs); | |
| 104: | } | |
| 105: | ||
| 106: | public static void SearchDirectory(string directory, string filemask, StringCollection collection) | |
| 107: | { | |
| 108: | string[] file = Directory.GetFiles(directory, filemask); | |
| 109: | foreach (string f in file) | |
| 110: | collection.Add(f); | |
| 111: | ||
| 112: | string[] dir = Directory.GetDirectories(directory); | |
| 113: | foreach (string d in dir) | |
| 114: | SearchDirectory(d, filemask, collection); | |
| 115: | } | |
| 116: | ||
| 117: | public static ImageList ImageList { | |
| 118: | get { | |
| 119: | return imagelist; | |
| 120: | } | |
| 121: | } | |
| 122: | ||
| 123: | static int offset = 0; | |
| 124: | static int initalsize = 0; | |
| 125: | public static int GetImageIndexFor(string filename) | |
| 126: | { | |
| 127: | string extension = Path.GetExtension(filename).ToUpper(); | |
| 128: | if (hashtable[extension] == null) { | |
| 129: | string name = filename.ToUpper(); | |
| 130: | if (hashtable[name] == null) { | |
| 131: | return 0; | |
| 132: | } | |
| 133: | return (int)hashtable[name]; | |
| 134: | } | |
| 135: | return (int)hashtable[extension]; | |
| 136: | } | |
| 137: | ||
| 138: | public static Bitmap GetBitmap(string name) | |
| 139: | { | |
| 140: | if (Resource.GetBitmap("Icons.16x16." + name) != null) | |
| 141: | return Resource.GetBitmap("Icons.16x16." + name); | |
| 142: | if (Resource.GetBitmap("Icons.32x32." + name) != null) | |
| 143: | return Resource.GetBitmap("Icons.32x32." + name); | |
| 144: | if (Resource.GetBitmap(name) != null) | |
| 145: | return Resource.GetBitmap(name); | |
| 146: | ||
| 147: | foreach (SharpDevelop.Internal.Modules.Module info in ModuleManager.Modules) { | |
| 148: | if (info.Icons[name] != null) { | |
| 149: | return (Bitmap)info.Icons[name]; | |
| 150: | } | |
| 151: | } | |
| 152: | return Resource.GetBitmap("Icons.16x16.MiscFiles"); | |
| 153: | } | |
| 154: | ||
| 155: | public static void LoadImageList() | |
| 156: | { | |
| 157: | imagelist = new ImageList(); | |
| 158: | ||
| 159: | initalsize = imagelist.Images.Count; | |
| 160: | ||
| 161: | imagelist.Images.Add(Resource.GetBitmap("Icons.16x16.MiscFiles")); | |
| 162: | ||
| 163: | hashtable[".PRJX"] = imagelist.Images.Count; | |
| 164: | imagelist.Images.Add(Resource.GetBitmap("Icons.16x16.SolutionIcon")); | |
| 165: | ||
| 166: | hashtable[".CMBX"] = imagelist.Images.Count; | |
| 167: | imagelist.Images.Add(Resource.GetBitmap("Icons.16x16.CombineIcon")); | |
| 168: | ||
| 169: | offset = imagelist.Images.Count; | |
| 170: | foreach (SharpDevelop.Internal.Modules.Module info in ModuleManager.Modules) { | |
| 171: | foreach (DictionaryEntry entry in info.FileIcons) { | |
| 172: | string name = entry.Key.ToString(); | |
| 173: | hashtable[name.ToUpper()] = imagelist.Images.Count; | |
| 174: | imagelist.Images.Add((Bitmap)entry.Value); | |
| 175: | ++offset; | |
| 176: | } | |
| 177: | } | |
| 178: | } | |
| 179: | ||
| 180: | public static void DisposeImageList() | |
| 181: | { | |
| 182: | imagelist.Dispose(); | |
| 183: | } | |
| 184: | } | |
| 185: | } |
This page was automatically generated by SharpDevelop.