Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

FileProxy.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file FileProxy.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         @author         Kris, Brad Anderson, teqdruid
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.io.FileProxy;
00040 
00041 private import  mango.base.System;
00042 
00043 private import  mango.io.Utf8,
00044                 mango.io.FilePath,
00045                 mango.io.FileStyle,
00046                 mango.io.Exception;
00047 
00048 version (Posix)
00049          private extern (C) int strlen (char *s);
00050 
00051 version (Win32)
00052          private extern (C) int wcslen (wchar *s);
00053 
00054 /*******************************************************************************
00055 
00056         Models a generic file. Use this to manipulate files and directories
00057         in conjunction with FilePath, FileSystem and FileConduit. Doxygen
00058         has a hard time with D version() statements, so part of this class
00059         is documented in FileProxy::VersionWin32 instead.
00060         
00061 *******************************************************************************/
00062 
00063 class FileProxy
00064 {
00065         private FilePath path;
00066 
00067         /***********************************************************************
00068         
00069                 Construct a FileProxy from the provided FilePath
00070 
00071         ***********************************************************************/
00072                                   
00073         this (FilePath path)
00074         {
00075                 this.path = path;
00076         }
00077 
00078         /***********************************************************************
00079         
00080                 Construct a FileProxy from a text string
00081 
00082         ***********************************************************************/
00083 
00084         this (char[] path)
00085         {
00086                 this (new FilePath (path));
00087         }
00088 
00089         /***********************************************************************
00090         
00091                 Return the FilePath associated with this FileProxy
00092 
00093         ***********************************************************************/
00094 
00095         FilePath getPath ()
00096         {
00097                 return path;
00098         }               
00099 
00100         /***********************************************************************
00101         
00102                 Return the name of the associated path
00103 
00104         ***********************************************************************/
00105 
00106         char[] toString ()
00107         {
00108                 return path.toString();
00109         }               
00110 
00111         /***********************************************************************
00112         
00113                 Does this path currently exist?
00114 
00115         ***********************************************************************/
00116 
00117         bool isExisting ()               
00118         {
00119                 try {
00120                     getSize();
00121                     return true;
00122                     } catch (IOException){}
00123                 return false;
00124         }            
00125 
00126         /***********************************************************************
00127         
00128                 List the files contained within the associated path:
00129 
00130                 @code
00131                 FileProxy proxy = new FileProxy (".");
00132 
00133                 foreach (FilePath path; proxy.toList())
00134                          Stdout.put(path).cr();
00135                 @endcode
00136 
00137         ***********************************************************************/
00138 
00139         FilePath[] toList ()
00140         {
00141                 bool accept (FilePath fp) {return true;}
00142 
00143                 return toList (&accept);
00144         }              
00145 
00146         /***********************************************************************
00147 
00148         ***********************************************************************/
00149 
00150         version (Win32)
00151         {
00152                 private import std.c.windows.windows;
00153 
00154                 /***************************************************************
00155 
00156                         Throw an exception using the last known error
00157 
00158                 ***************************************************************/
00159 
00160                 private void exception ()
00161                 {
00162                         throw new IOException (path.toString ~ ": " ~ System.error);
00163                 }
00164 
00165                 /***************************************************************
00166 
00167                         Get info about this path
00168 
00169                 ***************************************************************/
00170 
00171                 private uint getInfo (void delegate (WIN32_FIND_DATAW info) dg)
00172                 {
00173                         WIN32_FIND_DATAW info;
00174 
00175                         HANDLE h = FindFirstFileW (path.toUtf16, &info);
00176                         if (h == INVALID_HANDLE_VALUE)
00177                             exception ();
00178 
00179                         if (dg)
00180                             dg (info);
00181                         FindClose (h);
00182 
00183                         return info.dwFileAttributes;
00184                 }
00185 
00186                 /***************************************************************
00187                         
00188                         Return the file length (in bytes)
00189 
00190                 ***************************************************************/
00191 
00192                 long getSize ()
00193                 {
00194                         long _size;
00195 
00196                         void size (WIN32_FIND_DATAW info)
00197                         {
00198                                 _size = (cast(ulong) info.nFileSizeHigh << 32) + 
00199                                                      info.nFileSizeLow;
00200                         }
00201 
00202                         getInfo (&size);
00203                         return _size;
00204                 }
00205 
00206                 /***************************************************************
00207 
00208                         Is this file writable?
00209 
00210                 ***************************************************************/
00211 
00212                 bool isWritable ()               
00213                 {
00214                         return (getInfo(null) & FILE_ATTRIBUTE_READONLY) == 0;
00215                 }            
00216 
00217                 /***************************************************************
00218 
00219                         Is this file really a directory?
00220 
00221                 ***************************************************************/
00222 
00223                 bool isDirectory ()               
00224                 {
00225                         return (getInfo(null) & FILE_ATTRIBUTE_DIRECTORY) != 0;
00226                 }            
00227 
00228                 /***************************************************************
00229 
00230                         Return the time when the file was last modified
00231 
00232                 ***************************************************************/
00233 
00234                 long getModifiedTime ()
00235                 {
00236                         long _time;
00237 
00238                         void time (WIN32_FIND_DATAW info)
00239                         {
00240                                 _time = (cast(ulong) info.ftLastWriteTime.dwHighDateTime << 32) + 
00241                                                      info.ftLastWriteTime.dwLowDateTime;
00242                         }
00243 
00244                         getInfo (&time);
00245                         return _time;
00246                 }               
00247 
00248                 /***************************************************************
00249 
00250                         Remove the file/directory from the file-system
00251 
00252                 ***************************************************************/
00253 
00254                 void remove ()
00255                 {
00256                         if (isDirectory ())
00257                            {
00258                            if (! RemoveDirectoryW (path.toUtf16))
00259                                  exception();
00260                            }
00261                         else
00262                            {
00263                            if (! DeleteFileW (path.toUtf16))
00264                                  exception();
00265                            }
00266                 }           
00267 
00268                 /***************************************************************
00269 
00270                         Create a new file 
00271 
00272                 ***************************************************************/
00273 
00274                 FileProxy createFile ()
00275                 {
00276                         HANDLE h;
00277 
00278                         h = CreateFileW (path.toUtf16, GENERIC_WRITE, 0, null, 
00279                                          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, null);
00280 
00281                         if (h == INVALID_HANDLE_VALUE)
00282                             exception();
00283 
00284                         if (! CloseHandle (h))
00285                               exception();
00286 
00287                         return this;
00288                 }
00289 
00290                 /***************************************************************
00291 
00292                         Create a new directory
00293 
00294                 ***************************************************************/
00295 
00296                 FileProxy createDirectory ()
00297                 {
00298                         if (! CreateDirectoryW (path.toUtf16, null))
00299                               exception();
00300 
00301                         return this;
00302                 }
00303 
00304                 /***************************************************************
00305                         
00306                         List the set of children within this directory. See
00307                         toList() above.
00308 
00309                 ***************************************************************/
00310 
00311                 FilePath[] toList (bool delegate(FilePath fp) filter)
00312                 {
00313                         int                     i;
00314                         wchar[]                 c;
00315                         HANDLE                  h;
00316                         FilePath[]              list;
00317                         WIN32_FIND_DATAW        fileinfo;
00318 
00319                         list = new FilePath[50];
00320 
00321                         c = path.toUtf16 ~ cast(wchar[]) "\\*\0";
00322                         h = FindFirstFileW (c, &fileinfo);
00323 
00324                         if (h != INVALID_HANDLE_VALUE)
00325                             try {
00326                                 do {
00327                                    int len = wcslen (fileinfo.cFileName);
00328 
00329                                    // make a copy of the file name for listing
00330                                    FilePath fp = new FilePath (Utf8.encode(fileinfo.cFileName [0 .. len]));
00331 
00332                                    if (i >= list.length)
00333                                       list.length = list.length * 2;
00334 
00335                                    if (filter (fp))
00336                                       {
00337                                       list[i] = fp;
00338                                       ++i;
00339                                       }
00340                                    } while (FindNextFileW (h, &fileinfo));
00341                                 } finally {
00342                                           FindClose (h);
00343                                           }
00344                         list.length = i;
00345                         return list;
00346                 }
00347         }
00348 
00349         /***********************************************************************
00350 
00351         ***********************************************************************/
00352 
00353         version (Posix)
00354         {
00355                 private import std.c.linux.linux;
00356 
00357                 /***************************************************************
00358 
00359                         Throw an exception using the last known error
00360 
00361                 ***************************************************************/
00362 
00363                 private void exception ()
00364                 {
00365                         throw new IOException (path.toString ~ ": " ~ System.error);
00366                 }
00367 
00368                 /***************************************************************
00369 
00370                         Get info about this path
00371 
00372                 ***************************************************************/
00373 
00374                 private uint getInfo (void delegate (struct_stat info) dg)
00375                 {
00376                         struct_stat stats;
00377 
00378                         if (std.c.linux.linux.stat (path.toUtf8, &stats))
00379                             exception();
00380 
00381                         if (dg)
00382                             dg (stats);
00383 
00384                         return stats.st_mode;
00385                 }               
00386 
00387                 /***************************************************************
00388 
00389                         Return the file length (in bytes)
00390 
00391                 ***************************************************************/
00392 
00393                 long getSize ()
00394                 {
00395                         long _size;
00396 
00397                         void size (struct_stat info)
00398                         {
00399                                 _size = cast(ulong) info.st_size;    // 32 bits only
00400                         }
00401 
00402                         getInfo (&size);
00403                         return _size;
00404                 }
00405 
00406                 /***************************************************************
00407 
00408                         Is this file writable?
00409 
00410                 ***************************************************************/
00411 
00412                 bool isWritable ()               
00413                 {
00414                         return (getInfo(null) & O_RDONLY) == 0;
00415                 }            
00416 
00417                 /***************************************************************
00418 
00419                         Is this file really a directory?
00420 
00421                 ***************************************************************/
00422 
00423                 bool isDirectory ()               
00424                 {
00425                         return (getInfo(null) & S_IFDIR) != 0;
00426                 }            
00427 
00428                 /***************************************************************
00429 
00430                         Return the time when the file was last modified
00431 
00432                 ***************************************************************/
00433 
00434                 long getModifiedTime ()
00435                 {
00436                         long _time;
00437 
00438                         void time (struct_stat info)
00439                         {
00440                                _time = cast(ulong) info.st_mtime; // seconds since 1/1/1970
00441                         }
00442 
00443                         getInfo (&time);
00444                         return _time;
00445                 }               
00446 
00447                 /***************************************************************
00448 
00449                         Remove the file/directory from the file-system
00450 
00451                 ***************************************************************/
00452 
00453                 void remove ()
00454                 {
00455                         if (isDirectory())
00456                            {
00457                            if (std.c.linux.linux.rmdir (path.toUtf8))
00458                                exception ();
00459                            }
00460                         else           
00461                            if (std.c.stdio.remove (path.toUtf8) == -1)
00462                                exception ();
00463                 }              
00464 
00465                 /***************************************************************
00466 
00467                         Create a new file 
00468 
00469                 ***************************************************************/
00470 
00471                 FileProxy createFile ()
00472                 {
00473                         int fd;
00474 
00475                         fd = std.c.linux.linux.open (path.toUtf8, O_CREAT | O_WRONLY | O_TRUNC, 0660);
00476                         if (fd == -1)
00477                             exception();
00478 
00479                         if (std.c.linux.linux.close(fd) == -1)
00480                             exception();
00481 
00482                         return this;
00483                 }              
00484 
00485                 /***************************************************************
00486 
00487                         Create a new directory
00488 
00489                 ***************************************************************/
00490 
00491                 FileProxy createDirectory ()
00492                 {
00493                         if (std.c.linux.linux.mkdir (path.toUtf8, 0777))
00494                             exception();
00495 
00496                         return this;
00497                 }
00498 
00499                 /***************************************************************
00500                 
00501                         List the set of children within this directory. See
00502                         toList() above.
00503 
00504                 ***************************************************************/
00505 
00506                 FilePath[] toList (bool delegate(FilePath fp) filter)
00507                 {
00508                         int             i;
00509                         DIR*            dir;
00510                         dirent*         entry;
00511                         FilePath[]      list;
00512 
00513                         dir = opendir (path.toUtf8);
00514                         if (! dir) 
00515                               exception();
00516 
00517                         list = new FilePath [50];
00518                         while ((entry = readdir(dir)) != null)
00519                               {
00520                               int len = strlen (entry.d_name);
00521 
00522                               // make a copy of the file name for listing
00523                               FilePath fp = new FilePath (entry.d_name[0 ..len].dup);
00524 
00525                               if (i >= list.length)
00526                                   list.length = list.length * 2;
00527 
00528                               if (filter (fp))
00529                                  {
00530                                  list[i] = fp;
00531                                  ++i;
00532                                  }
00533                               }  
00534 
00535                         list.length = i;
00536                         closedir (dir);
00537                         return list;
00538                 }
00539 
00540         }
00541 }

Generated on Tue Jan 25 21:18:21 2005 for Mango by doxygen 1.3.6