using KssSmaPlaLib.IO.File;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Commons
{
public static class DictionaryUxer
{
// ///
// /// 指定されたキーに対応する値(string)を取得します。キーが存在しない場合は 指定されたデフォルト値(未指定時はnull) を返します。
// ///
// public static TValue GetValueOrDefault(
// Dictionary dict,
// TKey key,
// TValue defaultValue = default)
// {
// return dict.TryGetValue(key, out TValue value) ? value : defaultValue;
// }
public static bool CreateNameToIdDic(string pcsv, out Dictionary dic, out string resultMessage)
{
resultMessage = string.Empty;
dic = new Dictionary();
if (string.IsNullOrEmpty(pcsv)) return true;
var csvArray = pcsv.Split('|');
foreach (var csv in csvArray)
{
var items = csv.Split(',');
if (items.Length != 2)
{
resultMessage = $"要素数が{items.Length}個のCSVあり[CSV:{csv}]";
return false;
}
// IDの数値チェック
if (!int.TryParse(items[1], out int id))
{
resultMessage = $"IDが数値でないCSVあり[CSV:{csv}]";
return false;
}
// Nameのカンマ記号復元
items[0] = items[0].Replace("@@@COMMA@@@", ",");
dic[items[0]] = id;
}
return true;
}
}
}