Annotation of nono/lib/config.h, revision 1.1.1.12

1.1       root        1: //
                      2: // nono
1.1.1.2   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
1.1.1.4   root        7: //
                      8: // 設定
                      9: //
                     10: 
1.1       root       11: // 設定ファイルは、テキスト形式、行志向で順序を持たない key=value ペアの羅列、
                     12: // アプリケーションが書き戻しを行わないためユーザが任意にコメント行を入れる
                     13: // ことが出来る、タイプのもの。
                     14: //
1.1.1.6   root       15: // 設定(Config)は次の4段階によって確定する。
                     16: // c0. 初期値
1.1       root       17: //    項目はそれぞれ固有の初期値を持っている。ここでいう初期値は単なるゼロ
                     18: //    クリアではなく、その項目に最も自然な、あるいは極力問題なく動作するよう
                     19: //    選択した値のこと。
1.1.1.6   root       20: // c1. ~/.nono.cfg
                     21: // c2. VM ディレクトリの設定ファイル
1.1       root       22: //    設定ファイルで指定された値はそれを上書きする。
                     23: // c3. コマンドライン引数
                     24: //    コマンドライン引数で指定された値はそれを上書きする。
                     25: //
                     26: // おそらくたぶんすべての項目が例外なくこのように動作するようにしたい。
                     27: 
                     28: #pragma once
                     29: 
