/// <summary>
        /// For an attribute this method will seperate its name and Parameters.
        /// </summary>
        /// <param name="attributeCode">The attribute code</param>
        /// <param name="attributeName">Return the attribute name</param>
        /// <param name="parameterStrList">Return the parameter string list</param>
        public static void Attribute_Parts(string attributeCode, out string attributeName, out List <string> parameterStrList)
        {
            attributeName    = "";
            parameterStrList = new List <string>();

            if (attributeCode.zContains_All("[", "]") == false)
            {
                var ex = new ArgumentException($"Error! No attribute input parameter: '{attributeCode}'", nameof(attributeCode));
                ex.zLogLibraryMsg();
                throw ex;
            }

            attributeCode = attributeCode.Replace("[", "").Replace("]", "");  //.Replace("(", " ").Replace(")", "");
            if (attributeCode.zContains_Any(" ", "(") == false)
            {
                attributeName = attributeCode;
                return;
            }

            attributeName = "(".zVar_Next(ref attributeCode);
            attributeCode = attributeCode.zSubStr_RemoveLastWord(")");

            while (attributeCode != "")
            {
                var parameter = ",".zVar_Next(ref attributeCode);
                parameterStrList.Add(parameter);
            }
        }
Example #2
0
        private readonly LamedalCore_ _lamed = LamedalCore_.Instance; // system library

        /// <summary>
        /// Function to  return document of parsed the XML.
        /// </summary>
        /// <param name="xml">The XML</param>
        /// <param name="autoFix">if set to <c>true</c> [automatic fix].</param>
        /// <returns>
        /// XDocument
        /// </returns>
        public XDocument Document(string xml, bool autoFix = false)
        {
            if (autoFix)
            {
                xml = _lamed.lib.XML.Setup.Fix_XMLErrorRootElements(xml);
            }

            XDocument result = null;

            try
            {
                result = XDocument.Parse(xml);
            }
            catch (Exception e)
            {
                if (autoFix)
                {
                    return(Document(xml, false));          // Retry with autofix
                }
                var ex = new ArgumentException($"Error in XML: '{xml}'", nameof(xml), e);
                ex.zLogLibraryMsg();
                throw ex;
            }
            return(result);
        }
Example #3
0
        private readonly LamedalCore_ _lamed = LamedalCore_.Instance; // system library

        /// <summary>
        /// Query string list items.
        /// </summary>
        /// <param name="strList">The string list</param>
        /// <param name="level">The level</param>
        /// <param name="delimiter">The delimiter setting. Default value = &quot;.&quot;.</param>
        /// <param name="filter">The filter setting. Default value = &quot;&quot;.</param>
        /// <returns>List<string/></returns>
        public IList <string> Query(IList <string> strList, int level = 1, string delimiter = ".", string filter = "")
        {
            if (strList == null)
            {
                throw new ArgumentNullException(nameof(strList));
            }
            if (level < 1)
            {
                var ex = new ArgumentException("Error: Level parameter must always be > 0", nameof(level));
                ex.zLogLibraryMsg();
                throw  ex;
            }
            var result = new List <string>();

            foreach (var nspace in strList)
            {
                if (filter != "")
                {
                    if (nspace.Contains(filter) == false)
                    {
                        continue;                                    // Filter the results
                    }
                }

                var spaces = nspace.zConvert_Array_FromStr(delimiter);
                if (level <= spaces.Count)
                {
                    var value = spaces[level - 1];
                    result.Add(value);
                }
            }
            return(_lamed.Types.List.Action.Unique(result));
        }
Example #4
0
        /// <summary>
        /// Remove a flag from an enum type
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public T Flag_Remove <T>(Enum type, T value)
        {
            T   result         = default(T);
            var underlyingType = Enum.GetUnderlyingType(value.GetType());

            try
            {
                if (underlyingType == typeof(int))
                {
                    result = (T)(object)(((int)(object)type & ~(int)(object)value));
                }
                else if (underlyingType == typeof(uint))
                {
                    result = (T)(object)(((uint)(object)type & ~(uint)(object)value));
                }
            }
            catch (Exception e)
            {
                var ex = new ArgumentException($"Could not remove value '{value}' from enumerated type '{underlyingType}'.", nameof(value), e);
                ex.zLogLibraryMsg();
                throw ex;
            }

            return(result);
        }
Example #5
0
        /// <summary>Write to file name</summary>
        /// <param name="pathAndFile">The path and file.</param>
        /// <param name="txt">The text.</param>
        /// <param name="writeAction">The write action.</param>
        public void File_Write(string pathAndFile, string txt, enIO_WriteAction writeAction = enIO_WriteAction.WriteFile)
        {
            if (writeAction == enIO_WriteAction.OverWriteFile)
            {
                File.WriteAllText(pathAndFile, txt);
                return;
            }

            bool fileExist = _io.File.Exists(pathAndFile);

            if (writeAction == enIO_WriteAction.WriteFile)
            {
                if (fileExist)
                {
                    var ex = new ArgumentException("Error! Can not write to file because it already exists.", nameof(writeAction));
                    ex.zLogLibraryMsg();
                    throw ex;
                }
                else
                {
                    File.WriteAllText(pathAndFile, txt);
                }
            }
            else if (writeAction == enIO_WriteAction.AppendFile)
            {
                if (_io.File.Exists(pathAndFile))
                {
                    txt = "".NL() + txt;                               // Add extra space into file
                }
                File.AppendAllText(pathAndFile, txt);
            }
        }
