home *** CD-ROM | disk | FTP | other *** search
- /*
- * MakeXFile.c
- * v1.1 940206 - flushes more often to compensate for bug in CodeWarrior STDIO
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- extern char * strdup ( char * ) ;
- char *
- strdup ( char * str ) {
- int len = strlen ( str ) ;
- char * ptr = malloc ( len + 1 ) ;
-
- if ( ptr ) {
- strcpy ( ptr , str ) ;
- } else{
- fprintf ( stderr , "strdup: malloc failed\n" ) ;
- exit ( 1 ) ;
- }
- return ptr ;
- }
-
-
- typedef struct classes {
- char * clsName ;
- char * supName ;
- struct classes * supClass ;
- } Classes ;
-
- Classes cList [ 800 ] ; // OOH!
- int numCls = 0 ;
-
-
- static int
- find_cls ( char * name ) {
- int ix ;
-
- for ( ix = 0 ; ix < numCls ; ix ++ ) {
- if ( ! strcmp ( cList [ ix ] . clsName , name ) ) {
- return ix ;
- }
- }
- return -1 ;
- }
-
-
- static void
- add_class ( char * clsName , char * supName ) {
-
- int ix = find_cls ( clsName ) ;
-
- if ( ix < 0 ) {
- cList [ numCls ] . clsName = strdup ( clsName ) ;
- cList [ numCls ] . supName = strdup ( supName ) ;
- cList [ numCls ] . supClass = NULL ;
- numCls ++ ;
- }
- }
-
-
- static void
- build_tree ( void ) {
-
- int cPos ;
- int ix ;
-
- for ( cPos = 0 ; cPos < numCls ; cPos ++ ) {
- ix = find_cls ( cList [ cPos ] . supName ) ;
- if ( ix >= 0 ) {
- cList [ cPos ] . supClass = & cList [ ix ] ;
- }
- }
- }
-
-
- void scan_infile ( FILE * infile ) ;
- char sLine [ 1024 ] ;
-
- void
- scan_infile ( FILE * infile ) {
- char * ptr , * end ;
- char * const line = sLine ;
-
- while ( 1 ) {
- line [ 0 ] = 0 ;
- fgets ( line , 1023 , infile ) ;
- if ( ! line [ 0 ] || ferror ( infile ) || feof ( infile ) ) {
- break ;
- }
- ptr = strchr ( line , ' ' ) ;
- if ( ! ptr ) {
- fprintf ( stderr , "suspicious line: %s" , line ) ;
- continue ;
- }
- * ptr = 0 ;
- ptr ++ ;
- end = strchr ( ptr , ' ' ) ;
- if ( ! end ) {
- fprintf ( stderr , "suspicious line: %s" , line ) ;
- continue ;
- }
- * end = 0 ;
- add_class ( line , ptr ) ;
- }
- build_tree ( ) ;
- }
-
-
- static void
- print_bases ( void ) {
-
- int ix ;
-
- fprintf ( stderr , "Num classes: %d\n" , numCls ) ;
- for ( ix = 0 ; ix < numCls ; ix ++ ) {
- if ( ! cList [ ix ] . supClass ) {
- fprintf ( stderr , "base class: %s\n" , cList [ ix ] . clsName ) ;
- }
- }
- }
-
-
- static void
- write_header ( FILE * outfile ) {
-
- int ix ;
-
- fprintf ( outfile , "#include <string.h>\n\n" ) ;
- for ( ix = 0 ; ix < numCls ; ix ++ ) {
- fprintf ( outfile , "#include \"%s.h\"\n" , cList [ ix ] . clsName ) ;
- }
- fprintf ( outfile , "\n" ) ;
- fflush ( outfile ) ;
- }
-
-
- static void
- write_table ( FILE * outfile ) {
-
- int ix ;
-
- fprintf ( outfile , "typedef struct c_info_s {\n" ) ;
- fprintf ( outfile , " char * c_name ;\n" ) ;
- fprintf ( outfile , " CObject * ( * c_new ) ( void ) ;\n" ) ;
- fprintf ( outfile , " int c_sup_i ;\n" ) ;
- fprintf ( outfile , "} C_Info_S ;\n\n" ) ;
-
- fprintf ( outfile , "C_Info_S s_c_info [ ] = {\n" ) ;
- for ( ix = 0 ; ix < numCls ; ix ++ ) {
- fprintf ( outfile , " \"%s\" , new_%s , %d ,\n" , cList [ ix ] . clsName ,
- cList [ ix ] . clsName , cList [ ix ] . supClass ?
- ( int ) ( cList [ ix ] . supClass - cList ) : -1 ) ;
- if ( ( ix & 0xf ) == 15 ) {
- fflush ( outfile ) ;
- }
- }
- fprintf ( outfile , "} ;\n\n" ) ;
- }
-
-
- static void
- write_new ( FILE * outfile ) {
-
- int ix ;
-
- for ( ix = 0 ; ix < numCls ; ix ++ ) {
- fprintf ( outfile , "CObject *\nnew_%s ( void ) {\n return ( CObject * ) new %s ;\n}\n\n" ,
- cList [ ix ] . clsName , cList [ ix ] . clsName , cList [ ix ] . clsName ) ;
- if ( ( ix & 0xf ) == 15 ) {
- fflush ( outfile ) ;
- }
- }
- fprintf ( outfile , "\n" ) ;
- }
-
-
- void
- write_trailer ( FILE * outfile ) {
-
- fprintf ( outfile , "CObject * _new_by_name ( char * name ) ;\n\n" ) ;
-
- fprintf ( outfile , "CObject *\n" ) ;
- fprintf ( outfile , "_new_by_name ( char * name ) {\n" ) ;
- fprintf ( outfile , "register int ix ;\n" ) ;
- fprintf ( outfile , " for ( ix = 0 ; ix < %d ; ix ++ ) {\n" , numCls ) ;
- fprintf ( outfile , " if ( ! strcmp ( s_c_info [ ix ] . c_name , name ) ) {\n" ) ;
- fprintf ( outfile , " return ( * s_c_info [ ix ] . c_new ) ( ) ;\n" ) ;
- fprintf ( outfile , " }\n" ) ;
- fprintf ( outfile , " }\n" ) ;
- fprintf ( outfile , " return ( CObject * ) NULL ;\n" ) ;
- fprintf ( outfile , "}\n\n" ) ;
-
- fprintf ( outfile , "Boolean _member ( char * clsName , char * name ) ;\n\n" ) ;
-
- fprintf ( outfile , "Boolean\n" ) ;
- fprintf ( outfile , "_member ( char * clsName , char * name ) {\n" ) ;
- fprintf ( outfile , "register int cIndex ;\n" ) ;
- fprintf ( outfile , " if ( ! strcmp ( clsName , name ) ) {\n" ) ;
- fprintf ( outfile , " return true ;\n" ) ;
- fprintf ( outfile , " }\n" ) ;
- fprintf ( outfile , " for ( cIndex = 0 ; cIndex < %d ; cIndex ++ ) {\n" , numCls ) ;
- fprintf ( outfile , " if ( ! strcmp ( clsName , s_c_info [ cIndex ] . c_name ) ) {\n" ) ;
- fprintf ( outfile , " goto hasIt ;\n" ) ;
- fprintf ( outfile , " }\n" ) ;
- fprintf ( outfile , " }\n" ) ;
- fprintf ( outfile , " return false ;\n" ) ;
- fprintf ( outfile , "hasIt :\n" ) ;
- fprintf ( outfile , " while ( cIndex >= 0 ) {\n" ) ;
- fprintf ( outfile , " if ( ! strcmp ( name , s_c_info [ cIndex ] . c_name ) ) {\n" ) ;
- fprintf ( outfile , " return true ;\n" ) ;
- fprintf ( outfile , " }\n" ) ;
- fprintf ( outfile , " cIndex = s_c_info [ cIndex ] . c_sup_i ;\n" ) ;
- fprintf ( outfile , " }\n" ) ;
- fprintf ( outfile , " return false ;\n" ) ;
- fprintf ( outfile , "}\n\n" ) ;
-
- fflush ( outfile ) ;
- }
-
-
- static void
- write_constr ( FILE * outfile ) {
-
- int ix ;
-
- for ( ix = 0 ; ix < numCls ; ix ++ ) {
- fprintf ( outfile , "%s :: %s ( ) {\n" , cList [ ix ] . clsName ,
- cList [ ix ] . clsName ) ;
- fprintf ( outfile , " _clsName = s_c_info [ %d ] . c_name ;\n" , ix ) ;
- fprintf ( outfile , "}\n\n" ) ;
- if ( ( ix & 0xf ) == 15 ) {
- fflush ( outfile ) ;
- }
- }
- }
-
-
- void
- main ( void ) {
-
- FILE * infile = fopen ( " CLASSLIST" , "r" ) ;
- FILE * outfile = fopen ( "XFile.cp" , "w" ) ;
-
- if ( ! infile ) {
- fprintf ( stderr , "no CLASSLIST" ) ;
- exit ( 1 ) ;
- }
- if ( ! outfile ) {
- fprintf ( stderr , "can't create XFile.c" ) ;
- exit ( 1 ) ;
- }
- scan_infile ( infile ) ;
- fclose ( infile ) ;
- print_bases ( ) ;
- write_header ( outfile ) ;
- write_new ( outfile ) ;
- write_table ( outfile ) ;
- write_constr ( outfile ) ;
- write_trailer ( outfile ) ;
- fclose ( outfile ) ;
- fprintf ( stderr , "OK" ) ;
- exit ( 0 ) ;
- }
-