home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gui / GenCodeC2_2d.lha / GenCodeC2.2d / source / GenCodeC.c next >
Encoding:
C/C++ Source or Header  |  1997-01-22  |  8.0 KB  |  305 lines

  1. #include "Tools.h"
  2. #include "WriteCatalogFiles.h"
  3. #include "WriteExternalFile.h"
  4. #include "WriteGUIFiles.h"
  5. #include "WriteMainFile.h"
  6. #include "GenCodeCGUI.h"
  7. #include <libraries/mui.h>
  8.  
  9. #include <ctype.h>
  10.  
  11. /* Prototypes */
  12. #ifdef __SASC
  13. #include <clib/alib_protos.h>
  14. #include <clib/exec_protos.h>
  15. #include <clib/dos_protos.h>
  16. #else
  17. #include <proto/alib.h>
  18. #include <proto/exec.h>
  19. #include <proto/dos.h>
  20. #endif
  21. #include <exec/memory.h>
  22.  
  23. /* ANSI */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27.  
  28. /* Pragmas */
  29. #include <pragmas/dos_pragmas.h>
  30.  
  31. /* MUIBuilder library */
  32. #include "MB.h"
  33. #include "MB_pragmas.h"
  34. #include "MB_protos.h"
  35.  
  36. #ifdef __SASC
  37. extern struct Library * DOSBase;
  38. #endif /* __SASC */
  39.  
  40. /****************************************************************************************************************/
  41. /*****                                                                                                        *****/
  42. /**                                                Global variables                                                **/
  43. /*****                                                                                                        *****/
  44. /****************************************************************************************************************/
  45.  
  46. ULONG    varnb;                    /* number of variables */
  47.  
  48. BOOL    Code, Env;                /* flags-options */
  49. BOOL    Locale, Declarations;
  50. BOOL    Notifications, Application;
  51. BOOL    ExternalExist = FALSE;
  52. char    *FileName, *CatalogName;/* Strings */
  53. char    *GetString;
  54. char    *GetMBString;
  55.  
  56. char    *HeaderFile;
  57. char    *GUIFile;
  58. char    *MBDir;
  59. char    *Externals;
  60. char    *MainFile;
  61. char    *Catalog_h_File;
  62. char    *Catalog_c_File;
  63.  
  64. extern void end(void *);
  65.  
  66. void Quit(void *);
  67.  
  68. /****************************************************************************************************************/
  69. /*****                                                                                                        *****/
  70. /**                                                 Init                                                           **/
  71. /*****                                                                                                        *****/
  72. /****************************************************************************************************************/
  73.  
  74. BOOL Init(void)
  75. {
  76.     HeaderFile         = NULL;
  77.     GUIFile         = NULL;
  78.     MBDir            = NULL;
  79.     Externals         = NULL;
  80.     MainFile        = NULL;
  81.     Catalog_h_File    = NULL;
  82.     Catalog_c_File    = NULL;
  83.  
  84.     /* Get all needed variables */
  85.     MB_Get    (
  86.             MUIB_VarNumber        , &varnb,
  87.             MUIB_Code            , &Code,
  88.             MUIB_Environment    , &Env,
  89.             MUIB_Locale            , &Locale,
  90.             MUIB_Notifications    , &Notifications,
  91.             MUIB_Declarations    , &Declarations,
  92.             MUIB_Application    , &Application,
  93.             MUIB_FileName        , &FileName,
  94.             MUIB_CatalogName    , &CatalogName,
  95.             MUIB_GetStringName    , &GetString,
  96.             TAG_END
  97.         );
  98.  
  99.     /* Verify some varaiables*/
  100.     if (*FileName=='\0')
  101.     {
  102.         DisplayMsg("Please give a name to your application in the field \"CODE\"\n");
  103.         return FALSE;
  104.     }
  105.     if (Locale && *CatalogName=='\0')
  106.     {
  107.         DisplayMsg("Please give a catalog name in the field \"CATALOG\"\n");
  108.         return FALSE;
  109.     }
  110.     if (Locale && *GetString=='\0')
  111.     {
  112.         DisplayMsg("Please give a \"GetString\" name in the field \"GetString\"\n");
  113.         return FALSE;
  114.     }
  115.  
  116.     /* Create 'GetMBString' name */
  117.     if (strcmp(GetString, "GetMBString") == 0) 
  118.         GetMBString  = "GetMBString2";
  119.     else
  120.         GetMBString = "GetMBString";
  121.  
  122.     /* Create File Names */
  123.     remove_extend(FileName);
  124.  
  125.     GUIFile = AllocMemory(strlen(FileName)+6,TRUE);
  126.     strcpy(GUIFile, FileName);
  127.     add_extend(GUIFile, "GUI.c");
  128.  
  129.     HeaderFile = AllocMemory(strlen(FileName)+6,TRUE);
  130.     strcpy(HeaderFile, FileName);
  131.     add_extend(HeaderFile, "GUI.h");
  132.  
  133.     Externals = AllocMemory(strlen(FileName)+9,TRUE);
  134.     strcpy(Externals, FileName);
  135.     strcat(Externals, "Extern");
  136.     add_extend(Externals, ".h");
  137.  
  138.     MainFile = AllocMemory(strlen(FileName)+7,TRUE);
  139.     strcpy(MainFile, FileName);
  140.     strcat(MainFile, "Main");
  141.     add_extend(MainFile, ".c");
  142.  
  143.     Catalog_h_File = AllocMemory(strlen(FileName)+7,TRUE);
  144.     strcpy(Catalog_h_File, FileName);
  145.     strcat(Catalog_h_File, "_cat");
  146.     add_extend(Catalog_h_File, ".h");
  147.  
  148.     Catalog_c_File = AllocMemory(strlen(FileName)+7,TRUE);
  149.     strcpy(Catalog_c_File, FileName);
  150.     strcat(Catalog_c_File, "_cat");
  151.     add_extend(Catalog_c_File, ".c");
  152.  
  153.     /* Get Current Directory Name */
  154.     MBDir = GetCurrentDirectory();
  155.  
  156.     return TRUE;
  157. }
  158.  
  159.  
  160. /****************************************************************************************************************/
  161. /*****                                                                                                        *****/
  162. /**                                             FreeFileNameMemory                                                   **/
  163. /*****                                                                                                        *****/
  164. /****************************************************************************************************************/
  165.  
  166. void FreeFileNameMemory(void)
  167. {
  168.     FreeMemory(HeaderFile);
  169.     FreeMemory(GUIFile);
  170.     FreeMemory(Externals);
  171.     FreeMemory(MainFile);
  172.     FreeMemory(MBDir);
  173.     FreeMemory(Catalog_h_File);
  174.     FreeMemory(Catalog_c_File);
  175. }
  176.  
  177. /****************************************************************************************************************/
  178. /*****                                                                                                        *****/
  179. /**                                                 Quit                                                           **/
  180. /*****                                                                                                        *****/
  181. /****************************************************************************************************************/
  182.  
  183. void Quit(void *App)
  184. {
  185.     FreeFileNameMemory();
  186.     end(App);
  187. }
  188.  
  189. /****************************************************************************************************************/
  190. /*****                                                                                                        *****/
  191. /**                                            PrintInfo                                                           **/
  192. /*****                                                                                                        *****/
  193. /****************************************************************************************************************/
  194. void PrintInfo(struct ObjApp *App,char *str)
  195. {
  196.     char *msg;
  197.  
  198.     if ((msg = (char *)AllocMemory(9+strlen(str)+4+1,FALSE)))
  199.     {
  200.         sprintf(msg,"Generate %s ...",str);
  201.         set(App->TX_Prg_Name,MUIA_Text_Contents,msg);
  202.         FreeMemory(msg);
  203.     }
  204. }
  205.  
  206. /****************************************************************************************************************/
  207. /*****                                                                                                        *****/
  208. /**                                             GenerateCode                                                       **/
  209. /*****                                                                                                        *****/
  210. /****************************************************************************************************************/
  211.  
  212. void GenerateCode(struct ObjApp *App,char *H_Header_Text,char *C_Header_Text,char *Main_Header_Text)
  213. {
  214.     ULONG                Main;
  215.     BPTR                lock;
  216.     TypeQuitFunction    quit_f;
  217.     void                *data;
  218.  
  219.     data   = SetDataQuit((void *)App);
  220.     quit_f = SetFunctionQuit(Quit);
  221.  
  222.     if (!Init())
  223.         Quit((void *)App);
  224.  
  225.     /* test catalog description file if locale */
  226.     if (Locale)
  227.     {
  228.         if (!(lock=Lock(CatalogName,ACCESS_READ)))
  229.         {
  230.             DisplayMsg("The catalog description file doesn't exist !!\nPlease generate it with MUIBuilder");
  231.             Quit((void *)App);
  232.         }
  233.         UnLock(lock);
  234.     }
  235.  
  236.     if (Declarations)
  237.     {
  238.         PrintInfo(App,FilePart(HeaderFile));
  239.         WriteHeaderFile(HeaderFile,H_Header_Text,FileName,varnb,Env,Notifications,Locale);
  240.     }
  241.  
  242.     if (Env)
  243.     {
  244.         PrintInfo(App,FilePart(Externals));
  245.         ExternalExist = WriteExternalFile(Externals,varnb);
  246.     }
  247.  
  248.     PrintInfo(App,FilePart(GUIFile));
  249.     WriteGUIFile(MBDir,HeaderFile,GUIFile,C_Header_Text,Externals,GetString,GetMBString,varnb,
  250.                  ExternalExist,Env,Locale,Declarations,Code,Notifications);
  251.  
  252.     get(App->CH_Generate_Main_File,MUIA_Selected,&Main);
  253.     if (Main)
  254.     {
  255.         PrintInfo(App,FilePart(MainFile));
  256.         WriteMainFile(HeaderFile,MainFile,Main_Header_Text,varnb,Locale);
  257.     }
  258.  
  259.     if (Locale)
  260.     {
  261.         ULONG    Catalog;
  262.  
  263.         get(App->CH_Add_new_entries_in_Catalog_Description_File,
  264.             MUIA_Selected,&Catalog);
  265.         if (Catalog)
  266.         {
  267.             char    *command;
  268.  
  269.             command=AllocMemory(strlen(MBDir)+1+25+15+strlen(CatalogName)+6+strlen(GetString)+1,TRUE);
  270.  
  271.             strcpy(command,MBDir);
  272.             AddPart(command,"WriteCatalog/WriteCatalog",strlen(MBDir)+1+25+1);
  273.             if (lock = Lock(command, ACCESS_READ))
  274.             {
  275.                 UnLock(lock);
  276.                 strcat(command," Reserved CDN \"");
  277.                 strcat(command,CatalogName);
  278.                 strcat(command,"\" GSN ");
  279.                 strcat(command,GetString);
  280.                 if (!Execute(command,NULL,NULL))
  281.                     DisplayMsg("Can't launch WriteCatalog !!!");
  282.             }
  283.             else
  284.                 DisplayMsg("Can't find WriteCatalog !!!");
  285.  
  286.             FreeMemory(command);
  287.         }
  288.  
  289.         PrintInfo(App,FilePart(Catalog_h_File));
  290.         if (!Write_Catalog_h_File(Catalog_h_File,CatalogName,GetString))
  291.             DisplayMsg("Can't Create H Catalog File");
  292.  
  293.         PrintInfo(App,FilePart(Catalog_c_File));
  294.         if (!Write_Catalog_c_File(Catalog_c_File,CatalogName,GetString))
  295.             DisplayMsg("Can't Create C Catalog File");
  296.     }
  297.  
  298.     set(App->TX_Prg_Name,MUIA_Text_Contents,"");
  299.  
  300.     FreeFileNameMemory();
  301.     
  302.     SetDataQuit(data);
  303.     SetFunctionQuit(quit_f);
  304. }
  305.