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 private import mango.io.FileStyle, 00042 mango.io.FileProxy, 00043 mango.io.FileConduit; 00044 00045 /******************************************************************************* 00046 00047 A wrapper atop of FileConduit to expose a simpler API. This one 00048 return the entire file content as a void[], and sets the content 00049 to reflect a given void[]. 00050 00051 Method read() returns the current content of the file, whilst write() 00052 sets the file content, and file length, to the provided array. Method 00053 append() adds content to the tail of the file. 00054 00055 Methods to inspect the file system, check the status of a file or 00056 directory, and other facilities are made available via the FileProxy 00057 superclass. 00058 00059 *******************************************************************************/ 00060 00061 class File : FileProxy 00062 { 00063 /*********************************************************************** 00064 00065 Construct a File from a text string 00066 00067 ***********************************************************************/ 00068 00069 this (char[] path) 00070 { 00071 super (path); 00072 } 00073 00074 /*********************************************************************** 00075 00076 Construct a File from the provided FilePath 00077 00078 ***********************************************************************/ 00079 00080 this (FilePath path) 00081 { 00082 super (path); 00083 } 00084 00085 /*********************************************************************** 00086 00087 Return the content of the file. This will be the original 00088 content until a put operation occurs. After that, it will 00089 return content set via the last put() operation. 00090 00091 ***********************************************************************/ 00092 00093 void[] read () 00094 { 00095 auto FileConduit conduit = new FileConduit (this); 00096 uint length = conduit.length; 00097 Buffer buffer = new Buffer (length); 00098 buffer.setConduit (conduit); 00099 00100 // read the entire file into memory and return it 00101 return buffer.get (length); 00102 } 00103 00104 /*********************************************************************** 00105 00106 Set the file content and length to reflect the given array. 00107 00108 ***********************************************************************/ 00109 00110 File write (void[] content) 00111 { 00112 return write (content, FileStyle.WriteTruncate); 00113 } 00114 00115 /*********************************************************************** 00116 00117 Append content to the file. 00118 00119 ***********************************************************************/ 00120 00121 File append (void[] content) 00122 { 00123 return write (content, FileStyle.WriteAppending); 00124 } 00125 00126 /*********************************************************************** 00127 00128 Set the file content and length to reflect the given array. 00129 00130 ***********************************************************************/ 00131 00132 private File write (void[] content, FileStyle style) 00133 { 00134 auto FileConduit conduit = new FileConduit (this, style); 00135 00136 Buffer buffer = new Buffer (content, content.length); 00137 buffer.setConduit (conduit); 00138 buffer.flush (); 00139 return this; 00140 } 00141 }