Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

FileSystem.d

Go to the documentation of this file.
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
00035                         John Reimer
00036                         Chris Sauls (Win95 file support)
00037 
00038 *******************************************************************************/
00039 
00040 module mango.io.FileSystem;
00041 
00042 private import  mango.io.Utf8,
00043                 mango.io.FilePath,
00044                 mango.io.FileConst,
00045                 mango.io.Exception;
00046 
00047 private import  mango.utils.Text;
00048 
00049 // add some externs for linux
00050 version (Posix)
00051 {
00052         extern (C) int strlen (char *);
00053 }
00054 
00055 version (Win32)
00056         {
00057         private import std.c.windows.windows;
00058         extern (Windows) DWORD GetLogicalDriveStringsA (DWORD, LPTSTR);
00059         }
00060 
00061 
00062 /*******************************************************************************
00063 
00064         Models an OS-specific file-system. Included here are methods to 
00065         list the system roots ("C:", "D:", etc) and to manipulate the
00066         current working directory.
00067 
00068 *******************************************************************************/
00069 
00070 class FileSystem
00071 {
00072         version (Win32)
00073         {
00074                 /***********************************************************************
00075                         
00076                         List the set of root devices (C:, D: etc)
00077 
00078                         @todo not currently implemented.
00079 
00080                 ***********************************************************************/
00081 
00082                 static char[][] listRoots ()
00083                 {
00084                         int             len;
00085                         char[]          str;
00086                         char[][]        roots;
00087 
00088                         // acquire drive strings
00089                         len = GetLogicalDriveStringsA (0, null);
00090                         if (len)
00091                            {
00092                            str = new char [len];
00093                            GetLogicalDriveStringsA (len, str);
00094 
00095                            // split roots into seperate strings
00096                            roots = Text.split (str [0..str.length-1], "\0");
00097                            }
00098                         return roots;
00099                 }
00100 
00101                 /***********************************************************************
00102 
00103                         Set the current working directory
00104 
00105                 ***********************************************************************/
00106 
00107                 static void setDirectory (FilePath fp)
00108                 {
00109                         version (Win32SansUnicode)
00110                                 {
00111                                 if (! SetCurrentDirectoryA (fp.toUtf8))
00112                                        throw new IOException ("Failed to set current directory");
00113                                 }
00114                              else
00115                                 {
00116                                 if (! SetCurrentDirectoryW (fp.toUtf16))
00117                                       throw new IOException ("Failed to set current directory");
00118                                 }
00119                 }
00120 
00121                 /***********************************************************************
00122 
00123                         Get the current working directory
00124                 
00125                 ***********************************************************************/
00126 
00127                 static FilePath getDirectory ()
00128                 {
00129                         version (Win32SansUnicode)
00130                                 {
00131                                 int length = GetCurrentDirectoryA (0, null);
00132                                 if (length)
00133                                    {
00134                                    char[] dir = new char [length];
00135                                    GetCurrentDirectoryA (length, dir);
00136                                    return new FilePath (dir);
00137                                    }
00138                                 }
00139                              else
00140                                 {
00141                                 int length = GetCurrentDirectoryW (0, null);
00142                                 if (length)
00143                                    {
00144                                    wchar[] dir = new wchar [length];
00145                                    GetCurrentDirectoryW (length, dir);
00146                                    return new FilePath (Utf8.encode (dir));
00147                                    }
00148                                 }
00149                         throw new IOException ("Failed to get current directory");
00150                 }
00151         }
00152         
00153         
00154         version (Posix)
00155         {
00156                 private import std.c.linux.linux;
00157 
00158                 /***********************************************************************
00159 
00160                         List the set of root devices.
00161 
00162                         @todo not currently implemented.
00163 
00164                 ***********************************************************************/
00165 
00166                 static char[][] listRoots ()
00167                 {
00168                         assert(0);
00169                         return null;
00170                 }
00171 
00172                 /***********************************************************************
00173 
00174                         Set the current working directory
00175 
00176                 ***********************************************************************/
00177 
00178                 static void setDirectory (FilePath fp)
00179                 {
00180                         if (std.c.linux.linux.chdir (fp.toUtf8))
00181                             throw new IOException ("Failed to set current directory");
00182                 }
00183 
00184                 /***********************************************************************
00185 
00186                         Get the current working directory
00187                 
00188                 ***********************************************************************/
00189 
00190                 static FilePath getDirectory ()
00191                 {
00192                         char *s = std.c.linux.linux.getcwd (null, 0);
00193                         if (s) 
00194                             // dup the string so we can hang onto it                            
00195                             return new FilePath (s[0..strlen(s)].dup);
00196 
00197                         throw new IOException ("Failed to get current directory");
00198                 }
00199         }   
00200 
00201         /***********************************************************************
00202        
00203                 These have been moved
00204 
00205         ***********************************************************************/
00206 
00207         alias FileConst.PathSeparatorChar PathSeparatorChar; 
00208         alias FileConst.FileSeparatorChar FileSeparatorChar; 
00209         alias FileConst.RootSeparatorChar RootSeparatorChar; 
00210         alias FileConst.PathSeparatorString PathSeparatorString; 
00211         alias FileConst.FileSeparatorString FileSeparatorString; 
00212         alias FileConst.RootSeparatorString RootSeparatorString; 
00213         alias FileConst.NewlineString NewlineString; 
00214              
00215         /***********************************************************************
00216        
00217                 My bogus mispelling of the word Separator ...
00218 
00219         ***********************************************************************/
00220 
00221         alias PathSeparatorChar         PathSeperatorChar;
00222         alias FileSeparatorChar         FileSeperatorChar;
00223         alias RootSeparatorChar         RootSeperatorChar;
00224         alias PathSeparatorString       PathSeperatorString;
00225         alias FileSeparatorString       FileSeperatorString; 
00226         alias RootSeparatorString       RootSeperatorString;               
00227 }

Generated on Sat Apr 9 20:11:26 2005 for Mango by doxygen 1.3.6