Update PointConverter.cs
[mono.git] / mcs / class / Mono.Posix / Mono.Unix.Native / NativeConvert.cs
1 /*
2  * This file was automatically generated by make-map from Mono.Posix.dll.
3  *
4  * DO NOT MODIFY.
5  */
6
7 using System;
8 using System.IO;
9 using System.Runtime.InteropServices;
10 using Mono.Unix.Native;
11
12 namespace Mono.Unix.Native {
13
14         [CLSCompliant (false)]
15         public sealed /* static */ partial class NativeConvert
16         {
17                 //
18                 // Non-generated exports
19                 //
20
21                 [DllImport (LIB, EntryPoint="Mono_Posix_FromRealTimeSignum")]
22                 private static extern int FromRealTimeSignum (Int32 offset, out Int32 rval);
23
24                 // convert a realtime signal to os signal
25                 public static int FromRealTimeSignum (RealTimeSignum sig)
26                 {
27                         int sigNum;
28                         if (FromRealTimeSignum (sig.Offset, out sigNum) == -1)
29                                 ThrowArgumentException (sig.Offset);
30                         return sigNum;
31                 }
32
33                 // convert an offset to an rt signum
34                 public static RealTimeSignum ToRealTimeSignum (int offset)
35                 {
36                         return new RealTimeSignum (offset);
37                 }
38
39                 // convert from octal representation.
40                 public static FilePermissions FromOctalPermissionString (string value)
41                 {
42                         uint n = Convert.ToUInt32 (value, 8);
43                         return ToFilePermissions (n);
44                 }
45
46                 public static string ToOctalPermissionString (FilePermissions value)
47                 {
48                         string s = Convert.ToString ((int) (value & ~FilePermissions.S_IFMT), 8);
49                         return new string ('0', 4-s.Length) + s;
50                 }
51
52                 public static FilePermissions FromUnixPermissionString (string value)
53                 {
54                         if (value == null)
55                                 throw new ArgumentNullException ("value");
56                         if (value.Length != 9 && value.Length != 10)
57                                 throw new ArgumentException ("value", "must contain 9 or 10 characters");
58
59                         int i = 0;
60                         FilePermissions perms = new FilePermissions ();
61
62                         if (value.Length == 10) {
63                                 perms |= GetUnixPermissionDevice (value [i]);
64                                 ++i;
65                         }
66
67                         perms |= GetUnixPermissionGroup (
68                                 value [i++], FilePermissions.S_IRUSR,
69                                 value [i++], FilePermissions.S_IWUSR,
70                                 value [i++], FilePermissions.S_IXUSR,
71                                 's', 'S', FilePermissions.S_ISUID);
72
73                         perms |= GetUnixPermissionGroup (
74                                 value [i++], FilePermissions.S_IRGRP,
75                                 value [i++], FilePermissions.S_IWGRP,
76                                 value [i++], FilePermissions.S_IXGRP,
77                                 's', 'S', FilePermissions.S_ISGID);
78
79                         perms |= GetUnixPermissionGroup (
80                                 value [i++], FilePermissions.S_IROTH,
81                                 value [i++], FilePermissions.S_IWOTH,
82                                 value [i++], FilePermissions.S_IXOTH,
83                                 't', 'T', FilePermissions.S_ISVTX);
84
85                         return perms;
86                 }
87
88                 private static FilePermissions GetUnixPermissionDevice (char value)
89                 {
90                         switch (value) {
91                         case 'd': return FilePermissions.S_IFDIR;
92                         case 'c': return FilePermissions.S_IFCHR;
93                         case 'b': return FilePermissions.S_IFBLK;
94                         case '-': return FilePermissions.S_IFREG;
95                         case 'p': return FilePermissions.S_IFIFO;
96                         case 'l': return FilePermissions.S_IFLNK;
97                         case 's': return FilePermissions.S_IFSOCK;
98                         }
99                         throw new ArgumentException ("value", "invalid device specification: " + 
100                                 value);
101                 }
102
103                 private static FilePermissions GetUnixPermissionGroup (
104                         char read, FilePermissions readb, 
105                         char write, FilePermissions writeb, 
106                         char exec, FilePermissions execb,
107                         char xboth, char xbitonly, FilePermissions xbit)
108                 {
109                         FilePermissions perms = new FilePermissions ();
110                         if (read == 'r')
111                                 perms |= readb;
112                         if (write == 'w')
113                                 perms |= writeb;
114                         if (exec == 'x')
115                                 perms |= execb;
116                         else if (exec == xbitonly)
117                                 perms |= xbit;
118                         else if (exec == xboth)
119                                 perms |= (execb | xbit);
120                         return perms;
121                 }
122
123                 // Create ls(1) drwxrwxrwx permissions display
124                 public static string ToUnixPermissionString (FilePermissions value)
125                 {
126                         char [] access = new char[] {
127                                 '-',            // device
128                                 '-', '-', '-',  // owner
129                                 '-', '-', '-',  // group
130                                 '-', '-', '-',  // other
131                         };
132                         bool have_device = true;
133                         switch (value & FilePermissions.S_IFMT) {
134                                 case FilePermissions.S_IFDIR:   access [0] = 'd'; break;
135                                 case FilePermissions.S_IFCHR:   access [0] = 'c'; break;
136                                 case FilePermissions.S_IFBLK:   access [0] = 'b'; break;
137                                 case FilePermissions.S_IFREG:   access [0] = '-'; break;
138                                 case FilePermissions.S_IFIFO:   access [0] = 'p'; break;
139                                 case FilePermissions.S_IFLNK:   access [0] = 'l'; break;
140                                 case FilePermissions.S_IFSOCK:  access [0] = 's'; break;
141                                 default:                        have_device = false; break;
142                         }
143                         SetUnixPermissionGroup (value, access, 1, 
144                                 FilePermissions.S_IRUSR, FilePermissions.S_IWUSR, FilePermissions.S_IXUSR,
145                                 's', 'S', FilePermissions.S_ISUID);
146                         SetUnixPermissionGroup (value, access, 4, 
147                                 FilePermissions.S_IRGRP, FilePermissions.S_IWGRP, FilePermissions.S_IXGRP,
148                                 's', 'S', FilePermissions.S_ISGID);
149                         SetUnixPermissionGroup (value, access, 7, 
150                                 FilePermissions.S_IROTH, FilePermissions.S_IWOTH, FilePermissions.S_IXOTH,
151                                 't', 'T', FilePermissions.S_ISVTX);
152                         return have_device 
153                                 ? new string (access)
154                                 : new string (access, 1, 9);
155                 }
156
157                 private static void SetUnixPermissionGroup (FilePermissions value,
158                         char[] access, int index,
159                         FilePermissions read, FilePermissions write, FilePermissions exec,
160                         char both, char setonly, FilePermissions setxbit)
161                 {
162                         if (UnixFileSystemInfo.IsSet (value, read))
163                                 access [index] = 'r';
164                         if (UnixFileSystemInfo.IsSet (value, write))
165                                 access [index+1] = 'w';
166                         access [index+2] = GetSymbolicMode (value, exec, both, setonly, setxbit);
167                 }
168
169                 // Implement the GNU ls(1) permissions spec; see `info coreutils ls`,
170                 // section 10.1.2, the `-l' argument information.
171                 private static char GetSymbolicMode (FilePermissions value, 
172                         FilePermissions xbit, char both, char setonly, FilePermissions setxbit)
173                 {
174                         bool is_x  = UnixFileSystemInfo.IsSet (value, xbit);
175                         bool is_sx = UnixFileSystemInfo.IsSet (value, setxbit);
176                         
177                         if (is_x && is_sx)
178                                 return both;
179                         if (is_sx)
180                                 return setonly;
181                         if (is_x)
182                                 return 'x';
183                         return '-';
184                 }
185
186                 public static readonly DateTime LocalUnixEpoch = 
187                         new DateTime (1970, 1, 1);
188                 public static readonly TimeSpan LocalUtcOffset = 
189                         TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.UtcNow);
190
191                 public static DateTime ToDateTime (long time)
192                 {
193                         return FromTimeT (time);
194                 }
195
196                 public static long FromDateTime (DateTime time)
197                 {
198                         return ToTimeT (time);
199                 }
200
201                 public static DateTime FromTimeT (long time)
202                 {
203                         DateTime r = LocalUnixEpoch.AddSeconds ((double) time + 
204                                         LocalUtcOffset.TotalSeconds);
205                         return r;
206                 }
207
208                 public static long ToTimeT (DateTime time)
209                 {
210                         TimeSpan unixTime = time.Subtract (LocalUnixEpoch) - LocalUtcOffset;
211                         return (long) unixTime.TotalSeconds;
212                 }
213
214                 public static OpenFlags ToOpenFlags (FileMode mode, FileAccess access)
215                 {
216                         OpenFlags flags = 0;
217                         switch (mode) {
218                         case FileMode.CreateNew:
219                                 flags = OpenFlags.O_CREAT | OpenFlags.O_EXCL;
220                                 break;
221                         case FileMode.Create:
222                                 flags = OpenFlags.O_CREAT | OpenFlags.O_TRUNC;
223                                 break;
224                         case FileMode.Open:
225                                 // do nothing
226                                 break;
227                         case FileMode.OpenOrCreate:
228                                 flags = OpenFlags.O_CREAT;
229                                 break;
230                         case FileMode.Truncate:
231                                 flags = OpenFlags.O_TRUNC;
232                                 break;
233                         case FileMode.Append:
234                                 flags = OpenFlags.O_APPEND;
235                                 break;
236                         default:
237                                 throw new ArgumentException (Locale.GetText ("Unsupported mode value"), "mode");
238                         }
239
240                         // Is O_LARGEFILE supported?
241                         int _v;
242                         if (TryFromOpenFlags (OpenFlags.O_LARGEFILE, out _v))
243                                 flags |= OpenFlags.O_LARGEFILE;
244
245                         switch (access) {
246                         case FileAccess.Read:
247                                 flags |= OpenFlags.O_RDONLY;
248                                 break;
249                         case FileAccess.Write:
250                                 flags |= OpenFlags.O_WRONLY;
251                                 break;
252                         case FileAccess.ReadWrite:
253                                 flags |= OpenFlags.O_RDWR;
254                                 break;
255                         default:
256                                 throw new ArgumentOutOfRangeException (Locale.GetText ("Unsupported access value"), "access");
257                         }
258
259                         return flags;
260                 }
261
262                 public static string ToFopenMode (FileAccess access)
263                 {
264                         switch (access) {
265                                 case FileAccess.Read:       return "rb";
266                                 case FileAccess.Write:      return "wb";
267                                 case FileAccess.ReadWrite:  return "r+b";
268                                 default:                    throw new ArgumentOutOfRangeException ("access");
269                         }
270                 }
271
272                 public static string ToFopenMode (FileMode mode)
273                 {
274                         switch (mode) {
275                                 case FileMode.CreateNew: case FileMode.Create:        return "w+b";
276                                 case FileMode.Open:      case FileMode.OpenOrCreate:  return "r+b";
277                                 case FileMode.Truncate: return "w+b";
278                                 case FileMode.Append:   return "a+b";
279                                 default:                throw new ArgumentOutOfRangeException ("mode");
280                         }
281                 }
282
283                 private static readonly string[][] fopen_modes = new string[][]{
284                         //                                         Read                       Write ReadWrite
285                         /*    FileMode.CreateNew: */  new string[]{"Can't Read+Create",       "wb", "w+b"},
286                         /*       FileMode.Create: */  new string[]{"Can't Read+Create",       "wb", "w+b"},
287                         /*         FileMode.Open: */  new string[]{"rb",                      "wb", "r+b"},
288                         /* FileMode.OpenOrCreate: */  new string[]{"rb",                      "wb", "r+b"},
289                         /*     FileMode.Truncate: */  new string[]{"Cannot Truncate and Read","wb", "w+b"},
290                         /*       FileMode.Append: */  new string[]{"Cannot Append and Read",  "ab", "a+b"},
291                 };
292
293                 public static string ToFopenMode (FileMode mode, FileAccess access)
294                 {
295                         int fm = -1, fa = -1;
296                         switch (mode) {
297                                 case FileMode.CreateNew:    fm = 0; break;
298                                 case FileMode.Create:       fm = 1; break;
299                                 case FileMode.Open:         fm = 2; break;
300                                 case FileMode.OpenOrCreate: fm = 3; break;
301                                 case FileMode.Truncate:     fm = 4; break;
302                                 case FileMode.Append:       fm = 5; break;
303                         }
304                         switch (access) {
305                                 case FileAccess.Read:       fa = 0; break;
306                                 case FileAccess.Write:      fa = 1; break;
307                                 case FileAccess.ReadWrite:  fa = 2; break;
308                         }
309
310                         if (fm == -1)
311                                 throw new ArgumentOutOfRangeException ("mode");
312                         if (fa == -1)
313                                 throw new ArgumentOutOfRangeException ("access");
314
315                         string fopen_mode = fopen_modes [fm][fa];
316                         if (fopen_mode [0] != 'r' && fopen_mode [0] != 'w' && fopen_mode [0] != 'a')
317                                 throw new ArgumentException (fopen_mode);
318                         return fopen_mode;
319                 }
320
321                 [DllImport (LIB, EntryPoint="Mono_Posix_FromStat")]
322                 private static extern int FromStat (ref Stat source, IntPtr destination);
323
324                 public static bool TryCopy (ref Stat source, IntPtr destination)
325                 {
326                         return FromStat (ref source, destination) == 0;
327                 }
328
329                 [DllImport (LIB, EntryPoint="Mono_Posix_ToStat")]
330                 private static extern int ToStat (IntPtr source, out Stat destination);
331
332                 public static bool TryCopy (IntPtr source, out Stat destination)
333                 {
334                         return ToStat (source, out destination) == 0;
335                 }
336
337                 [DllImport (LIB, EntryPoint="Mono_Posix_FromStatvfs")]
338                 private static extern int FromStatvfs (ref Statvfs source, IntPtr destination);
339
340                 public static bool TryCopy (ref Statvfs source, IntPtr destination)
341                 {
342                         return FromStatvfs (ref source, destination) == 0;
343                 }
344
345                 [DllImport (LIB, EntryPoint="Mono_Posix_ToStatvfs")]
346                 private static extern int ToStatvfs (IntPtr source, out Statvfs destination);
347
348                 public static bool TryCopy (IntPtr source, out Statvfs destination)
349                 {
350                         return ToStatvfs (source, out destination) == 0;
351                 }
352         }
353 }
354
355 // vim: noexpandtab