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

Generated on Fri May 27 18:11:55 2005 for Mango by  doxygen 1.4.0