fix 2 compiler warnings in modelgen
[gnucap-felix.git] / lib / findbr.cc
blob3f15a6e6d0390af8c855c2721fd8720b145a7193
1 /*$Id: findbr.cc,v 1.2 2009-12-13 17:55:01 felix Exp $ -*- C++ -*-
2 * Copyright (C) 2001 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 * find a branch with matching label
23 * returns the branch pointer
25 //testing=script,sparse 2006.07.17
26 #include "l_lib.h"
27 #include "constant.h"
28 #include "e_cardlist.h"
29 #include "ap.h"
30 #include "e_card.h"
31 /*--------------------------------------------------------------------------*/
32 /* findbranch: find a matching label, by (ugh) linear search
33 * label to look for is in command line (cmd).
34 * start "here". Look only after "here".
35 * return pointer to next match if exists (and eat input)
36 * pointer to a non-existent branch if no match (don't eat input)
37 * caution: caller must check return value before using
39 CARD_LIST::fat_iterator findbranch(CS& cmd, CARD_LIST::fat_iterator here)
41 unsigned save = cmd.cursor();
43 char labelwanted[BUFLEN+1];
44 cmd.ctostr(labelwanted, BUFLEN, TOKENTERM);
46 if (!labelwanted[0]) { // nothing to look for
47 cmd.reset(save);
48 return here.end();
49 }else{
52 char* local_part; // part before last dot, the thing inside
53 char* last_part; // part after last dot, top level
55 char* dot = strrchr(labelwanted,'.');
56 if (dot) {itested(); // split the string into 2 parts at the last dot
57 *dot = '\0'; // before is the new "local_part", shortened
58 last_part = dot + 1; // after is the "last_part".
59 local_part = labelwanted;
60 }else{
61 last_part = labelwanted;
62 local_part = NULL;
66 for (;;) {
67 if (here.is_end()) {
68 // at the end of the list - done, fails.
69 cmd.reset(save);
70 return here;
71 }else if (wmatch((**here).short_label(), last_part)) {
72 // last_part matches
73 if (!local_part) {
74 // requested a simple name, found it .. done.
75 return here;
76 }else{untested();
77 // there are dots, so look inside subckt
78 untested();
79 if ((**here).subckt()) {untested();
80 untested();
81 // has a subckt, and its name matches, doing fine
82 CS want(CS::_STRING, local_part); // recursion
83 CARD_LIST::fat_iterator subbrh=findbranch(want,(**here).subckt());
84 if (!subbrh.is_end()) {untested();
85 // found it in a subckt
86 return subbrh;
87 }else{untested();
88 // didn't find anything in this subckt
89 // keep looking for another with the same name
90 // why?
91 ++here;
93 }else{untested();
94 // no subckt
95 // keep looking for something with this name that has a subckt
96 // why?
97 untested();
98 ++here;
101 }else{
102 // label doesn't match
103 // keep looking for one that does. (linear search)
104 ++here;
108 /*--------------------------------------------------------------------------*/
109 /*--------------------------------------------------------------------------*/
110 // vim:ts=8:sw=2:noet: