--- nono/lib/config.h 2026/04/29 17:05:18 1.1.1.7 +++ nono/lib/config.h 2026/04/29 17:05:52 1.1.1.12 @@ -28,6 +28,7 @@ #pragma once #include "mainapp.h" +#include #include // 設定の1項目 @@ -38,8 +39,11 @@ class ConfigItem public: enum from_t { FromInitial = 0, // 初期値 - FromConfig, // 設定ファイルで更新 + FromHome, // 共通設定ファイル(~/.nono.cfg)で更新 + FromConfig, // VM 設定ファイルで更新 FromOption, // コマンドライン引数で更新 + FromPerf, // --perf オプション(を展開したもの)で更新 + FromRunning, // 実行中に更新 }; public: @@ -72,6 +76,11 @@ class ConfigItem // double 値として評価出来ないときは false を返す。 bool TryDouble(double *val) const; + // 値を固定小数点数として取得する。 + // 変換できたら true を返す。 + // 数値として評価出来ないときは false を返す。 + bool TryFixedDecimal(int *val, int digit) const; + // デフォルト値を差し替える。 // この変数が FromInitial なら値(初期値)を val に差し替える。この時 // from は FromInitial のまま。 @@ -91,17 +100,22 @@ class ConfigItem // where を取得する const std::string& GetWhere() const { return where; } + // この項目で "Invalid argument" のエラーメッセージ文字列を返す。 + std::string ErrMsg() const; + // この項目で指定のメッセージのエラーメッセージ文字列を返す。 + std::string ErrMsg(const char *fmt, ...) const __printflike(2, 3); + std::string ErrMsg(const std::string& msg) const; + // この項目で "invalid argument" を warnx() で出力する void Err() const; - // この項目での指定のエラーメッセージを warnx() で出力する - void Err(const std::string& msg) const; void Err(const char *fmt, ...) const __printflike(2, 3); + void Err(const std::string& msg) const; private: - static void PrintErr(ConfigItem::from_t from, const std::string& where, - const std::string& key, const std::string& value, - const std::string& msg); + static std::string ErrFrom(ConfigItem::from_t from, + const std::string& where, + const std::string& key, const std::string& value); // key が変数名、value が値。 // value はここでは文字列でありそれ以上の構造は持たないが、 @@ -118,7 +132,7 @@ class ConfigItem // from (と where) はこの変数の由来を示す。 // o FromOption なら値はコマンドラインオプションで指定されたことを示す。 // この場合 where に "-V" とかが入っている。 - // o FromConfig なら値は設定ファイルで指定されたことを示す。 + // o FromConfig, FromHome なら値は設定ファイルで指定されたことを示す。 // この場合 where に "ファイルパス:行番号" が入っている。 // o FromInitial なら値は初期値のままであることを示す。引数や // 設定ファイルで値を指定した場合は例え初期値と同じ値を指定したと @@ -131,7 +145,7 @@ class ConfigItem class ConfigFile { public: - ConfigFile(const std::string& filepath_); + ConfigFile(const std::string& filepath_, ConfigItem::from_t from_); ~ConfigFile(); // ファイルの読み込み。 @@ -141,6 +155,9 @@ class ConfigFile // パス std::string filepath {}; + // 設定元 + ConfigItem::from_t from {}; + // 読み込んだ行一覧 // 行(改行と前後の空白を除く) と行番号(1から始まる)の pair。 // コメント行や空行は取り除いてあるが、文法チェックはしていないので @@ -161,6 +178,9 @@ class Config // コマンドライン引数の内容で上書きする bool Update(const std::string& line, const std::string& optname); + // 実行中に動的に変更する + bool UpdateRunning(const std::string& line); + // 内容を表示する void Show(bool all) const; @@ -182,6 +202,9 @@ class Config // エイリアスを追加 bool AddAlias(const std::string& alias, const std::string& key); + // 廃止項目を追加 + void AddObsolete(const std::string& old, const std::string& alter); + // 項目を削除 void Delete(const std::string& key); @@ -194,8 +217,8 @@ class Config ConfigItem *FindItem(const std::string& key); // 更新1行分の共通処理 - bool UpdateLine(const std::string& line, ConfigItem::from_t, - const std::string& from); + bool UpdateLine(const std::string& line, ConfigItem::from_t from, + const std::string& where); // キーの正規名を返す const std::string& GetCanonKey(const std::string& key) const; @@ -204,12 +227,20 @@ class Config static bool SplitKeyVal(std::string *keyp, std::string *valp, const std::string& line); + // key が obsolete[] に含まれていれば true を返す。 + bool IsObsolete(const std::string& key) const { + return (obsolete.find(key) != obsolete.end()); + } + // 設定項目の集合 std::vector list {}; // エイリアス(別名)の集合 std::vector> aliases {}; + // 廃止項目名の集合 + std::map obsolete {}; + // 設定はある時点までは追加更新可能だが、途中からは読み込み専用としたい。 // プログラムミスを避けるため。 bool fixed {};