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

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