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>
47 void strupper(char *string
)
58 int getSizeindex(int value
)
70 printf("Error : unknown value size %d\n", value
);
75 /*****************************************************************************
77 * getSize : translate from string to integer
79 * in : input file handle
82 *****************************************************************************/
84 int getSize(parse_file
*in
)
89 if(in
->type
== NUMBER
) {
90 if(strcmp(token
,"1") == 0) return 0;
91 else if(strcmp(token
,"2") == 0) return 1;
92 else if(strcmp(token
,"4") == 0) return 2;
93 else if(strcmp(token
,"8") == 0) return 3;
95 else if(in
->type
== NAME
) {
96 if(strcmp(token
,"short") == 0) return 4;
97 else if(strcmp(token
,"medium") == 0) return 5;
98 else if(strcmp(token
,"long") == 0) return 6;
100 in
->error(in
,"incorrect size specification");
104 /*****************************************************************************
106 * error_callback : print out error info
108 * in : input file handle
109 * msg : message to be printed
110 ****************************************************************************/
112 void error_callback(parse_file
*in
, char *msg
)
115 printf("Error in file %s, line %d: %s\n", in
->name
, in
->lineno
, msg
);
122 /*****************************************************************************
124 * memAlloc : allocate memory
126 * size : required memory size
128 * void * : pointer to allocate memory or NULL
129 ****************************************************************************/
131 void * memAlloc(int size
)
134 if(size
== 0) return NULL
;
137 printf("Failed to allocate memory");
143 /*****************************************************************************
145 * allocAndCopy : allocate memory and initialize it
147 * str : string to be put in memory
149 * char * : pointer to allocate memory or NULL
150 ****************************************************************************/
152 char *allocAndCopy(char *str
)
155 if(str
== NULL
) return NULL
;
156 addr
= (char *)memAlloc(strlen(str
)+1);
161 /**************************************************************************
165 * Read the attribute from the input file.
168 * in , input file handle.
169 * t , the type descriptor to fill.
171 **************************************************************************/
173 void getTypeAttributes(parse_file
*in
, type_descriptor
*t
)
183 token
= getToken(in
);
184 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
189 if(!strcmp("format",token
)) {
191 t
->fmt
= allocAndCopy(getQuotedString(in
));
192 //} else if(!strcmp("name",token)) {
194 // car = seekNextChar(in);
195 // if(car == EOF) in->error(in,"name was expected");
196 // else if(car == '\"') t->type_name = allocAndCopy(getQuotedString(in));
197 // else t->type_name = allocAndCopy(getName(in));
198 } else if(!strcmp("size",token
)) {
200 t
->size
= getSize(in
);
201 } else if(!strcmp("align",token
)) {
203 t
->alignment
= getNumber(in
);
208 /**************************************************************************
212 * Read the attribute from the input file.
215 * in , input file handle.
216 * ev , the event to fill.
218 **************************************************************************/
220 void getEventAttributes(parse_file
*in
, event
*ev
)
228 token
= getToken(in
);
229 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
234 if(!strcmp("name",token
)) {
236 car
= seekNextChar(in
);
237 if(car
== EOF
) in
->error(in
,"name was expected");
238 else if(car
== '\"') ev
->name
= allocAndCopy(getQuotedString(in
));
239 else ev
->name
= allocAndCopy(getName(in
));
244 /**************************************************************************
246 * getFacilityAttributes
248 * Read the attribute from the input file.
251 * in , input file handle.
252 * fac , the facility to fill.
254 **************************************************************************/
256 void getFacilityAttributes(parse_file
*in
, facility
*fac
)
264 token
= getToken(in
);
265 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
270 if(!strcmp("name",token
)) {
272 car
= seekNextChar(in
);
273 if(car
== EOF
) in
->error(in
,"name was expected");
274 else if(car
== '\"') fac
->name
= allocAndCopy(getQuotedString(in
));
275 else fac
->name
= allocAndCopy(getName(in
));
280 /**************************************************************************
284 * Read the attribute from the input file.
287 * in , input file handle.
288 * f , the field to fill.
290 **************************************************************************/
292 void getFieldAttributes(parse_file
*in
, field
*f
)
300 token
= getToken(in
);
301 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
306 if(!strcmp("name",token
)) {
308 car
= seekNextChar(in
);
309 if(car
== EOF
) in
->error(in
,"name was expected");
310 else if(car
== '\"') f
->name
= allocAndCopy(getQuotedString(in
));
311 else f
->name
= allocAndCopy(getName(in
));
316 char *getNameAttribute(parse_file
*in
)
323 token
= getToken(in
);
324 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
329 if(!strcmp("name",token
)) {
331 car
= seekNextChar(in
);
332 if(car
== EOF
) in
->error(in
,"name was expected");
333 else if(car
== '\"') name
= allocAndCopy(getQuotedString(in
));
334 else name
= allocAndCopy(getName(in
));
337 if(name
== NULL
) in
->error(in
, "Name was expected");
344 //for <label name=label_name value=n format="..."/>, value is an option
345 char * getValueStrAttribute(parse_file
*in
)
349 token
= getToken(in
);
350 if(strcmp("/",token
) == 0){
355 if(strcmp("value",token
))in
->error(in
,"value was expected");
357 token
= getToken(in
);
358 if(in
->type
!= NUMBER
) in
->error(in
,"number was expected");
362 char * getDescription(parse_file
*in
)
365 char * token
, car
, *str
;
369 getLAnglebracket(in
);
371 if(strcmp("description",token
)){
372 fseek(in
->fp
, pos
, SEEK_SET
);
376 getRAnglebracket(in
);
379 while((car
= getc(in
->fp
)) != EOF
) {
380 if(car
== '<') break;
381 if(car
== '\0') continue;
382 in
->buffer
[pos
] = car
;
385 if(car
== EOF
)in
->error(in
,"not a valid description");
386 in
->buffer
[pos
] = '\0';
388 str
= allocAndCopy(in
->buffer
);
392 if(strcmp("description", token
))in
->error(in
,"not a valid description");
393 getRAnglebracket(in
);
398 /*****************************************************************************
400 * parseFacility : generate event list
402 * in : input file handle
403 * fac : empty facility
405 * fac : facility filled with event list
406 ****************************************************************************/
408 void parseFacility(parse_file
*in
, facility
* fac
)
413 getFacilityAttributes(in
, fac
);
414 if(fac
->name
== NULL
) in
->error(in
, "Attribute not named");
416 fac
->capname
= allocAndCopy(fac
->name
);
417 strupper(fac
->capname
);
418 getRAnglebracket(in
);
420 fac
->description
= getDescription(in
);
423 getLAnglebracket(in
);
425 token
= getToken(in
);
426 if(in
->type
== ENDFILE
)
427 in
->error(in
,"the definition of the facility is not finished");
429 if(strcmp("event",token
) == 0){
430 ev
= (event
*) memAlloc(sizeof(event
));
431 sequence_push(&(fac
->events
),ev
);
432 parseEvent(in
,ev
, &(fac
->unnamed_types
), &(fac
->named_types
));
433 }else if(strcmp("type",token
) == 0){
434 parseTypeDefinition(in
, &(fac
->unnamed_types
), &(fac
->named_types
));
435 }else if(in
->type
== FORWARDSLASH
){
437 }else in
->error(in
,"event or type token expected\n");
441 if(strcmp("facility",token
)) in
->error(in
,"not the end of the facility");
442 getRAnglebracket(in
); //</facility>
445 /*****************************************************************************
447 * parseEvent : generate event from event definition
449 * in : input file handle
451 * unnamed_types : array of unamed types
452 * named_types : array of named types
454 * ev : new event (parameters are passed to it)
455 ****************************************************************************/
457 void parseEvent(parse_file
*in
, event
* ev
, sequence
* unnamed_types
,
463 //<event name=eventtype_name>
464 getEventAttributes(in
, ev
);
465 if(ev
->name
== NULL
) in
->error(in
, "Event not named");
466 getRAnglebracket(in
);
468 //<description>...</descriptio>
469 ev
->description
= getDescription(in
);
471 //event can have STRUCT, TYPEREF or NOTHING
472 getLAnglebracket(in
);
474 token
= getToken(in
);
475 if(in
->type
== FORWARDSLASH
){ //</event> NOTHING
477 }else if(in
->type
== NAME
){
478 if(strcmp("struct",token
)==0 || strcmp("typeref",token
)==0){
480 ev
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
481 if(ev
->type
->type
!= STRUCT
&& ev
->type
->type
!= NONE
)
482 in
->error(in
,"type must be a struct");
483 }else in
->error(in
, "not a valid type");
485 getLAnglebracket(in
);
487 }else in
->error(in
,"not a struct type");
490 if(strcmp("event",token
))in
->error(in
,"not an event definition");
491 getRAnglebracket(in
); //</event>
494 /*****************************************************************************
496 * parseField : get field infomation from buffer
498 * in : input file handle
499 * t : type descriptor
500 * unnamed_types : array of unamed types
501 * named_types : array of named types
502 ****************************************************************************/
504 void parseFields(parse_file
*in
, type_descriptor
*t
, sequence
* unnamed_types
,
510 f
= (field
*)memAlloc(sizeof(field
));
511 sequence_push(&(t
->fields
),f
);
513 //<field name=field_name> <description> <type> </field>
514 getFieldAttributes(in
, f
);
515 if(f
->name
== NULL
) in
->error(in
, "Field not named");
516 getRAnglebracket(in
);
518 f
->description
= getDescription(in
);
521 getLAnglebracket(in
);
522 f
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
524 getLAnglebracket(in
);
527 if(strcmp("field",token
))in
->error(in
,"not a valid field definition");
528 getRAnglebracket(in
); //</field>
532 /*****************************************************************************
534 * parseType : get type information, type can be :
536 * int(size,fmt); uint(size,fmt); float(size,fmt);
537 * string(fmt); enum(size,fmt,(label1,label2...))
539 * array(arraySize, type); sequence(lengthSize,type)
540 * struct(field(name,type,description)...)
544 * in : input file handle
545 * inType : a type descriptor
546 * unnamed_types : array of unamed types
547 * named_types : array of named types
549 * type_descriptor* : a type descriptor
550 ****************************************************************************/
552 type_descriptor
*parseType(parse_file
*in
, type_descriptor
*inType
,
553 sequence
* unnamed_types
, table
* named_types
)
559 t
= (type_descriptor
*) memAlloc(sizeof(type_descriptor
));
563 sequence_push(unnamed_types
,t
);
569 if(strcmp(token
,"struct") == 0) {
571 getTypeAttributes(in
, t
);
572 getRAnglebracket(in
); //<struct>
573 getLAnglebracket(in
); //<field name=..>
574 token
= getToken(in
);
575 sequence_init(&(t
->fields
));
576 while(strcmp("field",token
) == 0){
577 parseFields(in
,t
, unnamed_types
, named_types
);
580 getLAnglebracket(in
);
581 token
= getToken(in
);
583 if(strcmp("/",token
))in
->error(in
,"not a valid structure definition");
585 if(strcmp("struct",token
)!=0)
586 in
->error(in
,"not a valid structure definition");
587 getRAnglebracket(in
); //</struct>
589 else if(strcmp(token
,"union") == 0) {
591 getTypeAttributes(in
, t
);
592 if(t
->size
== -1) in
->error(in
, "Union has empty size");
593 getRAnglebracket(in
); //<union typecodesize=isize>
595 getLAnglebracket(in
); //<field name=..>
596 token
= getToken(in
);
597 sequence_init(&(t
->fields
));
598 while(strcmp("field",token
) == 0){
599 parseFields(in
,t
, unnamed_types
, named_types
);
602 getLAnglebracket(in
);
603 token
= getToken(in
);
605 if(strcmp("/",token
))in
->error(in
,"not a valid union definition");
607 if(strcmp("union",token
)!=0)
608 in
->error(in
,"not a valid union definition");
609 getRAnglebracket(in
); //</union>
611 else if(strcmp(token
,"array") == 0) {
613 getTypeAttributes(in
, t
);
614 if(t
->size
== -1) in
->error(in
, "Array has empty size");
615 getRAnglebracket(in
); //<array size=n>
617 getLAnglebracket(in
); //<type struct>
618 t
->nested_type
= parseType(in
, NULL
, unnamed_types
, named_types
);
620 getLAnglebracket(in
); //</array>
623 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
624 getRAnglebracket(in
); //</array>
626 else if(strcmp(token
,"sequence") == 0) {
628 getTypeAttributes(in
, t
);
629 if(t
->size
== -1) in
->error(in
, "Sequence has empty size");
630 getRAnglebracket(in
); //<array lengthsize=isize>
632 getLAnglebracket(in
); //<type struct>
633 t
->nested_type
= parseType(in
,NULL
, unnamed_types
, named_types
);
635 getLAnglebracket(in
); //</sequence>
638 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
639 getRAnglebracket(in
); //</sequence>
641 else if(strcmp(token
,"enum") == 0) {
644 sequence_init(&(t
->labels
));
645 sequence_init(&(t
->labels_description
));
646 t
->already_printed
= 0;
647 getTypeAttributes(in
, t
);
648 if(t
->size
== -1) in
->error(in
, "Sequence has empty size");
649 getRAnglebracket(in
);
651 //<label name=label1 value=n/>
652 getLAnglebracket(in
);
653 token
= getToken(in
); //"label" or "/"
654 while(strcmp("label",token
) == 0){
655 str
= allocAndCopy(getNameAttribute(in
));
656 token
= getValueStrAttribute(in
);
658 str1
= appendString(str
,"=");
660 str
= appendString(str1
,token
);
662 sequence_push(&(t
->labels
),str
);
665 sequence_push(&(t
->labels
),str
);
668 getRAnglebracket(in
);
670 //read description if any. May be NULL.
671 str
= allocAndCopy(getDescription(in
));
672 sequence_push(&(t
->labels_description
),str
);
674 //next label definition
675 getLAnglebracket(in
);
676 token
= getToken(in
); //"label" or "/"
678 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
680 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
681 getRAnglebracket(in
); //</label>
683 else if(strcmp(token
,"int") == 0) {
685 getTypeAttributes(in
, t
);
686 if(t
->size
== -1) in
->error(in
, "int has empty size");
688 getRAnglebracket(in
);
690 else if(strcmp(token
,"uint") == 0) {
692 getTypeAttributes(in
, t
);
693 if(t
->size
== -1) in
->error(in
, "uint has empty size");
695 getRAnglebracket(in
);
697 else if(strcmp(token
,"pointer") == 0) {
699 getTypeAttributes(in
, t
);
701 getRAnglebracket(in
);
703 else if(strcmp(token
,"long") == 0) {
705 getTypeAttributes(in
, t
);
707 getRAnglebracket(in
);
709 else if(strcmp(token
,"ulong") == 0) {
711 getTypeAttributes(in
, t
);
713 getRAnglebracket(in
);
715 else if(strcmp(token
,"size_t") == 0) {
717 getTypeAttributes(in
, t
);
719 getRAnglebracket(in
);
721 else if(strcmp(token
,"ssize_t") == 0) {
723 getTypeAttributes(in
, t
);
725 getRAnglebracket(in
);
727 else if(strcmp(token
,"off_t") == 0) {
729 getTypeAttributes(in
, t
);
731 getRAnglebracket(in
);
733 else if(strcmp(token
,"float") == 0) {
735 getTypeAttributes(in
, t
);
737 getRAnglebracket(in
);
739 else if(strcmp(token
,"string") == 0) {
741 getTypeAttributes(in
, t
);
743 getRAnglebracket(in
);
745 else if(strcmp(token
,"typeref") == 0){
746 // Must be a named type
748 in
->error(in
,"Named type cannot refer to a named type");
751 sequence_pop(unnamed_types
);
752 token
= getNameAttribute(in
);
753 t
= find_named_type(token
, named_types
);
754 getForwardslash(in
); //<typeref name=type_name/>
755 getRAnglebracket(in
);
758 }else in
->error(in
,"not a valid type");
763 /*****************************************************************************
765 * find_named_type : find a named type from hash table
768 * named_types : array of named types
770 * type_descriptor * : a type descriptor
771 *****************************************************************************/
773 type_descriptor
* find_named_type(char *name
, table
* named_types
)
777 t
= table_find(named_types
,name
);
779 t
= (type_descriptor
*)memAlloc(sizeof(type_descriptor
));
780 t
->type_name
= allocAndCopy(name
);
783 table_insert(named_types
,t
->type_name
,t
);
784 // table_insert(named_types,allocAndCopy(name),t);
789 /*****************************************************************************
791 * parseTypeDefinition : get type information from type definition
793 * in : input file handle
794 * unnamed_types : array of unamed types
795 * named_types : array of named types
796 *****************************************************************************/
798 void parseTypeDefinition(parse_file
* in
, sequence
* unnamed_types
,
804 token
= getNameAttribute(in
);
805 if(token
== NULL
) in
->error(in
, "Type has empty name");
806 t
= find_named_type(token
, named_types
);
808 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
809 getRAnglebracket(in
); //<type name=type_name>
810 getLAnglebracket(in
); //<
812 //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
814 parseType(in
,t
, unnamed_types
, named_types
);
817 getLAnglebracket(in
);
820 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
821 getRAnglebracket(in
); //</type>
824 /**************************************************************************
826 * getComa, getName, getNumber, getEqual
828 * Read a token from the input file, check its type, return it scontent.
831 * in , input file handle.
834 * address of token content.
836 **************************************************************************/
838 char *getName(parse_file
* in
)
842 token
= getToken(in
);
843 if(in
->type
!= NAME
) in
->error(in
,"Name token was expected");
847 int getNumber(parse_file
* in
)
851 token
= getToken(in
);
852 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
856 char *getForwardslash(parse_file
* in
)
860 token
= getToken(in
);
861 if(in
->type
!= FORWARDSLASH
) in
->error(in
, "forward slash token was expected");
865 char *getLAnglebracket(parse_file
* in
)
869 token
= getToken(in
);
870 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
874 char *getRAnglebracket(parse_file
* in
)
878 token
= getToken(in
);
879 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
883 char *getQuotedString(parse_file
* in
)
887 token
= getToken(in
);
888 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
892 char * getEqual(parse_file
*in
)
896 token
= getToken(in
);
897 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
901 char seekNextChar(parse_file
*in
)
904 while((car
= getc(in
->fp
)) != EOF
) {
913 /******************************************************************
915 * getToken, ungetToken
917 * Read a token from the input file and return its type and content.
918 * Line numbers are accounted for and whitespace/comments are skipped.
921 * in, input file handle.
924 * address of token content.
926 ******************************************************************/
928 void ungetToken(parse_file
* in
)
933 char *getToken(parse_file
* in
)
937 int pos
= 0, escaped
;
944 /* skip whitespace and comments */
946 while((car
= getc(fp
)) != EOF
) {
949 if(car1
== '*') skipComment(in
);
950 else if(car1
== '/') skipEOL(in
);
952 car1
= ungetc(car1
,fp
);
956 else if(car
== '\n') in
->lineno
++;
957 else if(!isspace(car
)) break;
965 in
->type
= FORWARDSLASH
;
966 in
->buffer
[pos
] = car
;
970 in
->type
= LANGLEBRACKET
;
971 in
->buffer
[pos
] = car
;
975 in
->type
= RANGLEBRACKET
;
976 in
->buffer
[pos
] = car
;
981 in
->buffer
[pos
] = car
;
986 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
987 if(car
== '\\' && escaped
== 0) {
988 in
->buffer
[pos
] = car
;
993 if(car
== '"' && escaped
== 0) break;
994 if(car
== '\n' && escaped
== 0) {
995 in
->error(in
, "non escaped newline inside quoted string");
997 if(car
== '\n') in
->lineno
++;
998 in
->buffer
[pos
] = car
;
1002 if(car
== EOF
) in
->error(in
,"no ending quotemark");
1003 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
1004 in
->type
= QUOTEDSTRING
;
1008 in
->buffer
[pos
] = car
;
1010 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1015 in
->buffer
[pos
] = car
;
1018 if(car
== EOF
) ungetc(car
,fp
);
1019 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
1022 else if(isalpha(car
)) {
1023 in
->buffer
[0] = car
;
1025 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1026 if(!(isalnum(car
) || car
== '_')) {
1030 in
->buffer
[pos
] = car
;
1033 if(car
== EOF
) ungetc(car
,fp
);
1034 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
1037 else in
->error(in
, "invalid character, unrecognized token");
1039 in
->buffer
[pos
] = 0;
1043 void skipComment(parse_file
* in
)
1046 while((car
= getc(in
->fp
)) != EOF
) {
1047 if(car
== '\n') in
->lineno
++;
1048 else if(car
== '*') {
1050 if(car
==EOF
) break;
1051 if(car
== '/') return;
1055 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
1058 void skipEOL(parse_file
* in
)
1061 while((car
= getc(in
->fp
)) != EOF
) {
1067 if(car
== EOF
)ungetc(car
, in
->fp
);
1070 /*****************************************************************************
1072 * checkNamedTypesImplemented : check if all named types have definition
1073 ****************************************************************************/
1075 void checkNamedTypesImplemented(table
* named_types
)
1081 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
1082 t
= (type_descriptor
*) named_types
->values
.array
[pos
];
1083 if(t
->type
== NONE
){
1084 sprintf(str
,"named type '%s' has no definition",(char*)named_types
->keys
.array
[pos
]);
1085 error_callback(NULL
,str
);
1091 /*****************************************************************************
1093 * generateChecksum : generate checksum for the facility
1095 * facName : name of facility
1097 * checksum : checksum for the facility
1098 ****************************************************************************/
1100 void generateChecksum( char* facName
, unsigned long * checksum
, sequence
* events
)
1107 crc
= crc32(facName
);
1108 for(pos
= 0; pos
< events
->position
; pos
++){
1109 ev
= (event
*)(events
->array
[pos
]);
1110 crc
= partial_crc32(ev
->name
,crc
);
1111 if(!ev
->type
) continue; //event without type
1112 if(ev
->type
->type
!= STRUCT
){
1113 sprintf(str
,"event '%s' has a type other than STRUCT",ev
->name
);
1114 error_callback(NULL
, str
);
1116 crc
= getTypeChecksum(crc
, ev
->type
);
1121 /*****************************************************************************
1123 * getTypeChecksum : generate checksum by type info
1125 * crc : checksum generated so far
1126 * type : type descriptor containing type info
1128 * unsigned long : checksum
1129 *****************************************************************************/
1131 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor
* type
)
1133 unsigned long crc
= aCrc
;
1134 char * str
= NULL
, buf
[16];
1140 str
= intOutputTypes
[type
->size
];
1143 str
= uintOutputTypes
[type
->size
];
1146 str
= allocAndCopy("void *");
1150 str
= allocAndCopy("long");
1154 str
= allocAndCopy("unsigned long");
1158 str
= allocAndCopy("size_t");
1162 str
= allocAndCopy("ssize_t");
1166 str
= allocAndCopy("off_t");
1170 str
= floatOutputTypes
[type
->size
];
1173 str
= allocAndCopy("string");
1177 str
= appendString("enum ", uintOutputTypes
[type
->size
]);
1181 sprintf(buf
,"%d\0",type
->size
);
1182 str
= appendString("array ",buf
);
1186 sprintf(buf
,"%d\0",type
->size
);
1187 str
= appendString("sequence ",buf
);
1191 str
= allocAndCopy("struct");
1195 str
= allocAndCopy("union");
1199 error_callback(NULL
, "named type has no definition");
1203 crc
= partial_crc32(str
,crc
);
1206 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1208 if(type
->type
== ARRAY
|| type
->type
== SEQUENCE
){
1209 crc
= getTypeChecksum(crc
,type
->nested_type
);
1210 }else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1211 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1212 fld
= (field
*) type
->fields
.array
[pos
];
1213 crc
= partial_crc32(fld
->name
,crc
);
1214 crc
= getTypeChecksum(crc
, fld
->type
);
1216 }else if(type
->type
== ENUM
){
1217 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1218 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1225 /* Event type descriptors */
1226 void freeType(type_descriptor
* tp
)
1231 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1232 if(tp
->type
== ENUM
) {
1233 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1234 free(tp
->labels
.array
[pos2
]);
1236 sequence_dispose(&(tp
->labels
));
1238 if(tp
->type
== STRUCT
) {
1239 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1240 f
= (field
*) tp
->fields
.array
[pos2
];
1242 free(f
->description
);
1245 sequence_dispose(&(tp
->fields
));
1249 void freeNamedType(table
* t
)
1252 type_descriptor
* td
;
1254 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1255 free((char *)t
->keys
.array
[pos
]);
1256 td
= (type_descriptor
*)t
->values
.array
[pos
];
1262 void freeTypes(sequence
*t
)
1265 type_descriptor
*tp
;
1268 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1269 tp
= (type_descriptor
*)t
->array
[pos
];
1275 void freeEvents(sequence
*t
)
1280 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1281 ev
= (event
*) t
->array
[pos
];
1283 free(ev
->description
);
1290 /* Extensible array */
1292 void sequence_init(sequence
*t
)
1296 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1299 void sequence_dispose(sequence
*t
)
1306 void sequence_push(sequence
*t
, void *elem
)
1310 if(t
->position
>= t
->size
) {
1312 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1313 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1314 t
->size
= t
->size
* 2;
1317 t
->array
[t
->position
] = elem
;
1321 void *sequence_pop(sequence
*t
)
1323 return t
->array
[t
->position
--];
1327 /* Hash table API, implementation is just linear search for now */
1329 void table_init(table
*t
)
1331 sequence_init(&(t
->keys
));
1332 sequence_init(&(t
->values
));
1335 void table_dispose(table
*t
)
1337 sequence_dispose(&(t
->keys
));
1338 sequence_dispose(&(t
->values
));
1341 void table_insert(table
*t
, char *key
, void *value
)
1343 sequence_push(&(t
->keys
),key
);
1344 sequence_push(&(t
->values
),value
);
1347 void *table_find(table
*t
, char *key
)
1350 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1351 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1352 return(t
->values
.array
[pos
]);
1357 void table_insert_int(table
*t
, int *key
, void *value
)
1359 sequence_push(&(t
->keys
),key
);
1360 sequence_push(&(t
->values
),value
);
1363 void *table_find_int(table
*t
, int *key
)
1366 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1367 if(*key
== *(int *)t
->keys
.array
[pos
])
1368 return(t
->values
.array
[pos
]);
1374 /* Concatenate strings */
1376 char *appendString(char *s
, char *suffix
)
1379 if(suffix
== NULL
) return s
;
1381 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.062263 seconds and 4 git commands to generate.