--- nono/lib/config.h 2026/04/29 17:05:15 1.1.1.6 +++ nono/lib/config.h 2026/04/29 17:05:44 1.1.1.11 @@ -38,8 +38,11 @@ class ConfigItem public: enum from_t { FromInitial = 0, // 初期値 - FromConfig, // 設定ファイルで更新 + FromHome, // 共通設定ファイル(~/.nono.cfg)で更新 + FromConfig, // VM 設定ファイルで更新 FromOption, // コマンドライン引数で更新 + FromPerf, // --perf オプション(を展開したもの)で更新 + FromRunning, // 実行中に更新 }; public: @@ -72,6 +75,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 +99,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 +131,7 @@ class ConfigItem // from (と where) はこの変数の由来を示す。 // o FromOption なら値はコマンドラインオプションで指定されたことを示す。 // この場合 where に "-V" とかが入っている。 - // o FromConfig なら値は設定ファイルで指定されたことを示す。 + // o FromConfig, FromHome なら値は設定ファイルで指定されたことを示す。 // この場合 where に "ファイルパス:行番号" が入っている。 // o FromInitial なら値は初期値のままであることを示す。引数や // 設定ファイルで値を指定した場合は例え初期値と同じ値を指定したと @@ -131,7 +144,7 @@ class ConfigItem class ConfigFile { public: - ConfigFile(const std::string& filepath_); + ConfigFile(const std::string& filepath_, ConfigItem::from_t from_); ~ConfigFile(); // ファイルの読み込み。 @@ -141,6 +154,9 @@ class ConfigFile // パス std::string filepath {}; + // 設定元 + ConfigItem::from_t from {}; + // 読み込んだ行一覧 // 行(改行と前後の空白を除く) と行番号(1から始まる)の pair。 // コメント行や空行は取り除いてあるが、文法チェックはしていないので @@ -161,6 +177,9 @@ class Config // コマンドライン引数の内容で上書きする bool Update(const std::string& line, const std::string& optname); + // 実行中に動的に変更する + bool UpdateRunning(const std::string& line); + // 内容を表示する void Show(bool all) const; @@ -179,6 +198,9 @@ class Config bool Add(const std::string& key, const std::string& value, VMCap vmcap = VMCap::ALL); + // エイリアスを追加 + bool AddAlias(const std::string& alias, const std::string& key); + // 項目を削除 void Delete(const std::string& key); @@ -191,8 +213,11 @@ 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; // "key=val" 形式の行から key, val を取り出す。 static bool SplitKeyVal(std::string *keyp, std::string *valp, @@ -201,6 +226,9 @@ class Config // 設定項目の集合 std::vector list {}; + // エイリアス(別名)の集合 + std::vector> aliases {}; + // 設定はある時点までは追加更新可能だが、途中からは読み込み専用としたい。 // プログラムミスを避けるため。 bool fixed {};