1 |
// AprSharp, a wrapper library around the Apache Portable Runtime Library |
---|
2 |
#region Copyright (C) 2004 SOFTEC sa. |
---|
3 |
// |
---|
4 |
// This library is free software; you can redistribute it and/or |
---|
5 |
// modify it under the terms of the GNU Lesser General Public |
---|
6 |
// License as published by the Free Software Foundation; either |
---|
7 |
// version 2.1 of the License, or (at your option) any later version. |
---|
8 |
// |
---|
9 |
// This library is distributed in the hope that it will be useful, |
---|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 |
// Lesser General Public License for more details. |
---|
13 |
// |
---|
14 |
// You should have received a copy of the GNU Lesser General Public |
---|
15 |
// License along with this library; if not, write to the Free Software |
---|
16 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 |
// |
---|
18 |
// Sources, support options and lastest version of the complete library |
---|
19 |
// is available from: |
---|
20 |
// http://www.softec.st/AprSharp |
---|
21 |
// |
---|
22 |
// |
---|
23 |
// Initial authors : |
---|
24 |
// Denis Gervalle |
---|
25 |
// Olivier Desaive |
---|
26 |
#endregion |
---|
27 |
// |
---|
28 |
using System; |
---|
29 |
using System.Diagnostics; |
---|
30 |
|
---|
31 |
namespace Softec.AprSharp |
---|
32 |
{ |
---|
33 |
public struct AprFile : IAprUnmanaged |
---|
34 |
{ |
---|
35 |
[Flags] |
---|
36 |
public enum Flags |
---|
37 |
{ |
---|
38 |
Read = 0x00001, |
---|
39 |
Write = 0x00002, |
---|
40 |
Create = 0x00004, |
---|
41 |
Append = 0x00008, |
---|
42 |
Truncate = 0x00010, |
---|
43 |
Binary = 0x00020, |
---|
44 |
Exclusive = 0x00040, |
---|
45 |
Buffered = 0x00080, |
---|
46 |
DelayOnClose = 0x00100, |
---|
47 |
XThread = 0x00200, |
---|
48 |
ShareLock = 0x00400, |
---|
49 |
FileNoCleanUp = 0x00800, |
---|
50 |
SendFileEnabled = 0x01000 |
---|
51 |
} |
---|
52 |
|
---|
53 |
[Flags] |
---|
54 |
public enum Perms |
---|
55 |
{ |
---|
56 |
SetUserId = 0x8000, |
---|
57 |
UserRead = 0x0400, |
---|
58 |
UserWrite = 0x0200, |
---|
59 |
UserExecute = 0x0100, |
---|
60 |
SetGroupId = 0x4000, |
---|
61 |
GroupRead = 0x0040, |
---|
62 |
GroupWrite = 0x0020, |
---|
63 |
GroupExecute = 0x0010, |
---|
64 |
WorldSticky = 0x2000, |
---|
65 |
WorldRead = 0x0004, |
---|
66 |
WorldWrite = 0x0002, |
---|
67 |
WorldExecute = 0x0001, |
---|
68 |
OSDefault = 0x0FFF, |
---|
69 |
SourcePerms = 0x1000 |
---|
70 |
} |
---|
71 |
|
---|
72 |
public enum Std |
---|
73 |
{ |
---|
74 |
In, |
---|
75 |
Out, |
---|
76 |
Err |
---|
77 |
} |
---|
78 |
|
---|
79 |
IntPtr mFile; |
---|
80 |
|
---|
81 |
#region Generic embedding functions of an IntPtr |
---|
82 |
public AprFile(IntPtr ptr) |
---|
83 |
{ |
---|
84 |
mFile = ptr; |
---|
85 |
} |
---|
86 |
|
---|
87 |
public bool IsNull |
---|
88 |
{ |
---|
89 |
get |
---|
90 |
{ |
---|
91 |
return( mFile == IntPtr.Zero ); |
---|
92 |
} |
---|
93 |
} |
---|
94 |
|
---|
95 |
private void CheckPtr() |
---|
96 |
{ |
---|
97 |
if( IsNull ) |
---|
98 |
throw new AprNullReferenceException(); |
---|
99 |
} |
---|
100 |
|
---|
101 |
public void ClearPtr() |
---|
102 |
{ |
---|
103 |
mFile = IntPtr.Zero; |
---|
104 |
} |
---|
105 |
|
---|
106 |
public IntPtr ToIntPtr() |
---|
107 |
{ |
---|
108 |
return mFile; |
---|
109 |
} |
---|
110 |
|
---|
111 |
public bool ReferenceEquals(IAprUnmanaged obj) |
---|
112 |
{ |
---|
113 |
return(obj.ToIntPtr() == ToIntPtr()); |
---|
114 |
} |
---|
115 |
|
---|
116 |
public static implicit operator IntPtr(AprFile file) |
---|
117 |
{ |
---|
118 |
return file.mFile; |
---|
119 |
} |
---|
120 |
|
---|
121 |
public static implicit operator AprFile(IntPtr ptr) |
---|
122 |
{ |
---|
123 |
return new AprFile(ptr); |
---|
124 |
} |
---|
125 |
|
---|
126 |
public override string ToString() |
---|
127 |
{ |
---|
128 |
return("[apr_file_t:"+mFile.ToInt32().ToString("X")+"]"); |
---|
129 |
} |
---|
130 |
#endregion |
---|
131 |
|
---|
132 |
#region Wrapper methods |
---|
133 |
public static AprFile Open(string fname, Flags flag, Perms perm, AprPool pool) |
---|
134 |
{ |
---|
135 |
IntPtr ptr; |
---|
136 |
|
---|
137 |
Debug.Write(String.Format("apr_file_open({0},{1},{2},{3})...",fname,flag,perm,pool)); |
---|
138 |
int res = Apr.apr_file_open(out ptr, fname, (int)flag, (int)perm, pool); |
---|
139 |
if(res != 0 ) |
---|
140 |
throw new AprException(res); |
---|
141 |
Debug.WriteLine(String.Format("Done({0:X})",((Int32)ptr))); |
---|
142 |
|
---|
143 |
return(ptr); |
---|
144 |
} |
---|
145 |
|
---|
146 |
public static AprFile Open(Std stream, AprPool pool) |
---|
147 |
{ |
---|
148 |
IntPtr ptr = IntPtr.Zero; |
---|
149 |
int res = 0; |
---|
150 |
|
---|
151 |
switch(stream) |
---|
152 |
{ |
---|
153 |
case Std.In: |
---|
154 |
Debug.Write(String.Format("apr_file_open_stdin({0})...",pool)); |
---|
155 |
res = Apr.apr_file_open_stdin(out ptr, pool); |
---|
156 |
break; |
---|
157 |
case Std.Out: |
---|
158 |
Debug.Write(String.Format("apr_file_open_stdout({0})...",pool)); |
---|
159 |
res = Apr.apr_file_open_stdout(out ptr, pool); |
---|
160 |
break; |
---|
161 |
case Std.Err: |
---|
162 |
Debug.Write(String.Format("apr_file_open_stderr({0})...",pool)); |
---|
163 |
res = Apr.apr_file_open_stderr(out ptr, pool); |
---|
164 |
break; |
---|
165 |
} |
---|
166 |
if(res != 0 ) |
---|
167 |
throw new AprException(res); |
---|
168 |
Debug.WriteLine(String.Format("Done({0:X})",((Int32)ptr))); |
---|
169 |
|
---|
170 |
return(ptr); |
---|
171 |
} |
---|
172 |
|
---|
173 |
public void Close() |
---|
174 |
{ |
---|
175 |
CheckPtr(); |
---|
176 |
Debug.Write(String.Format("apr_file_close({0})...",this)); |
---|
177 |
int res = Apr.apr_file_close(mFile); |
---|
178 |
if(res != 0 ) |
---|
179 |
throw new AprException(res); |
---|
180 |
mFile = IntPtr.Zero; |
---|
181 |
} |
---|
182 |
#endregion |
---|
183 |
} |
---|
184 |
} |
---|