Пример #1
0
        /// <summary>
        /// Set a configuration to the pointer file
        /// </summary>
        /// <param name="configuration"></param>
        public void SetConfiguration(IConfiguration configuration)
        {
            var rawConfig = ReadFile().ToList();

            var internalModel = new InternalConfiguration(configuration);

            var categoryIndex = rawConfig.IndexOf(internalModel.ConfigurationNameStandard);

            if (categoryIndex != -1)
            {
                for (int i = categoryIndex + 1; i < rawConfig.Count; i++)
                {
                    if (FormatsStandars.IsValidCategoryName(rawConfig[i]))
                    {
                        // it is a category name, ignore it
                        continue;
                    }

                    string propertyString = rawConfig[i];
                    bool   isComment      = FormatsStandars.IsComment(propertyString);
                    bool   isEmpty        = String.IsNullOrWhiteSpace(propertyString);

                    if (isComment == false && isEmpty == false)
                    {
                        var property = MyConfigParser.ParseProperty(propertyString);

                        if (configuration.GetConfigurations().Any(x => x.Key == property.Key))
                        {
                            var newProperty = configuration.GetConfigurations().First(x => x.Key == property.Key);
                            // update the property
                            rawConfig[i] = "\t" + newProperty.Key + " = " + newProperty.Value;
                        }
                    }
                }
            }
            else
            {
                int lastIndexOfArray = rawConfig.Count == 0 ? 0 : rawConfig.Count - 1;

                if (lastIndexOfArray == 0)
                {
                    rawConfig.Add(internalModel.ConfigurationNameStandard);
                }
                else
                {
                    rawConfig[lastIndexOfArray] += Environment.NewLine + internalModel.ConfigurationNameStandard;
                }

                foreach (var item in internalModel.GetConfigurations())
                {
                    rawConfig[lastIndexOfArray] += Environment.NewLine + "\t" + item.Key + " = " + item.Value;
                }
            }

            File.WriteAllLines(this.Path, rawConfig);
        }
Пример #2
0
        internal IEnumerable <string> GetCategoriesNames()
        {
            var data = ReadFile();

            foreach (var item in data)
            {
                if (FormatsStandars.IsValidCategoryName(item.Trim()))
                {
                    yield return(item);
                }
            }
        }
Пример #3
0
        internal IConfiguration GetConfigurationByCategory(string categoryName)
        {
            if (!FormatsStandars.IsValidCategoryName(categoryName))
            {
                categoryName = categoryName.Replace("[", string.Empty).Replace("]", string.Empty);
                categoryName = String.Format("[{0}]", categoryName);
            }

            Configuration conf = new Configuration(categoryName);

            var data = ReadFile();

            for (int i = 0, total = data.Length; i < total; i++)
            {
                if (data[i] == categoryName)
                {
                    // reading the properties values
                    for (int y = i + 1; y < total; y++)
                    {
                        //|| String.IsNullOrEmpty(data[y]) ||
                        var trimed = data[y].Trim();
                        if (y >= data.Length || FormatsStandars.IsValidCategoryName(data[y]))
                        {
                            break;
                        }
                        else if (trimed.StartsWith(COMMENT) || String.IsNullOrEmpty(trimed))
                        {
                            continue;
                        }
                        else
                        {
                            var property = MyConfigParser.ParseProperty(data[y]);
                            conf.Add(property.Key, property.Value);
                        }
                    }
                }
            }

            return(conf);
        }