home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gui / GenCodeC2_2d.lha / GenCodeC2.2d / source / WriteCatalog / WriteCatalogMain.c < prev   
Encoding:
C/C++ Source or Header  |  1997-01-22  |  9.4 KB  |  417 lines

  1. /* Main-Header File inserted by GenCodeC */
  2. /* Libraries */
  3. #include <libraries/mui.h>
  4. #include <libraries/gadtools.h> /* for BARLABEL in MenuItem */
  5.  
  6. /* Prototypes */ 
  7. #ifdef __SASC      
  8. #include <clib/muimaster_protos.h>
  9. #include <clib/exec_protos.h>
  10. #include <clib/alib_protos.h>
  11. #include <clib/dos_protos.h>
  12. #else
  13. #include <proto/muimaster.h>
  14. #include <proto/exec.h>
  15. #include <proto/alib.h>
  16. #include <proto/dos.h>
  17. #endif /* __SASC */
  18.  
  19. /*  Pragmas  */
  20. #include <pragmas/muimaster_pragmas.h>
  21. #include <pragmas/textfield_pragmas.h>
  22.  
  23. /*  Ansi  */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. /* GenCodeC header end */
  27.  
  28. /* Include generated by GenCodeC */
  29. #include "WriteCatalogGUI.h"
  30.  
  31. #include <clib/asl_protos.h>
  32. #include <string.h>
  33.  
  34. #include "Tools.h"
  35. #include "TextField.h"
  36. #include "WriteCatalogFiles.h"
  37.  
  38. struct ObjTextField     *TF_Catalog = NULL;
  39. BOOL                    NormalEnd = FALSE;
  40.  
  41. /* Declarations for libraries (inserted by GenCodeC) */
  42. struct Library * MUIMasterBase;
  43.  
  44. struct Library * IntuitionBase = NULL;
  45. struct Library * TextFieldBase = NULL;
  46.  
  47. /* Init() function */
  48. void init( void )
  49. {
  50.     if (!(IntuitionBase = OpenLibrary("intuition.library",36)))
  51.     {
  52.         exit(20);
  53.     }
  54.  
  55.     if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
  56.     {
  57.         DisplayMsg("Can't Open MUIMaster Library\n");
  58.         CloseLibrary(IntuitionBase);
  59.         exit(20);
  60.     }
  61.  
  62.     if (!(TextFieldBase = OpenLibrary("gadgets/textfield.gadget", 0)))
  63.     {
  64.         DisplayMsg("Can't Open Textfield gadget\n");
  65.         CloseLibrary(MUIMasterBase);
  66.         CloseLibrary(IntuitionBase);
  67.         exit(20);
  68.     }
  69. }
  70. /* GenCodeC init() end */
  71.  
  72. /* End() function */
  73. void end(void *App )
  74. {
  75.     struct ObjApp * app = App;
  76.  
  77.     if (app)
  78.     {
  79.         set(app->WI_WriteCatalog,
  80.             MUIA_Window_Open,FALSE);
  81.  
  82.         if (TF_Catalog && TF_Catalog->textfield)
  83.             DoMethod(app->GR_Text, OM_REMOVE, TF_Catalog->textfield);
  84.  
  85.         DisposeTextField(TF_Catalog);
  86.  
  87.         DisposeApp((struct ObjApp *)App);
  88.     }
  89.  
  90.     CloseAllFiles(!NormalEnd);
  91.     ClearMemory(!NormalEnd);
  92.  
  93.     CloseLibrary(TextFieldBase);
  94.     CloseLibrary(MUIMasterBase);
  95.     CloseLibrary(IntuitionBase);
  96.     exit(20);
  97. }
  98. /* GenCodeC end() end */
  99.  
  100. /* Get a Catalog Description File Name with a File Requester */
  101. BOOL GetFile(STRPTR *FileName)
  102. {
  103.     struct Library            *AslBase;
  104.     struct FileRequester    *FileRequest;
  105.  
  106.     if (!(AslBase = OpenLibrary(AslName,0)))
  107.     {
  108.         DisplayMsg("Can't Open Asl Library\n");
  109.         return FALSE;
  110.     }
  111.  
  112.     if (!(FileRequest=AllocAslRequestTags(ASL_FileRequest,
  113.                                           ASLFR_TitleText,"Choose a file",
  114.                                           ASLFR_InitialDrawer,"PROGDIR:",
  115.                                           TAG_DONE)))
  116.     {
  117.         DisplayMsg("Can't alloc a FileRequester \n");
  118.         CloseLibrary(AslBase);
  119.         return FALSE;
  120.     }
  121.  
  122.     if (AslRequest(FileRequest,NULL) &&
  123.         strcmp(FileRequest->fr_File,""))
  124.     {
  125.         if (strlen(FileRequest->fr_File)>=3 &&
  126.             FileRequest->fr_File[strlen(FileRequest->fr_File)-3]=='.' &&
  127.             FileRequest->fr_File[strlen(FileRequest->fr_File)-2]=='c' &&
  128.             FileRequest->fr_File[strlen(FileRequest->fr_File)-1]=='d')
  129.         {
  130.             if (!(*FileName=AllocMemory(strlen(FileRequest->fr_Drawer)+1+
  131.                                         strlen(FileRequest->fr_File)+1,FALSE)))
  132.             {
  133.                 FreeAslRequest(FileRequest);
  134.                 CloseLibrary(AslBase);
  135.                 return FALSE;
  136.             }
  137.             strcpy(*FileName,FileRequest->fr_Drawer);
  138.             AddPart(*FileName,FileRequest->fr_File,strlen(FileRequest->fr_Drawer)+1+
  139.                                                    strlen(FileRequest->fr_File)+1);
  140.             FreeAslRequest(FileRequest);
  141.             CloseLibrary(AslBase);
  142.             return TRUE;
  143.         }
  144.         else
  145.         {
  146.             char *msg;
  147.  
  148.             if (!(msg=AllocMemory(strlen(FileRequest->fr_File)+38+1,FALSE)))
  149.             {
  150.                 FreeAslRequest(FileRequest);
  151.                 CloseLibrary(AslBase);
  152.                 return FALSE;
  153.             }
  154.             sprintf(msg,"%s is not a Catalog Description File !!\n",FileRequest->fr_File);
  155.             DisplayMsg(msg);
  156.             FreeMemory(msg);
  157.             FreeAslRequest(FileRequest);
  158.             CloseLibrary(AslBase);
  159.             return FALSE;
  160.         }
  161.     }
  162.     else
  163.     {
  164.         FreeAslRequest(FileRequest);
  165.         CloseLibrary(AslBase);
  166.         return FALSE;
  167.     }
  168. }
  169.  
  170. /* Main Function inserted by GenCodeC */
  171. int main(int argc,char **argv)
  172. {
  173.     struct ObjApp             *App = NULL;    /* Object */
  174.     BOOL                    running = TRUE;
  175.     ULONG                    signal;
  176.     LONG                    Args[3]={0L,0L,0L};
  177.     struct RDArgs             *result;
  178.     char                    *CatalogName;
  179.     char                    *GetString = NULL;
  180.     char                    *CatalogText;
  181.     BOOL                    NoGenerate=FALSE;
  182.  
  183.     if (!(result=ReadArgs("CDN=CatalogDescriptionName/K,GSN=GetStringName/K,Reserved/S",Args,NULL)))
  184.     {
  185.         printf("Error\n");
  186.         printf("Usage : WriteCatalog CDN=CatalogDescriptionName/A/K GSN=GetStringName/A/K\n");
  187.         exit(20);
  188.     }
  189.  
  190.     /* test reserved argument */
  191.     NoGenerate=((Args[2]!=0) ? TRUE : FALSE);
  192.  
  193.     /* test CatalogDescriptionName */
  194.     if (Args[0]==0)
  195.     {
  196.         if (!GetFile((STRPTR *)&CatalogName))
  197.         {
  198.             FreeArgs(result);
  199.             exit(20);
  200.         }
  201.     }
  202.     else
  203.     {
  204.         if (!(CatalogName=AllocMemory(strlen((char *)Args[0])+1,FALSE)))
  205.         {
  206.             FreeArgs(result);
  207.             exit(20);
  208.         }
  209.         strcpy(CatalogName,(char *)Args[0]);
  210.         
  211.         if (strlen(CatalogName)<3 ||
  212.             CatalogName[strlen(CatalogName)-3]!='.' ||
  213.             CatalogName[strlen(CatalogName)-2]!='c' ||
  214.             CatalogName[strlen(CatalogName)-1]!='d')
  215.         {
  216.             char *msg;
  217.  
  218.             if (!(msg=AllocMemory(strlen(CatalogName)+38+1,FALSE)))
  219.             {
  220.                 FreeMemory(CatalogName);
  221.                 FreeArgs(result);
  222.                 exit(20);
  223.             }
  224.             sprintf(msg,"%s is not a Catalog Description File !!\n",CatalogName);
  225.             DisplayMsg(msg);
  226.             FreeMemory(msg);
  227.             FreeMemory(CatalogName);
  228.             FreeArgs(result);
  229.             exit(20);
  230.         }
  231.     }
  232.     
  233.     /* test GetString Name */
  234.     if (Args[1]!=0)
  235.     {
  236.         if (!(GetString=AllocMemory(strlen((char *)Args[1])+1,FALSE)))
  237.         {
  238.             FreeMemory(CatalogName);
  239.             FreeArgs(result);
  240.             exit(20);
  241.         }
  242.         strcpy(GetString,(char *)Args[1]);
  243.     }
  244.  
  245.     FreeArgs(result);
  246.  
  247.     /* Program initialisation : generated by GenCodeC */
  248.     init();
  249.  
  250.     /* Create Object : generated by GenCodeC */
  251.     if (!(App = CreateApp()))
  252.     {
  253.         DisplayMsg("Can't Create App\n");
  254.         end(NULL);
  255.     }
  256.     
  257.     SetDataQuit((void *)App);
  258.     SetFunctionQuit(end);    
  259.  
  260.     /* Create and fill a textfield object */
  261.     if (!(TF_Catalog = CreateTextField()))
  262.     {
  263.         DisplayMsg("Can't Create Textfield object\n");
  264.         end((void *)App);
  265.     }
  266.     DoMethod(App->GR_Text, OM_ADDMEMBER, TF_Catalog->textfield);
  267.     CatalogText = LoadFileInRAM(CatalogName,FALSE);
  268.     if (CatalogText)
  269.     {
  270.         set(TF_Catalog->text,TEXTFIELD_Text,CatalogText);
  271.         FreeMemory(CatalogText);
  272.     }
  273.  
  274.     set(App->STR_GetStringName,MUIA_String_Contents,GetString);
  275.  
  276.     if (NoGenerate)
  277.     {
  278.         set(App->BT_GenerateFiles,MUIA_ShowMe,FALSE);
  279.         set(App->GR_GetStringName,MUIA_ShowMe,FALSE);
  280.     }
  281.  
  282.     set(App->WI_WriteCatalog,
  283.         MUIA_Window_Open,TRUE);
  284.  
  285.     while (running)
  286.     {
  287.         switch (DoMethod(App->App,MUIM_Application_Input,&signal))
  288.         {
  289.             case MUIV_Application_ReturnID_Quit:
  290.                 running = FALSE;
  291.                 break;
  292.  
  293.             /* Insert your code between the "case" statement and comment "end of case ..." */
  294.             case ID_BT_GenerateFiles:
  295.             {
  296.                 ULONG size;
  297.                 char  *text;
  298.                 FILE  *file;
  299.                 char  *Catalog_h_File;
  300.                 char  *Catalog_c_File;
  301.                 char  *GetStringName;
  302.  
  303.                 get(App->STR_GetStringName,MUIA_String_Contents,&GetStringName);
  304.                 if (strlen(GetStringName)==0)
  305.                 {
  306.                     DisplayMsg("Please enter a string in the field \"GetString Name\"");
  307.                 }
  308.                 else
  309.                 {
  310.                     if (GetString==0)
  311.                     {
  312.                         GetString=AllocMemory(strlen(GetStringName)+1,TRUE);
  313.                         strcpy(GetString,GetStringName);
  314.                     }
  315.  
  316.                     set(TF_Catalog->text,TEXTFIELD_ReadOnly,TRUE);
  317.                     get(TF_Catalog->text,TEXTFIELD_Size,&size);
  318.                     get(TF_Catalog->text,TEXTFIELD_Text,&text);
  319.  
  320.                     if (!(file=fopenFile(CatalogName,"w",FALSE)))
  321.                     {
  322.                         char *msg;
  323.  
  324.                         msg=AllocMemory(14+strlen(CatalogName)+1,TRUE);
  325.                         sprintf(msg,"Can't update %s\n",CatalogName);
  326.                         DisplayMsg(msg);
  327.                         FreeMemory(msg);
  328.                         FreeMemory(CatalogName);
  329.                         FreeMemory(GetString);
  330.                         end((void *)App);
  331.                     }
  332.                     fwrite(text,size,1,file);
  333.                     fcloseFile(file);
  334.  
  335.                     remove_extend(CatalogName);
  336.  
  337.                     Catalog_h_File = AllocMemory(strlen(CatalogName)+7,TRUE);
  338.                     strcpy(Catalog_h_File, CatalogName);
  339.                     strcat(Catalog_h_File, "_cat");
  340.                     add_extend(Catalog_h_File, ".h");
  341.  
  342.                     Catalog_c_File = AllocMemory(strlen(CatalogName)+7,TRUE);
  343.                     strcpy(Catalog_c_File, CatalogName);
  344.                     strcat(Catalog_c_File, "_cat");
  345.                     add_extend(Catalog_c_File, ".c");
  346.  
  347.                     add_extend(CatalogName,".cd");
  348.  
  349.                     if (!Write_Catalog_h_File(Catalog_h_File,CatalogName,GetString))
  350.                     {
  351.                         FreeMemory(CatalogName);
  352.                         FreeMemory(GetString);
  353.                         FreeMemory(Catalog_h_File);
  354.                         FreeMemory(Catalog_c_File);
  355.                         end((void *)App);
  356.                     }
  357.  
  358.                     if (!Write_Catalog_c_File(Catalog_c_File,CatalogName,GetString))
  359.                     {
  360.                         FreeMemory(CatalogName);
  361.                         FreeMemory(GetString);
  362.                         FreeMemory(Catalog_h_File);
  363.                         FreeMemory(Catalog_c_File);
  364.                         end((void *)App);
  365.                     }
  366.                  
  367.                     FreeMemory(Catalog_h_File);
  368.                     FreeMemory(Catalog_c_File);
  369.  
  370.                     set(TF_Catalog->text,TEXTFIELD_ReadOnly,FALSE);
  371.                 }
  372.             }
  373.             break;
  374.             /* end of case ID_BT_GenerateFiles */
  375.  
  376.             case ID_BT_Save:
  377.             {
  378.                 ULONG size;
  379.                 char  *text;
  380.                 FILE  *file;
  381.  
  382.                 set(TF_Catalog->text,TEXTFIELD_ReadOnly,TRUE);
  383.                 get(TF_Catalog->text,TEXTFIELD_Size,&size);
  384.                 get(TF_Catalog->text,TEXTFIELD_Text,&text);
  385.  
  386.                 if (!(file=fopenFile(CatalogName,"w",FALSE)))
  387.                 {
  388.                     char *msg;
  389.  
  390.                     msg=AllocMemory(14+strlen(CatalogName)+1,TRUE);
  391.                     sprintf(msg,"Can't update %s\n",CatalogName);
  392.                     DisplayMsg(msg);
  393.                     FreeMemory(msg);
  394.                     FreeMemory(CatalogName);
  395.                     FreeMemory(GetString);
  396.                     end((void *)App);
  397.                 }
  398.                 fwrite(text,size,1,file);
  399.                 fcloseFile(file);
  400.  
  401.                 set(TF_Catalog->text,TEXTFIELD_ReadOnly,FALSE);
  402.             }
  403.             break;
  404.             /* end of case ID_BT_Save */
  405.  
  406.             /* End computing of IDCMP */
  407.         }
  408.         if (running && signal) Wait(signal);
  409.     }
  410.  
  411.     FreeMemory(CatalogName);
  412.     FreeMemory(GetString);
  413.  
  414.     NormalEnd = TRUE;
  415.     end((void *)App);
  416. }
  417.