home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / MUI / MUIBuilder22.lha / MUIBuilder / MB / Developer / C / Sources_GenCodeC / GenCodeC.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-03  |  20.7 KB  |  809 lines

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