1 /* Michael Widlok 2 Jun 1999 */
2 /* New commands for unloading circuits */
3
4 #include "ngspice/ngspice.h"
5 #include "ngspice/cpdefs.h"
6 #include "ngspice/ftedefs.h"
7 #include "ngspice/ftedev.h"
8 #include "ngspice/ftedebug.h"
9 #include "ngspice/dvec.h"
10
11 #include "circuits.h"
12 #include "mw_coms.h"
13 #include "variable.h"
14 #include "runcoms.h"
15 #include "spiceif.h"
16
17 /* Clears ckt and removes current circ. form database */
18
19 void
com_removecirc(wordlist * wl)20 com_removecirc(wordlist *wl)
21 {
22 struct variable *v, *next;
23 struct circ *ct;
24 struct circ *caux = NULL;
25 struct plot *p;
26 struct plot *paux;
27 int auxCir = 1, i, auxPlot;
28
29 char* namecircuit;
30
31 NG_IGNORE(wl);
32
33 if (!ft_curckt) {
34 fprintf(cp_err, "Error: there is no circuit loaded.\n");
35 return;
36 }
37
38 ct = ft_curckt;
39
40 if_cktfree(ct->ci_ckt, ct->ci_symtab);
41
42 for (v = ct->ci_vars; v; v = next) {
43 next = v->va_next;
44 tfree(v);
45 }
46
47 /* PN FTESTATS*/
48 tfree(ct->FTEstats);
49
50 ct->ci_vars = NULL;
51 caux = ft_circuits;
52 namecircuit = copy(ft_curckt->ci_name);
53
54 /* The circuit being removed is the first loaded and you have more circuits */
55 if (ft_curckt == ft_circuits && ft_circuits->ci_next)
56 ft_circuits = ft_circuits->ci_next;
57
58 /* The circuit being removed id the first loaded and there are no more circuits */
59 else if (ft_circuits->ci_next == NULL)
60 ft_circuits = NULL;
61
62 else {
63
64 /* Run over the circuit list to find how many of them are
65 * in front of the one to be removed
66 */
67 for (; ft_curckt != caux && caux; caux = caux->ci_next)
68 auxCir++;
69
70 caux = ft_circuits;
71
72 /* Remove the circuit and move pointer to the next one */
73 for (i = 1; i < auxCir-1; i++)
74 caux = caux->ci_next;
75
76 caux->ci_next = caux->ci_next->ci_next;
77 /* ft_curckt = ft_circuits; */
78
79 }
80
81
82 /* If the plot is the first one and there are no other plots */
83 if (!plot_list->pl_next && strcmp(plot_list->pl_title, namecircuit) == 0)
84 plot_list = NULL;
85
86 else if (plot_list && plot_list->pl_next) {
87 p = plot_list;
88 while (p) {
89 auxPlot = 1;
90 /* If the plot is in the first position */
91 if (plot_list->pl_next && strcmp(plot_list->pl_title, namecircuit) == 0)
92 plot_list = plot_list->pl_next;
93 /* otherwise we run over the list of plots */
94 else {
95 for (; strcmp(p->pl_title, namecircuit) != 0 && p->pl_next; p = p->pl_next)
96 auxPlot++;
97 if (strcmp(p->pl_title, namecircuit) == 0) {
98 paux = plot_list;
99 for (i = 1; i < auxPlot-1; i++)
100 paux = paux->pl_next;
101 paux->pl_next = paux->pl_next->pl_next;
102 }
103 }
104 p = p->pl_next;
105 }
106 }
107
108 /* if (ft_curckt) {
109 ft_curckt->ci_devices = cp_kwswitch(CT_DEVNAMES, ft_circuits->ci_devices);
110 ft_curckt->ci_nodes = cp_kwswitch(CT_NODENAMES, ft_circuits->ci_nodes);
111 } */
112
113 if (ft_circuits && caux->ci_next) {
114 struct wordlist *wlist;
115 wlist = wl_cons(tprintf("%d", auxCir), NULL);
116 com_scirc(wlist);
117 wl_free(wlist);
118 }
119 else if (ft_circuits) {
120 struct wordlist *wlist;
121 wlist = wl_cons(tprintf("%d", auxCir - 1), NULL);
122 com_scirc(wlist);
123 wl_free(wlist);
124 }
125 else
126 ft_curckt = NULL;
127 }
128