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

Generated on Sat Dec 24 17:28:32 2005 for Mango by  doxygen 1.4.0