<commit_before>/*
 * Lab# - Matlab interaction library for .Net
 * 
 * Copyright (C) 2005 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Text;
using LabSharp.NativeFunctions;

namespace LabSharp
{
    /// <summary>
    /// <para>A class to wrap a matlab engine instance.</para>
    /// <para>Functions are the same that the ones in LibEng but are C#
    /// friendlly and check return codes.</para>
    /// </summary>
    public class Engine : IDisposable
    {
        public class VariableNotFoundException : Exception
        {
            public VariableNotFoundException(string var_name)
                : base(string.Format("Unable to find variable \"{0}\" in matlab workspace",
                var_name))
            {
            }
        }

        IntPtr m_engine;
        public IntPtr NativeObject { get { return m_engine; } }

        bool m_closeOnDispose;

        private void CheckPointer()
        {
            if (m_engine == IntPtr.Zero)
            {
                throw new Exception("Invalid engine pointer");
            }
        }

        public static Engine Open()
        {
            return Open(false);
        }

        public static Engine Open(bool closeOnDispose)
        {
            return new Engine(LibEng.engOpen(), closeOnDispose);
        }

        public static Engine OpenSingleUse()
        {
            return new Engine(LibEng.engOpenSingleUse(), true);
        }

        private Engine(IntPtr engine, bool closeOnDispose)
        {
            if (engine == IntPtr.Zero) throw new Exception("engine pointer is NULL");
            m_engine = engine;
            m_closeOnDispose = closeOnDispose;
        }

        public void Close()
        {
            CheckPointer();
            if (LibEng.engClose(m_engine) != 0)
            {
                throw new Exception("Unable to close this Engine, it may already have been closed");
            }
        }

        public void Eval(string cmd)
        {
            CheckPointer();
            if (LibEng.engEvalString(m_engine, cmd) != 0)
            {
                throw new Exception("Unable to EvalString, it is possible that this engine doesn't exists anymore");
            }
        }

        public bool Visible
        {
            get
            {
                CheckPointer();
                bool value;
                LibEng.engGetVisible(m_engine, out value);
                return value;
            }
            set
            {
                CheckPointer();
                LibEng.engSetVisible(m_engine, value);
            }
        }

        public void OutputToBuffer(IntPtr buffer, int bufferSize)
        {
            CheckPointer();
            LibEng.engOutputBuffer(m_engine, buffer, bufferSize);
        }

        public void DisableOutputToBuffer()
        {
            OutputToBuffer(IntPtr.Zero, 0);
        }

        #region Get/Set MxArray

        public void SetVariable(string var_name, MxArray array)
        {
            CheckPointer();
            if (LibEng.engPutVariable(m_engine, var_name, array.NativeObject) != 0)
            {
                throw new Exception(String.Format("Unable to put variable {0} in Matlab Engine", var_name));
            }
        }<commit_msg>Some more NULL argument checks.<commit_after>/*
 * Lab# - Matlab interaction library for .Net
 * 
 * Copyright (C) 2005 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Text;
using LabSharp.NativeFunctions;

namespace LabSharp
{
    /// <summary>
    /// <para>A class to wrap a matlab engine instance.</para>
    /// <para>Functions are the same that the ones in LibEng but are C#
    /// friendlly and check return codes.</para>
    /// </summary>
    public class Engine : IDisposable
    {
        public class VariableNotFoundException : Exception
        {
            public VariableNotFoundException(string var_name)
                : base(string.Format("Unable to find variable \"{0}\" in matlab workspace",
                var_name))
            {
            }
        }

        IntPtr m_engine;
        public IntPtr NativeObject { get { return m_engine; } }

        bool m_closeOnDispose;

        private void CheckPointer()
        {
            if (m_engine == IntPtr.Zero)
            {
                throw new Exception("Invalid engine pointer");
            }
        }

        public static Engine Open()
        {
            return Open(false);
        }

        public static Engine Open(bool closeOnDispose)
        {
            return new Engine(LibEng.engOpen(), closeOnDispose);
        }

        public static Engine OpenSingleUse()
        {
            return new Engine(LibEng.engOpenSingleUse(), true);
        }

        private Engine(IntPtr engine, bool closeOnDispose)
        {
            if (engine == IntPtr.Zero) throw new Exception("engine pointer is null.");
            m_engine = engine;
            m_closeOnDispose = closeOnDispose;
        }

        public void Close()
        {
            CheckPointer();
            if (LibEng.engClose(m_engine) != 0)
            {
                throw new Exception("Unable to close this Engine, it may already have been closed");
            }
        }

        public void Eval(string cmd)
        {
            CheckPointer();
            if (LibEng.engEvalString(m_engine, cmd) != 0)
            {
                throw new Exception("Unable to EvalString, it is possible that this engine doesn't exists anymore");
            }
        }

        public bool Visible
        {
            get
            {
                CheckPointer();
                bool value;
                LibEng.engGetVisible(m_engine, out value);
                return value;
            }
            set
            {
                CheckPointer();
                LibEng.engSetVisible(m_engine, value);
            }
        }

        public void OutputToBuffer(IntPtr buffer, int bufferSize)
        {
            CheckPointer();
            LibEng.engOutputBuffer(m_engine, buffer, bufferSize);
        }

        public void DisableOutputToBuffer()
        {
            OutputToBuffer(IntPtr.Zero, 0);
        }

        #region Get/Set MxArray

        public void SetVariable(string var_name, MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            CheckPointer();
            if (LibEng.engPutVariable(m_engine, var_name, array.NativeObject) != 0)
            {
                throw new Exception(String.Format("Unable to put variable {0} in Matlab Engine", var_name));
            }
        }
{'fraction_non_alphanumeric': 0.0787333661902415, 'fraction_numerical': 0.005667816658452439, 'mean_word_length': 1.7797945205479453, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4366245', 'n_tokens_mistral': 2085, 'n_tokens_neox': 1981, 'n_words': 702}