/// <summary> /// ヱンコンのセクションを長方形の大きさを表す型'<see cref="System.Drawing.Rectangle"/>'に変換します。 /// </summary> /// <param name="section">変換前のセクションです。</param> /// <returns>変換後の長方形を表す新しいインスタンスです。</returns> /// <exception cref="System.ArgumentNullException"> /// <paramref name="section"/>が<see langword="null"/>の場合に発生します。 /// </exception> public static Rectangle ToRectangle(this YSection section) { section = section ?? throw new ArgumentNullException(nameof(section)); return(new Rectangle( unchecked ((int)((section.GetNode("x") as YNumber)?.SInt64Value)), unchecked ((int)((section.GetNode("y") as YNumber)?.SInt64Value)), unchecked ((int)((section.GetNode("w") as YNumber)?.SInt64Value)), unchecked ((int)((section.GetNode("h") as YNumber)?.SInt64Value)))); }
private static T GetKey <T>(YSection inix, YSection cfg, string keyName, Func <T> defaultValueFactory) where T : YNode { var result = inix.GetNode(keyName) as T ?? cfg.GetNode(keyName) as T; if (result == null) { result = defaultValueFactory(); result.Name = keyName; cfg.Add(result); } return(result); }
/// <summary> /// <paramref name="section"/>から文字列値を取得します。 /// </summary> /// <param name="section">取得元の<see cref="Yencon.YSection"/>です。</param> /// <param name="keyname">取得する文字列キーの名前です。</param> /// <returns> /// 指定された名前のノードが、 /// 文字列キーの場合はキーが保持している文字列を返し、 /// それ以外の場合は、キーの値を文字列に変換して返します。 /// </returns> public static string GetNodeAsString(this YSection section, string keyname) { var node = section.GetNode(keyname); if (node is YString strKey) { return(strKey.Text); } else { return(node?.GetValue()?.ToString() ?? string.Empty); } }
/// <summary> /// <paramref name="section"/>から子セクションを取得します。 /// </summary> /// <param name="section">取得元の<see cref="Yencon.YSection"/>です。</param> /// <param name="keyname">取得する子セクションの名前です。</param> /// <returns> /// 指定された名前のノードが、 /// 子セクションの場合はそのセクションを、 /// それ以外の場合は<see langword="null"/>を返します。 /// </returns> public static YSection GetNodeAsSection(this YSection section, string keyname) { var node = section.GetNode(keyname); if (node is YSection secKey) { return(secKey); } else { return(null); } }
/// <summary> /// <paramref name="section"/>から論理値を取得します。 /// </summary> /// <param name="section">取得元の<see cref="Yencon.YSection"/>です。</param> /// <param name="keyname">取得する論理値キーの名前です。</param> /// <returns> /// 指定された名前のノードが、 /// 論理値キーの場合はキーが保持している論理値を返し、 /// それ以外の場合は、<see langword="null"/>を返します。 /// </returns> public static bool?GetNodeAsBoolean(this YSection section, string keyname) { var node = section.GetNode(keyname); if (node is YBoolean flgKey) { return(flgKey.Flag); } else { return(null); } }
/// <summary> /// <paramref name="section"/>から数値を取得します。 /// </summary> /// <param name="section">取得元の<see cref="Yencon.YSection"/>です。</param> /// <param name="keyname">取得する数値キーの名前です。</param> /// <returns> /// 指定された名前のノードが、 /// 数値キーの場合はキーが保持している符号付き64ビット数値を返し、 /// それ以外の場合は、<c>0</c>を返します。 /// </returns> public static long GetNodeAsNumber(this YSection section, string keyname) { var node = section.GetNode(keyname); if (node is YNumber numKey) { return(numKey.SInt64Value); } else { return(0); } }
/// <summary> /// <paramref name="section"/>に論理値を設定します。 /// </summary> /// <param name="section">設定先の<see cref="Yencon.YSection"/>です。</param> /// <param name="keyname">設定する論理値キーの名前です。</param> /// <param name="value">設定する論理値です。</param> public static void SetNodeAsBoolean(this YSection section, string keyname, bool value) { var node = section.GetNode(keyname); if (node is YBoolean flgKey) { flgKey.Flag = value; } else { section.Add(new YBoolean() { Name = keyname, Flag = value }); } }
/// <summary> /// <paramref name="section"/>に数値を設定します。 /// </summary> /// <param name="section">設定先の<see cref="Yencon.YSection"/>です。</param> /// <param name="keyname">設定する数値キーの名前です。</param> /// <param name="value">設定する数値です。</param> public static void SetNodeAsNumber(this YSection section, string keyname, long value) { var node = section.GetNode(keyname); if (node is YNumber numKey) { numKey.SInt64Value = value; } else { section.Add(new YNumber() { Name = keyname, SInt64Value = value }); } }
/// <summary> /// <paramref name="section"/>に文字列値を設定します。 /// </summary> /// <param name="section">設定先の<see cref="Yencon.YSection"/>です。</param> /// <param name="keyname">設定する文字列キーの名前です。</param> /// <param name="value">設定する文字列です。</param> public static void SetNodeAsString(this YSection section, string keyname, string value) { var node = section.GetNode(keyname); if (node is YString strKey) { strKey.Text = value; } else { section.Add(new YString() { Name = keyname, Text = value }); } }
static int Main(string[] args) { _fname = Messages.Fname_Untitled; _ypath = "/"; _root = _current = new YSection(); Console.WriteLine(Messages.ToolTitle); Console.WriteLine(Messages.FileNotOpened); Console.WriteLine(); while (true) { Console.WriteLine(_fname); Console.Write(_ypath + "> "); try { string[] cmd = SplitCommand(Console.ReadLine()); if (cmd.Length > 0) { switch (cmd[0]) { // 終了 case "exit": case "quit": goto end; // 説明系 case "help": if (cmd.Length > 1) { Manual.Help(cmd[1]); } else { Manual.Help(); } break; case "about": case "ver": Manual.Version(Assembly.GetExecutingAssembly()); Manual.Version(Assembly.GetAssembly(typeof(YNode))); Manual.Version(Assembly.GetAssembly(typeof(StringUtils))); break; // ファイルアクセス系 case "load": string fnr = cmd.Length > 1 ? cmd[1] : _fname; var objr = FileAccessor.Load(fnr); if (objr != null) { _root = _current = objr; _fname = fnr; _ypath = "/"; } break; case "loadt": string fnt = cmd.Length > 1 ? cmd[1] : _fname; var objt = FileAccessor.LoadTxt(fnt); if (objt != null) { _root = _current = objt; _fname = fnt; _ypath = "/"; } break; case "loadb": string fnb = cmd.Length > 1 ? cmd[1] : _fname; var objb = FileAccessor.LoadBin(fnb); if (objb != null) { _root = _current = objb; _fname = fnb; _ypath = "/"; } break; case "save": FileAccessor.Save(_fname = cmd.Length > 1 ? cmd[1] : _fname, _root); break; case "savet": FileAccessor.SaveTxt(_fname = cmd.Length > 1 ? cmd[1] : _fname, _root); break; case "saveb": FileAccessor.SaveBin(_fname = cmd.Length > 1 ? cmd[1] : _fname, _root); break; case "binhdr": FileAccessor.ShowBinHeader(); break; // セクション case "into": if (cmd.Length > 1) { var s = _current.GetNode(cmd[1]) as YSection; if (s == null) { Console.WriteLine(Messages.SectionNotFound); } else { _current = s; _ypath += s.Name + "/"; } break; } else { goto default; } case "goroot": _current = _root; _ypath = "/"; break; // セクションとキーの制御 case "list": Operator.List(_current); break; case "set": if (cmd.Length > 3) { Operator.Set(_current, cmd[1], cmd[2], cmd[3]); break; } else { goto default; } case "adds": if (cmd.Length > 1) { Operator.Adds(_current, cmd[1]); break; } else { goto default; } // コマンドが見つからなかった場合 default: Console.WriteLine(string.Format(Messages.CommandNotFound, cmd[0])); break; } } } catch (Exception e) { var c = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.Message); Console.ForegroundColor = c; } Console.WriteLine(); } end: Console.WriteLine(); ConsoleUtils.Pause(); return(0); }