Example #1
0
        private static void Delete(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            NSJSString rawUri    = arguments.Length > 0 ? arguments[0] as NSJSString : null;
            bool       success   = false;
            Exception  exception = null;

            if (rawUri != null)
            {
                string path = rawUri.Value;
                if (FILE.Exists(path))
                {
                    try
                    {
                        FILE.Delete(path);
                        success = true;
                    }
                    catch (Exception e)
                    {
                        exception = e;
                        success   = false;
                    }
                }
            }
            if (exception == null)
            {
                arguments.SetReturnValue(success);
            }
            else
            {
                Throwable.Exception(arguments.VirtualMachine, exception);
            }
        }
Example #2
0
        private static void ReadAllBytes(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            string path = arguments.Length > 0 ? (arguments[0] as NSJSString)?.Value : null;

            byte[]    buffer    = null;
            Exception exception = null;

            if (FILE.Exists(path))
            {
                try
                {
                    buffer = FILE.ReadAllBytes(path);
                }
                catch (Exception e)
                {
                    exception = e;
                }
            }
            if (buffer != null)
            {
                arguments.SetReturnValue(buffer);
            }
            else if (exception == null)
            {
                Throwable.FileNotFoundException(arguments.VirtualMachine);
            }
            else
            {
                Throwable.Exception(arguments.VirtualMachine, exception);
            }
        }
Example #3
0
        private static void Exists(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            NSJSString path = arguments.Length > 0 ? arguments[0] as NSJSString : null;

            if (path == null)
            {
                arguments.SetReturnValue(false);
            }
            else
            {
                arguments.SetReturnValue(FILE.Exists(path.Value));
            }
        }
Example #4
0
        private static void CallGetFileInfo(IntPtr info, Action <NSJSFunctionCallbackInfo, string> callback)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            string path = arguments.Length > 0 ? (arguments[0] as NSJSString)?.Value : null;

            if (!FILE.Exists(path))
            {
                Throwable.FileNotFoundException(arguments.VirtualMachine);
            }
            else
            {
                callback(arguments, path);
            }
        }
Example #5
0
        private static void ReadAllText(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            string    path      = arguments.Length > 0 ? (arguments[0] as NSJSString)?.Value : null;
            string    contents  = null;
            Exception exception = null;

            if (FILE.Exists(path))
            {
                Encoding encodings = NSJSEncoding.DefaultEncoding;
                if (arguments.Length > 1)
                {
                    encodings = NSJSEncoding.GetEncoding(arguments[1] as NSJSObject);
                }
                try
                {
                    contents = FILE.ReadAllText(path, encodings);
                }
                catch (Exception e)
                {
                    exception = e;
                }
            }
            if (contents != null)
            {
                arguments.SetReturnValue(contents);
            }
            else if (exception == null)
            {
                Throwable.FileNotFoundException(arguments.VirtualMachine);
            }
            else
            {
                Throwable.Exception(arguments.VirtualMachine, exception);
            }
        }