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)
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
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"
30 /*--------------------------------------------------------------------------*/
31 void PARAM_LIST::parse(CS
& cmd
)
33 (cmd
>> "real |integer "); // ignore type
34 unsigned here
= cmd
.cursor();
36 if (!(cmd
.more() && (cmd
.is_alpha() || cmd
.match1('_')))) {
41 PARAMETER
<double> Value
;
42 cmd
>> Name
>> '=' >> Value
;
43 if (cmd
.stuck(&here
)) {untested();
47 if (OPT::case_insensitive
) {
48 notstd::to_lower(&Name
);
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
);
65 /*--------------------------------------------------------------------------*/
66 bool PARAM_LIST::is_printable(int i
)const
68 //BUG// ugly linear search
70 for (const_iterator ii
= _pl
.begin(); ii
!= _pl
.end(); ++ii
) {
72 return ii
->second
.has_hard_value();
78 /*--------------------------------------------------------------------------*/
79 std::string
PARAM_LIST::name(int i
)const
81 //BUG// ugly linear search
83 for (const_iterator ii
= _pl
.begin(); ii
!= _pl
.end(); ++ii
) {
91 /*--------------------------------------------------------------------------*/
92 std::string
PARAM_LIST::value(int i
)const
94 //BUG// ugly linear search
96 for (const_iterator ii
= _pl
.begin(); ii
!= _pl
.end(); ++ii
) {
98 return ii
->second
.string();
104 /*--------------------------------------------------------------------------*/
105 void PARAM_LIST::eval_copy(PARAM_LIST
& p
, const CARD_LIST
* scope
)
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
);
115 _pl
[i
->first
] = i
->second
.e_val(NOT_INPUT
, scope
);
121 /*--------------------------------------------------------------------------*/
122 const PARAMETER
<double>& PARAM_LIST::deep_lookup(std::string Name
)const
124 if (OPT::case_insensitive
) {
125 notstd::to_lower(&Name
);
128 PARAMETER
<double> & rv
= _pl
[Name
];
129 if (rv
.has_hard_value()) {
130 // found a value, return it
132 }else if (_try_again
) {
133 // didn't find one, look in enclosing scope
134 return _try_again
->deep_lookup(Name
);
136 // no enclosing scope to look in
137 // really didn't find it, give up
138 // return garbage value (NOT_INPUT)
142 /*--------------------------------------------------------------------------*/
143 void PARAM_LIST::set(std::string Name
, const std::string
& Value
)
145 if (OPT::case_insensitive
) {
146 notstd::to_lower(&Name
);
151 /*--------------------------------------------------------------------------*/
152 /*--------------------------------------------------------------------------*/
153 bool Get(CS
& cmd
, const std::string
& key
, PARAMETER
<bool>* val
)
155 if (cmd
.umatch(key
+ ' ')) {
156 if (cmd
.skip1b('=')) {
162 }else if (cmd
.umatch("no" + key
)) {
169 /*--------------------------------------------------------------------------*/
170 bool Get(CS
& cmd
, const std::string
& key
, PARAMETER
<int>* val
)
172 if (cmd
.umatch(key
+ " {=}")) {
173 *val
= int(cmd
.ctof());
179 /*--------------------------------------------------------------------------*/
180 /*--------------------------------------------------------------------------*/
181 // vim:ts=8:sw=2:noet: