Esempio n. 1
0
        /// <summary>
        /// Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
        /// </summary>
        internal static void AppendJson(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue contents, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[appendJson] First parameter should be defined and be a string.");
            if (!contents.IsStrictlyObject)
                throw new ArgumentException("[appendJson] Second parameter should be defined and be an object.");
            if (!onComplete.IsUndefined && !onComplete.IsFunction)
                throw new ArgumentException("[appendJson] Third parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Unbox the array of lines and execute the append
                File.AppendAllText(
                    path.Unbox<string>(),
                    Native.Serialize(instance.Env, contents, false).Unbox<string>(),
                    encoding
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance);
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.
        /// </summary>
        internal static void WriteLines(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue contents, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[writeLines] TFirst parameter should be defined and be a string.");
            if (!contents.IsArray)
                throw new ArgumentException("[writeLines] TSecond parameter should be defined and be an array.");
            if (!onComplete.IsUndefined && !onComplete.IsFunction)
                throw new ArgumentException("[writeLines] Third parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Unbox the array of lines and execute the append
                File.WriteAllLines(
                    path.Unbox<string>(),
                    contents.Array.ToArray<string>(),
                    encoding
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance);
            });
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
        /// </summary>
        internal static void WriteBuffer(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue contents, BoxedValue onComplete)
        {
            if (!path.IsString)
                throw new ArgumentException("[writeBuffer] First parameter should be defined and be a string.");
            if (!contents.IsObject || !(contents.Object is BufferObject))
                throw new ArgumentException("[writeBuffer] Second parameter should be defined and be a Buffer.");
            if (!onComplete.IsUndefined && !onComplete.IsFunction)
                throw new ArgumentException("[writeBuffer] Third parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // Get the buffer
                var buffer = contents.Object as BufferObject;

                // Write the contents
                File.WriteAllBytes(
                    path.Unbox<string>(),
                    buffer.Array
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance);
            });
        }
Esempio n. 4
0
        /// <summary>
        /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
        /// </summary>
        internal static void ReadText(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[readText] First parameter should be defined and be a string.");
            if (!onComplete.IsFunction)
                throw new ArgumentException("[readText] Second parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Read the text
                var text = BoxedValue.Box(
                        File.ReadAllText(path.Unbox<string>(), encoding)
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance, text);
            });
        }
Esempio n. 5
0
        /// <summary>
        /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
        /// </summary>
        internal static void ReadLines(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[readLines] First parameter should be defined and be a string.");
            if (!onComplete.IsFunction)
                throw new ArgumentException("[readLines] Second parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Unbox the array of lines and execute the append
                var lines = File.ReadAllLines(
                    path.Unbox<string>(),
                    encoding
                    );

                // Create a new array
                var array = new ArrayObject(instance.Env, (uint)lines.Length);
                for (uint i = 0; i < lines.Length; ++i)
                {
                    // Put a boxed string inside for each line
                    array.Put(i, BoxedValue.Box(lines[i]));
                }

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance, BoxedValue.Box(array));

            });
        }
Esempio n. 6
0
        /// <summary>
        /// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
        /// </summary>
        internal static void ReadBuffer(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue onComplete)
        {
            if (!path.IsString)
                throw new ArgumentException("[readBuffer] First parameter should be defined and be a string.");
            if (!onComplete.IsFunction)
                throw new ArgumentException("[readBuffer] Second parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // Read the file
                var arr = File.ReadAllBytes(path.Unbox<string>());
                var seg = new ArraySegment<byte>(arr);

                // Unbox the array of lines and execute the append
                var buffer = BoxedValue.Box(
                    new BufferObject(seg, instance.Env)
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance, buffer);
            });
        }