示例#1
0
        /// <summary>
        /// Converts this FDSSection to a textual representation of itself.
        /// </summary>
        /// <param name="tabulation">How many tabs to start with. Generally do not set this.</param>
        /// <param name="newline">What string to use as a new line. Generally do not set this.</param>
        /// <returns>The string.</returns>
        public string SaveToString(int tabulation = 0, string newline = null)
        {
            if (newline == null)
            {
                newline = "\n";
            }
            StringBuilder sb = new StringBuilder();

            foreach (string key in Data.Keys)
            {
                FDSData dat = Data[key];
                foreach (string str in dat.PrecedingComments)
                {
                    sb.Append('\t', tabulation);
                    sb.Append("#").Append(str).Append(newline);
                }
                sb.Append('\t', tabulation);
                sb.Append(FDSUtility.EscapeKey(key));
                if (dat.Internal is FDSSection)
                {
                    sb.Append(":").Append(newline).Append(((FDSSection)dat.Internal).SaveToString(tabulation + 1, newline));
                }
                else if (dat.Internal is byte[])
                {
                    sb.Append("= ").Append(dat.Outputable()).Append(newline);
                }
                else if (dat.Internal is List <FDSData> datums)
                {
                    sb.Append(":").Append(newline);
                    foreach (FDSData cdat in datums)
                    {
                        foreach (string com in cdat.PrecedingComments)
                        {
                            sb.Append('\t', tabulation);
                            sb.Append("#").Append(com).Append(newline);
                        }
                        sb.Append('\t', tabulation);
                        sb.Append("- ").Append(FDSUtility.Escape(cdat.Outputable())).Append(newline);
                    }
                }
                else
                {
                    sb.Append(": ").Append(FDSUtility.Escape(dat.Outputable())).Append(newline);
                }
            }
            return(sb.ToString());
        }
示例#2
0
 /// <summary>
 /// Returns the output-able string representation of this data.
 /// </summary>
 /// <returns>The resultant data.</returns>
 public string Outputable()
 {
     if (Internal is List <FDSData> )
     {
         StringBuilder sb = new StringBuilder();
         foreach (FDSData dat in (List <FDSData>)Internal)
         {
             sb.Append(dat.Outputable()).Append('|');
         }
         return(sb.ToString());
     }
     if (Internal is byte[])
     {
         return(Convert.ToBase64String((byte[])Internal, Base64FormattingOptions.None));
     }
     if (Internal is bool)
     {
         return(((bool)Internal) ? "true" : "false");
     }
     return(FDSUtility.Escape(Internal.ToString()));
 }
示例#3
0
        // TODO: Clean code base! Current code contains a lot of poorly named variables and messy code.

        /// <summary>
        /// Constructs the FDS Section from textual data.
        /// </summary>
        /// <param name="contents">The contents of the data file.</param>
        public FDSSection(string contents)
        {
            StartingLine = 1;
            contents     = FDSUtility.CleanFileData(contents);
            Dictionary <int, FDSSection> spacedsections = new Dictionary <int, FDSSection>()
            {
                { 0, this }
            };
            List <string> ccomments   = new List <string>();
            List <string> seccomments = new List <string>();
            FDSSection    csection    = this;

            string[]       data       = contents.SplitFast('\n');
            int            pspaces    = 0;
            string         secwaiting = null;
            List <FDSData> clist      = null;

            for (int i = 0; i < data.Length; i++)
            {
                string line = data[i];
                int    spaces;
                for (spaces = 0; spaces < line.Length; spaces++)
                {
                    if (line[spaces] != ' ')
                    {
                        break;
                    }
                }
                if (spaces == line.Length)
                {
                    continue;
                }
                string datum = line.Substring(spaces).TrimEnd(' ');
                if (datum.StartsWith("#"))
                {
                    ccomments.Add(datum.Substring(1));
                    continue;
                }
                if (spaces < pspaces)
                {
                    if (spacedsections.TryGetValue(spaces, out FDSSection temp))
                    {
                        csection = temp;
                        foreach (int test in new List <int>(spacedsections.Keys))
                        {
                            if (test > spaces)
                            {
                                spacedsections.Remove(test);
                            }
                        }
                    }
                    else
                    {
                        Exception(i, line, "Spaced incorrectly. Spacing length is less than previous spacing length,"
                                  + "but does not match the spacing value of any known section, valid: "
                                  + string.Join(" / ", spacedsections.Keys) + ", found: " + spaces + ", was: " + pspaces);
                    }
                }
                if (datum[0] == '-')
                {
                    string clistline = datum.Substring(1).TrimStart(' ');
                    if (clist == null)
                    {
                        if (spaces >= pspaces && secwaiting != null)
                        {
                            clist = new List <FDSData>();
                            csection.SetRootData(FDSUtility.UnEscapeKey(secwaiting), new FDSData()
                            {
                                PrecedingComments = new List <string>(seccomments), Internal = clist
                            });
                            seccomments.Clear();
                            secwaiting = null;
                        }
                        else
                        {
                            Exception(i, line, "Line purpose unknown, attempted list entry when not building a list");
                        }
                    }
                    clist.Add(new FDSData()
                    {
                        PrecedingComments = new List <string>(ccomments), Internal = FDSUtility.InterpretType(FDSUtility.UnEscape(clistline))
                    });
                    ccomments.Clear();
                    continue;
                }
                clist = null;
                string startofline = "";
                string endofline   = "";
                char   type        = '\0';
                for (int spot = 0; spot < datum.Length; spot++)
                {
                    if (datum[spot] == ':' || datum[spot] == '=')
                    {
                        type        = datum[spot];
                        startofline = datum.Substring(0, spot);
                        endofline   = spot == datum.Length - 1 ? "": datum.Substring(spot + 1);
                        break;
                    }
                }
                endofline = endofline.TrimStart(' ');
                if (type == '\0')
                {
                    Exception(i, line, "Line purpose unknown");
                }
                if (startofline.Length == 0)
                {
                    Exception(i, line, "Empty key label!");
                }
                if (spaces > pspaces && secwaiting != null)
                {
                    FDSSection sect = new FDSSection();
                    csection.SetRootData(FDSUtility.UnEscapeKey(secwaiting), new FDSData()
                    {
                        PrecedingComments = new List <string>(seccomments), Internal = sect
                    });
                    seccomments.Clear();
                    csection = sect;
                    spacedsections[spaces] = sect;
                    secwaiting             = null;
                }
                if (type == '=')
                {
                    if (endofline.Length == 0)
                    {
                        csection.SetRootData(FDSUtility.UnEscapeKey(startofline), new FDSData()
                        {
                            PrecedingComments = new List <string>(ccomments), Internal = new byte[0]
                        });
                    }
                    else
                    {
                        csection.SetRootData(FDSUtility.UnEscapeKey(startofline), new FDSData()
                        {
                            PrecedingComments = new List <string>(ccomments), Internal = Convert.FromBase64String(endofline)
                        });
                    }
                    ccomments.Clear();
                }
                else if (type == ':')
                {
                    if (endofline.Length == 0)
                    {
                        secwaiting  = startofline;
                        seccomments = new List <string>(ccomments);
                        ccomments.Clear();
                    }
                    else
                    {
                        csection.SetRootData(FDSUtility.UnEscapeKey(startofline), new FDSData()
                        {
                            PrecedingComments = new List <string>(ccomments), Internal = FDSUtility.InterpretType(FDSUtility.UnEscape(endofline))
                        });
                        ccomments.Clear();
                    }
                }
                else
                {
                    Exception(i, line, "Internal issue: unrecognize 'type' value: " + type);
                }
                pspaces = spaces;
            }
        }