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 
00027                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00028 
00029 
00030         @version        Initial version, March 2004      
00031         @author         Kris, John Reimer
00032 
00033 
00034 *******************************************************************************/
00035 
00036 module mango.io.FileSystem;
00037 
00038 private import  mango.io.FilePath,
00039                 mango.io.Exception;
00040 
00041 // add some externs for linux
00042 version (linux)
00043 {
00044         extern (C) int strlen (char *);
00045 }
00046 
00047 /*******************************************************************************
00048 
00049         Models an OS-specific file-system. Included here are methods to 
00050         list the system roots ("C:", "D:", etc) and to manipulate the
00051         current working directory.
00052 
00053 *******************************************************************************/
00054 
00055 class FileSystem
00056 {
00057         /***********************************************************************
00058         
00059                 A set of file-system specific constants for file and path
00060                 seperators (chars and strings).
00061 
00062         ***********************************************************************/
00063 
00064         version (Win32)
00065         {
00066                 static const char PathSeperatorChar = '\\';
00067                 static const char FileSeperatorChar = '.';
00068                 static const char RootSeperatorChar = ':';
00069 
00070                 static const char[] PathSeperatorString = "\\";
00071                 static const char[] FileSeperatorString = ".";
00072                 static const char[] RootSeperatorString = ":";
00073 
00074                 static const char[] NewlineString = "\r\n";
00075         }
00076         
00077         
00078         version (linux)
00079         {
00080                 static const char PathSeperatorChar = '/';
00081                 static const char FileSeperatorChar = '.';
00082                 static const char RootSeperatorChar = ':';
00083 
00084                 static const char[] PathSeperatorString = "/";
00085                 static const char[] FileSeperatorString = ".";
00086                 static const char[] RootSeperatorString = ":";
00087 
00088                 static const char[] NewlineString = "\n";
00089         }            
00090 
00091         version (Win32)
00092         {
00093                 private import std.c.windows.windows;
00094 
00095                 /***********************************************************************
00096                         
00097                         List the set of root devices (C:, D: etc)
00098 
00099                         @todo not currently implemented.
00100 
00101                 ***********************************************************************/
00102 
00103                 static char[][] listRoots ()
00104                 {
00105                         assert(0);
00106                         return null;
00107                 }
00108 
00109                 /***********************************************************************
00110 
00111                         Set the current working directory
00112 
00113                 ***********************************************************************/
00114 
00115                 static void setDirectory (FilePath fp)
00116                 {
00117                         if (! SetCurrentDirectoryA (fp.toStringZ()))
00118                               throw new IOException ("Failed to set current directory");
00119                 }
00120 
00121                 /***********************************************************************
00122 
00123                         Get the current working directory
00124                 
00125                 ***********************************************************************/
00126 
00127                 static FilePath getDirectory ()
00128                 {
00129                         char c;
00130                         char[] dir;
00131                         int length;
00132 
00133                         length = GetCurrentDirectoryA (0, &c);
00134                         if (length)
00135                            {
00136                            dir = new char[length];
00137                            length = GetCurrentDirectoryA (length, dir);
00138                            if (length)
00139                                return new FilePath (dir);
00140                            }
00141                         throw new IOException ("Failed to get current directory");
00142                 }
00143         }
00144         
00145         
00146         version (linux)
00147         {
00148                 private import std.c.linux.linux;
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.toStringZ()))
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 }

Generated on Sun Oct 24 22:31:14 2004 for Mango by doxygen 1.3.6