home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 266.lha / SetKey_v2.0 / src / getfile.c < prev    next >
C/C++ Source or Header  |  1989-07-10  |  16KB  |  526 lines

  1. /************************************************************************
  2. req.c
  3.     This file contains a general-purpose <requester> that
  4. will prompt the user for a filename input.
  5.     The program actually uses a window instead of a 'Requester'
  6. for greater flexibility. It will take control of your window's
  7. IDCMP Port when called.
  8.  
  9. ***    This material is copyright (c) 1986 by C. Heath of Microsmiths, Inc.
  10. Permission is granted to use these files in any way with the following
  11. exceptions:
  12.  
  13. 1) The files shall not be posted on any telecommunications service, public
  14. or private, except for BIX until January 15, 1987.
  15.  
  16. 2) The files may only be distributed in archive format, with no modifications.
  17. If you make any improvements on the file requester and would like to
  18. generally distribute them, please contact "cheath" on BIX, or write to:
  19.     Microsmiths Inc, PO Box 561, Cambridge, MA 02140
  20.  
  21. 3) The requester may be used in any commercial product, but must be in
  22. object code format.  You are free to make modifications for use in your
  23. product.  Permission is granted to Lattice, Inc, and to Manx, Inc, to
  24. include the source files in archive format.
  25.  
  26.     Thank you, and enjoy.
  27.         ...cheath
  28.  
  29. ************************************************************************/
  30.  
  31. #include "standard.h"
  32. #include "safeclose.h"
  33.  
  34. extern UBYTE *AllocMem();
  35. extern struct Window *OpenWindow();
  36. extern struct IntuiMessage *GetMsg();
  37. extern struct FileLock *Lock();
  38.  
  39. #define FAST register
  40. #define NL NULL
  41.  
  42. extern char *dmore(), *dinit();
  43.  
  44. static struct FileLock      *pdir = NL;
  45. static struct FileInfoBlock *dir_info;
  46.  
  47. static struct Window   *eW;        /* Parent Window. Uck    */
  48.  
  49. static struct TextAttr MyFont = 
  50.          {(UBYTE *)"topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT };
  51.  
  52. /* FGAD requires a few unique gadget-ids, origined at FGAD    */
  53. /* In the unlikely event this conflicts with any of your gadgets, */
  54. /* change this equate to allow 16 contiguous unique ID's    */
  55.  
  56. #define FGAD    0x76c0L
  57.  
  58. #define FCHARS    32L    /* Number of chars allowed in file name    */
  59.  
  60. #define DIR_SIZ    50L    /* Number of chars in a dir name...    */
  61. #define MAX_STR    DIR_SIZ+2L
  62.  
  63. #define DENTS    5L        /* Number of entries on screen   */
  64. /* It's not as simple as changing 'DENTS'...      */
  65.  
  66. #define DSIZE    FCHARS+1L    /* Size of a directory entry   */
  67.  
  68. #define    DBUFSIZ    3000L    /* Number of bytes to allocate for all ents */
  69.  
  70. #define BCOL    1L    /* Background color    */
  71. #define FCOL    2L    /* Foreground color    */
  72.  
  73. #define RHGHT    120L
  74. #define RWDTH    320L
  75.  
  76. #define ZWDTH    270L
  77.  
  78. static UBYTE    ubuf[MAX_STR];        /* Undo buffer    */
  79.  
  80. static struct dirent { struct dirent *next; BOOL isfile; char dE[DSIZE+2]; };
  81. static struct dirent *FirstEntry;
  82. static struct dirent *NextEntry;
  83.  
  84. static struct dirhead { struct dirent *next; };
  85. static struct dirhead ListHead;
  86.  
  87. static long   curent,maxent;
  88. static BOOL   more;
  89.  
  90. static struct Window    *wRq = NL;        /* Requester window   */
  91. static struct RastPort    *wRp;
  92.  
  93. /* Requester "Hailing" prompt */
  94. static struct IntuiText oTxt = {2,2,JAM1,10,11,NL, NL ,NL};
  95.  
  96. static struct IntuiText saydir = {0,1,JAM2,0,1,&MyFont,(UBYTE *)"(dir) ",NL};
  97.  
  98. static struct IntuiText rname[DENTS] = { /* File name list */
  99.    {2,1,JAM2,48,1,NL,NL, NL},
  100.    {2,1,JAM2,48,1,NL,NL, NL},
  101.    {2,1,JAM2,48,1,NL,NL, NL},
  102.    {2,1,JAM2,48,1,NL,NL, NL},
  103.    {2,1,JAM2,48,1,NL,NL, NL} };
  104.  
  105. /* Display for file name ... */
  106.  
  107. static SHORT oXY2[] = {-2,-2, RWDTH-78,-2, RWDTH-78,9, -2,9, -2,-2};
  108. static struct Border thebd = {0,0, 2,3,JAM1, 5,oXY2, NL};
  109.  
  110. static struct IntuiText otxt = {2,2,JAM1,-40,0,&MyFont,(UBYTE *)"file",NL};
  111. static struct StringInfo osx = { NL ,ubuf, NL,DSIZE,NL,NL,NL,NL,NL,NL,NL,NL,NL};
  112. static struct Gadget ogx = { NL, 60,RHGHT-35,RWDTH-80 ,10,     /* File name gadget */
  113.    GADGHCOMP, RELVERIFY , STRGADGET,
  114.    (APTR)&thebd,NL,&otxt,NL,(APTR)&osx, FGAD+11,NL };
  115.  
  116. static struct Gadget oga = { &ogx, 10,70, ZWDTH,10,   /* Gadgets For   */
  117.    GADGHCOMP, RELVERIFY, BOOLGADGET,     /* Directory entries   */
  118.    NL,NL, &rname[4] ,NL,NL, FGAD+10,NL };
  119. static struct Gadget og9 = {&oga, 10,60, ZWDTH,10,
  120.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  121.    NL,NL, &rname[3] ,NL,NL, FGAD+9,NL };
  122. struct Gadget og8 = {&og9, 10,50, ZWDTH,10,
  123.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  124.    NL,NL, &rname[2] ,NL,NL, FGAD+8,NL };
  125. static struct Gadget og7 = {&og8, 10,40, ZWDTH,10,
  126.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  127.    NL,NL, &rname[1] ,NL,NL, FGAD+7,NL };
  128. static struct Gadget og6 = {&og7, 10,30, ZWDTH,10,
  129.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  130.    NL,NL, &rname[0] ,NL,NL, FGAD+6,NL };
  131.  
  132.  
  133. /* Gadjets for requester  */
  134.  
  135. static SHORT oXY3[] = {0,0, 50,0, 50,9, 0,9, 0,0};
  136. static SHORT oXY4[] = {2,-2, 48,-2, 52,2, 52,7, 48,11, 2,11, -2,7, -2,2, 2,-2};
  137. static struct Border obd2 = {0,0, 2,3,JAM1, 9,oXY4, NL};
  138. static struct Border obd1 = {0,0, 3,2,JAM1, 5,oXY3, &obd2};  /* OTAY / CANCEL box */
  139.  
  140. static struct IntuiText ot1 = {0,0,JAM1,1,1,&MyFont,(UBYTE *)"  OK  ",NL};
  141. static struct IntuiText ot2 = {0,0,JAM1,1,1,&MyFont,(UBYTE *)"Cancel",NL};
  142.  
  143. static struct IntuiText dtxt = {2,2,JAM1,-60,0,NL,(UBYTE *)"drawer",NL};
  144. static struct StringInfo os5 = { NL ,ubuf, NL,DIR_SIZ,NL,NL,NL,NL,NL,NL,NL,NL,NL};
  145. static struct Gadget og5 = { &og6, RWDTH/2-80,19,190,10,     /* Directory */
  146.    GADGHCOMP, RELVERIFY, STRGADGET,
  147.    NL,NL,&dtxt,NL,(APTR)&os5, FGAD+5,NL };
  148.  
  149. static struct Image   cc_img;
  150. static struct PropInfo   cc_prop = {AUTOKNOB | FREEVERT, 0,0, 0,0x1000,0,0,0,0,0,0 };
  151. static struct Gadget og3 = { &og5,RWDTH-39,20,20,60,      /* Scroll Bar   */
  152.     GADGHCOMP,GADGIMMEDIATE | FOLLOWMOUSE, PROPGADGET,
  153.     (APTR)&cc_img,NL,NL,NL,(APTR)&cc_prop,FGAD+3,NL };
  154.  
  155. static struct Gadget og2 = { &og3, RWDTH-70,RHGHT-20, 50,10,  /* CANCEL */
  156.    GADGHCOMP,  RELVERIFY, BOOLGADGET,
  157.    (APTR)&obd1,NL, &ot2,NL,NL, FGAD+2,NL };
  158.  
  159. static struct Gadget og1 = { &og2, 20,RHGHT-20, 50,10,      /* OTAY   */
  160.    GADGHCOMP,        /* Flags    */
  161.    RELVERIFY,        /* Activation    */
  162.    BOOLGADGET,
  163.    (APTR)&obd1,NL,    /* GadgetRender, SelectRender    */
  164.    &ot1,NL,NL,        /* IntuiText, MutualExclude,SpecialInfo   */
  165.    FGAD+1,NL };        /* Gadget Id, UserData    */
  166.  
  167. /* Open a requester "Window" */
  168.  
  169. static struct NewWindow NewFiles = {
  170.    160, 30, RWDTH,RHGHT, BCOL,FCOL, NL, /* Fill in AFTER opening ... */
  171.    SMART_REFRESH | ACTIVATE | RMBTRAP | WINDOWDRAG,
  172.    &og1,NL,NL, NL,
  173.    NL, RWDTH,RHGHT,RWDTH,RHGHT, WBENCHSCREEN };
  174.  
  175. IMPORT struct Library    *IntuitionBase;
  176.  
  177. /***************************************************
  178. *  get_fname(window,screen,hail,ddef,ddir);
  179. *
  180. *   Displays a window/requester that
  181. * gets a file name for device,directory,default file, extension
  182. *
  183. *   Calling args:
  184. * window:   Window making the request
  185. * screen:   Screen, if NULL assummed workbench
  186. * hail:   Text prompt at top of requester
  187. * ddef:   Input default file-name. Has NO DIRECTORY OR EXTENSION.
  188. * ddir:   Directory of file, may be null
  189.  
  190. /* Set a file-requester with prompt 'hail'   */
  191.  
  192. char *get_fname(cW,screen,hail,ddef,ddir)
  193.    struct Window *cW;        /* Calling Window   */
  194.    struct Screen *screen;    /* screen .... if null assumed workbench */
  195.    UBYTE    *hail;        /* Hailing prompt   */
  196.    UBYTE    *ddef;        /* Proable file-name   */
  197.    UBYTE    *ddir;        /* Directory in which to search   */
  198.    {
  199.    FAST struct IntuiMessage *imes;   /* Wait for message in HERE   */
  200.    FAST struct Gadget    *igad;   /* Get Gadjet Mumbo Jumbo   */
  201.    FAST long    i,class;
  202.    FAST TEXT    *pnam;
  203.  
  204.    FAST char    *retval;
  205.    FAST BOOL    dir_flag;
  206.    FAST BOOL    keepon;
  207.  
  208.    if ( ! (eW = cW) ) return(NL);
  209.  
  210.    osx.Buffer = ddef;    /* Set default file name   */
  211.    os5.Buffer = ddir;    /* Set default device name   */
  212.  
  213.    for ( i=0; i<DENTS; i++)
  214.       {
  215.       rname[i].IText = (UBYTE *)"";
  216.       rname[i].NextText = NL;
  217.       };
  218.  
  219.    NewFiles.Title = eW->Title;
  220.    if ((dir_info = (struct FileInfoBlock *)
  221.                    AllocMem((long)sizeof(struct FileInfoBlock),0L)) == NULL)
  222.       return(NL);
  223.  
  224.    if (screen)        /* User supplied a screen */
  225.       {
  226.       NewFiles.Type = CUSTOMSCREEN;
  227.       NewFiles.Screen = screen;
  228.       }
  229.  
  230.    if ( (FirstEntry = (struct dirent *)AllocMem(DBUFSIZ,0L)) == 0L ||
  231.         (wRq = OpenWindow( &NewFiles )) == 0L )
  232.       {
  233.       if ( FirstEntry )   FreeMem(FirstEntry,DBUFSIZ);
  234.       /* notify("Can't Open Requester..."); */
  235.       notify("Can't Open Requester..."); /* redone for debug */
  236.       FreeMem(dir_info,(long)sizeof(struct FileInfoBlock));
  237.       return(NL);
  238.       }
  239.  
  240.    /* Set up directory, notify any errors...   */
  241.    if ( pnam = (TEXT *)dinit(ddir) )   notify(pnam);
  242.  
  243.  
  244.    /* This optional line will activate a string gadget    */
  245.    if ( IntuitionBase->lib_Version > 32 )
  246.     {
  247.     ActivateGadget(&ogx,wRq,0L);
  248.     }
  249.  
  250.    wRp = wRq->RPort;
  251.  
  252.    wRq->UserPort = eW->UserPort;
  253.    ModifyIDCMP(wRq,(long)(MOUSEBUTTONS | GADGETDOWN | GADGETUP | MOUSEMOVE));
  254.  
  255.    SetAPen(wRp,1L);
  256.    RectFill(wRp,4L,10L,(long)(RWDTH-5),(long)(RHGHT-4));
  257.  
  258.    oTxt.IText = hail;   /* Set calling arg   */
  259.    oTxt.LeftEdge = (RWDTH - IntuiTextLength(&oTxt)) >> 1L;
  260.    PrintIText(wRp,&oTxt,0L,0L);
  261.  
  262.    RefreshGadgets(&og1,wRq,(long)NL);
  263.  
  264.    /*---------------------------------------------------*/
  265.    /*    retval will be NULL if no file was selected.    */
  266.  
  267.    for ( retval= NL, keepon=TRUE; keepon ; )
  268.       {
  269.       while ( (imes=GetMsg(wRq->UserPort)) == 0L )
  270.       {
  271.          if ( dir_flag )
  272.             {
  273.             i = (maxent-DENTS) * cc_prop.VertPot / MAXBODY;
  274.             if ( i > (maxent-DENTS) )
  275.             i = maxent-DENTS;
  276.             if ( i <0 )   i = 0;
  277.             curent = i;
  278.             cxxx();
  279.             dir_flag = FALSE;
  280.             }
  281.          if ( more )
  282.             {
  283.             if (pnam = (TEXT *)dmore())   /* Continue to read the directory */
  284.                notify(pnam);      /* Yucko error   */
  285.             if ( maxent <= DENTS ) dir_flag = TRUE;
  286.             }
  287.          else    WaitPort(wRq->UserPort);
  288.          }
  289.       igad = (struct Gadget *)imes->IAddress;
  290.       class = imes->Class;
  291.       ReplyMsg(imes);
  292.  
  293.       switch (class)
  294.          {
  295.          case MOUSEMOVE:      dir_flag = TRUE;   break;
  296.       
  297.          case GADGETUP:
  298.          case GADGETDOWN:
  299.             switch ( i = igad->GadgetID)
  300.             {
  301.             case FGAD+6:
  302.             case FGAD+7:
  303.             case FGAD+8:
  304.             case FGAD+9:
  305.             case FGAD+10:       /* Replace file or directory name   */
  306.                pnam = rname[i - (FGAD+6)].IText;
  307.                if ( rname[igad->GadgetID - (FGAD+6)].NextText == NL )
  308.                   {
  309.                   RemoveGadget(wRq,&ogx);
  310.                   for (i=0; i<DSIZE; i++)      ddef[i] = *pnam++;
  311.                   AddGadget(wRq,&ogx,-1L);
  312.                   RefreshGadgets(&ogx,wRq,(long)NL);
  313.                   break;
  314.                   }
  315.                   else
  316.                   {
  317.                   RemoveGadget(wRq,&og5);
  318.                   rfnam(ddir,pnam);
  319.                   AddGadget(wRq,&og5,-1L);
  320.                   RefreshGadgets(&og5,wRq,(long)NL);
  321.                   }
  322.             case FGAD+5:
  323.                if ( pnam = (TEXT *)dinit(ddir) )
  324.                   notify(pnam);
  325.             case FGAD+3:
  326.                dir_flag = TRUE;
  327.                break;
  328.  
  329.             case FGAD+11:      /* Name gadget, OTAY gadget   */
  330.             case FGAD+1:
  331.                retval = (char *)ddef;
  332.             case FGAD+2:      /* Cancel gadget   */
  333.                keepon = FALSE;
  334.             }
  335.          }
  336.       }
  337.  
  338.       FreeMem(FirstEntry,DBUFSIZ );
  339.       FreeMem(dir_info,(long)sizeof(struct FileInfoBlock));
  340.       free_pdir();
  341.  
  342.       CloseWindowSafely(wRq);
  343.       return(retval);
  344.    }
  345.  
  346. static free_pdir()
  347.    {
  348.    if ( pdir )
  349.       {
  350.       UnLock(pdir);
  351.       pdir = NL;
  352.       }
  353.    }
  354.  
  355. /*****************************************************************
  356. * dinit()
  357. *   Initialize the fib for directory muck.  Null return
  358. * is good, else return is a pointer to an error string      */
  359.  
  360. static char *dinit(subdir)
  361.    char *subdir;
  362.    {
  363.    more = FALSE;
  364.    curent = maxent = 0;
  365.  
  366.    NextEntry = FirstEntry;      /* Allocate from here   */
  367.    ListHead.next = NL;          /* Clear the boogie     */
  368.  
  369.    free_pdir();   /* Unlock any old lock... */
  370.  
  371.    if ((pdir=Lock(subdir,ACCESS_READ))==0L)
  372.       return("Wrong Diskette?");
  373.    if ( ! Examine(pdir, dir_info) )   return("Wierd Disk Error");
  374.    if ( dir_info->fib_DirEntryType < 0L )   return("Bizzare Alert!!");
  375.  
  376.    more = TRUE;
  377.    return(dmore());
  378.    }
  379.  
  380.  
  381. static char *dmore()
  382.    {
  383.    FAST struct dirent   *p_D = NextEntry;
  384.    FAST struct dirent   *ptr = (struct dirent *)&ListHead;
  385.    FAST struct dirent   *plink;
  386.  
  387.    FAST   TEXT   *p_mung;
  388.  
  389.    FAST long    i;
  390.  
  391.    if ( ! more )   return(NL);
  392.  
  393.    if ( ExNext( pdir, dir_info ) )
  394.       {
  395.  
  396.  
  397.       if ( (ULONG)p_D >=
  398.          ((ULONG)FirstEntry + (ULONG)DBUFSIZ - (ULONG)sizeof(struct dirent)) )
  399.          {
  400.          more = FALSE;
  401.          return("Directory Truncated!");
  402.          }
  403.  
  404.  
  405.       /* Here you can add a file/directory filter   */
  406.       /* filename text string is at &p_D->dE[0   */
  407.       p_D->isfile = ( dir_info->fib_DirEntryType < 0L );
  408.  
  409.       p_mung = (TEXT *)&p_D->dE[0];
  410.       for ( i=0; i<FCHARS; i++)
  411.          if ( ! (*p_mung++ = dir_info->fib_FileName[i]) )   break;
  412.  
  413.       i = (long)p_mung;
  414.       NextEntry = (struct dirent *)( (i+5L) & ~3L );
  415.  
  416.       for ( i=maxent++; i>=0; i--)
  417.          {
  418.          if ( ! (plink = ptr->next)  )   break;
  419.          if ( alpha_lower(p_D,plink) )   break;
  420.          ptr = plink;
  421.          }
  422.       p_D->next = plink;
  423.       ptr->next = p_D;
  424.  
  425.       return(NL);
  426.       }
  427.    else return ( IoErr() == ERROR_NO_MORE_ENTRIES) ?
  428.        (char *)(more = NL) : "Error Reading Directory!!!";
  429.    }
  430.  
  431.  
  432. /* dedicated alphabetizing function for dmore()   */
  433.  
  434. static alpha_lower(snew,sold)
  435.    struct dirent *snew,*sold;
  436.    {
  437.    FAST struct dirent *pnew = snew;
  438.    FAST TEXT *ps1,*ps2, c,d;
  439.  
  440.    if ( pnew->isfile == sold->isfile)
  441.       {
  442.       ps1 = (TEXT *)&pnew->dE[0];
  443.       ps2 = (TEXT *)&sold->dE[0];
  444.       while ( (c=*ps1++) )
  445.          {
  446.          if ( c > (d=*ps2++) )   return(FALSE);
  447.          else if ( c < d )      break;
  448.          }
  449.       return(TRUE);
  450.       }
  451.    return(pnew->isfile);
  452.    }   
  453.  
  454.  
  455.  
  456. /* Display directory stuff   */
  457.  
  458. static cxxx()
  459.    {
  460.    FAST long   i,new;
  461.    FAST long   x,y;
  462.    FAST struct dirent *ohboy = (struct dirent *)&ListHead;
  463.  
  464.    new = curent;
  465.    for ( i=0; i<new; i++)   ohboy = ohboy->next;
  466.  
  467.    y = 20L;
  468.    for (i=0; i<DENTS; i++)
  469.       {
  470.       y += (x=10);
  471.       rname[i].NextText = NL;
  472.       rname[i].IText = (UBYTE *)"";
  473.       rname[i].LeftEdge = 0;
  474.       if ( (new+i) < maxent )
  475.          {
  476.          ohboy = ohboy->next;
  477.          rname[i].IText = (UBYTE *)&ohboy->dE[0];
  478.          if ( ohboy->isfile )   PrintIText(wRp,&rname[i],10L,y);
  479.          else
  480.             {
  481.             rname[i].LeftEdge = 48;
  482.             PrintIText(wRp,&saydir,10L,y);
  483.             PrintIText(wRp,&rname[i],10L,y);
  484.             rname[i].NextText = &saydir;
  485.             }
  486.          x = wRp->cp_x;
  487.          }
  488.       if ( x < ZWDTH+10 )   RectFill(wRp,x,y,(ZWDTH+10L),(y+8L));
  489.       }
  490.    }
  491.  
  492.  
  493. /**************************************************
  494. * rfnam()
  495. *   Combines dir, plus name into dir   */
  496.  
  497. static rfnam(dir,fil_nam)
  498.    char *dir,*fil_nam;
  499.    {
  500.    FAST char   *pdst = dir;
  501.    FAST char   *psrc = fil_nam;
  502.    FAST char   c = ':';
  503.  
  504.    while ( *pdst )
  505.       c = *pdst++;
  506.    if ( c != ':')   *pdst++ = '/';
  507.  
  508.    while ( *pdst++ = *psrc++ )
  509.       ;
  510.    }
  511.  
  512. static struct IntuiText b_txt = {0,1,JAM2, 5,20,NL,NL,    NL};
  513. static struct IntuiText p_txt = {0,1,JAM2, 5,3,NL,(UBYTE *)"OK", NL};
  514.  
  515. /****************************************************************/
  516. /* notify(txt)                     */
  517. /*   Prompts for Yes/No response            */
  518.  
  519. static notify(txt)
  520.    UBYTE *txt;
  521.    {
  522.    b_txt.IText = txt;
  523.    AutoRequest(wRq,&b_txt,0L,&p_txt,0L,0L,
  524.       (long)(IntuiTextLength(&b_txt)+50L),70L);
  525.    }
  526.