home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / PTR-TCL v2.1 / 5) MakeXFile / MakeXFile.c next >
Encoding:
C/C++ Source or Header  |  1994-02-18  |  6.1 KB  |  266 lines  |  [TEXT/MMCC]

  1. /*
  2.  * MakeXFile.c
  3.  * v1.1 940206 - flushes more often to compensate for bug in CodeWarrior STDIO
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. extern char * strdup ( char * ) ;
  11. char *
  12. strdup ( char * str ) {
  13. int len = strlen ( str ) ;
  14. char * ptr = malloc ( len + 1 ) ;
  15.  
  16.     if ( ptr ) {
  17.         strcpy ( ptr , str ) ;
  18.     } else{
  19.         fprintf ( stderr , "strdup: malloc failed\n" ) ;
  20.         exit ( 1 ) ;
  21.     }
  22.     return ptr ;
  23. }
  24.  
  25.  
  26. typedef struct classes {
  27.     char * clsName ;
  28.     char * supName ;
  29.     struct classes * supClass ;
  30. } Classes ;
  31.  
  32. Classes cList [ 800 ] ; // OOH!
  33. int numCls = 0 ;
  34.  
  35.  
  36. static int
  37. find_cls ( char * name ) {
  38. int ix ;
  39.  
  40.     for ( ix = 0 ; ix < numCls ; ix ++ ) {
  41.         if ( ! strcmp ( cList [ ix ] . clsName , name ) ) {
  42.             return ix ;
  43.         }
  44.     }
  45.     return -1 ;
  46. }
  47.  
  48.  
  49. static void
  50. add_class ( char * clsName , char * supName ) {
  51.  
  52. int ix = find_cls ( clsName ) ;
  53.  
  54.     if ( ix < 0 ) {
  55.         cList [ numCls ] . clsName = strdup ( clsName ) ;
  56.         cList [ numCls ] . supName = strdup ( supName ) ;
  57.         cList [ numCls ] . supClass = NULL ;
  58.         numCls ++ ;
  59.     }
  60. }
  61.  
  62.  
  63. static void
  64. build_tree ( void ) {
  65.  
  66. int cPos ;
  67. int ix ;
  68.  
  69.     for ( cPos = 0 ; cPos < numCls ; cPos ++ ) {
  70.         ix = find_cls ( cList [ cPos ] . supName ) ;
  71.         if ( ix >= 0 ) {
  72.             cList [ cPos ] . supClass = & cList [ ix ] ;
  73.         }
  74.     }
  75. }
  76.  
  77.  
  78. void scan_infile ( FILE * infile ) ;
  79. char sLine [ 1024 ] ;
  80.  
  81. void
  82. scan_infile ( FILE * infile ) {
  83. char * ptr , * end ;
  84. char * const line = sLine ;
  85.  
  86.     while ( 1 ) {
  87.         line [ 0 ] = 0 ;
  88.         fgets ( line , 1023 , infile ) ;
  89.         if ( ! line [ 0 ] || ferror ( infile ) || feof ( infile ) ) {
  90.             break ;
  91.         }
  92.         ptr = strchr ( line , ' ' ) ;
  93.         if ( ! ptr ) {
  94.             fprintf ( stderr , "suspicious line: %s" , line ) ;
  95.             continue ;
  96.         }
  97.         * ptr = 0 ;
  98.         ptr ++ ;
  99.         end = strchr ( ptr , ' ' ) ;
  100.         if ( ! end ) {
  101.             fprintf ( stderr , "suspicious line: %s" , line ) ;
  102.             continue ;
  103.         }
  104.         * end = 0 ;
  105.         add_class ( line , ptr ) ;
  106.     }
  107.     build_tree ( ) ;
  108. }
  109.  
  110.  
  111. static void
  112. print_bases ( void ) {
  113.  
  114. int ix ;
  115.  
  116.     fprintf ( stderr , "Num classes: %d\n" , numCls ) ;
  117.     for ( ix = 0 ; ix < numCls ; ix ++ ) {
  118.         if ( ! cList [ ix ] . supClass ) {
  119.             fprintf ( stderr , "base class: %s\n" , cList [ ix ] . clsName ) ;
  120.         }
  121.     }
  122. }
  123.  
  124.  
  125. static void
  126. write_header ( FILE * outfile ) {
  127.  
  128. int ix ;
  129.  
  130.     fprintf ( outfile , "#include <string.h>\n\n" ) ;
  131.     for ( ix = 0 ; ix < numCls ; ix ++ ) {
  132.         fprintf ( outfile , "#include \"%s.h\"\n" , cList [ ix ] . clsName ) ;
  133.     }
  134.     fprintf ( outfile , "\n" ) ;
  135.     fflush ( outfile ) ;
  136. }
  137.  
  138.  
  139. static void
  140. write_table ( FILE * outfile ) {
  141.  
  142. int ix ;
  143.  
  144.     fprintf ( outfile , "typedef struct c_info_s {\n" ) ;
  145.     fprintf ( outfile , "    char * c_name ;\n" ) ;
  146.     fprintf ( outfile , "    CObject * ( * c_new ) ( void ) ;\n" ) ;
  147.     fprintf ( outfile , "    int c_sup_i ;\n" ) ;
  148.     fprintf ( outfile , "} C_Info_S ;\n\n" ) ;
  149.  
  150.     fprintf ( outfile , "C_Info_S s_c_info [ ] = {\n" ) ;
  151.     for ( ix = 0 ; ix < numCls ; ix ++ ) {
  152.         fprintf ( outfile , "    \"%s\" , new_%s , %d ,\n" , cList [ ix ] . clsName ,
  153.             cList [ ix ] . clsName , cList [ ix ] . supClass ?
  154.             ( int ) ( cList [ ix ] . supClass - cList ) : -1 ) ;
  155.         if ( ( ix & 0xf ) == 15 ) {
  156.             fflush ( outfile ) ;
  157.         }
  158.     }
  159.     fprintf ( outfile , "} ;\n\n" ) ;
  160. }
  161.  
  162.  
  163. static void
  164. write_new ( FILE * outfile ) {
  165.  
  166. int ix ;
  167.  
  168.     for ( ix = 0 ; ix < numCls ; ix ++ ) {
  169.         fprintf ( outfile , "CObject *\nnew_%s ( void ) {\n    return ( CObject * ) new %s ;\n}\n\n" ,
  170.             cList [ ix ] . clsName , cList [ ix ] . clsName , cList [ ix ] . clsName ) ;
  171.         if ( ( ix & 0xf ) == 15 ) {
  172.             fflush ( outfile ) ;
  173.         }
  174.     }
  175.     fprintf ( outfile , "\n" ) ;
  176. }
  177.  
  178.  
  179. void
  180. write_trailer ( FILE * outfile ) {
  181.  
  182.     fprintf ( outfile , "CObject * _new_by_name ( char * name ) ;\n\n" ) ;
  183.  
  184.     fprintf ( outfile , "CObject *\n" ) ;
  185.     fprintf ( outfile , "_new_by_name ( char * name ) {\n" ) ;
  186.     fprintf ( outfile , "register int ix ;\n" ) ;
  187.     fprintf ( outfile , "    for ( ix = 0 ; ix < %d ; ix ++ ) {\n" , numCls ) ;
  188.     fprintf ( outfile , "        if ( ! strcmp ( s_c_info [ ix ] . c_name , name ) ) {\n" ) ;
  189.     fprintf ( outfile , "            return ( * s_c_info [ ix ] . c_new ) ( ) ;\n" ) ;
  190.     fprintf ( outfile , "        }\n" ) ;
  191.     fprintf ( outfile , "    }\n" ) ;
  192.     fprintf ( outfile , "    return ( CObject * ) NULL ;\n" ) ;
  193.     fprintf ( outfile , "}\n\n" ) ;
  194.  
  195.     fprintf ( outfile , "Boolean _member ( char * clsName , char * name ) ;\n\n" ) ;
  196.  
  197.     fprintf ( outfile , "Boolean\n" ) ;
  198.     fprintf ( outfile , "_member ( char * clsName , char * name ) {\n" ) ;
  199.     fprintf ( outfile , "register int cIndex ;\n" ) ;
  200.     fprintf ( outfile , "    if ( ! strcmp ( clsName , name ) ) {\n" ) ;
  201.     fprintf ( outfile , "        return true ;\n" ) ;
  202.     fprintf ( outfile , "    }\n" ) ;
  203.     fprintf ( outfile , "    for ( cIndex = 0 ; cIndex < %d ; cIndex ++ ) {\n" , numCls ) ;
  204.     fprintf ( outfile , "        if ( ! strcmp ( clsName , s_c_info [ cIndex ] . c_name ) ) {\n" ) ;
  205.     fprintf ( outfile , "            goto hasIt ;\n" ) ;
  206.     fprintf ( outfile , "        }\n" ) ;
  207.     fprintf ( outfile , "    }\n" ) ;
  208.     fprintf ( outfile , "    return false ;\n" ) ;
  209.     fprintf ( outfile , "hasIt :\n" ) ;
  210.     fprintf ( outfile , "    while ( cIndex >= 0 ) {\n" ) ;
  211.     fprintf ( outfile , "        if ( ! strcmp ( name , s_c_info [ cIndex ] . c_name ) ) {\n" ) ;
  212.     fprintf ( outfile , "            return true ;\n" ) ;
  213.     fprintf ( outfile , "        }\n" ) ;
  214.     fprintf ( outfile , "        cIndex = s_c_info [ cIndex ] . c_sup_i ;\n" ) ;
  215.     fprintf ( outfile , "    }\n" ) ;
  216.     fprintf ( outfile , "    return false ;\n" ) ;
  217.     fprintf ( outfile , "}\n\n" ) ;
  218.  
  219.     fflush ( outfile ) ;
  220. }
  221.  
  222.  
  223. static void
  224. write_constr ( FILE * outfile ) {
  225.  
  226. int ix ;
  227.  
  228.     for ( ix = 0 ; ix < numCls ; ix ++ ) {
  229.         fprintf ( outfile , "%s :: %s ( ) {\n" , cList [ ix ] . clsName ,
  230.             cList [ ix ] . clsName ) ;
  231.         fprintf ( outfile , "    _clsName = s_c_info [ %d ] . c_name ;\n" , ix ) ;
  232.         fprintf ( outfile , "}\n\n" ) ;
  233.         if ( ( ix & 0xf ) == 15 ) {
  234.             fflush ( outfile ) ;
  235.         }
  236.     }
  237. }
  238.  
  239.  
  240. void
  241. main ( void ) {
  242.  
  243. FILE * infile = fopen ( " CLASSLIST" , "r" ) ;
  244. FILE * outfile = fopen ( "XFile.cp" , "w" ) ;
  245.  
  246.     if ( ! infile ) {
  247.         fprintf ( stderr , "no CLASSLIST" ) ;
  248.         exit ( 1 ) ;
  249.     }
  250.     if ( ! outfile ) {
  251.         fprintf ( stderr , "can't create XFile.c" ) ;
  252.         exit ( 1 ) ;
  253.     }
  254.     scan_infile ( infile ) ;
  255.     fclose ( infile ) ;
  256.     print_bases ( ) ;
  257.     write_header ( outfile ) ;
  258.     write_new ( outfile ) ;
  259.     write_table ( outfile ) ;
  260.     write_constr ( outfile ) ;
  261.     write_trailer ( outfile ) ;
  262.     fclose ( outfile ) ;
  263.     fprintf ( stderr , "OK" ) ;
  264.     exit ( 0 ) ;
  265. }
  266.