Example #1
0
        /// <summary>
        /// Parse ini data into data structure
        /// </summary>
        /// <param name="lines">array of text lines</param>
        /// <param name="path">path from where lines came</param>
        /// <returns></returns>
        public static IniData ReadFromStringArray(string[] lines, string path)
        {
            // Create a dictionary for the first section
            Dictionary <string, string> entryData = new Dictionary <string, string>();
            // Name for the current section
            string entryName = "";
            // Create object to hold output
            IniData output = new IniData();

            // Section headers have these chars removed
            char[] charsToTrim = { '[', ']' };

            // Parse all lines
            foreach (string l in lines)
            {
                // ignore comments
                if (l.Length > 0 && l.Trim()[0] == '#')
                {
                    continue;
                }

                // Start of new section
                if (l.Length > 0 && l.Trim()[0] == '[')
                {
                    // If not first section, add the last section of data
                    if (entryName != "")
                    {
                        if (!output.Add(entryName, entryData))
                        {
                            ValkyrieDebug.Log("Warning: duplicate section \"" + entryName + "\" in " + path + " will be ignored.");
                        }
                    }
                    // create new data for new section
                    entryData = new Dictionary <string, string>();
                    // Get name of new section
                    entryName = l.Trim().Trim(charsToTrim);
                    // Blank section names not allowed, but not fatal
                    if (entryName.Equals(""))
                    {
                        ValkyrieDebug.Log("Warning: empty section in " + path + " will be ignored.");
                    }
                }
                // If the line is not a comment (starts with ;)
                else if (l.Length > 0 && l.Trim()[0] != ';')
                {
                    int equalsLocation = l.IndexOf('=');
                    // Add data as entry with no value
                    if (equalsLocation == -1)
                    {
                        if (entryData.ContainsKey(l.Trim()))
                        {
                            ValkyrieDebug.Log("Warning: duplicate \"" + l.Trim() + "\" data in section \"" + entryName + "\" in " + path + " will be ignored.");
                        }
                        else
                        {
                            entryData.Add(l.Trim(), "");
                        }
                    }
                    // If there is an = add data as key and value
                    else
                    {
                        string key = l.Substring(0, equalsLocation).Trim();
                        if (entryData.ContainsKey(key))
                        {
                            ValkyrieDebug.Log("Warning: duplicate \"" + key + "\" data in section \"" + entryName + "\" in " + path + " will be ignored.");
                        }
                        else
                        {
                            string value = l.Substring(equalsLocation + 1).Trim().Trim('\"');
                            //string translatedValue = LocalizationRead.FFGLookup(value);
                            entryData.Add(key, value);
                        }
                    }
                    // This won't go anywhere if we don't have a section
                    if (entryName.Equals(""))
                    {
                        ValkyrieDebug.Log("Warning: data " + l + " without section in " + path + " will be ignored.");
                    }
                }
            }

            // Add the last section
            if (entryName != "")
            {
                if (!output.Add(entryName, entryData))
                {
                    ValkyrieDebug.Log("Warning: duplicate section \"" + entryName + "\" in " + path + " will be ignored.");
                }
            }

            return(output);
        }