Example #6
0
        /// <summary>
        /// Function to adds the XML element to the root element.
        /// </summary>
        /// <param name="element">The root element</param>
        /// <param name="elementValue">The element</param>
        /// <returns>
        /// XElement
        /// </returns>
        public XElement Element_Add(XElement element, string elementValue)
        {
            if (element == null)
            {
                var ex = new ArgumentException("Error! Can not add element value to undefined element!", nameof(element));
                ex.zLogLibraryMsg();
                throw ex;
            }
            var result = new XElement(elementValue);

            element.Add(result);
            return(result);
        }
Example #7
0
        public static ClassNT_ Create(string classFile, out bool error, out ClassNTBlueprintRule_ blueprintRule /*,ProjectNT_ project = null*/)
        {
            blueprintRule = null;
            if (1f.zIO().File.Exists(classFile) == false)
            {
                var ex = new ArgumentException($"Error! File '{classFile}' does not exists.", nameof(classFile));
                ex.zLogLibraryMsg();
                throw ex;
            }

            string[] codeLines = 1f.zIO().RW.File_Read2StrArray(classFile);
            return(ClassNT_.Create(codeLines.ToList(), out error, out blueprintRule, classFile /*, project*/));
        }
Example #8
0
        /// <summary>Executes the command on the console and provide ability to show the output.</summary>
        /// <param name="exeName">The executable name</param>
        /// <param name="arguments">The arguments</param>
        /// <param name="OnOutputFromProcess">The on output from process setting. Default value = null.</param>
        /// <param name="OnErrorFromProcess">The on error from process setting. Default value = null.</param>
        /// <param name="Object">The object setting. Default value = null.</param>
        public void Execute(string exeName, string arguments             = "",
                            DataReceivedEventHandler OnOutputFromProcess = null, DataReceivedEventHandler OnErrorFromProcess = null,
                            object Object = null)
        {
            //var outputStr = new StringBuilder();
            if (exeName == null)
            {
                var ex = new ArgumentException("Error! exeName can not be null!", nameof(exeName));
                ex.zLogLibraryMsg();
                throw ex;
            }

            var processInfo = new ProcessStartInfo();

            processInfo.CreateNoWindow         = true;
            processInfo.RedirectStandardInput  = true;
            processInfo.RedirectStandardOutput = true;
            processInfo.UseShellExecute        = false;
            processInfo.FileName  = exeName;
            processInfo.Arguments = arguments;

            var process = new Process();
            var state   = process.zzState();

            state.TextBox = Object as TextBox;

            process.StartInfo           = processInfo;
            process.EnableRaisingEvents = true;
            if (OnOutputFromProcess != null)
            {
                process.OutputDataReceived -= OnOutputFromProcess;
                process.OutputDataReceived += OnOutputFromProcess;
            }

            if (OnErrorFromProcess != null)
            {
                process.ErrorDataReceived -= OnErrorFromProcess;
                process.ErrorDataReceived += OnErrorFromProcess;
            }

            process.Start();
            process.BeginOutputReadLine();

            //process.WaitForExit();
            //while (process.StandardOutput.EndOfStream == false)
            //{
            //    Thread.Sleep(100);
            //}
            //process.CancelOutputRead();
        }
Example #9
0
        /// <summary>
        /// Update the generated form from the object.
        /// </summary>
        /// <param name="form">The form</param>
        /// <param name="Object">The object</param>
        /// <param name="fieldName">Name of the field.</param>
        public void Form_FromObject(Form form, IObjectModel Object, string fieldName = "")
        {
            if (Object == null)
            {
                var ex = new ArgumentException("Error! The object can not be null!", nameof(Object));
                ex.zLogLibraryMsg();
                throw ex;
            }

            var type     = Object.GetType();
            var controls = IamWindows.libUI.WinForms.Controls.Control.FindComponents <IUControl_ObjectModel>(form);
            List <IUControl_ObjectModel> controlFields;

            if (fieldName == "")
            {
                controlFields = controls.Where(x => x.Field_Name != null).ToList();
            }
            else
            {
                controlFields = controls.Where(x => x.Field_Name == fieldName).ToList();
            }

            FieldInfo[]    fields     = LamedalCore_.Instance.Types.Class.ClassInfo.Fields_AsFieldInfo(type);
            PropertyInfo[] properties = LamedalCore_.Instance.Types.Class.ClassInfo.Properties_AsPropertyInfo(type);

            foreach (IUControl_ObjectModel control in controlFields)
            {
                // Find the field or the property form the object and
                // get the value
                // =================================================
                object value = null;
                fieldName = control.Field_Name;
                FieldInfo field = fields.FirstOrDefault(x => x.Name == fieldName);
                if (field != null)
                {
                    value = field.GetValue(Object);
                }
                else
                {
                    PropertyInfo property = properties.FirstOrDefault(x => x.Name == fieldName);
                    if (property == null)
                    {
                        var errMsg = "Error! Unable to find form control in the model for field name '{0}'.".zFormat(fieldName);
                        throw new Exception(errMsg);
                    }
                    value = property.GetValue(Object, null);
                }


                // Assign the value to the control on the form
                // ===========================================
                //var test = value == null;
                //if (test == false) test = value is string;

                if (control is ListBox /*&& test == false*/)
                {
                    var listbox = control as ListBox;
                    if (value == null)
                    {
                        listbox.Items.Clear();
                    }
                    else
                    {
                        var listValues = (IList)value;
                        if (listbox.Items.Count != listValues.Count)
                        {
                            listbox.Items.zFrom_IList(listValues);
                        }
                    }
                }
                else
                {
                    string fieldValue = value.zObject().AsStr();
                    control.Field_Value = fieldValue;
                }
            }
        }