summaryrefslogtreecommitdiff
path: root/lib/findbr.cc
blob: 7d5844db5d060e1ad270cc0be3f12035df24d388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*$Id: findbr.cc,v 26.133 2009/11/26 04:58:04 al Exp $ -*- C++ -*-
 * Copyright (C) 2001 Albert Davis
 * Author: Albert Davis <[email protected]>
 *
 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *------------------------------------------------------------------
 * find a branch with matching label
 * returns the branch pointer
 */
//testing=script,sparse 2006.07.17
#include "l_lib.h"
#include "constant.h"
#include "e_cardlist.h"
#include "ap.h"
#include "e_card.h"
/*--------------------------------------------------------------------------*/
/* findbranch: find a matching label, by (ugh) linear search
 *	label to look for is in command line (cmd).
 *	start "here".  Look only after "here".
 * return pointer to next match if exists (and eat input)
 *	  pointer to a non-existent branch if no match (don't eat input)
 * caution: caller must check return value before using
 */
CARD_LIST::fat_iterator findbranch(CS& cmd, CARD_LIST::fat_iterator here)
{
  size_t save = cmd.cursor();

  char labelwanted[BUFLEN+1];
  cmd.ctostr(labelwanted, BUFLEN, TOKENTERM);
  
  if (!labelwanted[0]) {		// nothing to look for
    cmd.reset(save);
    return here.end();
  }else{
  }
  
  char* local_part;	    // part before last dot, the thing inside
  char* last_part;	    // part after last dot, top level
  {
    char* dot = strrchr(labelwanted,'.');
    if (dot) {itested();		    // split the string into 2 parts at the last dot
      *dot = '\0';	    // before is the new "local_part", shortened
      last_part = dot + 1;  // after is the "last_part".
      local_part = labelwanted;
    }else{
      last_part = labelwanted;
      local_part = NULL;
    }
  }

  for (;;) {
    if (here.is_end()) {
      // at the end of the list - done, fails.
      cmd.reset(save);
      return here;
    }else if (wmatch((**here).short_label(), last_part)) { 
      // last_part matches
      if (!local_part) {
	// requested a simple name, found it .. done.
	return here;
      }else{untested();
	// there are dots, so look inside subckt
	untested();
	if ((**here).subckt()) {untested();
	  untested();
	  // has a subckt, and its name matches, doing fine
	  CS want(CS::_STRING, local_part); // recursion
	  CARD_LIST::fat_iterator subbrh=findbranch(want,(**here).subckt());
	  if (!subbrh.is_end()) {untested();
	    // found it in a subckt
	    return subbrh;
	  }else{untested();
	    // didn't find anything in this subckt
	    // keep looking for another with the same name
	    // why?
	    ++here;
	  }
	}else{untested(); 
	  // no subckt
	  // keep looking for something with this name that has a subckt
	  // why?
	  untested();
	  ++here;
	}
      }
    }else{
      // label doesn't match
      // keep looking for one that does.  (linear search)
      ++here;
    }
  }
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// vim:ts=8:sw=2:noet: