home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / YADME10.LHA / YADME10 / src / filereq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-19  |  14.6 KB  |  576 lines

  1. /* Changes by Karl Lukas 06/94: arp.library support has been dropped */
  2.  
  3. /*  FileReq.c
  4.  *
  5.  *  ASL interface
  6.  */
  7.  
  8. #include "defs.h"
  9. #include "cb.h"
  10. #include <stdarg.h>
  11. #include <dos/dos.h>
  12. #include <libraries/asl.h>
  13. #include <clib/asl_protos.h>
  14. #include <clib/dos_protos.h>
  15. #include <reqtools/reqtools.h>
  16.  
  17. extern char MForceTitle;
  18.  
  19. struct rtFileRequester *RFReq;
  20. struct ReqToolsBase *ReqToolsBase;
  21.  
  22. /*
  23.  *  The ASL interface uses the asl.library if it exists, else tries to
  24.  *  use the reqtools.
  25.  */
  26.  
  27. void do_aslload(void)
  28. {
  29.     char file[64];
  30.     char dir[64];
  31.  
  32.     BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);
  33.  
  34.     splitpath(Ep->Name, file, dir);     /* does this EVER return a DIR? */
  35.  
  36.     fixdirname(dir);
  37.  
  38.     if (FReq)
  39.     {                /* If we have asl.library & requester */
  40.         if (AslRequestTags((APTR)FReq,
  41.                 ASL_Hail,       (UBYTE *)"DME Load New File",
  42.                 ASL_OKText,     (UBYTE *)"Load",
  43.                 ASL_Window,     Ep->Win,
  44.                 ASL_FuncFlags,  FILF_NEWIDCMP,
  45.                 ASL_Dir,        dir,
  46.                 ASL_File,       file,
  47.                 ASL_LeftEdge,   Ep->ASLleft,     /* use config coords */
  48.                 ASL_TopEdge,    Ep->ASLtop,
  49.                 ASL_Width,      Ep->ASLwidth,
  50.                 ASL_Height,     Ep->ASLheight,
  51.                 TAG_DONE )) {
  52.  
  53.             BPTR newlock;
  54.  
  55.             if (newlock = Lock( FReq->rf_Dir, SHARED_LOCK))
  56.             {
  57.                 UnLock(CurrentDir(oldlock));
  58.                 Ep->dirlock = (long)newlock;
  59.  
  60.                 Ep->ASLtop = FReq->rf_TopEdge;      /* save coords */
  61.                 Ep->ASLleft = FReq->rf_LeftEdge;
  62.                 Ep->ASLwidth = FReq->rf_Width;
  63.                 Ep->ASLheight = FReq->rf_Height;
  64.  
  65.                 /* fixfile(FReq->rf_File,FReq->rf_Dir); */
  66.                 av[0] = (ubyte *)"n";
  67.                 av[1] = (ubyte *) FReq->rf_File;
  68.                 do_edit();
  69.                 return;
  70.             }
  71.         }
  72.         CurrentDir(oldlock);
  73.     }
  74.     else {              /* no asl.library? try reqtools */
  75.         do_reqload();
  76.     }
  77. }
  78.  
  79.  
  80. void do_aslsave(void)
  81. {
  82.     char file[64];
  83.     char dir[64];
  84.     BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);
  85.  
  86.     splitpath(Ep->Name, file, dir);
  87.  
  88.     fixdirname(dir);
  89.  
  90.     if (FReq)
  91.     {
  92.         if (AslRequestTags( (APTR)FReq,
  93.                 ASL_Hail,       (UBYTE *)"DME Save File",
  94.                 ASL_OKText,     (UBYTE *)"Save",
  95.                 ASL_Window,     Ep->Win,
  96.                 ASL_FuncFlags,  FILF_NEWIDCMP | FILF_SAVE,
  97.                 ASL_Dir,        dir,
  98.                 ASL_File,       file,
  99.                 ASL_LeftEdge,   Ep->ASLleft,
  100.                 ASL_TopEdge,    Ep->ASLtop,
  101.                 ASL_Width,      Ep->ASLwidth,
  102.                 ASL_Height,     Ep->ASLheight,
  103.                 TAG_DONE ))
  104.         {
  105.             BPTR newlock;
  106.  
  107.             if (newlock = Lock( FReq->rf_Dir, SHARED_LOCK))
  108.             {
  109.                 UnLock(CurrentDir(oldlock));
  110.                 Ep->dirlock = (long)newlock;
  111.  
  112.                 Ep->ASLtop = FReq->rf_TopEdge;
  113.                 Ep->ASLleft = FReq->rf_LeftEdge;
  114.                 Ep->ASLwidth = FReq->rf_Width;
  115.                 Ep->ASLheight = FReq->rf_Height;
  116.  
  117.                 strncpy(Ep->Name, FReq->rf_File, 63);
  118.                 MForceTitle = 1;
  119.  
  120.                 do_command("saveold");
  121.             }
  122.         }
  123.         else
  124.         {
  125.             CurrentDir(oldlock);
  126.         }
  127.     }
  128.     else
  129.     {
  130.      do_reqsave();
  131.     }
  132. }
  133.  
  134. void do_aslinsfile(void)
  135. {
  136.     char file[64];
  137.     char dir[64];
  138.     BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);
  139.  
  140.     splitpath(Ep->Name, file, dir);
  141.  
  142.     fixdirname(dir);
  143.  
  144.     if (FReq)
  145.     {
  146.         if (AslRequestTags( (APTR)FReq,
  147.                 ASL_Hail,       (UBYTE *)"DME Insert File",
  148.                 ASL_OKText,     (UBYTE *)"Load",
  149.                 ASL_Window,     Ep->Win,
  150.                 ASL_FuncFlags,  FILF_NEWIDCMP,
  151.                 ASL_Dir,        dir,
  152.                 ASL_File,       file,
  153.                 ASL_LeftEdge,   Ep->ASLleft,
  154.                 ASL_TopEdge,    Ep->ASLtop,
  155.                 ASL_Width,      Ep->ASLwidth,
  156.                 ASL_Height,     Ep->ASLheight,
  157.                 TAG_DONE ))
  158.         {
  159.  
  160.             CurrentDir(oldlock);
  161.             Ep->ASLtop = FReq->rf_TopEdge;
  162.             Ep->ASLleft = FReq->rf_LeftEdge;
  163.             Ep->ASLwidth = FReq->rf_Width;
  164.             Ep->ASLheight = FReq->rf_Height;
  165.             fixfile( FReq->rf_File, FReq->rf_Dir);
  166.             av[0] = (ubyte *)"i";
  167.             av[1] = (ubyte *)FReq->rf_File;
  168.             do_edit();
  169.             return;
  170.         }
  171.         CurrentDir(oldlock);
  172.     }
  173.     else
  174.     {
  175.         do_reqinsfile();
  176.     }
  177. }
  178.  
  179. void do_aslfont(void)
  180. {
  181.     if (FReq)
  182.     {
  183.         struct FontRequester *fontreq;
  184.  
  185.         struct TagItem FontTags[]=
  186.         {
  187.             ASL_Hail,       (ULONG)"DME fixed width font",
  188.             ASL_Window,     NULL,
  189.             ASL_FuncFlags,  FILF_NEWIDCMP | FONF_FIXEDWIDTH,
  190.             ASL_MinHeight,  6,
  191.             ASL_MaxHeight,  24,         /* more than reasonable, eh? */
  192.             TAG_DONE,       NULL
  193.         };
  194.  
  195.         FontTags[1].ti_Data = (ULONG)Ep->Win;
  196.  
  197.         if (fontreq = (struct FontRequester *)
  198.                       AllocAslRequest(ASL_FontRequest,FontTags) ) {
  199.  
  200.             if (AslRequest(fontreq, NULL)) {
  201.  
  202.                 /*  Since we have GetFont() already, just pass
  203.                  *   the name and size from fontreq's TextAttr to it.
  204.                  *
  205.                  *  What follows mimics SETFONT (in cmnd3.c)
  206.                  */
  207.  
  208.                 FONT *font = (FONT *)GetFont( fontreq->fo_Attr.ta_Name,
  209.                                              (short) fontreq->fo_Attr.ta_YSize );
  210.                 ED *ep = Ep;
  211.                 if (font) {
  212.                     text_sync();
  213.                     if (ep->Font)
  214.                         CloseFont(ep->Font);
  215.                     ep->Font = font;
  216.                     SetFont(ep->Win->RPort, font);
  217.                     SetRast(ep->Win->RPort, 0);
  218.                     RefreshWindowFrame(ep->Win);
  219.                     set_window_params();
  220.                     text_redisplay();
  221.                 } else {
  222.                     title("Unable to find font");
  223.                 }
  224.             }
  225.             FreeAslRequest(fontreq);
  226.         }
  227.     } else {
  228.         do_reqfont();
  229.     }
  230. }
  231.  
  232. /*  this little kludge gets a DIR: string for the ASL requester
  233.  */
  234.  
  235. void fixdirname(char *dir)
  236. {
  237.     if (!strlen(dir)) {                 /* if no DIR; KLUDGE ALERT! */
  238.  
  239.         if (NameFromLock((BPTR)Ep->dirlock, dir, 64)!=DOSTRUE) {
  240.  
  241.             dir[0] = 0; /* assumes NameFromLock doeesn't clean up on err(??) */
  242.         }
  243.     }
  244. }
  245.  
  246.  
  247. void fixfile(char *file, char *dir)
  248. {
  249.     char *ptr;
  250.     short len = strlen(dir);
  251.     char hasdev = 0;
  252.  
  253.     /*
  254.      *  do we need to add a slash to the directory spec?
  255.      */
  256.  
  257.     if (len && dir[len-1] != '/' && dir[len-1] != ':')
  258.     {
  259.         dir[len++] = '/';
  260.         dir[len] = 0;
  261.     }
  262.  
  263.     /*
  264.      *  Is file spec really a full path spec?
  265.      */
  266.  
  267.     for (ptr = file; *ptr; ++ptr)
  268.     {
  269.         if (ptr[0] == ':')
  270.             hasdev = 1;
  271.     }
  272.     if (!hasdev)
  273.     {
  274.         movmem(file,file+len,strlen(file)+1);
  275.         movmem(dir,file,len);
  276.     }
  277. }
  278.  
  279. /*
  280.  *  Search backwards for first ':' or '/' and split path there.
  281.  *  This subroutine may appear to be coded incorrectly to a novice
  282.  *  programmer.  It isn't [now].
  283.  */
  284.  
  285. void splitpath(char *name, char *file, char *dir)
  286. {
  287.     short i;
  288.  
  289.     for (i = strlen(name); i >= 0; --i)
  290.     {       /* was (incorrectly) "i > 0" */
  291.         if (name[i] == ':' || name[i] == '/')
  292.             break;
  293.     }
  294.     ++i;
  295.     strcpy(file, name + i);
  296.     movmem(name, dir, i);
  297.     dir[i] = 0;
  298. }
  299.  
  300. /*
  301.  *  The ASL interface uses the asl.library if it exists.
  302.  */
  303.  
  304. void do_reqload(void)
  305. {
  306.     char file[108];
  307.     char dir[64];
  308.  
  309.     TagItem tagitem[3];
  310.  
  311.     BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);
  312.  
  313.     splitpath(Ep->Name, file, dir);     /* does this EVER return a DIR? */
  314.  
  315.     fixdirname(dir);
  316.  
  317.     if (RFReq)
  318.     {                /* If we have req.library & requester */
  319.         tagitem[0].ti_Tag = RTFI_Dir;
  320.         tagitem[0].ti_Data = (ULONG)dir;
  321.         tagitem[1].ti_Tag = TAG_DONE;
  322.         tagitem[1].ti_Data = 0;
  323.  
  324.         rtChangeReqAttrA(RFReq, tagitem);
  325.  
  326.         tagitem[0].ti_Tag = RT_Window;
  327.         tagitem[0].ti_Data = (ULONG)Ep->Win;
  328.         tagitem[1].ti_Tag = RTFI_OkText;
  329.         tagitem[1].ti_Data = (ULONG)"Load";
  330.         tagitem[2].ti_Tag = TAG_DONE;
  331.         tagitem[2].ti_Data = 0;
  332.  
  333.         if(rtFileRequestA(RFReq, file, (UBYTE *)"DME Load New File", tagitem))
  334.         {
  335.             BPTR newlock;
  336.  
  337.             if (newlock = Lock( RFReq->Dir, SHARED_LOCK))
  338.             {
  339.                 UnLock(CurrentDir(oldlock));
  340.                 Ep->dirlock = (long)newlock;
  341.  
  342.                 av[0] = (ubyte *)"n";
  343.                 av[1] = (ubyte *) file;
  344.                 do_edit();
  345.                 return;
  346.             }
  347.         }
  348.         CurrentDir(oldlock);
  349.     }
  350.     else
  351.         title("No reqtools.library");
  352. }
  353.  
  354.  
  355. void do_reqsave(void)
  356. {
  357.     char file[108];
  358.     char dir[64];
  359.     BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);
  360.  
  361.     TagItem tagitem[4];
  362.  
  363.     splitpath(Ep->Name, file, dir);
  364.  
  365.     fixdirname(dir);
  366.  
  367.     if (RFReq)
  368.     {
  369.         tagitem[0].ti_Tag = RTFI_Dir;
  370.         tagitem[0].ti_Data = (ULONG)dir;
  371.         tagitem[1].ti_Tag = TAG_DONE;
  372.         tagitem[1].ti_Data = 0;
  373.  
  374.         rtChangeReqAttrA(RFReq, tagitem);
  375.  
  376.         tagitem[0].ti_Tag = RT_Window;
  377.         tagitem[0].ti_Data = (ULONG)Ep->Win;
  378.         tagitem[1].ti_Tag = RTFI_OkText;
  379.         tagitem[1].ti_Data = (ULONG)"Save";
  380.         tagitem[2].ti_Tag = RTFI_Flags;
  381.         tagitem[2].ti_Data = FREQF_SAVE;
  382.         tagitem[3].ti_Tag = TAG_DONE;
  383.         tagitem[3].ti_Data = 0;
  384.  
  385.         if(rtFileRequestA(RFReq, file, (UBYTE *)"DME Save File", tagitem))
  386.         {
  387.  
  388.             BPTR newlock;
  389.  
  390.             if (newlock = Lock( RFReq->Dir, SHARED_LOCK))
  391.             {
  392.                 UnLock(CurrentDir(oldlock));
  393.                 Ep->dirlock = (long)newlock;
  394.  
  395.                 strncpy(Ep->Name, file, 63);
  396.                 MForceTitle = 1;
  397.  
  398.                 do_command("saveold");
  399.             }
  400.         }
  401.         else
  402.         {
  403.             CurrentDir(oldlock);
  404.         }
  405.     }
  406.     else
  407.         title("No reqtools.library");
  408. }
  409.  
  410. void do_reqinsfile(void)
  411. {
  412.     char file[108];
  413.     char dir[64];
  414.     BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);
  415.  
  416.     TagItem tagitem[3];
  417.  
  418.     splitpath(Ep->Name, file, dir);
  419.  
  420.     fixdirname(dir);
  421.  
  422.     if (FReq)
  423.     {
  424.         tagitem[0].ti_Tag = RTFI_Dir;
  425.         tagitem[0].ti_Data = (ULONG)dir;
  426.         tagitem[1].ti_Tag = TAG_DONE;
  427.         tagitem[1].ti_Data = 0;
  428.  
  429.         rtChangeReqAttrA(RFReq, tagitem);
  430.  
  431.         tagitem[0].ti_Tag = RT_Window;
  432.         tagitem[0].ti_Data = (ULONG)Ep->Win;
  433.         tagitem[1].ti_Tag = RTFI_OkText;
  434.         tagitem[1].ti_Data = (ULONG)"Save";
  435.         tagitem[2].ti_Tag = TAG_DONE;
  436.         tagitem[2].ti_Data = 0;
  437.  
  438.         if(rtFileRequestA(RFReq, file, (UBYTE *)"DME Insert File", tagitem))
  439.         {
  440.             CurrentDir(oldlock);
  441.             fixfile( file, RFReq->Dir);
  442.             av[0] = (ubyte *)"i";
  443.             av[1] = (ubyte *)file;
  444.             do_edit();
  445.             return;
  446.         }
  447.         CurrentDir(oldlock);
  448.     }
  449.     else
  450.         title("No reqtools.library");
  451. }
  452.  
  453. void do_reqfont(void)
  454. {
  455.     if (ReqToolsBase)
  456.     {
  457.         struct rtFontRequester *fontreq;
  458.  
  459.         struct TagItem FontTags[] =
  460.         {
  461.             RT_Window,      NULL,
  462.             RTFO_Flags,     FREQF_FIXEDWIDTH, /* | FILF_NEWIDCMP */
  463.             RTFO_MinHeight, 6,
  464.             RTFO_MaxHeight, 24,
  465.             TAG_DONE
  466.         };
  467.  
  468.         FontTags[0].ti_Data=(ULONG)Ep->Win;
  469.  
  470.         if (fontreq = rtAllocRequestA (RT_FONTREQ, NULL))
  471.         {
  472.             if (rtFontRequestA (fontreq, (UBYTE *)"DME fixed width font", FontTags)) {
  473.  
  474.                 /*  Since we have GetFont() already, just pass
  475.                  *   the name and size from fontreq's TextAttr to it.
  476.                  *
  477.                  *  What follows mimics SETFONT (in cmnd3.c)
  478.                  */
  479.  
  480.                 FONT *font = (FONT *)GetFont( fontreq->Attr.ta_Name,
  481.                                              (short) fontreq->Attr.ta_YSize );
  482.                 ED *ep = Ep;
  483.                 if (font)
  484.                 {
  485.                     text_sync();
  486.                     if (ep->Font)
  487.                         CloseFont(ep->Font);
  488.  
  489.                     ep->Font = font;
  490.                     SetFont(ep->Win->RPort, font);
  491.                     SetRast(ep->Win->RPort, 0);
  492.                     RefreshWindowFrame(ep->Win);
  493.                     set_window_params();
  494.                     text_redisplay();
  495.  
  496.                     strcpy(ep->Fontname, fontreq->Attr.ta_Name);
  497.                     ep->Fontsize = fontreq->Attr.ta_YSize;
  498.                 }
  499.                 else
  500.                 {
  501.                     title("Unable to find font");
  502.                 }
  503.             }
  504.             rtFreeRequest (fontreq);
  505.         }
  506.     }
  507.     else
  508.     {
  509.         title("Need ASL.LIBRARY or REQTOOLS.LIBRARY for Font Requester!");
  510.     }
  511. }
  512.  
  513.  
  514. void do_saveclip(void)
  515. {
  516.    struct IOClipReq *ior;
  517.    struct cbbuf *clipbuf;
  518.    TagItem tagitem[4];
  519.    char fullname[256];
  520.    int len;
  521.    char file[108];
  522.    BPTR fptr;
  523.  
  524.    if (ior = CBOpen(0))
  525.    {
  526.       if (CBQueryFTXT(ior))
  527.       {
  528.          if (clipbuf = CBReadCHRS(ior))
  529.          {
  530.             /* clipbuf->count; */
  531.             if (RFReq)
  532.             {
  533.                 tagitem[0].ti_Tag = RT_Window;
  534.                 tagitem[0].ti_Data = (ULONG)Ep->Win;
  535.                 tagitem[1].ti_Tag = RTFI_OkText;
  536.                 tagitem[1].ti_Data = (ULONG)"Save";
  537.                 tagitem[2].ti_Tag = RTFI_Flags;
  538.                 tagitem[2].ti_Data = FREQF_SAVE;
  539.                 tagitem[3].ti_Tag = TAG_DONE;
  540.                 tagitem[3].ti_Data = 0;
  541.  
  542.                 if(rtFileRequestA(RFReq, file, (UBYTE *)"Save clipboard", tagitem))
  543.                 {
  544.                    strcpy(fullname, RFReq->Dir);
  545.                    len = strlen(fullname);
  546.                    if (len && (fullname[len - 1] != ':'))
  547.                       strcat(fullname,  "/");
  548.                    strcat(fullname, file);
  549.  
  550.                    if (fptr = Open(fullname, MODE_NEWFILE))
  551.                    {
  552.                       if (Write(fptr, clipbuf->mem, clipbuf->count) != clipbuf->count)
  553.                          title("Error while writing file");
  554.                       Close(fptr);
  555.                    }
  556.                    else
  557.                      title("Error: Could not open file");
  558.                 }
  559.             }
  560.             else
  561.                title("Sorry, no reqtools.library");
  562.             CBFreeBuf(clipbuf);
  563.          }
  564.          else
  565.             title("Reading from clipboard failed");
  566.          CBReadDone(ior);
  567.       }
  568.       else
  569.          title("Clipboard is empty");
  570.       CBClose(ior);
  571.    }
  572.    else
  573.       title("Error: Cannot open clipboard device");
  574. }
  575.  
  576.