PageRenderTime 68ms CodeModel.GetById 62ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Manos/Manos.IO/FileSystem.cs

http://github.com/jacksonh/manos
C# | 85 lines | 48 code | 14 blank | 23 comment | 0 complexity | d358187ed56d9124898100bb6cf69fa1 MD5 | raw file
 1//
 2// Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
 3//
 4// Permission is hereby granted, free of charge, to any person obtaining
 5// a copy of this software and associated documentation files (the
 6// "Software"), to deal in the Software without restriction, including
 7// without limitation the rights to use, copy, modify, merge, publish,
 8// distribute, sublicense, and/or sell copies of the Software, and to
 9// permit persons to whom the Software is furnished to do so, subject to
10// the following conditions:
11// 
12// The above copyright notice and this permission notice shall be
13// included in all copies or substantial portions of the Software.
14// 
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22//
23//
24
25
26
27using System;
28using System.Linq;
29using System.Text;
30using System.Threading;
31using System.Collections;
32using System.Collections.Generic;
33using System.Runtime.InteropServices;
34
35using Libev;
36using System.IO;
37
38
39namespace Manos.IO {
40
41	public static class FileSystem {
42#if WINDOWS
43        public static void GetFileLength(string path, Action<long, int> cb)
44        {
45            long l = 0;
46            int e = 0;
47            try
48            {
49                l = new System.IO.FileInfo(path).Length;
50            }
51            catch (FileNotFoundException)
52            {
53                e = 1;
54            }
55            catch (IOException)
56            {
57                e = 2;
58            }
59            cb(l, e);
60        }
61
62#else
63		public static void GetFileLength (string path, Action<long,int> cb)
64		{
65			GCHandle handle = GCHandle.Alloc (cb);
66			manos_file_get_length (path, LengthCallbackHandler, GCHandle.ToIntPtr (handle));
67
68		}
69
70		public static void LengthCallbackHandler (IntPtr gchandle, IntPtr length, int error)
71		{
72			GCHandle handle = GCHandle.FromIntPtr (gchandle);
73			Action<long,int> cb = (Action<long,int>) handle.Target;
74
75			handle.Free ();
76
77			cb (length.ToInt64 (), error);
78		}
79		
80		[DllImport ("libmanos", CallingConvention = CallingConvention.Cdecl)]
81		private static extern void manos_file_get_length (string path, Action<IntPtr,IntPtr,int> cb, IntPtr gchandle);
82#endif
83	}
84}
85