internal static void LoadTemplateCommonProperties(ref int appCnt, XmlElement docx, AVRProject proj)
        {
            foreach (XmlElement param in docx.GetElementsByTagName("ClockFreq"))
            {
                proj.ClockFreq = decimal.Parse(param.InnerText, System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowLeadingWhite | System.Globalization.NumberStyles.AllowTrailingWhite);
                appCnt++;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("Device"))
            {
                proj.Device = param.InnerText;
                appCnt++;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("LinkerOpt"))
            {
                proj.LinkerOptions = param.InnerText;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("OtherOpt"))
            {
                proj.OtherOptions = param.InnerText;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("OtherOptionsForC"))
            {
                proj.OtherOptionsForC = param.InnerText;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("OtherOptionsForCPP"))
            {
                proj.OtherOptionsForCPP = param.InnerText;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("OtherOptionsForS"))
            {
                proj.OtherOptionsForS = param.InnerText;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("Optimization"))
            {
                proj.Optimization = param.InnerText;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("UseInitStack"))
            {
                proj.UseInitStack = Program.StringToBool(param);
            }
            foreach (XmlElement param in docx.GetElementsByTagName("InitStackAddr"))
            {
                try
                {
                    if (param.InnerText.ToLowerInvariant().StartsWith("0x"))
                    {
                        proj.InitStackAddr = Convert.ToUInt32(param.InnerText, 16);
                    }
                    else
                    {
                        proj.InitStackAddr = Convert.ToUInt32("0x" + param.InnerText, 16);
                    }
                }
                catch { }
            }
            foreach (XmlElement param in docx.GetElementsByTagName("PackStructs"))
            {
                proj.PackStructs = Program.StringToBool(param);
            }
            foreach (XmlElement param in docx.GetElementsByTagName("ShortEnums"))
            {
                proj.ShortEnums = Program.StringToBool(param);
            }
            foreach (XmlElement param in docx.GetElementsByTagName("UnsignedBitfields"))
            {
                proj.UnsignedBitfields = Program.StringToBool(param);
            }
            foreach (XmlElement param in docx.GetElementsByTagName("UnsignedChars"))
            {
                proj.UnsignedChars = Program.StringToBool(param);
            }
            foreach (XmlElement param in docx.GetElementsByTagName("FunctionSections"))
            {
                proj.FunctionSections = Program.StringToBool(param);
            }
            foreach (XmlElement param in docx.GetElementsByTagName("DataSections"))
            {
                proj.DataSections = Program.StringToBool(param);
            }

            foreach (XmlElement param in docx.GetElementsByTagName("BurnPart"))
            {
                proj.BurnPart = param.InnerText;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("BurnProgrammer"))
            {
                proj.BurnProgrammer = param.InnerText;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("BurnOptions"))
            {
                proj.BurnOptions = param.InnerText;
            }
            foreach (XmlElement param in docx.GetElementsByTagName("BurnBaud"))
            {
                try { proj.BurnBaud = int.Parse(param.InnerText); }
                catch { }
            }
            foreach (XmlElement param in docx.GetElementsByTagName("BurnAutoReset"))
            {
                proj.BurnAutoReset = Program.StringToBool(param);
            }
            foreach (XmlElement param in docx.GetElementsByTagName("BurnFuseBox"))
            {
                proj.BurnFuseBox = param.InnerText;
            }

            foreach (XmlElement container in docx.GetElementsByTagName("IncludeDirList"))
            {
                foreach (XmlElement i in container.GetElementsByTagName("DirPath"))
                {
                    if (proj.IncludeDirList.Contains(i.InnerText) == false)
                        proj.IncludeDirList.Add(i.InnerText);
                }
            }

            foreach (XmlElement container in docx.GetElementsByTagName("LibraryDirList"))
            {
                foreach (XmlElement i in container.GetElementsByTagName("DirPath"))
                {
                    if (proj.LibraryDirList.Contains(i.InnerText) == false)
                        proj.LibraryDirList.Add(i.InnerText);
                }
            }

            foreach (XmlElement container in docx.GetElementsByTagName("LinkObjList"))
            {
                foreach (XmlElement i in container.GetElementsByTagName("Obj"))
                {
                    if (proj.LinkObjList.Contains(i.InnerText) == false)
                        proj.LinkObjList.Add(i.InnerText);
                }
            }

            foreach (XmlElement container in docx.GetElementsByTagName("LinkLibList"))
            {
                foreach (XmlElement i in container.GetElementsByTagName("Lib"))
                {
                    if (proj.LinkLibList.Contains(i.InnerText) == false)
                        proj.LinkLibList.Add(i.InnerText);
                }
            }

            foreach (XmlElement container in docx.GetElementsByTagName("MemorySegList"))
            {
                foreach (XmlElement i in container.GetElementsByTagName("Segment"))
                {
                    try
                    {
                        XmlElement type = (XmlElement)i.GetElementsByTagName("Type")[0];
                        XmlElement name = (XmlElement)i.GetElementsByTagName("Name")[0];
                        XmlElement addr = (XmlElement)i.GetElementsByTagName("Addr")[0];
                        uint address;
                        if (addr.InnerText.ToLowerInvariant().StartsWith("0x"))
                        {
                            address = Convert.ToUInt32(addr.InnerText, 16);
                        }
                        else
                        {
                            address = Convert.ToUInt32("0x" + addr.InnerText, 16);
                        }

                        string nameStr = name.InnerText.Trim();
                        string typeStr = type.InnerText.Trim();

                        if (string.IsNullOrEmpty(nameStr) == false && string.IsNullOrEmpty(typeStr) == false)
                        {
                            MemorySegment seg = new MemorySegment(typeStr, nameStr, address);

                            if (proj.MemorySegList.ContainsKey(nameStr) == false)
                                proj.MemorySegList.Add(nameStr, seg);
                        }
                    }
                    catch { }
                }
            }

            foreach (XmlElement param in docx.GetElementsByTagName("ArduinoCoreOverride"))
            {
                proj.ArduinoCoreOverride = param.InnerText;
            }
        }
        public bool ImportAPS(string path)
        {
            bool success = true;

            try
            {
                path = Program.CleanFilePath(path);
                string pathDir = path.Substring(0, path.LastIndexOf(Path.DirectorySeparatorChar));

                dirPath = pathDir;

                XmlDocument doc = new XmlDocument();
                doc.Load(path);

                XmlElement docx = doc.DocumentElement;

                if (docx.GetElementsByTagName("AVRGCCPLUGIN").Count > 0)
                {
                    foreach (XmlElement param in docx.GetElementsByTagName("PART"))
                    {
                        Device = param.InnerText;
                        BurnPart = Device;
                    }

                    IncludeDirList.Clear();
                    foreach (XmlElement container in docx.GetElementsByTagName("INCDIRS"))
                    {
                        foreach (XmlElement i in container.GetElementsByTagName("INCLUDE"))
                        {
                            IncludeDirList.Add(Program.AbsPathFromRel(pathDir, i.InnerText));
                        }
                    }

                    LibraryDirList.Clear();
                    foreach (XmlElement container in docx.GetElementsByTagName("LIBDIRS"))
                    {
                        foreach (XmlElement i in container.GetElementsByTagName("LIBDIR"))
                        {
                            LibraryDirList.Add(Program.AbsPathFromRel(pathDir, i.InnerText));
                        }
                    }

                    LinkObjList.Clear();
                    foreach (XmlElement container in docx.GetElementsByTagName("LINKOBJECTS"))
                    {
                        foreach (XmlElement i in container.GetElementsByTagName("LINKOBJECT"))
                        {
                            LinkObjList.Add(Program.AbsPathFromRel(pathDir, i.InnerText));
                        }
                    }

                    LinkLibList.Clear();
                    foreach (XmlElement container in docx.GetElementsByTagName("LIBS"))
                    {
                        foreach (XmlElement i in container.GetElementsByTagName("LIB"))
                        {
                            LinkLibList.Add(i.InnerText);
                        }
                    }

                    MemorySegList.Clear();
                    foreach (XmlElement container in docx.GetElementsByTagName("SEGMENTS"))
                    {
                        foreach (XmlElement i in container.GetElementsByTagName("SEGMENT"))
                        {
                            if (i.HasChildNodes)
                            {
                                try
                                {
                                    XmlElement type = (XmlElement)i.GetElementsByTagName("SEGMENT")[0];
                                    XmlElement name = (XmlElement)i.GetElementsByTagName("NAME")[0];
                                    XmlElement addr = (XmlElement)i.GetElementsByTagName("ADDRESS")[0];
                                    uint address;
                                    if (addr.InnerText.ToLowerInvariant().StartsWith("0x"))
                                    {
                                        address = Convert.ToUInt32(addr.InnerText, 16);
                                    }
                                    else
                                    {
                                        address = Convert.ToUInt32("0x" + addr.InnerText, 16);
                                    }

                                    string nameStr = name.InnerText.Trim();
                                    string typeStr = type.InnerText.Trim();

                                    if (string.IsNullOrEmpty(nameStr) == false && string.IsNullOrEmpty(typeStr) == false)
                                    {
                                        MemorySegment seg = new MemorySegment(typeStr, nameStr, address);

                                        if (MemorySegList.ContainsKey(nameStr) == false)
                                            MemorySegList.Add(nameStr, seg);
                                    }
                                }
                                catch { }
                            }
                        }
                    }

                    if (docx.GetElementsByTagName("OPTIONSFORALL").Count > 0)
                    {
                        UnsignedChars = false;
                        UnsignedBitfields = false;
                        PackStructs = false;
                        ShortEnums = false;
                        OtherOptions = "";
                        XmlElement param = (XmlElement)docx.GetElementsByTagName("OPTIONSFORALL")[0];
                        string OPTIONSFORALL = param.InnerText.Trim();
                        string[] opts = OPTIONSFORALL.Split(new char[] { ' ', '\t', '\r', '\n', });
                        foreach (string o in opts)
                        {
                            if (string.IsNullOrEmpty(o) == false)
                            {
                                if (o == "-funsigned-char")
                                {
                                    UnsignedChars = true;
                                }
                                else if (o == "-funsigned-bitfields")
                                {
                                    UnsignedBitfields = true;
                                }
                                else if (o == "-fpack-struct")
                                {
                                    PackStructs = true;
                                }
                                else if (o == "-fshort-enums")
                                {
                                    ShortEnums = true;
                                }
                                else if (o == "-ffunction-sections")
                                {
                                    FunctionSections = true;
                                }
                                else if (o == "-fdata-sections")
                                {
                                    DataSections = true;
                                }
                                else if (o.StartsWith("-DF_CPU="))
                                {
                                    string oo = o.Substring("-DF_CPU=".Length).ToUpperInvariant();
                                    oo = oo.TrimEnd('L').TrimEnd('L');
                                    oo = oo.TrimEnd('I');
                                    oo = oo.TrimEnd('S');
                                    oo = oo.TrimEnd('C');
                                    oo = oo.TrimEnd('F');
                                    oo = oo.TrimEnd('D');
                                    oo = oo.TrimEnd('U');
                                    if (decimal.TryParse(oo, out clkFreq) == false)
                                    {
                                        ClockFreq = 8000000;
                                    }
                                }
                                else if (o.StartsWith("-O"))
                                {
                                    Optimization = o;
                                }
                                else
                                {
                                    OtherOptions += " " + o;
                                }
                            }
                        }
                    }

                    foreach (XmlElement param in docx.GetElementsByTagName("LINKEROPTIONS"))
                    {
                        UseInitStack = false;
                        string[] linkerOptList = param.InnerText.Split(new char[] { ' ', '\t', '\r', '\n', });
                        foreach (string o in linkerOptList)
                        {
                            if (o.StartsWith("-Wl,--defsym=__stack="))
                            {
                                UseInitStack = true;
                                if (uint.TryParse(o.Substring("-Wl,--defsym=__stack=".Length), out initStackAddr) == false)
                                    initStackAddr = uint.MaxValue;
                            }
                            else
                            {
                                LinkerOptions += " " + param.InnerText;
                            }
                        }
                    }

                    foreach (XmlElement param in docx.GetElementsByTagName("OUTPUTDIR"))
                    {
                        OutputDir = Program.CleanFilePath(param.InnerText);
                    }
                }

                foreach (XmlElement param in docx.GetElementsByTagName("SaveFolder"))
                {
                    dirPath = Program.CleanFilePath(param.InnerText);
                }

                fileList.Clear();
                if (docx.GetElementsByTagName("ProjectFiles").Count > 0)
                {
                    XmlElement xProjFilesContainer = (XmlElement)docx.GetElementsByTagName("ProjectFiles")[0];

                    foreach (XmlElement i in xProjFilesContainer.GetElementsByTagName("Name"))
                    {
                        ProjectFile file = new ProjectFile(i.InnerText, this);
                        fileList.Add(file.FileName.ToLowerInvariant(), file);
                    }
                }
                else if (docx.GetElementsByTagName("AVRGCCPLUGIN").Count > 0)
                {
                    XmlElement pluginContainer = (XmlElement)docx.GetElementsByTagName("AVRGCCPLUGIN")[0];

                    foreach (XmlElement filesContainer in pluginContainer.GetElementsByTagName("FILES"))
                    {
                        foreach (XmlElement i in filesContainer.ChildNodes)
                        {
                            ProjectFile file = new ProjectFile(Program.AbsPathFromRel(dirPath, i.InnerText), this);
                            fileList.Add(file.FileName.ToLowerInvariant(), file);
                        }
                    }
                }

                foreach (XmlElement pluginContainer in docx.GetElementsByTagName("AVRGCCPLUGIN"))
                {
                    foreach (XmlElement optionsContainer in pluginContainer.GetElementsByTagName("OPTIONS"))
                    {
                        foreach (XmlElement i in optionsContainer.GetElementsByTagName("OPTION"))
                        {
                            if (i.GetElementsByTagName("FILE").Count > 0 && i.GetElementsByTagName("OPTIONLIST").Count > 0)
                            {
                                XmlElement param = (XmlElement)i.GetElementsByTagName("FILE")[0];
                                string fileName = Program.CleanFilePath(param.InnerText);
                                param = (XmlElement)i.GetElementsByTagName("OPTIONLIST")[0];
                                string optionsForFile = param.InnerText;

                                ProjectFile file;
                                if (fileList.TryGetValue(fileName.ToLowerInvariant(), out file))
                                {
                                    file.Options = optionsForFile;
                                }
                            }
                        }
                    }
                }

                APSXmlElementList.Clear();
                string[] otherXEles = new string[] {"AVRSimulator", "DEBUG_TARGET", "Debugger", "IOView", "Events", "Trace", };
                foreach (string xEleName in otherXEles)
                {
                    if (docx.GetElementsByTagName(xEleName).Count > 0)
                    {
                        APSXmlElementList.Add((XmlElement)docx.GetElementsByTagName(xEleName)[0]);
                    }
                }

            }
            catch (XmlException ex)
            {
                MessageBox.Show("Error in APS File: " + ex.Message);
                success = false;
            }
            catch { success = false; }

            if (success)
            {
                isReady = true;
            }

            return success;
        }