1.1.1.5   root       30: #include "mainapp.h"
1.1.1.12! root       31: #include <map>
1.1       root       32: #include <vector>
                     33: 
                     34: // 設定の1項目
                     35: class ConfigItem
                     36: {
                     37:        friend class Config;
                     38: 
                     39:  public:
                     40:        enum from_t {
                     41:                FromInitial = 0,        // 初期値
1.1.1.8   root       42:                FromHome,                       // 共通設定ファイル(~/.nono.cfg)で更新
                     43:                FromConfig,                     // VM 設定ファイルで更新
1.1       root       44:                FromOption,                     // コマンドライン引数で更新
1.1.1.9   root       45:                FromPerf,                       // --perf オプション(を展開したもの)で更新
1.1.1.11  root       46:                FromRunning,            // 実行中に更新
1.1       root       47:        };
                     48: 
                     49:  public:
1.1.1.2   root       50:        ConfigItem() { }
1.1.1.5   root       51:        ConfigItem(const std::string& key_, const std::string& val_, VMCap vmcap_,
1.1       root       52:                from_t from_, const std::string& where_) {
                     53:                key   = key_;
                     54:                value = val_;
1.1.1.5   root       55:                vmcap = vmcap_;
1.1       root       56:                from  = from_;
                     57:                where = where_;
                     58:        }
                     59: 
                     60:        // 値を文字列のまま取得する。
                     61:        const std::string& AsString() const { return value; }
                     62: 
                     63:        // 値を整数値として取得する。
                     64:        // この関数はよく似たことをたびたびやるのを避けるために提供してある
                     65:        // ただのユーティリティ関数。変換できない場合や空文字列の場合 0 に
                     66:        // なるので、それでもよければ使ってよい。
                     67:        int AsInt() const { return atoi(value.c_str()); }
                     68: 
1.1.1.4   root       69:        // 値を整数値として取得する。
                     70:        // 変換できたら true を返す。
                     71:        // 数値として評価出来ないときは false を返す。
                     72:        bool TryInt(int *val) const;
                     73: 
1.1.1.6   root       74:        // 値を double 値として取得する。
                     75:        // 変換できたら true を返す。
                     76:        // double 値として評価出来ないときは false を返す。
                     77:        bool TryDouble(double *val) const;
                     78: 
1.1.1.9   root       79:        // 値を固定小数点数として取得する。
                     80:        // 変換できたら true を返す。
                     81:        // 数値として評価出来ないときは false を返す。
                     82:        bool TryFixedDecimal(int *val, int digit) const;
                     83: 
1.1       root       84:        // デフォルト値を差し替える。
                     85:        // この変数が FromInitial なら値(初期値)を val に差し替える。この時
                     86:        // from は FromInitial のまま。
                     87:        // FromInitial でなければ (ユーザが値を上書きしていれば) 何もしない。
                     88:        // 本来は初期値設定 -> 設定ファイルやオプションで上書きの順に動作する
                     89:        // のが理想だが、設定ファイルを読まないと VM 種別が確定しないにわたま
                     90:        // 問題のため、VM 種別によって初期値が決定する場合などはデバイスの
                     91:        // コンストラクタでこれを呼び出すこと。例えば mpu-clock など。
                     92:        void SetDefault(const std::string& val);
                     93: 
1.1.1.5   root       94:        // vmcap を取得する
                     95:        VMCap GetVMCap() const { return vmcap; }
                     96: 
1.1       root       97:        // from を取得する
                     98:        from_t GetFrom() const { return from; }
                     99: 
1.1.1.6   root      100:        // where を取得する
                    101:        const std::string& GetWhere() const { return where; }
                    102: 
1.1.1.11  root      103:        // この項目で "Invalid argument" のエラーメッセージ文字列を返す。
                    104:        std::string ErrMsg() const;
                    105:        // この項目で指定のメッセージのエラーメッセージ文字列を返す。
                    106:        std::string ErrMsg(const char *fmt, ...) const __printflike(2, 3);
                    107:        std::string ErrMsg(const std::string& msg) const;
                    108: 
1.1       root      109:        // この項目で "invalid argument" を warnx() で出力する
                    110:        void Err() const;
                    111:        // この項目での指定のエラーメッセージを warnx() で出力する
1.1.1.2   root      112:        void Err(const char *fmt, ...) const __printflike(2, 3);
1.1.1.11  root      113:        void Err(const std::string& msg) const;
1.1       root      114: 
                    115:  private:
1.1.1.11  root      116:        static std::string ErrFrom(ConfigItem::from_t from,
                    117:                const std::string& where,
                    118:                const std::string& key, const std::string& value);
1.1.1.4   root      119: 
1.1       root      120:        // key が変数名、value が値。
                    121:        // value はここでは文字列でありそれ以上の構造は持たないが、
                    122:        // 整数値として解釈する或いはカンマ区切りにするなどは使う側の裁量。
                    123:        // value からは前後の空白は削除する。
                    124:        // またエスケープの類も今の所ない。
                    125:        std::string key {};
                    126:        std::string value {};
                    127: 
1.1.1.5   root      128:        // ケーパビリティ。
                    129:        // 機種確定後に、この機種に不要な変数は削除する。
                    130:        VMCap vmcap {};
                    131: 
1.1       root      132:        // from (と where) はこの変数の由来を示す。
1.1.1.6   root      133:        // o FromOption なら値はコマンドラインオプションで指定されたことを示す。
                    134:        //   この場合 where に "-V" とかが入っている。
1.1.1.8   root      135:        // o FromConfig, FromHome なら値は設定ファイルで指定されたことを示す。
1.1       root      136:        //   この場合 where に "ファイルパス:行番号" が入っている。
                    137:        // o FromInitial なら値は初期値のままであることを示す。引数や
                    138:        //   設定ファイルで値を指定した場合は例え初期値と同じ値を指定したと
                    139:        //   しても初期値扱いとはしない。
                    140:        from_t from {};
                    141:        std::string where {};
                    142: };
                    143: 
                    144: // 設定ファイル
                    145: class ConfigFile
                    146: {
                    147:  public:
1.1.1.8   root      148:        ConfigFile(const std::string& filepath_, ConfigItem::from_t from_);
1.1.1.6   root      149:        ~ConfigFile();
1.1       root      150: 
                    151:        // ファイルの読み込み。
                    152:        // 成功すれば true を返す。失敗すればエラーを表示して false を返す。
                    153:        bool Load();
                    154: 
                    155:        // パス
                    156:        std::string filepath {};
                    157: 
1.1.1.8   root      158:        // 設定元
                    159:        ConfigItem::from_t from {};
                    160: 
1.1       root      161:        // 読み込んだ行一覧
                    162:        // 行(改行と前後の空白を除く) と行番号(1から始まる)の pair。
                    163:        // コメント行や空行は取り除いてあるが、文法チェックはしていないので
                    164:        // 何か書いてある行の集合。
                    165:        std::vector<std::pair<std::string, int>> lines {};
                    166: };
                    167: 
                    168: // 設定
                    169: class Config
                    170: {
                    171:  public:
                    172:        Config();
1.1.1.6   root      173:        ~Config();
1.1       root      174: 
                    175:        // 設定ファイルの内容で上書きする
                    176:        bool Update(const ConfigFile& file);
                    177: 
                    178:        // コマンドライン引数の内容で上書きする
1.1.1.6   root      179:        bool Update(const std::string& line, const std::string& optname);
1.1       root      180: 
1.1.1.11  root      181:        // 実行中に動的に変更する
                    182:        bool UpdateRunning(const std::string& line);
                    183: 
1.1       root      184:        // 内容を表示する
1.1.1.2   root      185:        void Show(bool all) const;
1.1       root      186: 
1.1.1.2   root      187:        // key から ConfigItem を取得する。key は必ず存在するので参照を返す。
1.1       root      188:        // 見付からなければ assert する。それはプログラムミスなので。
1.1.1.2   root      189:        const ConfigItem& Find(const std::string& key) const;
1.1       root      190: 
1.1.1.2   root      191:        // デフォルト値を差し替える。
                    192:        // key が見付からなければ assert する。それはプログラムミスなので。
                    193:        void SetDefault(const std::string& key, int value);
                    194:        void SetDefault(const std::string& key, const std::string& value);
                    195: 
                    196:        // 項目を追加
1.1.1.5   root      197:        bool Add(const std::string& key, VMCap vmcap = VMCap::ALL);
                    198:        bool Add(const std::string& key, int value, VMCap vmcap = VMCap::ALL);
                    199:        bool Add(const std::string& key, const std::string& value,
                    200:                VMCap vmcap = VMCap::ALL);
1.1       root      201: 
1.1.1.7   root      202:        // エイリアスを追加
                    203:        bool AddAlias(const std::string& alias, const std::string& key);
                    204: 
1.1.1.12! root      205:        // 廃止項目を追加
        !           206:        void AddObsolete(const std::string& old, const std::string& alter);
        !           207: 
1.1.1.3   root      208:        // 項目を削除
                    209:        void Delete(const std::string& key);
                    210: 
1.1.1.2   root      211:        // 以降書き込み禁止にする (プログラムミスを避けるため)
1.1.1.6   root      212:        bool Fix();
1.1.1.2   root      213: 
                    214:  private:
1.1       root      215:        // key から ConfigItem を返す。見付からなければ NULL を返す。
                    216:        // (こっちは内部で変更するために使う)
                    217:        ConfigItem *FindItem(const std::string& key);
                    218: 
                    219:        // 更新1行分の共通処理
1.1.1.10  root      220:        bool UpdateLine(const std::string& line, ConfigItem::from_t from,
                    221:                const std::string& where);
1.1       root      222: 
1.1.1.7   root      223:        // キーの正規名を返す
                    224:        const std::string& GetCanonKey(const std::string& key) const;
                    225: 
1.1       root      226:        // "key=val" 形式の行から key, val を取り出す。
                    227:        static bool SplitKeyVal(std::string *keyp, std::string *valp,
                    228:                const std::string& line);
                    229: 
1.1.1.12! root      230:        // key が obsolete[] に含まれていれば true を返す。
        !           231:        bool IsObsolete(const std::string& key) const {
        !           232:                return (obsolete.find(key) != obsolete.end());
        !           233:        }
        !           234: 
1.1       root      235:        // 設定項目の集合
                    236:        std::vector<ConfigItem> list {};
1.1.1.2   root      237: 
1.1.1.7   root      238:        // エイリアス(別名)の集合
                    239:        std::vector<std::pair<std::string, std::string>> aliases {};
                    240: 
1.1.1.12! root      241:        // 廃止項目名の集合
        !           242:        std::map<std::string, std::string> obsolete {};
        !           243: 
1.1.1.2   root      244:        // 設定はある時点までは追加更新可能だが、途中からは読み込み専用としたい。
                    245:        // プログラムミスを避けるため。
                    246:        bool fixed {};
1.1       root      247: };
                    248: 
1.1.1.4   root      249: extern Config *gConfig;

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.