home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / gui / muibuilder21.lha / MUIBuilder / MB / Developer / C / Sources_GenCodeC / GenCodeC.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-24  |  20.1 KB  |  779 lines

  1. #include "gencodec.h"
  2.  
  3. char *version = "$VER: GenCodeC 2.1 (15-11-94)";
  4.  
  5. struct Library * MUIBBase = NULL;
  6.  
  7. /* Global variables */
  8. ULONG    varnb;            /* number of variables */
  9.  
  10. BOOL    Code, Env;        /* flags-options */
  11. BOOL    Locale, Declarations;
  12. BOOL    Notifications;
  13. BOOL    ExternalExist = FALSE;
  14. char    *FileName, *CatalogName;/* Strings */
  15. char    *GetString;
  16. char    *GetMBString;
  17.  
  18. FILE    *file;
  19.  
  20. char    HeaderFile[512];
  21. char    GUIFile[512];
  22. char    MBDir[512];
  23. char    Externals[512];
  24. char    Main[512];
  25.  
  26. /* variable types */
  27. char    *STR_type[] =
  28.     {
  29.         "BOOL",
  30.         "int",
  31.         "char *",
  32.         "char *",
  33.         "APTR",
  34.         "",
  35.         "",
  36.         "",
  37.         "APTR"
  38.     };
  39.  
  40. void Indent(int nb)
  41. {
  42.         int     i;
  43.  
  44.         for(i=0;i<nb;i++) fprintf(file, "\t");
  45. }
  46.  
  47. void WriteDeclarations(int vartype)
  48. {
  49.     int    i;
  50.     char    *varname;
  51.     ULONG    type, size;
  52.     char    *typename;
  53.     int    nb_ident = 1;
  54.     char    buffer[150];
  55.     
  56.     typename = STR_type[ vartype - 1 ];        /* find the name 'BOOL ...'    */
  57.     buffer[0] = '\0';
  58.     for(i=0;i<varnb;i++)
  59.     {
  60.         MB_GetVarInfo (i,
  61.                    MUIB_VarType, &type,
  62.                    MUIB_VarName, &varname,
  63.                    MUIB_VarSize, &size,
  64.                 TAG_END
  65.                  );
  66.         if (type == vartype)
  67.             switch(type)
  68.             {
  69.             case TYPEVAR_TABSTRING:
  70.                 fprintf(file, "\t%s\t%s[%d];\n",
  71.                      typename,
  72.                      varname,
  73.                      size+1
  74.                     );
  75.                 break;
  76.             case TYPEVAR_IDENT:
  77.                 fprintf(file,"#define %s %d\n", varname, nb_ident++);
  78.                 break;
  79.             case TYPEVAR_LOCAL_PTR:
  80.                 if (strlen(buffer)==0) sprintf(buffer, "\t%s\t%s", typename, varname);
  81.                 else
  82.                 {
  83.                     strcat(buffer, ", ");
  84.                     strcat(buffer, varname);
  85.                 }
  86.                 if (strlen(buffer)>=70)
  87.                 {
  88.                     strcat(buffer, ";\n");
  89.                     fprintf(file, "%s", buffer);
  90.                     buffer[0] = '\0';
  91.                 }
  92.             break;
  93.             default:
  94.                 fprintf(file, "\t%s\t%s;\n",typename, varname);
  95.                 break;
  96.             }
  97.     }
  98.     if (strlen(buffer)>0) fprintf(file, "%s;\n", buffer);
  99. }
  100.  
  101. void WriteInitialisations(int vartype)
  102. {
  103.     int    i, j;
  104.     ULONG    type, size;
  105.     char    *inits, *name;
  106.     BOOL    enter = FALSE;
  107.  
  108.     for(i=0;i<varnb;i++)
  109.     {
  110.         MB_GetVarInfo(i,
  111.                    MUIB_VarType    , &type,
  112.                    MUIB_VarName    , &name,
  113.                    MUIB_VarSize    , &size,
  114.                    MUIB_VarInitPtr    , &inits,
  115.                    TAG_END
  116.                  );
  117.         if (type == vartype)
  118.         {
  119.             enter = TRUE;
  120.             switch(type)
  121.             {
  122.             case TYPEVAR_TABSTRING:
  123.                 for(j=0;j<size;j++)
  124.                 {
  125.                     if (!Locale) fprintf(file, "\tObject->%s[%d] = \"%s\";\n", name, j, inits);
  126.                     else        fprintf(file, "\tObject->%s[%d] = %s(%s);\n", name, j, GetMBString, inits);
  127.                     inits = inits + strlen(inits) + 1;
  128.                 }
  129.                 fprintf(file, "\tObject->%s[%d] = NULL;\n", name, j);
  130.                 break;
  131.             case TYPEVAR_STRING:
  132.                  if (*inits != 0)
  133.                 {
  134.                     if (!Locale) fprintf(file, "\tObject->%s = \"%s\";\n", name, inits);
  135.                     else       fprintf(file, "\tObject->%s = %s(%s);\n", name, GetMBString, inits);
  136.                 }
  137.                 else fprintf(file, "\tObject->%s = NULL;\n", name);
  138.                 break;
  139.             case TYPEVAR_HOOK:
  140.                 fprintf(file, "\tstatic const struct Hook %sHook = { { NULL,NULL },(VOID *)%s,NULL,NULL };\n", name, name);
  141.                 break;
  142.             default:
  143.                 break;
  144.             }
  145.         }
  146.     }
  147.     if (enter) fprintf(file, "\n");
  148. }
  149.  
  150. void WriteCode(void)
  151. {
  152.     ULONG    type;
  153.     char*    code;
  154.     BOOL    InFunction     = FALSE;
  155.     BOOL    IndentFunction = TRUE ;
  156.     BOOL    obj_function             ;
  157.     BOOL    InObj              ;
  158.     int    nb_indent      = 1    ;
  159.     int    nb_function    = 0    ;
  160.     int    name;
  161.  
  162.     extern void End(void);
  163.  
  164.     MB_GetNextCode(&type, &code);
  165.     while(type != -1)
  166.     {
  167.         switch(type)
  168.         {
  169.         case TC_CREATEOBJ:
  170.             name = atoi(code);
  171.             fprintf(file, "%s,\n",MUIStrings[name]);
  172.             nb_indent++;
  173.             IndentFunction = TRUE;
  174.             MB_GetNextCode(&type, &code);
  175.             InObj = TRUE;
  176.             break;
  177.         case TC_ATTRIBUT:
  178.             Indent(nb_indent);
  179.             name = atoi(code);
  180.             fprintf(file, "%s, ",MUIStrings[name]);
  181.             IndentFunction = FALSE;
  182.             MB_GetNextCode(&type, &code);
  183.             break;
  184.         case TC_END:
  185.             nb_indent--;
  186.             InObj = FALSE;
  187.             Indent(nb_indent);
  188.             name = atoi(code);
  189.             fprintf(file, "%s",MUIStrings[name]);
  190.             IndentFunction = TRUE;
  191.             MB_GetNextCode(&type, &code);
  192.             fprintf(file, ";\n\n");
  193.             break;
  194.         case TC_MUIARG_OBJFUNCTION:
  195.             if (IndentFunction) Indent(nb_indent);
  196.             nb_function++;
  197.             name = atoi(code);
  198.             fprintf(file, "%s(",MUIStrings[name]);
  199.             IndentFunction = FALSE;
  200.             InFunction     = TRUE;
  201.             MB_GetNextCode(&type, &code);
  202.             obj_function = TRUE;
  203.             InFunction = TRUE;
  204.             break;
  205.         case TC_MUIARG_FUNCTION:
  206.         case TC_FUNCTION:
  207.             if (IndentFunction) Indent(nb_indent);
  208.             nb_function++;
  209.             name = atoi(code);
  210.             fprintf(file, "%s(",MUIStrings[name]);
  211.             IndentFunction = FALSE;
  212.             InFunction     = TRUE;
  213.             MB_GetNextCode(&type, &code);
  214.             obj_function = FALSE;
  215.             break;
  216.         case TC_OBJFUNCTION:
  217.             if (IndentFunction) Indent(nb_indent);
  218.             nb_function++;
  219.             name = atoi(code);
  220.             fprintf(file, "%s(",MUIStrings[name]);
  221.             InFunction     = TRUE;
  222.             IndentFunction = FALSE;
  223.             MB_GetNextCode(&type,&code);
  224.             obj_function = TRUE;
  225.             break;
  226.         case TC_STRING:
  227.             fprintf(file, "\"%s\"",code);
  228.             MB_GetNextCode(&type, &code);
  229.             IndentFunction = TRUE;
  230.             if (InFunction)
  231.             {
  232.                 if (type  != TC_END_FUNCTION) fprintf(file, ", ");
  233.                 IndentFunction = FALSE;
  234.             }
  235.             else    fprintf(file, ",\n");
  236.             break;
  237.         case TC_LOCALESTRING:
  238.             fprintf(file, "%s(%s)",GetMBString, code);
  239.                         MB_GetNextCode(&type, &code);
  240.                         IndentFunction = TRUE;
  241.                         if (InFunction)
  242.                         {
  243.                                 if (type  != TC_END_FUNCTION) fprintf(file, ", ");
  244.                                 IndentFunction = FALSE;
  245.                         }
  246.                         else    fprintf(file, ",\n");
  247.                         break;
  248.         case TC_LOCALECHAR:
  249.             fprintf(file, "%s(%s)[0]",GetString, code);
  250.                         MB_GetNextCode(&type, &code);
  251.                         IndentFunction = TRUE;
  252.                         if (InFunction)
  253.                         {
  254.                                 if (type  != TC_END_FUNCTION) fprintf(file, ", ");
  255.                                 IndentFunction = FALSE;
  256.                         }
  257.                         else    fprintf(file, ",\n");
  258.                         break;
  259.         case TC_INTEGER:
  260.             fprintf(file, "%s", code);
  261.                         MB_GetNextCode(&type, &code);
  262.                         IndentFunction = TRUE;
  263.                         if (InFunction)
  264.                         {
  265.                                 if (type  != TC_END_FUNCTION) fprintf(file, ", ");
  266.                                 IndentFunction = FALSE;
  267.                         }
  268.                         else    fprintf(file, ",\n");
  269.                         break;
  270.         case TC_CHAR:
  271.             fprintf(file, "'%s'",code);
  272.                         MB_GetNextCode(&type, &code);
  273.                         IndentFunction = TRUE;
  274.                         if (InFunction)
  275.                         {
  276.                                 if (type  != TC_END_FUNCTION) fprintf(file, ", ");
  277.                                 IndentFunction = FALSE;
  278.                         }
  279.                         else    fprintf(file, ",\n");
  280.                         break;    
  281.         case TC_VAR_AFFECT:
  282.             name = atoi(code);
  283.             MB_GetVarInfo(name, MUIB_VarName, &code, MUIB_VarType, &type, TAG_END);
  284.             if (type == TYPEVAR_LOCAL_PTR) fprintf( file, "\t%s = ", code);
  285.             else fprintf(file, "\tObject->%s = ", code); 
  286.             IndentFunction = FALSE;
  287.             MB_GetNextCode(&type, &code);
  288.             break;
  289.         case TC_OBJ_ARG:
  290.         case TC_VAR_ARG:
  291.             name = atoi(code);
  292.             MB_GetVarInfo(name, MUIB_VarName, &code, MUIB_VarType, &type, TAG_END);
  293.             if (type == TYPEVAR_LOCAL_PTR) fprintf(file, "%s", code);
  294.             else                   fprintf(file, "Object->%s", code);
  295.             MB_GetNextCode(&type, &code);
  296.             if ((InFunction)&&(type != TC_END_FUNCTION)) fprintf(file, ", ");
  297.             if (!InFunction)
  298.             {
  299.                 fprintf(file, ",\n");
  300.                 IndentFunction = TRUE;
  301.             }
  302.             break;
  303.         case TC_END_FUNCTION:
  304.             MB_GetNextCode(&type, &code);
  305.             if (nb_function>1)
  306.             {
  307.                 if (type != TC_END_FUNCTION) fprintf(file, "),");
  308.                 else                 fprintf(file, ")");
  309.             }    
  310.             else
  311.             {
  312.                 if (obj_function) fprintf(file, ");\n\n");
  313.                 else          fprintf(file, "),\n");
  314.                 IndentFunction = TRUE;
  315.                 InFunction     = FALSE;
  316.                 obj_function   = FALSE;
  317.             }
  318.             nb_function--;
  319.             break;
  320.         case TC_BOOL:
  321.             if (*code == '0')    fprintf(file, "FALSE");
  322.             else            fprintf(file, "TRUE" );
  323.             MB_GetNextCode(&type, &code);
  324.             if (InFunction)
  325.             {
  326.                 if (type != TC_END_FUNCTION)
  327.                 {
  328.                     fprintf(file, ", ");
  329.                     IndentFunction = FALSE;
  330.                 }
  331.             }
  332.             else fprintf(file, ",\n");
  333.             break;
  334.         case TC_MUIARG:
  335.             if (IndentFunction) Indent(nb_indent);
  336.             name = atoi(code);
  337.             fprintf(file, "%s", MUIStrings[name]);
  338.             MB_GetNextCode(&type, &code);
  339.             if (InFunction)
  340.             {
  341.                 if (type != TC_END_FUNCTION)
  342.                 {
  343.                     fprintf(file, ", ");
  344.                     IndentFunction = FALSE;
  345.                 }
  346.             }
  347.             else
  348.             {
  349.                 fprintf(file, ",\n");
  350.                 IndentFunction = TRUE;
  351.             }
  352.             break;
  353.         case TC_MUIARG_ATTRIBUT:
  354.             if (IndentFunction) Indent(nb_indent);
  355.             name = atoi(code);
  356.             MB_GetNextCode(&type, &code);
  357.             if ((InObj)) fprintf(file, "%s,\n", MUIStrings[name]);
  358.             else
  359.             {
  360.                 if (InFunction)
  361.                 {
  362.                     if (type != TC_END_FUNCTION)
  363.                         fprintf(file, "%s,", MUIStrings[name]);
  364.                     else    fprintf(file, "%s", MUIStrings[name]);
  365.                 }
  366.                 else
  367.                 {
  368.                     fprintf(file, "%s;\n\n", MUIStrings[name]);
  369.                 }
  370.             }
  371.             break;
  372.         case TC_MUIARG_OBJ:
  373.             if (IndentFunction) Indent(nb_indent);
  374.             name = atoi(code);
  375.             MB_GetNextCode(&type, &code);
  376.             fprintf(file, "%s;\n\n", MUIStrings[name]);
  377.             break;
  378.         case TC_EXTERNAL_FUNCTION:
  379.             fprintf(file, "&%sHook", code);
  380.             MB_GetNextCode(&type, &code);
  381.             if (InFunction)
  382.             {
  383.                 if (type != TC_END_FUNCTION)    
  384.                 {
  385.                     fprintf(file, ", ");
  386.                     IndentFunction = FALSE;
  387.                 }
  388.             }
  389.             else
  390.             {
  391.                 fprintf(file, ",\n");
  392.                 IndentFunction = TRUE;
  393.             }
  394.             break;
  395.         default:
  396.             printf("Type = %d\n", type);
  397.             printf("ERROR !!!!! THERE IS A PROBLEM WITH THIS FILE !!!\n");
  398.             End();
  399.             exit(1);
  400.             break;
  401.         }
  402.     }
  403. }
  404.  
  405. void WriteNotify(void)
  406. {
  407.     ULONG    type;
  408.     char*    code;
  409.     int    name;
  410.     BOOL    indent = FALSE;
  411.  
  412.     extern void End(void);
  413.  
  414.     fprintf(file, "\n");
  415.     MB_GetNextNotify(&type, &code);
  416.     while(type != -1)
  417.     {
  418.         if (indent) fprintf(file, "\t\t");
  419.         indent = TRUE;
  420.         switch(type)
  421.         {
  422.         case TC_END_FUNCTION:
  423.         case TC_END_NOTIFICATION:
  424.             fprintf(file, ");\n\n");
  425.             MB_GetNextNotify(&type, &code);
  426.             indent = FALSE;
  427.             break;
  428.         case TC_BEGIN_NOTIFICATION:
  429.             name = atoi(code);
  430.             MB_GetVarInfo(name, MUIB_VarName, &code, MUIB_VarType, &type, TAG_END);
  431.             if (type == TYPEVAR_LOCAL_PTR) fprintf(file, "\tDoMethod(%s,\n", code);
  432.             else                           fprintf(file, "\tDoMethod(Object->%s,\n", code);
  433.             MB_GetNextNotify(&type, &code);
  434.             break;
  435.         case TC_FUNCTION:
  436.             name = atoi(code);
  437.             fprintf(file, "\t%s(", MUIStrings[name]);
  438.             MB_GetNextNotify(&type, &code);
  439.             indent = FALSE;
  440.             break;
  441.         case TC_STRING:
  442.             fprintf(file, "\"%s\"",code);
  443.             MB_GetNextNotify(&type, &code);
  444.             if ((type  != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION))  fprintf(file, ",\n");
  445.             else fprintf(file, "\n");
  446.             break;
  447.         case TC_LOCALESTRING:
  448.             fprintf(file, "%s(%s)",GetMBString, code);
  449.                         MB_GetNextNotify(&type, &code);
  450.                         if ((type  != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  451.             else fprintf(file, "\n");
  452.                         break;
  453.         case TC_LOCALECHAR:
  454.             fprintf(file, "%s(%s)[0]\n",GetString, code);
  455.                         MB_GetNextNotify(&type, &code);
  456.                         if ((type  != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  457.             else fprintf(file, "\n");
  458.                         break;
  459.         case TC_INTEGER:
  460.             fprintf(file, "%s", code);
  461.                         MB_GetNextNotify(&type, &code);
  462.                         if ((type  != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  463.             else fprintf(file, "\n");
  464.                         break;
  465.         case TC_CHAR:
  466.             fprintf(file, "'%s'",code);
  467.                         MB_GetNextNotify(&type, &code);
  468.                         if ((type  != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  469.             else fprintf(file, "\n");
  470.                         break;    
  471.         case TC_VAR_ARG:
  472.             name = atoi(code);
  473.             MB_GetVarInfo(name, MUIB_VarName, &code, MUIB_VarType, &type, TAG_END);
  474.             if (type == TYPEVAR_LOCAL_PTR) fprintf(file, "%s", code);
  475.             else                           fprintf(file, "Object->%s", code);
  476.             MB_GetNextNotify(&type, &code);
  477.             if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  478.             else fprintf(file, "\n");
  479.             break;
  480.         case TC_BOOL:
  481.             if (*code == '0')    fprintf(file, "FALSE");
  482.             else            fprintf(file, "TRUE" );
  483.             MB_GetNextNotify(&type, &code);
  484.             if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  485.             else fprintf(file, "\n");
  486.             break;
  487.         case TC_MUIARG:
  488.         case TC_MUIARG_OBJ:
  489.             name = atoi(code);
  490.             fprintf(file, "%s", MUIStrings[name]);
  491.             MB_GetNextNotify(&type, &code);
  492.             if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ", ");
  493.             indent = FALSE;
  494.             break;
  495.         case TC_MUIARG_ATTRIBUT:
  496.             name = atoi(code);
  497.             fprintf(file, "%s", MUIStrings[name]);
  498.             MB_GetNextNotify(&type, &code);
  499.             if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  500.             else fprintf(file, "\n");
  501.             break;
  502.         case TC_EXTERNAL_CONSTANT:
  503.                         fprintf(file, "%s", code);
  504.                         MB_GetNextNotify(&type, &code);
  505.                         if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  506.             else fprintf(file, "\n");
  507.                         break;
  508.         case TC_EXTERNAL_FUNCTION:
  509.             fprintf(file, "&%sHook", code);
  510.             MB_GetNextNotify(&type, &code);
  511.                         if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  512.             else fprintf(file, "\n");
  513.             break;
  514.         case TC_EXTERNAL_VARIABLE:
  515.             fprintf(file, "%s", code);
  516.             MB_GetNextNotify(&type, &code);
  517.                         if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  518.             else fprintf(file, "\n");
  519.             break;
  520.         default:
  521.             printf("Type = %d\n", type);
  522.             printf("ERROR !!!!! THERE IS A PROBLEM WITH THIS FILE !!!\n");
  523.             End();
  524.             exit(1);
  525.             break;
  526.         }
  527.     }
  528. }
  529.  
  530. void Init(void)
  531. {
  532.     BPTR    lock;
  533.  
  534.     /* Get all needed variables */
  535.     MB_Get    (
  536.         MUIB_VarNumber        , &varnb,
  537.         MUIB_Code        , &Code,
  538.         MUIB_Environment    , &Env,
  539.         MUIB_Locale        , &Locale,
  540.         MUIB_Notifications    , &Notifications,
  541.         MUIB_Declarations    , &Declarations,
  542.         MUIB_FileName        , &FileName,
  543.         MUIB_CatalogName    , &CatalogName,
  544.         MUIB_GetStringName    , &GetString,
  545.         TAG_END
  546.         );
  547.  
  548.     /* Create 'GetMBString' name */
  549.     if (strcmp(GetString, "GetMBString") == 0) GetMBString  = "GetMBString2";
  550.     else                       GetMBString = "GetMBString";
  551.  
  552.     /* Create File Names */
  553.     remove_extend(FileName);
  554.     strncpy(GUIFile, FileName, 512);
  555.     add_extend(GUIFile, ".c");
  556.     strncpy(HeaderFile, FileName, 512);
  557.     add_extend(HeaderFile, ".h");
  558.     strncpy(Externals, FileName, 512);
  559.     strncat(Externals, "Extern", 512);
  560.     add_extend(Externals, ".h");
  561.     strncpy(Main, FileName, 512);
  562.     strncat(Main, "Main", 512);
  563.     add_extend(Main, ".c");
  564.  
  565.  
  566.     /* Get Current Directory Name */
  567.     lock = Lock("PROGDIR:", ACCESS_READ);
  568.     NameFromLock(lock, MBDir, 512);
  569.     UnLock(lock);
  570. }
  571.  
  572. void WriteHeaderFile(void)
  573. {
  574.     char    *name;
  575.     char    buffer[600];
  576.     char    buffer2[600];
  577.  
  578.     if (Env)
  579.     {
  580.         strncpy(buffer, MBDir, 600);
  581.         AddPart(buffer, "H-Header", 512);
  582.         sprintf(buffer2, "copy \"%s\" \"%s\"", buffer, HeaderFile);
  583.         Execute(buffer2,0,0);
  584.     }
  585.     else   DeleteFile(HeaderFile);
  586.     file = fopen(HeaderFile, "a+");
  587.     if (file)
  588.     {
  589.         MB_GetVarInfo(0, MUIB_VarName, &name, TAG_END);
  590.         fprintf(file, "struct Obj%s\n{\n", name);
  591.         WriteDeclarations(TYPEVAR_PTR        );
  592.         WriteDeclarations(TYPEVAR_BOOL       );
  593.         WriteDeclarations(TYPEVAR_INT        );
  594.         WriteDeclarations(TYPEVAR_STRING   );
  595.         WriteDeclarations(TYPEVAR_TABSTRING);
  596.         fprintf(file, "};\n\n");
  597.         if (Notifications)
  598.         {
  599.             WriteDeclarations(TYPEVAR_IDENT    );
  600.             fprintf(file, "\n");
  601.         }
  602.         if (Env)
  603.         {
  604.             fprintf(file,"extern struct Obj%s * Create%s(void);\n", name, name);
  605.             fprintf(file,"extern void Dispose%s(struct Obj%s *);\n", name, name);
  606.         }
  607.         fclose(file);
  608.     }
  609. }
  610.  
  611. void WriteGUIFile(void)
  612. {
  613.     char    buffer[600];
  614.     char    buffer2[600];
  615.     char    *name;
  616.  
  617.     if (Env)
  618.     {
  619.         strncpy(buffer, MBDir, 600);
  620.         AddPart(buffer, "C-Header", 512);
  621.         sprintf(buffer2, "copy \"%s\" \"%s\"", buffer, GUIFile);
  622.         Execute(buffer2,0,0);
  623.     }
  624.     else DeleteFile(GUIFile);
  625.     if (file = fopen(GUIFile, "a+"))
  626.       {
  627.         if (Env)
  628.           {
  629.         MB_GetVarInfo(0, MUIB_VarName, &name, TAG_END);
  630.         fprintf(file, "\n#include \"%s\"\n", FilePart(HeaderFile));
  631.         if (ExternalExist) fprintf(file, "#include \"%s\"\n\n", FilePart(Externals));
  632.         if (Locale)
  633.           {
  634.             remove_extend( CatalogName );
  635.             fprintf(file, "#include \"%s_cat.h\"\n\n", FilePart(CatalogName) );
  636.             fprintf(file, "extern char* %s(int);\n", GetString);
  637.             fprintf(file, "\nstatic char *%s(int ref)\n{\n", GetMBString);
  638.             fprintf(file, "\tchar *aux;\n\n");
  639.             fprintf(file, "\taux = %s(ref);\n", GetString);
  640.             fprintf(file, "\tif (aux[1] == '\\0') return(&aux[2]);\n");
  641.             fprintf(file, "\telse                return(aux);\n}\n");
  642.           }
  643.         fprintf(file, "\nstruct Obj%s * Create%s(void)\n", name, name);
  644.         fprintf(file, "{\n\tstruct Obj%s * Object;\n\n", name);
  645.           }
  646.         if (Declarations)
  647.           {
  648.         WriteDeclarations   (TYPEVAR_LOCAL_PTR);
  649.         WriteInitialisations(TYPEVAR_HOOK    );
  650.           }
  651.         if (Env) fprintf(file, "\n\tif (!(Object = AllocVec(sizeof(struct Obj%s), MEMF_PUBLIC|MEMF_CLEAR)))\n\t\treturn(NULL);\n", name);
  652.         if (Declarations)
  653.           {
  654.         WriteInitialisations(TYPEVAR_PTR      );
  655.         WriteInitialisations(TYPEVAR_BOOL     );
  656.         WriteInitialisations(TYPEVAR_INT      );
  657.         WriteInitialisations(TYPEVAR_STRING   );
  658.         WriteInitialisations(TYPEVAR_TABSTRING);
  659.           }
  660.         if (Code) WriteCode();
  661.         if (Env)
  662.           {
  663.         fprintf(file, "\n\tif (!Object->%s)\n\t{\n\t\tFreeVec(Object);", name);
  664.         fprintf(file, "\n\t\treturn(NULL);\n\t}\n");
  665.           }
  666.         if (Notifications)
  667.           {
  668.         WriteNotify();
  669.           }
  670.         if (Env)
  671.           {
  672.         fprintf(file, "\n\treturn(Object);\n}\n");
  673.         fprintf(file, "\nvoid Dispose%s(struct Obj%s * Object)\n{\n", name, name);
  674.         fprintf(file, "\tMUI_DisposeObject(Object->%s);\n", name);
  675.         fprintf(file, "\tFreeVec(Object);\n}\n");
  676.           }
  677.         fclose(file);
  678.       }
  679.     else printf("Unable to open GUI-File !\n");
  680. }
  681.  
  682. /* Create a file where are the external variables and functions declarations */
  683. BOOL WriteExternalFile( void )
  684. {
  685.     int    i;
  686.     ULONG    length, type;
  687.     BPTR    TMPfile;
  688.     char    *adr_file = NULL;
  689.     __aligned struct  FileInfoBlock   Info;
  690.     BOOL    bool_aux = FALSE;
  691.     char    *varname;
  692.     BOOL    result = FALSE;
  693.  
  694.     /* If the file already exists, we load it in memory */
  695.     if (TMPfile = Open(Externals, MODE_OLDFILE))
  696.     {
  697.         ExamineFH(TMPfile, &Info);
  698.         length = Info.fib_Size;
  699.         adr_file = AllocVec(length+1, MEMF_PUBLIC|MEMF_CLEAR);
  700.         Read( TMPfile, adr_file, length);
  701.         adr_file[length] = '\0';
  702.         Close(TMPfile);
  703.     }
  704.     if (file = fopen(Externals, "a+"))
  705.     {
  706.         for(i=0;i<varnb;i++)
  707.         {
  708.             MB_GetVarInfo (i,
  709.                        MUIB_VarType, &type,
  710.                        MUIB_VarName, &varname,
  711.                     TAG_END
  712.                      );
  713.             switch(type)    /* if the declaration doesn't exist, we generate it */
  714.             {
  715.             case TYPEVAR_EXTERNAL:
  716.                 if (adr_file) bool_aux = (strstr(adr_file,varname)!=NULL);
  717.                 if (!bool_aux) fprintf(file, "extern int %s;\n", varname);
  718.                 break;
  719.             case TYPEVAR_HOOK:
  720.                 if (adr_file) bool_aux = (strstr(adr_file,varname)!=NULL);
  721.                 if (!bool_aux) fprintf(file, "extern void %s( Object* );\n", varname);
  722.                 break;
  723.             }
  724.         }
  725.         fclose(file);
  726.     }
  727.     if (adr_file) FreeVec(adr_file);
  728.     if (TMPfile = Open(Externals, MODE_OLDFILE))    /* if the file is 0 bytes long : we remove it */
  729.     {
  730.         ExamineFH(TMPfile, &Info);
  731.         Close(TMPfile);
  732.         length = Info.fib_Size;
  733.         if (length == 0) DeleteFile(Externals);
  734.         else         result = TRUE;
  735.     }
  736.     return(result);
  737. }
  738.  
  739. void End(void)
  740. {
  741.     MB_Close();        /* Free Memory and Close Temporary Files */
  742.     if (MUIBBase) CloseLibrary(MUIBBase);    /* Close Library */
  743.     if (DOSBase) CloseLibrary(DOSBase);
  744. }
  745.  
  746. int main()
  747. {
  748.     /* Open MUIBuilder Library */
  749.     MUIBBase = OpenLibrary("muibuilder.library", 0);
  750.  
  751.     /* Open Dos Library */
  752.     DOSBase  = OpenLibrary("dos.library", 0);
  753.  
  754.     /* exit if it can't open */
  755.     if ((!MUIBBase)||(!DOSBase))
  756.     {
  757.         printf("Unable to open a library\n");
  758.         exit(20);
  759.     }
  760.  
  761.     /* exit if we can't init the Library */
  762.     if (!MB_Open())
  763.     {
  764.         printf("Unable to Get Temporary files !\n");
  765.         End();
  766.         exit(20);
  767.     }
  768.  
  769.     Init();
  770.  
  771.     if (Declarations)     WriteHeaderFile();
  772.     if (Env)         ExternalExist = WriteExternalFile();
  773.                 WriteGUIFile();
  774.  
  775.     End();
  776.  
  777.     exit(0);
  778. }
  779.