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

Generated on Mon Nov 14 10:59:38 2005 for Mango by  doxygen 1.4.0