home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_USR08B.LHA / gerlib / examples / add / normal / SaveSomething.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  7.6 KB  |  365 lines

  1. /* Mon Jul 19 02:38:27 1993 GM Erstellung, __aligned mit gcc versucht, will nicht, umgeschrieben */
  2.  
  3. #include <exec/types.h>
  4. #include <libraries/asl.h>
  5. #include <intuition/classusr.h>
  6. #include <intuition/intuition.h>
  7. #include <dos/dosextens.h>
  8. #include <inline/stubs.h>
  9. #ifdef __OPTIMIZE__
  10. #include <inline/exec.h>
  11. #include <inline/dos.h>
  12. #include <inline/asl.h>
  13. #include <inline/intuition.h>
  14. #else
  15. #include <clib/exec_protos.h>
  16. #include <clib/dos_protos.h>
  17. #include <clib/asl_protos.h>
  18. #include <clib/intuition_protos.h>
  19. #include <clib/tagdefines.h>
  20. #endif
  21.  
  22. #include "add.h"
  23.  
  24. static UBYTE         FullFileName[512],
  25.                       LastFileName[64];
  26.  
  27.  
  28. struct FileRequester* InitSaveSomething(void)
  29. {
  30.         /* Allocate the save file requester structure. */
  31.     struct FileRequester *SaveFileRequest;
  32.  
  33.     if(!(SaveFileRequest = (struct FileRequester *)AllocAslRequestTags(ASL_FileRequest,
  34.         ASLFR_DoSaveMode,    TRUE,
  35.         ASLFR_PositiveText,    (ULONG)"Save",
  36.     TAG_DONE)))
  37.         return(FALSE);
  38.     else
  39.         return(SaveFileRequest);
  40. }
  41.  
  42. void DeleteSaveSomething(struct FileRequester *SaveFileRequest)
  43. {
  44.     if(SaveFileRequest)
  45.         FreeAslRequest((APTR)SaveFileRequest);
  46. }
  47.  
  48. BOOL
  49. SaveSomething(struct Window *window,UBYTE *HailText, struct FileRequester *SaveFileRequest, SaveFunction UserSave)
  50. {
  51.         /* ptr to our process */
  52.  
  53.     struct Process *process;
  54.  
  55.         /* did we save really? */
  56.  
  57.     BOOL return_val = FALSE;
  58.  
  59.         /* due to a bug in gcc, we make it globally */
  60.  
  61.     struct FileInfoBlock *FileInfo;
  62.  
  63.         /* Temporary storage. */
  64.  
  65.     UBYTE DummyBuffer[512],*Dummy;
  66.  
  67.  
  68.     if(FileInfo = AllocDosObject(DOS_FIB,0))
  69.     {
  70.  
  71.             /* get the current Process */
  72.  
  73.         process = (struct Process *) FindTask(0);
  74.  
  75.  
  76.             /* If no file name has been selected yet, initialize
  77.              * the directory name buffer with the name of the
  78.              * current directory.
  79.              */
  80.  
  81. Start:    if(!FullFileName[0])
  82.         {
  83.             if(!NameFromLock(process -> pr_CurrentDir,FullFileName,512))
  84.                 LastFileName[0] = FullFileName[0] = 0;
  85.             else
  86.                 LastFileName[0] = 0;
  87.         }
  88.  
  89.             /* Save the file name. */
  90.  
  91.         strcpy(DummyBuffer,FullFileName);
  92.  
  93.             /* Look for the path name part. */
  94.  
  95.         Dummy = PathPart(DummyBuffer);
  96.  
  97.             /* Cut off the file name. */
  98.  
  99.         *Dummy = 0;
  100.  
  101.             /* Open the file requester. */
  102.  
  103.         if(AslRequestTags(SaveFileRequest,
  104.             ASLFR_Window,        (ULONG)window,
  105.             ASLFR_InitialDrawer,(ULONG)DummyBuffer,
  106.             ASLFR_InitialFile,    (ULONG)LastFileName,
  107.             ASLFR_TitleText,    (ULONG)HailText,
  108.         TAG_DONE))
  109.         {
  110.                 /* Did we get a file name to deal with? */
  111.  
  112.             if(SaveFileRequest -> fr_File[0])
  113.             {
  114.                     /* Save the directory name. */
  115.  
  116.                 StrnCpy(FullFileName,SaveFileRequest -> fr_Drawer,512);
  117.  
  118.                     /* Add the file name part. */
  119.  
  120.                 if(AddPart(FullFileName,SaveFileRequest -> fr_File,512))
  121.                 {
  122.                     BPTR    File    = NULL,
  123.                             FileLock;
  124.                     LONG    Error    = 0,
  125.                             Written    = 1,
  126.                             i;
  127.  
  128.                         /* Save the file name separately. */
  129.  
  130.                     StrnCpy(LastFileName,SaveFileRequest -> rf_File,64);
  131.  
  132.                         /* Try to get lock on the file name in
  133.                          * question and try to examine it.
  134.                          */
  135.  
  136.                     if(FileLock = Lock(FullFileName,ACCESS_READ))
  137.                     {
  138.                         /* struct FileInfoBlock FileInfo __attribute__ ((aligned (8))); */
  139.  
  140.                             /* Examine the file in question. */
  141.  
  142.                         if(Examine(FileLock,FileInfo))
  143.                         {
  144.                                 /* Does the name refer to a directory instead of a file? */
  145.  
  146.                             if(FileInfo -> fib_DirEntryType > 0)
  147.                             {
  148.                                 ShowRequest(window,HailText,"Destination \"%s\" is directory, not a file.","Continue",LastFileName);
  149.  
  150.                                 UnLock(FileLock);
  151.  
  152.                                 goto Start;
  153.                             }
  154.                             else
  155.                             {
  156.                                     /* Can we delete (overwrite) it? */
  157.  
  158.                                 if(FileInfo -> fib_Protection & FIBF_DELETE)
  159.                                 {
  160.                                     ShowRequest(window,HailText,"File \"%s\" is protected from deletion and\ncannot be overridden.","Continue",LastFileName);
  161.  
  162.                                     UnLock(FileLock);
  163.  
  164.                                     goto Start;
  165.                                 }
  166.                                 else
  167.                                 {
  168.                                         /* Can we write any data to it? */
  169.  
  170.                                     if(FileInfo -> fib_Protection & FIBF_WRITE)
  171.                                     {
  172.                                         ShowRequest(window,HailText,"File \"%s\" is not write-enabled and\ncannot be overridden.","Continue",LastFileName);
  173.  
  174.                                         UnLock(FileLock);
  175.  
  176.                                         goto Start;
  177.                                     }
  178.                                 }
  179.                             }
  180.  
  181.                                 /* Is there already data in the file which
  182.                                  * we may overwrite?
  183.                                  */
  184.  
  185.                             if(FileInfo -> fib_Size > 0)
  186.                             {
  187.                                     /* Ask the user what to do with the file. */
  188.  
  189.                                 switch(ShowRequest(window,HailText,"Destination file \"%s\" already exists,\ndo you still want to save?","Override file|Append file|Abort",LastFileName))
  190.                                 {
  191.                                         /* Overwrite the file. */
  192.  
  193.                                     case 1:    break;
  194.  
  195.                                         /* Append data to the file;
  196.                                          * release the filelock first.
  197.                                          */
  198.  
  199.                                     case 2:    UnLock(FileLock);
  200.  
  201.                                         FileLock = NULL;
  202.  
  203.                                             /* Open the file for mixed read and write access. */
  204.  
  205.                                         if(!(File = Open(FullFileName,MODE_READWRITE)))
  206.                                             goto OpenError;
  207.                                         else
  208.                                         {
  209.                                                 /* Seek to the end of the file.*/
  210.  
  211.                                             if(Seek(File,0,OFFSET_END) == -1)
  212.                                             {
  213.                                                     /* Query error condition. */
  214.  
  215.                                                 Error = IoErr();
  216.  
  217.                                                     /* Close the file. */
  218.  
  219.                                                 Close(File);
  220.  
  221.                                                     /* Clear the ID to avoid confusion. */
  222.  
  223.                                                 File = NULL;
  224.                                             }
  225.                                         }
  226.  
  227.                                         break;
  228.  
  229.                                         /* Abort the process. */
  230.  
  231.                                     case 0:    UnLock(FileLock);
  232.  
  233.                                         return;
  234.                                 }
  235.                             }
  236.                         }
  237.                         else
  238.                             Error = IoErr();
  239.  
  240.                             /* Unlock the filelock again. */
  241.  
  242.                         if(FileLock)
  243.                             UnLock(FileLock);
  244.                     }
  245.                     else
  246.                     {
  247.                         Error = IoErr();
  248.  
  249.                             /* If unable to find the object, don't
  250.                              * treat it as an error.
  251.                              */
  252.  
  253.                         if(Error == ERROR_OBJECT_NOT_FOUND)
  254.                             Error = 0;
  255.                     }
  256.  
  257.                         /* Display an error message. */
  258.  
  259.                     if(Error)
  260.                     {
  261.                             /* Get the AmigaDOS error message. */
  262.  
  263.                         if(Fault(Error,"",DummyBuffer,512))
  264.                         {
  265.                             ShowRequest(window,HailText,"An error occured while accessing file \"%s\":%s","Continue",LastFileName,DummyBuffer);
  266.  
  267.                             goto Start;
  268.                         }
  269.                         else
  270.                         {
  271.                             DisplayBeep(window -> WScreen);
  272.  
  273.                             return;
  274.                         }
  275.                     }
  276.  
  277.                         /* If not already open, create a new file. */
  278.  
  279.                     if(!File)
  280.                     {
  281.                         if(!(File = Open(FullFileName,MODE_NEWFILE)))
  282.                         {
  283.                                 /* Query error condition. */
  284.  
  285. OpenError:                        Error = IoErr();
  286.  
  287.                                 /* Get the AmigaDOS error message. */
  288.  
  289.                             if(Fault(Error,"",DummyBuffer,512))
  290.                             {
  291.                                 ShowRequest(window,HailText,"An error occured while opening file \"%s\":%s","Continue",LastFileName,DummyBuffer);
  292.  
  293.                                 goto Start;
  294.                             }
  295.                             else
  296.                             {
  297.                                 DisplayBeep(window -> WScreen);
  298.  
  299.                                 return;
  300.                             }
  301.                         }
  302.                     }
  303.  
  304.                         /* Call the routine the user provided */
  305.  
  306.  
  307.                     UserSave(File);
  308.  
  309.                         /* The save was successful */
  310.  
  311.                     return_val=TRUE;
  312.  
  313.  
  314.                         /* Close the file we opened. */
  315.  
  316.                     Close(File);
  317.  
  318.                         /* Take a look at the file we dealt with. */
  319.  
  320.                     if(FileLock = Lock(FullFileName,ACCESS_READ))
  321.                     {
  322.                         /* struct FileInfoBlock FileInfo __attribute__ ((aligned (4))); */
  323.  
  324.                             /* Examine the file... */
  325.  
  326.                         if(Examine(FileLock,FileInfo))
  327.                         {
  328.                                 /* Did it remain empty? */
  329.  
  330.                             if(!FileInfo -> fib_Size)
  331.                             {
  332.                                 /* the save wasn't successful */
  333.  
  334.                                 return_val = FALSE;
  335.  
  336.                                     /* Ask the user what to do with it. */
  337.  
  338.                                 if(ShowRequest(window,HailText,"Do you wish the incomplete file \"%s\"\nto be removed?","Yes|No",LastFileName))
  339.                                 {
  340.                                         /* Free the filelock referring to it. */
  341.  
  342.                                     UnLock(FileLock);
  343.  
  344.                                     FileLock = NULL;
  345.  
  346.                                         /* Delete the file. */
  347.  
  348.                                     DeleteFile(FullFileName);
  349.                                 }
  350.                             }
  351.                         }
  352.  
  353.                             /* Free the filelock if still available. */
  354.  
  355.                         if(FileLock)
  356.                             UnLock(FileLock);
  357.                     }
  358.                 }
  359.             }
  360.         }
  361.         FreeDosObject(DOS_FIB, FileInfo);
  362.     }
  363.     return (return_val);
  364. }
  365.