// Copyright (c) CNES 2008 // // This software is part of CelestLab, a CNES toolbox for Scilab // // This software is governed by the CeCILL license under French law and // abiding by the rules of distribution of free software. You can use, // modify and/ or redistribute the software under the terms of the CeCILL // license as circulated by CEA, CNRS and INRIA at the following URL // 'http://www.cecill.info'. // Loads configuration file an returns a structure of // (key, value) pairs // => returns [] if all names and values are not correct // NB: generates no error. (returns error message in 2nd argument) // confst: structure of configuration data. // errmsg: [] unless there is an error. // fname: name of configuration file function [confst, errmsg] = CL__configLoad(fname) confst = []; errmsg = []; // function to hide possibly unexpected variables in config file // (config file assumed to contain lines such as: conf.xxx = value. Other lines: ignored) // returns [] if error occurred function [conf] = configLoad(fname) conf = struct(); err = exec(fname, 'errcatch', -1); if (err <> 0) conf = []; return; end endfunction // get the configuration description config_desc = CL__configGetDesc(); if (~isfile(fname)) errmsg = "Configuration file not found"; return; end // Intialize configuration structure conf = configLoad(fname); if (conf == []) errmsg = "Error loading configuration file"; return; end // field names in conf conf_names = matrix(fieldnames(conf),1,-1); // Check that all configuration parameters exist in conf and vice-versa if (setdiff(config_desc.names, conf_names) <> [] | .. setdiff(conf_names, config_desc.names) <> []) errmsg = "Invalid configuration file (inconsistent parameter names)"; return; end // Check the values for (name = conf_names) val = conf(name); if (typeof(val) <> "string" | size(val, "*") <> 1) errmsg = "Invalid type or size for parameter ''" + name + "'' in configuration file"; return; end k = find(name == config_desc.names); if (find(val == config_desc.accval(k)) == []) errmsg = "Invalid value for parameter ''" + name + "'' in configuration file"; return; end end confst = conf; endfunction