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

Generated on Sun Nov 7 19:06:51 2004 for Mango by doxygen 1.3.6