LCOV - code coverage report
Current view: top level - muse/framework/global - configreader.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 50.7 % 67 34
Test Date: 2026-11-05 04:07:02

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2024 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : #include "configreader.h"
      23              : 
      24              : #include <string>
      25              : 
      26              : #include "io/file.h"
      27              : #include "serialization/json.h"
      28              : #include "types/bytearray.h"
      29              : #include "types/ret.h"
      30              : #include "types/val.h"
      31              : 
      32              : #include "log.h"
      33              : 
      34              : using namespace muse;
      35              : 
      36              : static void fillMap(ValMap& v, const std::string& key, const JsonValue& jv);
      37            5 : static void fillList(ValList& l, const JsonValue& jv)
      38              : {
      39            5 :     if (jv.isNull()) {
      40            0 :         return;
      41            5 :     } else if (jv.isBool()) {
      42            0 :         l.push_back(Val(jv.toBool()));
      43            5 :     } else if (jv.isNumber()) {
      44            0 :         l.push_back(Val(jv.toDouble()));
      45            5 :     } else if (jv.isString()) {
      46            5 :         l.push_back(Val(jv.toStdString()));
      47            0 :     } else if (jv.isArray()) {
      48            0 :         ValList list;
      49            0 :         JsonArray arr = jv.toArray();
      50            0 :         for (size_t i = 0; i < arr.size(); ++i) {
      51            0 :             JsonValue av = arr.at(i);
      52            0 :             fillList(list, av);
      53            0 :         }
      54            0 :         l.push_back(Val(list));
      55            0 :     } else if (jv.isObject()) {
      56            0 :         ValMap map;
      57            0 :         JsonObject obj = jv.toObject();
      58            0 :         for (const std::string& k : obj.keys()) {
      59            0 :             fillMap(map, k, obj.value(k));
      60            0 :         }
      61            0 :         l.push_back(Val(map));
      62            0 :     }
      63              : }
      64              : 
      65           10 : static void fillMap(ValMap& v, const std::string& key, const JsonValue& jv)
      66              : {
      67           10 :     if (jv.isNull()) {
      68            0 :         return;
      69           10 :     } else if (jv.isBool()) {
      70            0 :         v[key] = Val(jv.toBool());
      71           10 :     } else if (jv.isNumber()) {
      72            0 :         v[key] = Val(jv.toDouble());
      73           10 :     } else if (jv.isString()) {
      74            5 :         v[key] = Val(jv.toStdString());
      75            5 :     } else if (jv.isArray()) {
      76            5 :         ValList list;
      77            5 :         JsonArray arr = jv.toArray();
      78           10 :         for (size_t i = 0; i < arr.size(); ++i) {
      79            5 :             JsonValue av = arr.at(i);
      80            5 :             fillList(list, av);
      81            5 :         }
      82            5 :         v[key] = Val(list);
      83            5 :     } else if (jv.isObject()) {
      84            0 :         ValMap map;
      85            0 :         JsonObject obj = jv.toObject();
      86            0 :         for (const std::string& k : obj.keys()) {
      87            0 :             fillMap(map, k, obj.value(k));
      88            0 :         }
      89            0 :         v[key] = Val(map);
      90            0 :     }
      91              : }
      92              : 
      93            5 : Config ConfigReader::read(const io::path_t& path)
      94              : {
      95            5 :     ByteArray data;
      96            5 :     Ret ret = io::File::readFile(path, data);
      97            5 :     if (!ret) {
      98            0 :         LOGE() << "failed read file: " << path << ", err: " << ret.toString();
      99            0 :         return Config();
     100              :     }
     101              : 
     102            5 :     std::string err;
     103            5 :     JsonObject obj = JsonDocument::fromJson(data, &err).rootObject();
     104            5 :     if (!err.empty()) {
     105            0 :         LOGE() << "failed parse file: " << path << ", err: " << err;
     106            0 :         return Config();
     107              :     }
     108              : 
     109            5 :     ValMap cv;
     110           15 :     for (const std::string& k : obj.keys()) {
     111           10 :         fillMap(cv, k, obj.value(k));
     112            5 :     }
     113              : 
     114            5 :     return Config(cv);
     115            5 : }
        

Generated by: LCOV version 2.0-1