00001 /******************************************************************************* 00002 00003 @file FileSystem.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, March 2004 00034 @author Kris, John Reimer 00035 00036 00037 *******************************************************************************/ 00038 00039 module mango.io.FileSystem; 00040 00041 private import mango.io.Utf8, 00042 mango.io.FilePath, 00043 mango.io.Exception; 00044 00045 // add some externs for linux 00046 version (Posix) 00047 { 00048 extern (C) int strlen (char *); 00049 } 00050 00051 /******************************************************************************* 00052 00053 Models an OS-specific file-system. Included here are methods to 00054 list the system roots ("C:", "D:", etc) and to manipulate the 00055 current working directory. 00056 00057 *******************************************************************************/ 00058 00059 class FileSystem 00060 { 00061 /*********************************************************************** 00062 00063 A set of file-system specific constants for file and path 00064 seperators (chars and strings). 00065 00066 ***********************************************************************/ 00067 00068 version (Win32) 00069 { 00070 static const char PathSeperatorChar = '\\'; 00071 static const char FileSeperatorChar = '.'; 00072 static const char RootSeperatorChar = ':'; 00073 00074 static const char[] PathSeperatorString = "\\"; 00075 static const char[] FileSeperatorString = "."; 00076 static const char[] RootSeperatorString = ":"; 00077 00078 static const char[] NewlineString = "\r\n"; 00079 } 00080 00081 00082 version (Posix) 00083 { 00084 static const char PathSeperatorChar = '/'; 00085 static const char FileSeperatorChar = '.'; 00086 static const char RootSeperatorChar = ':'; 00087 00088 static const char[] PathSeperatorString = "/"; 00089 static const char[] FileSeperatorString = "."; 00090 static const char[] RootSeperatorString = ":"; 00091 00092 static const char[] NewlineString = "\n"; 00093 } 00094 00095 version (Win32) 00096 { 00097 private import std.c.windows.windows; 00098 00099 /*********************************************************************** 00100 00101 List the set of root devices (C:, D: etc) 00102 00103 @todo not currently implemented. 00104 00105 ***********************************************************************/ 00106 00107 static char[][] listRoots () 00108 { 00109 assert(0); 00110 return null; 00111 } 00112 00113 /*********************************************************************** 00114 00115 Set the current working directory 00116 00117 ***********************************************************************/ 00118 00119 static void setDirectory (FilePath fp) 00120 { 00121 if (! SetCurrentDirectoryW (fp.toUtf16)) 00122 throw new IOException ("Failed to set current directory"); 00123 } 00124 00125 /*********************************************************************** 00126 00127 Get the current working directory 00128 00129 ***********************************************************************/ 00130 00131 static FilePath getDirectory () 00132 { 00133 wchar[] dir; 00134 int length; 00135 00136 length = GetCurrentDirectoryW (0, null); 00137 if (length) 00138 { 00139 dir = new wchar [length]; 00140 GetCurrentDirectoryW (length, dir); 00141 return new FilePath (Utf8.encode (dir)); 00142 } 00143 throw new IOException ("Failed to get current directory"); 00144 } 00145 } 00146 00147 00148 version (Posix) 00149 { 00150 private import std.c.linux.linux; 00151 00152 /*********************************************************************** 00153 00154 List the set of root devices. 00155 00156 @todo not currently implemented. 00157 00158 ***********************************************************************/ 00159 00160 static char[][] listRoots () 00161 { 00162 assert(0); 00163 return null; 00164 } 00165 00166 /*********************************************************************** 00167 00168 Set the current working directory 00169 00170 ***********************************************************************/ 00171 00172 static void setDirectory (FilePath fp) 00173 { 00174 if (std.c.linux.linux.chdir (fp.toUtf8)) 00175 throw new IOException ("Failed to set current directory"); 00176 } 00177 00178 /*********************************************************************** 00179 00180 Get the current working directory 00181 00182 ***********************************************************************/ 00183 00184 static FilePath getDirectory () 00185 { 00186 char *s = std.c.linux.linux.getcwd (null, 0); 00187 if (s) 00188 // dup the string so we can hang onto it 00189 return new FilePath (s[0..strlen(s)].dup); 00190 00191 throw new IOException ("Failed to get current directory"); 00192 } 00193 } 00194 }