3 parser.c: Generate helper declarations and functions to trace events
4 from an event description file.
6 Copyright (C) 2002, Xianxiu Yang
7 Copyright (C) 2002, Michel Dagenais
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* This program reads the ".xml" event definitions input files
23 and constructs structure for each event.
25 The program uses a very simple tokenizer, called from a hand written
26 recursive descent parser to fill a data structure describing the events.
27 The result is a sequence of events definitions which refer to type
30 A table of named types is maintained to allow refering to types by name
31 when the same type is used at several places. Finally a sequence of
32 all types is maintained to facilitate the freeing of all type
33 information when the processing of an ".xml" file is finished. */
39 #include <linux/errno.h>
46 void strupper(char *string
)
57 int getSizeindex(int value
)
69 printf("Error : unknown value size %d\n", value
);
74 /*****************************************************************************
76 * getSize : translate from string to integer
78 * in : input file handle
81 *****************************************************************************/
83 int getSize(parse_file
*in
)
88 if(in
->type
== NUMBER
) {
89 if(strcmp(token
,"1") == 0) return 0;
90 else if(strcmp(token
,"2") == 0) return 1;
91 else if(strcmp(token
,"4") == 0) return 2;
92 else if(strcmp(token
,"8") == 0) return 3;
94 else if(in
->type
== NAME
) {
95 if(strcmp(token
,"short") == 0) return 4;
96 else if(strcmp(token
,"medium") == 0) return 5;
97 else if(strcmp(token
,"long") == 0) return 6;
99 in
->error(in
,"incorrect size specification");
103 /*****************************************************************************
105 * error_callback : print out error info
107 * in : input file handle
108 * msg : message to be printed
109 ****************************************************************************/
111 void error_callback(parse_file
*in
, char *msg
)
114 printf("Error in file %s, line %d: %s\n", in
->name
, in
->lineno
, msg
);
120 /*****************************************************************************
122 * memAlloc : allocate memory
124 * size : required memory size
126 * void * : pointer to allocate memory or NULL
127 ****************************************************************************/
129 void * memAlloc(int size
)
132 if(size
== 0) return NULL
;
135 printf("Failed to allocate memory");
141 /*****************************************************************************
143 * allocAndCopy : allocate memory and initialize it
145 * str : string to be put in memory
147 * char * : pointer to allocate memory or NULL
148 ****************************************************************************/
150 char *allocAndCopy(char *str
)
153 if(str
== NULL
) return NULL
;
154 addr
= (char *)memAlloc(strlen(str
)+1);
159 /**************************************************************************
161 * getNameAttribute,getFormatAttribute,getSizeAttribute,getValueAttribute
162 * getValueStrAttribute
164 * Read the attribute from the input file.
167 * in , input file handle.
170 * address of the attribute.
172 **************************************************************************/
174 char * getNameAttribute(parse_file
*in
)
178 if(strcmp("name",token
))in
->error(in
,"name was expected");
181 car
= seekNextChar(in
);
182 if(car
== EOF
)in
->error(in
,"name was expected");
183 else if(car
== '\"')token
= getQuotedString(in
);
184 else token
= getName(in
);
188 char * getFormatAttribute(parse_file
*in
)
192 //format is an option
193 token
= getToken(in
);
194 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
199 if(strcmp("format",token
))in
->error(in
,"format was expected");
201 token
= getQuotedString(in
);
205 int getSizeAttribute(parse_file
*in
)
214 int getValueAttribute(parse_file
*in
)
220 return getNumber(in
);
223 //for <label name=label_name value=n/>, value is an option
224 char * getValueStrAttribute(parse_file
*in
)
228 token
= getToken(in
);
229 if(strcmp("/",token
) == 0){
234 if(strcmp("value",token
))in
->error(in
,"value was expected");
236 token
= getToken(in
);
237 if(in
->type
!= NUMBER
) in
->error(in
,"number was expected");
241 char * getDescription(parse_file
*in
)
244 char * token
, car
, *str
;
248 getLAnglebracket(in
);
250 if(strcmp("description",token
)){
251 fseek(in
->fp
, pos
, SEEK_SET
);
255 getRAnglebracket(in
);
258 while((car
= getc(in
->fp
)) != EOF
) {
259 if(car
== '<') break;
260 if(car
== '\0') continue;
261 in
->buffer
[pos
] = car
;
264 if(car
== EOF
)in
->error(in
,"not a valid description");
265 in
->buffer
[pos
] = '\0';
267 str
= allocAndCopy(in
->buffer
);
271 if(strcmp("description", token
))in
->error(in
,"not a valid description");
272 getRAnglebracket(in
);
277 /*****************************************************************************
279 * parseFacility : generate event list
281 * in : input file handle
282 * fac : empty facility
284 * fac : facility filled with event list
285 ****************************************************************************/
287 void parseFacility(parse_file
*in
, facility
* fac
)
292 fac
->name
= allocAndCopy(getNameAttribute(in
));
293 fac
->capname
= allocAndCopy(fac
->name
);
294 strupper(fac
->capname
);
295 getRAnglebracket(in
);
297 fac
->description
= getDescription(in
);
300 getLAnglebracket(in
);
302 token
= getToken(in
);
303 if(in
->type
== ENDFILE
)
304 in
->error(in
,"the definition of the facility is not finished");
306 if(strcmp("event",token
) == 0){
307 ev
= (event
*) memAlloc(sizeof(event
));
308 sequence_push(&(fac
->events
),ev
);
309 parseEvent(in
,ev
, &(fac
->unnamed_types
), &(fac
->named_types
));
310 }else if(strcmp("type",token
) == 0){
311 parseTypeDefinition(in
, &(fac
->unnamed_types
), &(fac
->named_types
));
312 }else if(in
->type
== FORWARDSLASH
){
314 }else in
->error(in
,"event or type token expected\n");
318 if(strcmp("facility",token
)) in
->error(in
,"not the end of the facility");
319 getRAnglebracket(in
); //</facility>
322 /*****************************************************************************
324 * parseEvent : generate event from event definition
326 * in : input file handle
328 * unnamed_types : array of unamed types
329 * named_types : array of named types
331 * ev : new event (parameters are passed to it)
332 ****************************************************************************/
334 void parseEvent(parse_file
*in
, event
* ev
, sequence
* unnamed_types
,
340 //<event name=eventtype_name>
341 ev
->name
= allocAndCopy(getNameAttribute(in
));
342 getRAnglebracket(in
);
344 //<description>...</descriptio>
345 ev
->description
= getDescription(in
);
347 //event can have STRUCT, TYPEREF or NOTHING
348 getLAnglebracket(in
);
350 token
= getToken(in
);
351 if(in
->type
== FORWARDSLASH
){ //</event> NOTHING
353 }else if(in
->type
== NAME
){
354 if(strcmp("struct",token
)==0 || strcmp("typeref",token
)==0){
356 ev
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
357 if(ev
->type
->type
!= STRUCT
&& ev
->type
->type
!= NONE
)
358 in
->error(in
,"type must be a struct");
359 }else in
->error(in
, "not a valid type");
361 getLAnglebracket(in
);
363 }else in
->error(in
,"not a struct type");
366 if(strcmp("event",token
))in
->error(in
,"not an event definition");
367 getRAnglebracket(in
); //</event>
370 /*****************************************************************************
372 * parseField : get field infomation from buffer
374 * in : input file handle
375 * t : type descriptor
376 * unnamed_types : array of unamed types
377 * named_types : array of named types
378 ****************************************************************************/
380 void parseFields(parse_file
*in
, type_descriptor
*t
, sequence
* unnamed_types
,
386 f
= (field
*)memAlloc(sizeof(field
));
387 sequence_push(&(t
->fields
),f
);
389 //<field name=field_name> <description> <type> </field>
390 f
->name
= allocAndCopy(getNameAttribute(in
));
391 getRAnglebracket(in
);
393 f
->description
= getDescription(in
);
396 getLAnglebracket(in
);
397 f
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
399 getLAnglebracket(in
);
402 if(strcmp("field",token
))in
->error(in
,"not a valid field definition");
403 getRAnglebracket(in
); //</field>
407 /*****************************************************************************
409 * parseType : get type information, type can be :
411 * int(size,fmt); uint(size,fmt); float(size,fmt);
412 * string(fmt); enum(size,fmt,(label1,label2...))
414 * array(arraySize, type); sequence(lengthSize,type)
415 * struct(field(name,type,description)...)
419 * in : input file handle
420 * inType : a type descriptor
421 * unnamed_types : array of unamed types
422 * named_types : array of named types
424 * type_descriptor* : a type descriptor
425 ****************************************************************************/
427 type_descriptor
*parseType(parse_file
*in
, type_descriptor
*inType
,
428 sequence
* unnamed_types
, table
* named_types
)
434 t
= (type_descriptor
*) memAlloc(sizeof(type_descriptor
));
438 sequence_push(unnamed_types
,t
);
444 if(strcmp(token
,"struct") == 0) {
446 getRAnglebracket(in
); //<struct>
447 getLAnglebracket(in
); //<field name=..>
448 token
= getToken(in
);
449 sequence_init(&(t
->fields
));
450 while(strcmp("field",token
) == 0){
451 parseFields(in
,t
, unnamed_types
, named_types
);
454 getLAnglebracket(in
);
455 token
= getToken(in
);
457 if(strcmp("/",token
))in
->error(in
,"not a valid structure definition");
459 if(strcmp("struct",token
)!=0)
460 in
->error(in
,"not a valid structure definition");
461 getRAnglebracket(in
); //</struct>
463 else if(strcmp(token
,"union") == 0) {
465 t
->size
= getSizeAttribute(in
);
466 getRAnglebracket(in
); //<union typecodesize=isize>
468 getLAnglebracket(in
); //<field name=..>
469 token
= getToken(in
);
470 sequence_init(&(t
->fields
));
471 while(strcmp("field",token
) == 0){
472 parseFields(in
,t
, unnamed_types
, named_types
);
475 getLAnglebracket(in
);
476 token
= getToken(in
);
478 if(strcmp("/",token
))in
->error(in
,"not a valid union definition");
480 if(strcmp("union",token
)!=0)
481 in
->error(in
,"not a valid union definition");
482 getRAnglebracket(in
); //</union>
484 else if(strcmp(token
,"array") == 0) {
486 t
->size
= getValueAttribute(in
);
487 getRAnglebracket(in
); //<array size=n>
489 getLAnglebracket(in
); //<type struct>
490 t
->nested_type
= parseType(in
,NULL
, unnamed_types
, named_types
);
492 getLAnglebracket(in
); //</array>
495 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
496 getRAnglebracket(in
); //</array>
498 else if(strcmp(token
,"sequence") == 0) {
500 t
->size
= getSizeAttribute(in
);
501 getRAnglebracket(in
); //<array lengthsize=isize>
503 getLAnglebracket(in
); //<type struct>
504 t
->nested_type
= parseType(in
,NULL
, unnamed_types
, named_types
);
506 getLAnglebracket(in
); //</sequence>
509 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
510 getRAnglebracket(in
); //</sequence>
512 else if(strcmp(token
,"enum") == 0) {
515 sequence_init(&(t
->labels
));
516 t
->size
= getSizeAttribute(in
);
517 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
518 getRAnglebracket(in
);
520 //<label name=label1 value=n/>
521 getLAnglebracket(in
);
522 token
= getToken(in
); //"label" or "/"
523 while(strcmp("label",token
) == 0){
524 str
= allocAndCopy(getNameAttribute(in
));
525 token
= getValueStrAttribute(in
);
527 str1
= appendString(str
,"=");
529 str
= appendString(str1
,token
);
531 sequence_push(&(t
->labels
),str
);
533 sequence_push(&(t
->labels
),str
);
536 getRAnglebracket(in
);
538 //next label definition
539 getLAnglebracket(in
);
540 token
= getToken(in
); //"label" or "/"
542 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
544 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
545 getRAnglebracket(in
); //</label>
547 else if(strcmp(token
,"int") == 0) {
549 t
->size
= getSizeAttribute(in
);
550 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
552 getRAnglebracket(in
);
554 else if(strcmp(token
,"uint") == 0) {
556 t
->size
= getSizeAttribute(in
);
557 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
559 getRAnglebracket(in
);
561 else if(strcmp(token
,"pointer") == 0) {
564 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
566 getRAnglebracket(in
);
568 else if(strcmp(token
,"long") == 0) {
571 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
573 getRAnglebracket(in
);
575 else if(strcmp(token
,"ulong") == 0) {
578 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
580 getRAnglebracket(in
);
582 else if(strcmp(token
,"size_t") == 0) {
585 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
587 getRAnglebracket(in
);
589 else if(strcmp(token
,"ssize_t") == 0) {
592 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
594 getRAnglebracket(in
);
596 else if(strcmp(token
,"off_t") == 0) {
599 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
601 getRAnglebracket(in
);
603 else if(strcmp(token
,"float") == 0) {
605 t
->size
= getSizeAttribute(in
);
606 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
608 getRAnglebracket(in
);
610 else if(strcmp(token
,"string") == 0) {
612 t
->fmt
= allocAndCopy(getFormatAttribute(in
));
614 getRAnglebracket(in
);
616 else if(strcmp(token
,"typeref") == 0){
617 // Must be a named type
619 in
->error(in
,"Named type cannot refer to a named type");
622 sequence_pop(unnamed_types
);
623 token
= getNameAttribute(in
);
624 t
= find_named_type(token
, named_types
);
625 getForwardslash(in
); //<typeref name=type_name/>
626 getRAnglebracket(in
);
629 }else in
->error(in
,"not a valid type");
634 /*****************************************************************************
636 * find_named_type : find a named type from hash table
639 * named_types : array of named types
641 * type_descriptor * : a type descriptor
642 *****************************************************************************/
644 type_descriptor
* find_named_type(char *name
, table
* named_types
)
648 t
= table_find(named_types
,name
);
650 t
= (type_descriptor
*)memAlloc(sizeof(type_descriptor
));
651 t
->type_name
= allocAndCopy(name
);
654 table_insert(named_types
,t
->type_name
,t
);
655 // table_insert(named_types,allocAndCopy(name),t);
660 /*****************************************************************************
662 * parseTypeDefinition : get type information from type definition
664 * in : input file handle
665 * unnamed_types : array of unamed types
666 * named_types : array of named types
667 *****************************************************************************/
669 void parseTypeDefinition(parse_file
* in
, sequence
* unnamed_types
,
675 token
= getNameAttribute(in
);
676 t
= find_named_type(token
, named_types
);
678 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
679 getRAnglebracket(in
); //<type name=type_name>
680 getLAnglebracket(in
); //<struct>
682 if(strcmp("struct",token
))in
->error(in
,"not a valid type definition");
684 parseType(in
,t
, unnamed_types
, named_types
);
687 getLAnglebracket(in
);
690 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
691 getRAnglebracket(in
); //</type>
694 /**************************************************************************
696 * getComa, getName, getNumber, getEqual
698 * Read a token from the input file, check its type, return it scontent.
701 * in , input file handle.
704 * address of token content.
706 **************************************************************************/
708 char *getName(parse_file
* in
)
712 token
= getToken(in
);
713 if(in
->type
!= NAME
) in
->error(in
,"Name token was expected");
717 int getNumber(parse_file
* in
)
721 token
= getToken(in
);
722 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
726 char *getForwardslash(parse_file
* in
)
730 token
= getToken(in
);
731 if(in
->type
!= FORWARDSLASH
) in
->error(in
, "forward slash token was expected");
735 char *getLAnglebracket(parse_file
* in
)
739 token
= getToken(in
);
740 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
744 char *getRAnglebracket(parse_file
* in
)
748 token
= getToken(in
);
749 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
753 char *getQuotedString(parse_file
* in
)
757 token
= getToken(in
);
758 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
762 char * getEqual(parse_file
*in
)
766 token
= getToken(in
);
767 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
771 char seekNextChar(parse_file
*in
)
774 while((car
= getc(in
->fp
)) != EOF
) {
783 /******************************************************************
785 * getToken, ungetToken
787 * Read a token from the input file and return its type and content.
788 * Line numbers are accounted for and whitespace/comments are skipped.
791 * in, input file handle.
794 * address of token content.
796 ******************************************************************/
798 void ungetToken(parse_file
* in
)
803 char *getToken(parse_file
* in
)
807 int pos
= 0, escaped
;
814 /* skip whitespace and comments */
816 while((car
= getc(fp
)) != EOF
) {
819 if(car1
== '*') skipComment(in
);
820 else if(car1
== '/') skipEOL(in
);
822 car1
= ungetc(car1
,fp
);
826 else if(car
== '\n') in
->lineno
++;
827 else if(!isspace(car
)) break;
835 in
->type
= FORWARDSLASH
;
836 in
->buffer
[pos
] = car
;
840 in
->type
= LANGLEBRACKET
;
841 in
->buffer
[pos
] = car
;
845 in
->type
= RANGLEBRACKET
;
846 in
->buffer
[pos
] = car
;
851 in
->buffer
[pos
] = car
;
856 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
857 if(car
== '\\' && escaped
== 0) {
858 in
->buffer
[pos
] = car
;
863 if(car
== '"' && escaped
== 0) break;
864 if(car
== '\n' && escaped
== 0) {
865 in
->error(in
, "non escaped newline inside quoted string");
867 if(car
== '\n') in
->lineno
++;
868 in
->buffer
[pos
] = car
;
872 if(car
== EOF
) in
->error(in
,"no ending quotemark");
873 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
874 in
->type
= QUOTEDSTRING
;
878 in
->buffer
[pos
] = car
;
880 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
885 in
->buffer
[pos
] = car
;
888 if(car
== EOF
) ungetc(car
,fp
);
889 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
892 else if(isalpha(car
)) {
895 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
896 if(!(isalnum(car
) || car
== '_')) {
900 in
->buffer
[pos
] = car
;
903 if(car
== EOF
) ungetc(car
,fp
);
904 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
907 else in
->error(in
, "invalid character, unrecognized token");
913 void skipComment(parse_file
* in
)
916 while((car
= getc(in
->fp
)) != EOF
) {
917 if(car
== '\n') in
->lineno
++;
918 else if(car
== '*') {
921 if(car
== '/') return;
925 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
928 void skipEOL(parse_file
* in
)
931 while((car
= getc(in
->fp
)) != EOF
) {
937 if(car
== EOF
)ungetc(car
, in
->fp
);
940 /*****************************************************************************
942 * checkNamedTypesImplemented : check if all named types have definition
943 ****************************************************************************/
945 void checkNamedTypesImplemented(table
* named_types
)
951 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
952 t
= (type_descriptor
*) named_types
->values
.array
[pos
];
954 sprintf(str
,"named type '%s' has no definition",(char*)named_types
->keys
.array
[pos
]);
955 error_callback(NULL
,str
);
961 /*****************************************************************************
963 * generateChecksum : generate checksum for the facility
965 * facName : name of facility
967 * checksum : checksum for the facility
968 ****************************************************************************/
970 void generateChecksum( char* facName
, unsigned long * checksum
, sequence
* events
)
977 crc
= crc32(facName
);
978 for(pos
= 0; pos
< events
->position
; pos
++){
979 ev
= (event
*)(events
->array
[pos
]);
980 crc
= partial_crc32(ev
->name
,crc
);
981 if(!ev
->type
) continue; //event without type
982 if(ev
->type
->type
!= STRUCT
){
983 sprintf(str
,"event '%s' has a type other than STRUCT",ev
->name
);
984 error_callback(NULL
, str
);
986 crc
= getTypeChecksum(crc
, ev
->type
);
991 /*****************************************************************************
993 * getTypeChecksum : generate checksum by type info
995 * crc : checksum generated so far
996 * type : type descriptor containing type info
998 * unsigned long : checksum
999 *****************************************************************************/
1001 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor
* type
)
1003 unsigned long crc
= aCrc
;
1004 char * str
= NULL
, buf
[16];
1010 str
= intOutputTypes
[type
->size
];
1013 str
= uintOutputTypes
[type
->size
];
1016 str
= allocAndCopy("void *");
1020 str
= allocAndCopy("long");
1024 str
= allocAndCopy("unsigned long");
1028 str
= allocAndCopy("size_t");
1032 str
= allocAndCopy("ssize_t");
1036 str
= allocAndCopy("off_t");
1040 str
= floatOutputTypes
[type
->size
];
1043 str
= allocAndCopy("string");
1047 str
= appendString("enum ", uintOutputTypes
[type
->size
]);
1051 sprintf(buf
,"%d\0",type
->size
);
1052 str
= appendString("array ",buf
);
1056 sprintf(buf
,"%d\0",type
->size
);
1057 str
= appendString("sequence ",buf
);
1061 str
= allocAndCopy("struct");
1065 str
= allocAndCopy("union");
1069 error_callback(NULL
, "named type has no definition");
1073 crc
= partial_crc32(str
,crc
);
1076 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1078 if(type
->type
== ARRAY
|| type
->type
== SEQUENCE
){
1079 crc
= getTypeChecksum(crc
,type
->nested_type
);
1080 }else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1081 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1082 fld
= (field
*) type
->fields
.array
[pos
];
1083 crc
= partial_crc32(fld
->name
,crc
);
1084 crc
= getTypeChecksum(crc
, fld
->type
);
1086 }else if(type
->type
== ENUM
){
1087 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1088 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1095 /* Event type descriptors */
1096 void freeType(type_descriptor
* tp
)
1101 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1102 if(tp
->type
== ENUM
) {
1103 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1104 free(tp
->labels
.array
[pos2
]);
1106 sequence_dispose(&(tp
->labels
));
1108 if(tp
->type
== STRUCT
) {
1109 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1110 f
= (field
*) tp
->fields
.array
[pos2
];
1112 free(f
->description
);
1115 sequence_dispose(&(tp
->fields
));
1119 void freeNamedType(table
* t
)
1122 type_descriptor
* td
;
1124 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1125 free((char *)t
->keys
.array
[pos
]);
1126 td
= (type_descriptor
*)t
->values
.array
[pos
];
1132 void freeTypes(sequence
*t
)
1135 type_descriptor
*tp
;
1138 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1139 tp
= (type_descriptor
*)t
->array
[pos
];
1145 void freeEvents(sequence
*t
)
1150 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1151 ev
= (event
*) t
->array
[pos
];
1153 free(ev
->description
);
1160 /* Extensible array */
1162 void sequence_init(sequence
*t
)
1166 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1169 void sequence_dispose(sequence
*t
)
1176 void sequence_push(sequence
*t
, void *elem
)
1180 if(t
->position
>= t
->size
) {
1182 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1183 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1184 t
->size
= t
->size
* 2;
1187 t
->array
[t
->position
] = elem
;
1191 void *sequence_pop(sequence
*t
)
1193 return t
->array
[t
->position
--];
1197 /* Hash table API, implementation is just linear search for now */
1199 void table_init(table
*t
)
1201 sequence_init(&(t
->keys
));
1202 sequence_init(&(t
->values
));
1205 void table_dispose(table
*t
)
1207 sequence_dispose(&(t
->keys
));
1208 sequence_dispose(&(t
->values
));
1211 void table_insert(table
*t
, char *key
, void *value
)
1213 sequence_push(&(t
->keys
),key
);
1214 sequence_push(&(t
->values
),value
);
1217 void *table_find(table
*t
, char *key
)
1220 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1221 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1222 return(t
->values
.array
[pos
]);
1227 void table_insert_int(table
*t
, int *key
, void *value
)
1229 sequence_push(&(t
->keys
),key
);
1230 sequence_push(&(t
->values
),value
);
1233 void *table_find_int(table
*t
, int *key
)
1236 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1237 if(*key
== *(int *)t
->keys
.array
[pos
])
1238 return(t
->values
.array
[pos
]);
1244 /* Concatenate strings */
1246 char *appendString(char *s
, char *suffix
)
1249 if(suffix
== NULL
) return s
;
1251 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.065314 seconds and 5 git commands to generate.