Beispiel #1
0
        private static void WriteIniFileSection(string filePath, string section, IniSection hSection, System.Text.Encoding encoding)
        {
            section = section.Trim();

            // ファイル名・セクション名・キー名がない場合は終了する
            if (filePath == string.Empty || section == string.Empty || hSection == null)
            {
                return;
            }

            // 既存ファイルがあれば中身をすべて取得する
            StringCollection hStrings = GetFileContents(filePath, encoding);

            // 既存ファイルがない場合は終了する
            if (hStrings == null)
            {
                return;
            }

            // 最大行数と小文字化したセクションを取得する
            int iMaxCount = hStrings.Count - 1;
            //string nSecLower = section.ToLower();
            string nSecLower = section.Trim().ToLower();

            // 最大要素数まで 1 要素ずつ読み込む
            for (int i = 0; i <= iMaxCount; i++)
            {
                //hStrings[i] = hStrings[i].TrimStart();
                hStrings[i] = hStrings[i].Trim();

                // セクションの始まりかどうか判断する
                if (IsSectionBegin(hStrings[i], nSecLower))
                {
                    int iRow;

                    // セクションの終わりを発見した場合は挿入する
                    for (iRow = i + 1; iRow <= iMaxCount; iRow++)
                    {
                        //if (IsSectionEnd(hStrings[iRow].TrimStart()))
                        if (IsSectionEnd(hStrings[iRow].Trim()))
                        {
                            StringCollection hInserts = GetInsertAllPairOfSection(hStrings, hSection, i + 1, iRow - 1);
                            WriteInsertKeyValue(filePath, hInserts, hStrings, i, iRow, encoding);
                            return;
                        }
                    }

                    // EOF を発見した場合は挿入する
                    StringCollection hEofInserts = GetInsertAllPairOfSection(hStrings, hSection, i + 1, iMaxCount);
                    WriteInsertKeyValue(filePath, hEofInserts, hStrings, i, iRow, encoding);
                    return;
                }
            }

            // セクションがなかった場合はセクションも作成する
            WriteInsertSectionKeyValue(filePath, section, hSection, hStrings, encoding);
        }
Beispiel #2
0
        private static StringCollection GetInsertAllPairOfSection(StringCollection hSources, IniSection hSection, int iBegin, int iEnd)
        {
            int iBrank = 0;
            StringCollection hStrings = new StringCollection();

            for (int i = iBegin; i <= iEnd; i++)
            {
                hStrings.Add(hSources[i]);

                if (hSources[i] == string.Empty)
                {
                    iBrank++;
                }
                else
                {
                    iBrank = 0;
                }
            }

            for (int i = 0; i <= hSection.Count - 1; i++)
            {
                int iRow;

                for (iRow = 0; iRow <= iEnd - iBegin - iBrank; iRow++)
                {
                    string nUpdate = GetUpdateWriteLine(hStrings[iRow], hSection[i].Key.Trim(), hSection[i].Value.Trim());

                    if (nUpdate != null)
                    {
                        hStrings[iRow] = nUpdate;
                        break;
                    }
                }

                if (iRow > iEnd - iBegin - iBrank)
                {
                    iEnd++;

                    if (iEnd - iBegin - iBrank <= hStrings.Count - 1)
                    {
                        hStrings[iEnd - iBegin - iBrank] = hSection[i].Key.Trim() + " = " + hSection[i].Value.Trim();
                        hStrings.Add(string.Empty);
                    }
                    else
                    {
                        hStrings.Add(hSection[i].Key.Trim() + " = " + hSection[i].Value.Trim());
                    }
                }
            }

            return hStrings;
        }
Beispiel #3
0
 /// ---------------------------------------------------------------------------------------
 /// <summary>
 ///     指定したセクションに IniSection のキーと値を書き込みます。</summary>
 /// <param name="section">
 ///     書き込みに使用するセクション。</param>
 /// <param name="hSection">
 ///     セクションの構造を表す IniSection オブジェクト。</param>
 /// ---------------------------------------------------------------------------------------
 public void WriteSection(string section, IniSection hSection)
 {
     WriteIniFileSection(this.FilePath, section, hSection, this.Encoding);
 }
