/[ascend]/trunk/base/generic/compiler/qlfdid.c
ViewVC logotype

Contents of /trunk/base/generic/compiler/qlfdid.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 709 - (show annotations) (download) (as text)
Wed Jun 28 16:28:57 2006 UTC (13 years, 9 months ago) by johnpye
File MIME type: text/x-csrc
File size: 13124 byte(s)
Monster commit!
Lots of recommenting and reorganising of external relations-related stuff.
Replaced a lot of ascmalloc and asccalloc calls with the new ASC_NEW* macros.
Fixed (?) the problem Art is having with icons in PyGTK.
Turned on -Wall in SConstruct and fixed up a stack of warnings.
Removed the redundant exit(2) from after Asc_Panic calls and added __attribute__((noreturn)).
Set doxygen to create callgraphs to level 2, updated doxyfile to version 1.4.7.
Fixed up building of extfntest.c.
1 /*
2 * base/generic/compiler/qlfdid.c created from tcltk/generic/interface/Qlfdid.c
3 * Created: 1/94
4 * Version: $Revision: 1.22 $
5 * Version control file: $RCSfile: Qlfdid.c,v $
6 * Date last modified: $Date: 2003/08/23 18:43:07 $
7 * Last modified by: $Author: ballan $
8 *
9 * This file is part of ASCEND.
10 *
11 * Copyright 1997, Carnegie Mellon University
12 *
13 * ASCEND is free software; you can redistribute
14 * it and/or modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of the
16 * License, or (at your option) any later version.
17 *
18 * ASCEND is distributed in hope that it will be
19 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with the program; if not, write to the Free Software Foundation,
25 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
26 * COPYING. COPYING is found in ../compiler.
27 */
28
29 #include "qlfdid.h"
30
31 #include <utilities/ascConfig.h>
32 #include <utilities/ascPanic.h>
33 #include <utilities/ascMalloc.h>
34 #include <general/list.h>
35 #include <general/dstring.h>
36 #include "compiler.h"
37 #include "instance_enum.h"
38 #include "symtab.h"
39 #include "simlist.h"
40 #include "instance_io.h"
41 #include "instance_name.h"
42 #include "instquery.h"
43 #include "parentchild.h"
44 #include "fractions.h"
45 #include "dimen.h"
46 #include "expr_types.h"
47 #include "stattypes.h"
48 #include "instantiate.h"
49 #include <solver/slv_types.h>
50
51 #ifndef lint
52 static CONST char QlfdidID[] = "$Id: Qlfdid.c,v 1.22 2003/08/23 18:43:07 ballan Exp $";
53 #endif
54
55
56 #ifndef MAXIMUM_STRING_LENGTH
57 #define MAXIMUM_STRING_LENGTH 1024
58 #endif
59 #define QLFDIDMALLOC ASC_NEW(struct SearchEntry);
60
61
62 /* used for searching */
63 struct Instance *g_search_inst = NULL;
64 struct Instance *g_relative_inst = NULL;
65
66
67
68 char *Asc_MakeInitString(int len)
69 {
70 char *result;
71 int defaultlen = 40;
72 if (len<=0) {
73 FPRINTF(stderr,
74 "Setting length to %d due to invalid length given\n",defaultlen);
75 len = defaultlen;
76 }
77 result = ASC_NEW_ARRAY(char,(len + 1)*sizeof(char));
78 assert(result!=NULL);
79 sprintf(result,"%s","\0");
80 return result;
81 }
82
83 void Asc_ReInitString(char *str)
84 {
85 if ((str)&&(strlen(str))) {
86 strcpy(str,"");
87 }
88 }
89
90 /*
91 * Create a search entry node with a simple name
92 * such as : a.
93 */
94 struct SearchEntry *Asc_SearchEntryCreate(char *name,struct Instance *i)
95 {
96 struct SearchEntry *result;
97 result = QLFDIDMALLOC;
98 assert(result!=NULL);
99 result->name = ASC_NEW_ARRAY(char,strlen(name)+1);
100 strcpy(result->name,name);
101 result->i = i;
102 return result;
103 }
104
105 /*
106 * Create a search entry node with a name that
107 * is formatted like a integer array. such as:
108 * [14].
109 */
110 static
111 struct SearchEntry *SearchEntryCreateIntArray(char *name,struct Instance *i)
112 {
113 struct SearchEntry *result;
114 result = QLFDIDMALLOC;
115 assert(result!=NULL);
116 result->name = ASC_NEW_ARRAY(char,strlen(name)+8);
117 sprintf(result->name,"[%s]",name);
118 result->i = i;
119 return result;
120 }
121
122 /*
123 * Create a search entry node with a name that
124 * is formatted like a string array. such as:
125 * ['benzene.flow'].
126 */
127 static
128 struct SearchEntry *SearchEntryCreateStrArray(char *name,struct Instance *i)
129 {
130 struct SearchEntry *result;
131 result = QLFDIDMALLOC;
132 assert(result!=NULL);
133 result->name = ASC_NEW_ARRAY(char,strlen(name)+8);
134 sprintf(result->name,"[\'%s\']",name);
135 result->i = i;
136 return result;
137 }
138
139 struct Instance *Asc_SearchEntryInstance(struct SearchEntry *se)
140 {
141 assert(se!=NULL);
142 return (se->i);
143 }
144
145 char *Asc_SearchEntryName(struct SearchEntry *se)
146 {
147 assert(se!=NULL);
148 return (se->name);
149 }
150
151 void Asc_SearchEntryDestroy(struct SearchEntry *se)
152 {
153 if (!se) {
154 return;
155 }
156 ascfree(se->name);
157 se->name = NULL;
158 se->i = NULL;
159 ascfree(se);
160 }
161
162 void Asc_SearchListDestroy(struct gl_list_t *search_list)
163 {
164 struct SearchEntry *se;
165 unsigned long len,c;
166 if (!search_list) {
167 return;
168 }
169 len = gl_length(search_list);
170 for(c=1;c<=len;c++) {
171 se = (struct SearchEntry *)gl_fetch(search_list,c);
172 Asc_SearchEntryDestroy(se);
173 }
174 gl_destroy(search_list);
175 }
176
177 static
178 int CheckChildExist(struct InstanceName name)
179 {
180 unsigned long ndx,nch;
181 symchar *tablename; /* hacky, but centralized slop avoidance */
182 /* remember that a struct passed by value can be overwritten safely. */
183 nch = NumberChildren(g_search_inst);
184 if (!nch) {
185 g_search_inst = NULL;
186 return 0;
187 }
188 switch (InstanceNameType(name)) {
189 case IntArrayIndex:
190 break;
191 case StrName:
192 tablename = InstanceNameStr(name);
193 SetInstanceNameStrPtr(name,tablename);
194 break;
195 case StrArrayIndex:
196 tablename = InstanceStrIndex(name);
197 SetInstanceNameStrIndex(name,tablename);
198 break;
199 default:
200 Asc_Panic(2,"%s: CheckChildExist called with bad arguments.",__FILE__);
201 break;
202 }
203 ndx = ChildSearch(g_search_inst,&name); /* symchar safe */
204 if (ndx) {
205 g_search_inst = InstanceChild(g_search_inst,ndx);
206 return ndx;
207 } else {
208 g_search_inst = NULL;
209 return 0;
210 }
211 }
212
213 static void HandleLastPart(char *temp)
214 {
215 struct InstanceName name;
216 if (g_search_inst) {
217 SetInstanceNameType(name,StrName);
218 SetInstanceNameStrPtr(name,AddSymbol(temp));
219 CheckChildExist(name); /* sets g_search_inst regardless */
220 return;
221 } else {
222 g_search_inst = Asc_FindSimulationRoot(AddSymbol(temp));
223 }
224 }
225
226 struct gl_list_t *Asc_BrowQlfdidSearch(char *str, char *temp)
227 {
228 register char *ptr, *org;
229 struct InstanceName name;
230 struct gl_list_t *search_list = NULL;
231 struct SearchEntry *se;
232 int ndx = 0;
233 int open_bracket = 0;
234 int open_quote = 0;
235
236 g_search_inst = NULL; /* always start searches from the top */
237 if (!str) {
238 return NULL;
239 }
240 search_list = gl_create(8L);
241 ptr = temp;
242 org = str;
243 while(*str != '\0') {
244 switch(*str) {
245 case '.':
246 if (*(str-1) != ']') {
247 if (open_quote) { /* to deal b['funny.name'] */
248 *(ptr++) = *(str++); /* ---^--- */
249 break;
250 }
251 *ptr = '\0';
252 if (g_search_inst) {
253 SetInstanceNameType(name,StrName);
254 SetInstanceNameStrPtr(name,AddSymbol(temp));
255 if(0 == (ndx=CheckChildExist(name))) {
256 Asc_SearchListDestroy(search_list);
257 return NULL;
258 }
259 } else {
260 g_search_inst = Asc_FindSimulationRoot(AddSymbol(temp));
261 if (!g_search_inst) {
262 Asc_SearchListDestroy(search_list);
263 return NULL;
264 }
265 }
266 se = Asc_SearchEntryCreate(temp,g_search_inst);
267 gl_append_ptr(search_list,se);
268 }
269 str++;
270 ptr = temp;
271 break;
272 case '\'':
273 str++;
274 if (open_quote) {
275 open_quote--;
276 } else {
277 open_quote++;
278 }
279 break;
280 case '[':
281 if (*(str-1) != ']') {
282 *ptr = '\0';
283 if (g_search_inst) {
284 SetInstanceNameType(name,StrName);
285 SetInstanceNameStrPtr(name,AddSymbol(temp));
286 if(0 == (ndx=CheckChildExist(name))) {
287 Asc_SearchListDestroy(search_list);
288 return NULL;
289 }
290 } else {
291 g_search_inst = Asc_FindSimulationRoot(AddSymbol(temp));
292 if (!g_search_inst) {
293 Asc_SearchListDestroy(search_list);
294 return NULL;
295 }
296 }
297 se = Asc_SearchEntryCreate(temp,g_search_inst);
298 gl_append_ptr(search_list,se);
299 }
300 ptr = temp;
301 open_bracket++;
302 str++;
303 break;
304 case ']':
305 open_bracket--;
306 *ptr = '\0';
307 str++;
308 switch(InstanceKind(g_search_inst)) {
309 case ARRAY_INT_INST:
310 SetInstanceNameType(name,IntArrayIndex);
311 SetInstanceNameIntIndex(name,atol(temp));
312 if(0 == (ndx=CheckChildExist(name))) {
313 Asc_SearchListDestroy(search_list);
314 return NULL;
315 }
316 ptr = temp;
317 se = SearchEntryCreateIntArray(temp,g_search_inst);
318 gl_append_ptr(search_list,se);
319 break;
320 case ARRAY_ENUM_INST:
321 SetInstanceNameType(name,StrArrayIndex);
322 SetInstanceNameStrIndex(name,AddSymbol(temp));
323 if(0 == (ndx=CheckChildExist(name))) { /* sets g_search_inst */
324 Asc_SearchListDestroy(search_list);
325 return NULL;
326 }
327 ptr = temp;
328 se = SearchEntryCreateStrArray(temp,g_search_inst);
329 gl_append_ptr(search_list,se);
330 break;
331 default:
332 FPRINTF(stderr,"Mismatch in qlfdid (%s) and simulation.\n",org);
333 break;
334 }
335 break;
336 default:
337 *(ptr++) = *(str++);
338 break;
339 }
340 }
341 *ptr = '\0';
342 if (*temp == '\0') {
343 return search_list;
344 }
345 HandleLastPart(temp);
346 if (g_search_inst) {
347 se = Asc_SearchEntryCreate(temp,g_search_inst);
348 gl_append_ptr(search_list,se);
349 return search_list;
350 } else {
351 Asc_SearchListDestroy(search_list);
352 return NULL;
353 }
354 }
355
356 int Asc_QlfdidSearch2(char *str)
357 {
358 char temp[MAXIMUM_ID_LENGTH];
359 struct gl_list_t *search_list;
360
361 search_list = Asc_BrowQlfdidSearch(str,temp);
362 if ((g_search_inst==NULL) || (search_list==NULL)) {
363 return 1;
364 } else {
365 Asc_SearchListDestroy(search_list);
366 return 0;
367 }
368 }
369
370 /*
371 *********************************************************************
372 * This is the version of the function that should be used purely
373 * for the effect of leaving the g_search_inst looking at the result
374 * of a qualified id search.
375 *********************************************************************
376 */
377
378 static
379 void HandleLastPart3(char *temp)
380 {
381 struct InstanceName name;
382 if (g_search_inst) {
383 SetInstanceNameType(name,StrName);
384 SetInstanceNameStrPtr(name,AddSymbol(temp));
385 CheckChildExist(name); /* sets g_search_inst regardless */
386 return;
387 } else {
388 g_search_inst = Asc_FindSimulationRoot(AddSymbol(temp));
389 }
390 }
391
392 static
393 struct Instance *BrowQlfdidSearch3(CONST char *str, char *temp,int relative)
394 {
395 register char *ptr;
396 struct InstanceName name;
397 int ndx = 0;
398 int open_bracket = 0;
399 int open_quote = 0;
400 CONST char *org;
401
402 if (relative == 1) {
403 g_search_inst = g_relative_inst; /* could be NULL */
404 } else {
405 g_search_inst = NULL; /* start search from the top */
406 }
407 if (str == NULL) {
408 return NULL;
409 }
410 org = str;
411 ptr = temp;
412 while(*str != '\0') {
413 switch(*str) {
414 case '.':
415 if (*(str-1) != ']') {
416 if (open_quote) { /* to deal b['funny.name'] */
417 *(ptr++) = *(str++); /* ---^--- */
418 break;
419 }
420 *ptr = '\0';
421 if (g_search_inst) {
422 SetInstanceNameType(name,StrName);
423 SetInstanceNameStrPtr(name,AddSymbol(temp));
424 ndx=CheckChildExist(name);
425 if(ndx==0) {
426 return NULL;
427 }
428 } else {
429 g_search_inst = Asc_FindSimulationRoot(AddSymbol(temp));
430 if (!g_search_inst) {
431 return NULL;
432 }
433 }
434 }
435 str++;
436 ptr = temp;
437 break;
438 case '\'':
439 str++;
440 if (open_quote) {
441 open_quote--;
442 } else {
443 open_quote++;
444 }
445 break;
446 case '[':
447 if (*(str-1) != ']') {
448 *ptr = '\0';
449 if (g_search_inst) {
450 SetInstanceNameType(name,StrName);
451 SetInstanceNameStrPtr(name,AddSymbol(temp));
452 ndx=CheckChildExist(name);
453 if(ndx==0) {
454 return NULL;
455 }
456 } else {
457 g_search_inst = Asc_FindSimulationRoot(AddSymbol(temp));
458 if (!g_search_inst) {
459 return NULL;
460 }
461 }
462 }
463 ptr = temp;
464 open_bracket++;
465 str++;
466 break;
467 case ']':
468 open_bracket--;
469 *ptr = '\0';
470 str++;
471 switch(InstanceKind(g_search_inst)) {
472 case ARRAY_INT_INST:
473 SetInstanceNameType(name,IntArrayIndex);
474 SetInstanceNameIntIndex(name,atol(temp));
475 ndx=CheckChildExist(name);
476 if(ndx==0) {
477 return NULL;
478 }
479 ptr = temp;
480 break;
481 case ARRAY_ENUM_INST:
482 SetInstanceNameType(name,StrArrayIndex);
483 SetInstanceNameStrIndex(name,AddSymbol(temp));
484 ndx=CheckChildExist(name);
485 if(ndx==0) { /* sets g_search_inst */
486 return NULL;
487 }
488 ptr = temp;
489 break;
490 default:
491 FPRINTF(stderr,"Mismatch in values file (%s) and simulation.\n",org);
492 break;
493 }
494 break;
495 default:
496 *(ptr++) = *(str++);
497 break;
498 }
499 }
500 *ptr = '\0';
501 if (*temp == '\0') {
502 return g_search_inst;
503 }
504 HandleLastPart3(temp);
505 return g_search_inst; /* which may be NULL */
506 }
507
508 int Asc_QlfdidSearch3(CONST char *str, int relative)
509 {
510 char *temp;
511 struct Instance *found;
512
513 if (str==NULL) {
514 return 1;
515 }
516 temp = ascstrdup((char *)str);
517 if (temp==NULL) {
518 return 1;
519 }
520 found = BrowQlfdidSearch3(str,temp,relative);
521 ascfree(temp);
522 if (found != NULL) {
523 return 0;
524 } else {
525 return 1;
526 }
527 }
528
529
530
531
532
533
534
535

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22