00001 /******************************************************************************* 00002 00003 @file File.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 2005 00034 00035 @author Kris 00036 00037 *******************************************************************************/ 00038 00039 module mango.io.File; 00040 00041 public import mango.io.FilePath; 00042 00043 private import mango.io.FileProxy, 00044 mango.io.Exception, 00045 mango.io.FileConduit; 00046 00047 /******************************************************************************* 00048 00049 A wrapper atop of FileConduit to expose a simpler API. This one 00050 returns the entire file content as a void[], and sets the content 00051 to reflect a given void[]. 00052 00053 Method read() returns the current content of the file, whilst write() 00054 sets the file content, and file length, to the provided array. Method 00055 append() adds content to the tail of the file. 00056 00057 Methods to inspect the file system, check the status of a file or 00058 directory, and other facilities are made available via the FileProxy 00059 superclass. 00060 00061 *******************************************************************************/ 00062 00063 class File : FileProxy 00064 { 00065 /*********************************************************************** 00066 00067 Construct a File from a text string 00068 00069 ***********************************************************************/ 00070 00071 this (char[] path) 00072 { 00073 super (path); 00074 } 00075 00076 /*********************************************************************** 00077 00078 Construct a File from the provided FilePath 00079 00080 ***********************************************************************/ 00081 00082 this (FilePath path) 00083 { 00084 super (path); 00085 } 00086 00087 /*********************************************************************** 00088 00089 Return the content of the file. 00090 00091 ***********************************************************************/ 00092 00093 void[] read () 00094 { 00095 auto conduit = new FileConduit (this); 00096 try { 00097 ubyte[] content = new ubyte[cast(int) conduit.length]; 00098 00099 // read the entire file into memory and return it 00100 if (conduit.read (content) != content.length) 00101 throw new IOException ("eof whilst reading"); 00102 00103 return content; 00104 } finally { 00105 conduit.close (); 00106 } 00107 } 00108 00109 /*********************************************************************** 00110 00111 Set the file content and length to reflect the given array. 00112 00113 ***********************************************************************/ 00114 00115 File write (void[] content) 00116 { 00117 return write (content, FileStyle.WriteTruncate); 00118 } 00119 00120 /*********************************************************************** 00121 00122 Append content to the file. 00123 00124 ***********************************************************************/ 00125 00126 File append (void[] content) 00127 { 00128 return write (content, FileStyle.WriteAppending); 00129 } 00130 00131 /*********************************************************************** 00132 00133 Set the file content and length to reflect the given array. 00134 00135 ***********************************************************************/ 00136 00137 private File write (void[] content, FileStyle.Bits style) 00138 { 00139 auto conduit = new FileConduit (this, style); 00140 try { 00141 conduit.flush (content); 00142 } finally { 00143 conduit.close (); 00144 } 00145 return this; 00146 } 00147 }