Beispiel #4
0
        private static IniSection ReadIniFileSection(string filePath, string section, System.Text.Encoding encoding)
        {
            section = section.Trim().ToLower();

            // ファイル名・セクション名がない場合は終了する
            if (filePath == string.Empty || section == string.Empty)
            {
                return null;
            }

            // 指定したファイルが存在しない場合は Null を返す
            if (!System.IO.File.Exists(filePath))
            {
                return null;
            }

            using (System.IO.StreamReader cReader = new System.IO.StreamReader(filePath, encoding))
            {
                try
                {
                    while (cReader.Peek() > -1)
                    {
                        //string target = cReader.ReadLine().TrimStart();
                        string target = cReader.ReadLine().Trim();

                        // セクションの始まりかどうか判断する
                        if (IsSectionBegin(target, section))
                        {
                            IniSection hSection = new IniSection();

                            while (cReader.Peek() > -1)
                            {
                                //target = cReader.ReadLine().TrimStart();
                                target = cReader.ReadLine().Trim();
                                
                                // セクションの終わりかどうか判断する
                                if (IsSectionEnd(target))
                                {
                                    return hSection;
                                }

                                // イコールがあるかどうか判断する
                                int iLength = target.IndexOf('=');

                                if (iLength >= 1)
                                {
                                    //string key = target.Substring(0, iLength - 1).TrimEnd();
                                    string key = target.Substring(0, iLength - 1).Trim();

                                    string value = TrimDoubleQuote(target.Substring(iLength + 1).Trim());
                                    hSection.Add(new IniItem(key, value));
                                }
                            }

                            return hSection;
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    throw;
                }
                finally
                {
                    if (cReader != null)
                    {
                        cReader.Close();
                    }
                }
            }

            return null;
        }
Beispiel #5
0
 /// --------------------------------------------------------------------------------
 /// <summary>
 ///     Jeanne.Ini.IniSectionCollection の末尾に、IniSection オブジェクトを追加します。</summary>
 /// <param name="value">
 ///     末尾に追加する System.Ini.IniSection。</param>
 /// <returns>
 ///     追加された位置の Jeanne.Ini.IniSectionCollection インデックス。</returns>
 /// --------------------------------------------------------------------------------
 public int Add(IniSection value)
 {
     return base.Add(value);
 }
Beispiel #6
0
 /// ---------------------------------------------------------------------------------------
 /// <summary>
 ///     指定したセクションに IniSection のキーと値を書き込みます。</summary>
 /// <param name="hSection">
 ///     セクションの構造を表す IniSection オブジェクト。</param>
 /// ---------------------------------------------------------------------------------------
 public void WriteSection(IniSection hSection)
 {
     WriteIniFileSection(this.FilePath, this.Section, hSection, this.Encoding);
 }
Beispiel #7
0
        private static StringCollection GetInsertAllPairOfSection(StringCollection hSources, IniSection hSection, int iBegin, int iEnd)
        {
            int iBrank = 0;
            StringCollection hStrings = new StringCollection();

            for (int i = iBegin; i <= iEnd; i++)
            {
                hStrings.Add(hSources[i]);

                if (hSources[i] == string.Empty)
                {
                    iBrank++;
                }
                else
                {
                    iBrank = 0;
                }
            }

            for (int i = 0; i <= hSection.Count - 1; i++)
            {
                int iRow;

                for (iRow = 0; iRow <= iEnd - iBegin - iBrank; iRow++)
                {
                    string nUpdate = GetUpdateWriteLine(hStrings[iRow], hSection[i].Key.Trim(), hSection[i].Value.Trim());

                    if (nUpdate != null)
                    {
                        hStrings[iRow] = nUpdate;
                        break;
                    }
                }

                if (iRow > iEnd - iBegin - iBrank)
                {
                    iEnd++;

                    if (iEnd - iBegin - iBrank <= hStrings.Count - 1)
                    {
                        hStrings[iEnd - iBegin - iBrank] = hSection[i].Key.Trim() + " = " + hSection[i].Value.Trim();
                        hStrings.Add(string.Empty);
                    }
                    else
                    {
                        hStrings.Add(hSection[i].Key.Trim() + " = " + hSection[i].Value.Trim());
                    }
                }
            }

            return(hStrings);
        }
Beispiel #8
0
        private static void WriteInsertSectionKeyValue(string filePath, string section, IniSection hSection, StringCollection hSources, System.Text.Encoding encoding)
        {
            if (hSources == null)
            {
                return;
            }

            using (System.IO.StreamWriter hWriter = new System.IO.StreamWriter(filePath, false, encoding))
            {
                try
                {
                    int iLast;

                    for (iLast = 0; iLast <= hSources.Count - 1; iLast++)
                    {
                        hWriter.WriteLine(hSources[iLast]);
                    }

                    if (iLast > 0)
                    {
                        if (hSources[iLast - 1] != string.Empty)
                        {
                            hWriter.WriteLine();
                        }
                    }

                    hWriter.WriteLine("[" + section + "]");

                    for (int i = 0; i <= hSection.Count - 1; i++)
                    {
                        hWriter.WriteLine(hSection[i].Key.Trim() + " = " + hSection[i].Value.Trim());
                    }
                }
                catch (System.Exception ex)
                {
                    throw;
                }
                finally
                {
                    if (hWriter != null)
                    {
                        hWriter.Close();
                    }
                }
            }
        }
Beispiel #9
0
        private static IniSection ReadIniFileSection(string filePath, string section, System.Text.Encoding encoding)
        {
            section = section.Trim().ToLower();

            // ファイル名・セクション名がない場合は終了する
            if (filePath == string.Empty || section == string.Empty)
            {
                return(null);
            }

            // 指定したファイルが存在しない場合は Null を返す
            if (!System.IO.File.Exists(filePath))
            {
                return(null);
            }

            using (System.IO.StreamReader cReader = new System.IO.StreamReader(filePath, encoding))
            {
                try
                {
                    while (cReader.Peek() > -1)
                    {
                        //string target = cReader.ReadLine().TrimStart();
                        string target = cReader.ReadLine().Trim();

                        // セクションの始まりかどうか判断する
                        if (IsSectionBegin(target, section))
                        {
                            IniSection hSection = new IniSection();

                            while (cReader.Peek() > -1)
                            {
                                //target = cReader.ReadLine().TrimStart();
                                target = cReader.ReadLine().Trim();

                                // セクションの終わりかどうか判断する
                                if (IsSectionEnd(target))
                                {
                                    return(hSection);
                                }

                                // イコールがあるかどうか判断する
                                int iLength = target.IndexOf('=');

                                if (iLength >= 1)
                                {
                                    //string key = target.Substring(0, iLength - 1).TrimEnd();
                                    string key = target.Substring(0, iLength - 1).Trim();

                                    string value = TrimDoubleQuote(target.Substring(iLength + 1).Trim());
                                    hSection.Add(new IniItem(key, value));
                                }
                            }

                            return(hSection);
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    throw;
                }
                finally
                {
                    if (cReader != null)
                    {
                        cReader.Close();
                    }
                }
            }

            return(null);
        }
Beispiel #10
0
 /// ---------------------------------------------------------------------------------------
 /// <summary>
 ///     指定したセクションに IniSection のキーと値を書き込みます。</summary>
 /// <param name="section">
 ///     書き込みに使用するセクション。</param>
 /// <param name="hSection">
 ///     セクションの構造を表す IniSection オブジェクト。</param>
 /// ---------------------------------------------------------------------------------------
 public void WriteSection(string section, IniSection hSection)
 {
     WriteIniFileSection(this.FilePath, section, hSection, this.Encoding);
 }
Beispiel #11
0
 /// ---------------------------------------------------------------------------------------
 /// <summary>
 ///     指定したセクションに IniSection のキーと値を書き込みます。</summary>
 /// <param name="hSection">
 ///     セクションの構造を表す IniSection オブジェクト。</param>
 /// ---------------------------------------------------------------------------------------
 public void WriteSection(IniSection hSection)
 {
     WriteIniFileSection(this.FilePath, this.Section, hSection, this.Encoding);
 }
Beispiel #12
0
 /// --------------------------------------------------------------------------------
 /// <summary>
 ///     Jeanne.Ini.IniSectionCollection の末尾に、IniSection オブジェクトを追加します。</summary>
 /// <param name="value">
 ///     末尾に追加する System.Ini.IniSection。</param>
 /// <returns>
 ///     追加された位置の Jeanne.Ini.IniSectionCollection インデックス。</returns>
 /// --------------------------------------------------------------------------------
 public int Add(IniSection value)
 {
     return(base.Add(value));
 }
Beispiel #13
0
        private static void WriteInsertSectionKeyValue(string filePath, string section, IniSection hSection, StringCollection hSources, System.Text.Encoding encoding)
        {
            if (hSources == null)
            {
                return;
            }

            using (System.IO.StreamWriter hWriter = new System.IO.StreamWriter(filePath, false, encoding))
            {
                try
                {
                    int iLast;

                    for (iLast = 0; iLast <= hSources.Count - 1; iLast++)
                    {
                        hWriter.WriteLine(hSources[iLast]);
                    }

                    if (iLast > 0)
                    {
                        if (hSources[iLast - 1] != string.Empty)
                        {
                            hWriter.WriteLine();
                        }
                    }

                    hWriter.WriteLine("[" + section + "]");

                    for (int i = 0; i <= hSection.Count - 1; i++)
                    {
                        hWriter.WriteLine(hSection[i].Key.Trim() + " = " + hSection[i].Value.Trim());
                    }
                }
                catch (System.Exception ex)
                {
                    throw;
                }
                finally
                {
                    if (hWriter != null)
                    {
                        hWriter.Close();
                    }
                }
            }
        }