/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. using System;
  25. using System.Linq;
  26. using System.Text;
  27. using System.Threading;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Runtime.InteropServices;
  31. using Libev;
  32. using System.IO;
  33. namespace Manos.IO {
  34. public static class FileSystem {
  35. #if WINDOWS
  36. public static void GetFileLength(string path, Action<long, int> cb)
  37. {
  38. long l = 0;
  39. int e = 0;
  40. try
  41. {
  42. l = new System.IO.FileInfo(path).Length;
  43. }
  44. catch (FileNotFoundException)
  45. {
  46. e = 1;
  47. }
  48. catch (IOException)
  49. {
  50. e = 2;
  51. }
  52. cb(l, e);
  53. }
  54. #else
  55. public static void GetFileLength (string path, Action<long,int> cb)
  56. {
  57. GCHandle handle = GCHandle.Alloc (cb);
  58. manos_file_get_length (path, LengthCallbackHandler, GCHandle.ToIntPtr (handle));
  59. }
  60. public static void LengthCallbackHandler (IntPtr gchandle, IntPtr length, int error)
  61. {
  62. GCHandle handle = GCHandle.FromIntPtr (gchandle);
  63. Action<long,int> cb = (Action<long,int>) handle.Target;
  64. handle.Free ();
  65. cb (length.ToInt64 (), error);
  66. }
  67. [DllImport ("libmanos", CallingConvention = CallingConvention.Cdecl)]
  68. private static extern void manos_file_get_length (string path, Action<IntPtr,IntPtr,int> cb, IntPtr gchandle);
  69. #endif
  70. }
  71. }