private static void WriteAllText(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); bool success = false; Exception exception = null; if (arguments.Length > 1) { string path = (arguments[0] as NSJSString)?.Value; string contents = (arguments[1] as NSJSString)?.Value; Encoding encodings = NSJSEncoding.DefaultEncoding; if (arguments.Length > 2) { encodings = NSJSEncoding.GetEncoding(arguments[2] as NSJSObject); } try { FILE.WriteAllText(path, contents, encodings); success = true; } catch (Exception e) { exception = e; } } if (exception == null) { arguments.SetReturnValue(success); } else { Throwable.Exception(arguments.VirtualMachine, exception); } }
private static void WriteAllBytes(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); NSJSString url = arguments.Length > 0 ? arguments[0] as NSJSString : null; NSJSUInt8Array buffer = arguments.Length > 1 ? arguments[1] as NSJSUInt8Array : null; bool success = false; Exception exception = null; if (url != null) { string path = url.Value; byte[] bytes = (buffer == null ? BufferExtension.EmptryBuffer : buffer.Buffer); try { FILE.WriteAllBytes(path, bytes); success = true; } catch (Exception e) { exception = e; success = false; } } if (exception == null) { arguments.SetReturnValue(success); } else { Throwable.Exception(arguments.VirtualMachine, exception); } }
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); } }
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); } }
private static void MoveOrCopy(IntPtr info, bool copyMode) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); bool success = false; Exception exception = null; do { if (arguments.Length <= 0) { break; } NSJSString sourceFileName = arguments[0] as NSJSString; NSJSString destFileName = arguments[1] as NSJSString; if (sourceFileName == null || destFileName == null) { break; } try { if (copyMode) { if (arguments.Length < 3) { FILE.Copy(sourceFileName.Value, destFileName.Value); } else { bool overwrite = ValueAuxiliary.ToBoolean(arguments[2]); FILE.Copy(sourceFileName.Value, destFileName.Value, overwrite); } } else { FILE.Move(sourceFileName.Value, destFileName.Value); } success = true; } catch (Exception e) { exception = e; success = false; } } while (false); if (exception == null) { arguments.SetReturnValue(success); } else { Throwable.Exception(arguments.VirtualMachine, exception); } }
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)); } }
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); } }
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); } }
private static void GetLastAccessTime(IntPtr info) { CallGetFileInfo(info, (arguments, path) => arguments.SetReturnValue(FILE.GetLastAccessTime(path))); }
private static void GetCreationTimeUtc(IntPtr info) { CallGetFileInfo(info, (arguments, path) => arguments.SetReturnValue(FILE.GetCreationTimeUtc(path))); }
private static void SetAttributes(IntPtr info) { CallSetFileInfo(info, (arguments, path, value) => FILE.SetAttributes(path, unchecked ((FileAttributes)value))); }
private static void SetLastWriteTimeUtc(IntPtr info) { CallSetFileInfo(info, (arguments, path, datetime) => FILE.SetLastWriteTimeUtc(path, datetime)); }
private static void SetLastAccessTime(IntPtr info) { CallSetFileInfo(info, (arguments, path, datetime) => FILE.SetLastAccessTime(path, datetime)); }
private static void GetAttributes(IntPtr info) { CallGetFileInfo(info, (arguments, path) => arguments.SetReturnValue(unchecked ((int)FILE.GetAttributes(path)))); }