132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Mar 2005: Initial release
|
|
8 version: Feb 2007: No longer a proxy subclass
|
|
9
|
|
10 author: Kris
|
|
11
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module tango.io.File;
|
|
15
|
|
16 private import tango.io.FilePath,
|
|
17 tango.io.FileConduit;
|
|
18
|
|
19 private import tango.core.Exception;
|
|
20
|
|
21 /*******************************************************************************
|
|
22
|
|
23 A wrapper atop of FileConduit to expose a simpler API. This one
|
|
24 returns the entire file content as a void[], and sets the content
|
|
25 to reflect a given void[].
|
|
26
|
|
27 Method read() returns the current content of the file, whilst write()
|
|
28 sets the file content, and file length, to the provided array. Method
|
|
29 append() adds content to the tail of the file.
|
|
30
|
|
31 Methods to inspect the file system, check the status of a file or
|
|
32 directory and other facilities are made available via the associated
|
|
33 path (exposed via the path() method)
|
|
34
|
|
35 *******************************************************************************/
|
|
36
|
|
37 class File
|
|
38 {
|
|
39 private PathView path_;
|
|
40
|
|
41 /***********************************************************************
|
|
42
|
|
43 Construct a File from a text string
|
|
44
|
|
45 ***********************************************************************/
|
|
46
|
|
47 this (char[] path)
|
|
48 {
|
|
49 this (new FilePath (path));
|
|
50 }
|
|
51
|
|
52 /***********************************************************************
|
|
53
|
|
54 Construct a File from the provided FilePath
|
|
55
|
|
56 ***********************************************************************/
|
|
57
|
|
58 this (PathView path)
|
|
59 {
|
|
60 path_ = path;
|
|
61 }
|
|
62
|
|
63 /***********************************************************************
|
|
64
|
|
65 Call-site shortcut to create a File instance. This
|
|
66 enables the same syntax as struct usage, so may expose
|
|
67 a migration path
|
|
68
|
|
69 ***********************************************************************/
|
|
70
|
|
71 static File opCall (char[] path)
|
|
72 {
|
|
73 return new File (path);
|
|
74 }
|
|
75
|
|
76 /***********************************************************************
|
|
77
|
|
78 Return the path for this file instance
|
|
79
|
|
80 ***********************************************************************/
|
|
81
|
|
82 final PathView path ()
|
|
83 {
|
|
84 return path_;
|
|
85 }
|
|
86
|
|
87 /***********************************************************************
|
|
88
|
|
89 Return the content of the file.
|
|
90
|
|
91 ***********************************************************************/
|
|
92
|
|
93 final void[] read ()
|
|
94 {
|
|
95 scope conduit = new FileConduit (path_);
|
|
96 scope (exit)
|
|
97 conduit.close;
|
|
98
|
|
99 // allocate enough space for the entire file
|
|
100 auto content = new ubyte [cast(uint) conduit.length];
|
|
101
|
|
102 //read the content
|
|
103 if (conduit.input.read (content) != content.length)
|
|
104 conduit.error ("unexpected eof");
|
|
105
|
|
106 return content;
|
|
107 }
|
|
108
|
|
109 /***********************************************************************
|
|
110
|
|
111 Set the file content and length to reflect the given array.
|
|
112
|
|
113 ***********************************************************************/
|
|
114
|
|
115 final File write (void[] content)
|
|
116 {
|
|
117 return write (content, FileConduit.ReadWriteCreate);
|
|
118 }
|
|
119
|
|
120 /***********************************************************************
|
|
121
|
|
122 Append content to the file.
|
|
123
|
|
124 ***********************************************************************/
|
|
125
|
|
126 final File append (void[] content)
|
|
127 {
|
|
128 return write (content, FileConduit.WriteAppending);
|
|
129 }
|
|
130
|
|
131 /***********************************************************************
|
|
132
|
|
133 Set the file content and length to reflect the given array.
|
|
134
|
|
135 ***********************************************************************/
|
|
136
|
|
137 private File write (void[] content, FileConduit.Style style)
|
|
138 {
|
|
139 scope conduit = new FileConduit (path_, style);
|
|
140 scope (exit)
|
|
141 conduit.close;
|
|
142
|
|
143 conduit.output.write (content);
|
|
144 return this;
|
|
145 }
|
|
146 }
|
|
147
|
|
148 debug (File)
|
|
149 {
|
|
150 void main()
|
|
151 {
|
|
152 auto content = File("file.d").read;
|
|
153 }
|
|
154 }
|