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

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