viminfo
[gnucap-felix.git] / lib / u_parameter.cc
blob9ab0d507056d95554b15bf12333f3dbc1e568420
1 /*$Id: u_parameter.cc,v 26.119 2009/09/09 13:27:53 al Exp $ -*- C++ -*-
2 * Copyright (C) 2005 Albert Davis
3 * Author: Albert Davis <aldavis@gnu.org>
5 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 *------------------------------------------------------------------
22 * A class for parameterized values
23 * Used for .param statements
24 * and passing arguments to models and subcircuits
26 //testing=script,sparse 2006.07.14
27 #include "l_stlextra.h"
28 #include "u_parameter.h"
29 #include "u_lang.h"
30 /*--------------------------------------------------------------------------*/
31 void PARAM_LIST::parse(CS& cmd)
33 (cmd >> "real |integer "); // ignore type
34 unsigned here = cmd.cursor();
35 for (;;) {
36 if (!(cmd.more() && (cmd.is_alpha() || cmd.match1('_')))) {
37 break;
38 }else{
40 std::string Name;
41 PARAMETER<double> Value;
42 cmd >> Name >> '=' >> Value;
43 if (cmd.stuck(&here)) {untested();
44 break;
45 }else{
47 if (OPT::case_insensitive) {
48 notstd::to_lower(&Name);
49 }else{
51 _pl[Name] = Value;
53 cmd.check(bDANGER, "syntax error");
55 /*--------------------------------------------------------------------------*/
56 void PARAM_LIST::print(OMSTREAM& o, LANGUAGE* lang)const
58 for (const_iterator i = _pl.begin(); i != _pl.end(); ++i) {
59 if (i->second.has_hard_value()) {
60 print_pair(o, lang, i->first, i->second);
61 }else{
65 /*--------------------------------------------------------------------------*/
66 bool PARAM_LIST::is_printable(int i)const
68 //BUG// ugly linear search
69 int i_try = 0;
70 for (const_iterator ii = _pl.begin(); ii != _pl.end(); ++ii) {
71 if (i_try++ == i) {
72 return ii->second.has_hard_value();
73 }else{
76 return false;
78 /*--------------------------------------------------------------------------*/
79 std::string PARAM_LIST::name(int i)const
81 //BUG// ugly linear search
82 int i_try = 0;
83 for (const_iterator ii = _pl.begin(); ii != _pl.end(); ++ii) {
84 if (i_try++ == i) {
85 return ii->first;
86 }else{
89 return "";
91 /*--------------------------------------------------------------------------*/
92 std::string PARAM_LIST::value(int i)const
94 //BUG// ugly linear search
95 int i_try = 0;
96 for (const_iterator ii = _pl.begin(); ii != _pl.end(); ++ii) {
97 if (i_try++ == i) {
98 return ii->second.string();
99 }else{
102 return "";
104 /*--------------------------------------------------------------------------*/
105 void PARAM_LIST::eval_copy(PARAM_LIST& p, const CARD_LIST* scope)
107 assert(!_try_again);
108 _try_again = p._try_again;
110 for (iterator i = p._pl.begin(); i != p._pl.end(); ++i) {
111 if (i->second.has_hard_value()) {
112 if (_pl[i->first].has_hard_value()) {untested();
113 _pl[i->first] = i->second.e_val(_pl[i->first], scope);
114 }else{
115 _pl[i->first] = i->second.e_val(NOT_INPUT, scope);
117 }else{
121 /*--------------------------------------------------------------------------*/
122 const PARAMETER<double>& PARAM_LIST::deep_lookup(std::string Name)const
124 if (OPT::case_insensitive) {
125 notstd::to_lower(&Name);
126 }else{
128 PARAMETER<double> & rv = _pl[Name];
129 if (rv.has_hard_value()) {
130 // found a value, return it
131 return rv;
132 }else if (_try_again) {
133 // didn't find one, look in enclosing scope
134 return _try_again->deep_lookup(Name);
135 }else{
136 // no enclosing scope to look in
137 // really didn't find it, give up
138 // return garbage value (NOT_INPUT)
139 return rv;
142 /*--------------------------------------------------------------------------*/
143 void PARAM_LIST::set(std::string Name, const std::string& Value)
145 if (OPT::case_insensitive) {
146 notstd::to_lower(&Name);
147 }else{
149 _pl[Name] = Value;
151 /*--------------------------------------------------------------------------*/
152 /*--------------------------------------------------------------------------*/
153 bool Get(CS& cmd, const std::string& key, PARAMETER<bool>* val)
155 if (cmd.umatch(key + ' ')) {
156 if (cmd.skip1b('=')) {
157 cmd >> *val;
158 }else{
159 *val = true;
161 return true;
162 }else if (cmd.umatch("no" + key)) {
163 *val = false;
164 return true;
165 }else{
166 return false;
169 /*--------------------------------------------------------------------------*/
170 bool Get(CS& cmd, const std::string& key, PARAMETER<int>* val)
172 if (cmd.umatch(key + " {=}")) {
173 *val = int(cmd.ctof());
174 return true;
175 }else{
176 return false;
179 /*--------------------------------------------------------------------------*/
180 /*--------------------------------------------------------------------------*/
181 // vim:ts=8:sw=2:noet: