for Push.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KssSmaPlaLib.Ini
|
||||
{
|
||||
public class IniDto
|
||||
{
|
||||
public string Section { get; set; } = string.Empty;
|
||||
public string Key { get; set; } = string.Empty;
|
||||
public string Value { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace KssSmaPlaLib.Ini
|
||||
{
|
||||
public static class IniHelper
|
||||
{
|
||||
public const string INI_PATH = @".\Settings.ini";
|
||||
|
||||
public static List<IniDto> IniList = new List<IniDto>();
|
||||
|
||||
// Win32 API宣言
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern int GetPrivateProfileString(
|
||||
string lpAppName,
|
||||
string lpKeyName,
|
||||
string lpDefault,
|
||||
StringBuilder lpReturnedString,
|
||||
int nSize,
|
||||
string lpFileName);
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern bool WritePrivateProfileString(
|
||||
string lpAppName,
|
||||
string lpKeyName,
|
||||
string lpString,
|
||||
string lpFileName);
|
||||
|
||||
/// <summary>
|
||||
/// INIファイルから値を読み込み(セクション+キー指定)
|
||||
/// </summary>
|
||||
public static string ReadValue(string iniPath, string section, string key, string defaultValue = "")
|
||||
{
|
||||
// もしINIリストにあったら、そこから返す
|
||||
var value = IniList
|
||||
.FirstOrDefault(m => m.Section == section && m.Key == key)
|
||||
?.Value;
|
||||
if (value != null) return (string)value;
|
||||
|
||||
if (string.IsNullOrEmpty(iniPath)) iniPath = INI_PATH;
|
||||
var sb = new StringBuilder(1024);
|
||||
GetPrivateProfileString(section, key, defaultValue, sb, sb.Capacity, iniPath);
|
||||
var str = sb.ToString();
|
||||
str = string.IsNullOrEmpty(str) ? defaultValue : str;
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// bool値の読み込み
|
||||
/// </summary>
|
||||
/// <param name="iniPath"></param>
|
||||
/// <param name="section"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ReadBoolValue(string iniPath, string section, string key, bool defaultValue = false)
|
||||
{
|
||||
var str = ReadValue(iniPath, section, key, defaultValue.ToString());
|
||||
var boolValue = bool.TryParse(str, out var b) ? b : defaultValue;
|
||||
return boolValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日付値の読み込み
|
||||
/// </summary>
|
||||
public static DateTime ReadDateValue(string iniPath, string section, string key, DateTime? defaultDate = null)
|
||||
{
|
||||
var defDate = defaultDate ?? DateTime.MinValue;
|
||||
|
||||
var str = ReadValue(iniPath, section, key, defDate.ToString("yyyy/MM/dd"));
|
||||
var dateValue = DateTime.TryParse(str, out var d) ? d : defDate;
|
||||
return dateValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// INIファイルに値を書き込み(セクション+キー+値指定)
|
||||
/// </summary>
|
||||
public static bool WriteValue(string iniPath, string section, string key, string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(iniPath)) iniPath = INI_PATH;
|
||||
return WritePrivateProfileString(section, key, value, iniPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// bool値の書き込み
|
||||
/// </summary>
|
||||
/// <param name="iniPath"></param>
|
||||
/// <param name="section"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static bool WriteBoolValue(string iniPath, string section, string key, bool value)
|
||||
{
|
||||
return WriteValue(iniPath, section, key, value.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日付値の書き込み
|
||||
/// </summary>
|
||||
/// <param name="iniPath"></param>
|
||||
/// <param name="section"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static bool WriteDateValue(string iniPath, string section, string key, DateTime value)
|
||||
{
|
||||
return WriteValue(iniPath, section, key, value.ToString("yyyy/MM/dd"));
|
||||
}
|
||||
|
||||
public static string OpenIni(string initialFolderPath = "")
|
||||
{
|
||||
using (var dlg = new OpenFileDialog())
|
||||
{
|
||||
dlg.Multiselect = false;
|
||||
dlg.Title = "Settings.ini ファイルを選択してください。";
|
||||
dlg.AddExtension = true;
|
||||
dlg.CheckFileExists = true;
|
||||
dlg.CheckPathExists = true;
|
||||
dlg.RestoreDirectory = true;
|
||||
dlg.Filter = "設定ファイル(*.ini)|*.ini";
|
||||
if (!string.IsNullOrEmpty(initialFolderPath))
|
||||
{
|
||||
// 最初に見せるフォルダ(デスクトップなど)
|
||||
dlg.InitialDirectory = initialFolderPath;
|
||||
}
|
||||
|
||||
// ダイアログを表示して「OK」が押されたら処理
|
||||
if (dlg.ShowDialog() == DialogResult.Cancel) return string.Empty;
|
||||
|
||||
// 選択されたファイルのパスを取得
|
||||
return dlg.FileName;
|
||||
}
|
||||
}
|
||||
|
||||
public static object ReadValue(object value, string name, object iNI_KEY_ITAKUSAKI_MEI, string v)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using KssSmaPlaLib.Commons;
|
||||
|
||||
namespace KssSmaPlaLib.Ini
|
||||
{
|
||||
/// <summary>
|
||||
/// INIリーダー
|
||||
/// </summary>
|
||||
public static class IniReaderUxer
|
||||
{
|
||||
/// <summary>
|
||||
/// 指定セクションのINI値読み込み
|
||||
/// </summary>
|
||||
/// <param name="path">INIパス</param>
|
||||
/// <param name="section">セクション</param>
|
||||
/// <returns>指定のディクショナリ</returns>
|
||||
public static Dictionary<string, string> ReadSection(string path, string section)
|
||||
{
|
||||
var result = new Dictionary<string, string>();
|
||||
var lines = File.ReadAllLines(path, System.Text.Encoding.GetEncoding("shift_jis"));
|
||||
bool inSection = false;
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var trimmed = line.Trim();
|
||||
|
||||
// コメント行は飛ばす
|
||||
if (trimmed.StartsWith(";")) continue;
|
||||
|
||||
if (trimmed.StartsWith("[") && trimmed.EndsWith("]"))
|
||||
inSection = trimmed == $"[{section}]";
|
||||
else if (inSection && trimmed.Contains("="))
|
||||
{
|
||||
var index = trimmed.IndexOf('=');
|
||||
if (index > 0)
|
||||
{
|
||||
var key = trimmed.Substring(0, index).Trim();
|
||||
var value = trimmed.Substring(index + 1).Trim();
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public static bool IsEmptyByIniDic(Dictionary<string,string> iniDic,string key)
|
||||
{
|
||||
return string.IsNullOrEmpty(DictionaryUxer.GetValueOrDefault(iniDic, key, string.Empty));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KssSmaPlaLib.Ini
|
||||
{
|
||||
public static class IniReferenceResolveUxer
|
||||
{
|
||||
public static Dictionary<string, string> ReadSectionWithReference(string path, string section, int maxDepth = 3)
|
||||
{
|
||||
var allLines = File.ReadAllLines(path, System.Text.Encoding.GetEncoding("shift_jis"));
|
||||
var allSections = ParseAllSections(allLines);
|
||||
var result = new Dictionary<string, string>();
|
||||
|
||||
if (!allSections.ContainsKey(section)) return result;
|
||||
|
||||
foreach (var kvp in allSections[section])
|
||||
{
|
||||
result[kvp.Key] = ResolveValue(allSections, kvp.Value, maxDepth);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Dictionary<string, Dictionary<string, string>> ParseAllSections(string[] lines)
|
||||
{
|
||||
var sections = new Dictionary<string, Dictionary<string, string>>();
|
||||
string currentSection = null;
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var trimmed = line.Trim();
|
||||
if (trimmed.StartsWith(";") || string.IsNullOrEmpty(trimmed)) continue;
|
||||
|
||||
if (trimmed.StartsWith("[") && trimmed.EndsWith("]"))
|
||||
{
|
||||
currentSection = trimmed.Substring(1, trimmed.Length - 2);
|
||||
sections[currentSection] = new Dictionary<string, string>();
|
||||
}
|
||||
else if (currentSection != null && trimmed.Contains("="))
|
||||
{
|
||||
var index = trimmed.IndexOf('=');
|
||||
var key = trimmed.Substring(0, index).Trim();
|
||||
var value = trimmed.Substring(index + 1).Trim();
|
||||
sections[currentSection][key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return sections;
|
||||
}
|
||||
|
||||
private static string ResolveValue(Dictionary<string, Dictionary<string, string>> sections, string value, int depth)
|
||||
{
|
||||
if (depth <= 0 || string.IsNullOrEmpty(value)) return value;
|
||||
|
||||
var match = System.Text.RegularExpressions.Regex.Match(value, @"^\{\[(.+?)\](.+?)\}$");
|
||||
if (match.Success)
|
||||
{
|
||||
var refSection = match.Groups[1].Value;
|
||||
var refKey = match.Groups[2].Value;
|
||||
|
||||
if (sections.ContainsKey(refSection) && sections[refSection].ContainsKey(refKey))
|
||||
{
|
||||
var nextValue = sections[refSection][refKey];
|
||||
return ResolveValue(sections, nextValue, depth - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user