home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games 1996 October / Amiga_Games_Extra_CD_10-96.bin / userbox / publicdomain / grabkey / source / gk_misc.c < prev    next >
C/C++ Source or Header  |  1996-07-15  |  4KB  |  263 lines

  1. /*
  2. **    GrabKEY
  3. **
  4. **        © 1996 by Timo C. Nentwig
  5. **        All Rights Reserved !
  6. **
  7. **        Tcn@techbase.in-berlin.de
  8. **
  9. **
  10. */
  11.  
  12. /// #include
  13.  
  14. #include "gk_GST.h"
  15. #include "gk_Protos.h"
  16.  
  17. ///
  18. /// proto
  19.  
  20. proto BOOL      atob            (const STRPTR Key);
  21. proto ULONG     ErrorRequest    (const STRPTR Gadgets, const STRPTR Format, ...);
  22. proto BOOL      Exists          (const STRPTR PathFile);
  23. proto STRPTR    GetFile         (VOID);
  24. proto ULONG     ShowRequest     (const STRPTR Gadgets, const STRPTR Format, ...);
  25. proto BOOL      StrEmpty        (const STRPTR str);
  26.  
  27. ///
  28.  
  29. /// atob ()
  30.  
  31.     /*
  32.      *    FUNCTION    Convert a string to boolean.
  33.      *
  34.      *    NOTE
  35.      *
  36.      *    EXAMPLE     StrToBool ("on", FALSE);
  37.      *
  38.      */
  39.  
  40. BOOL
  41. atob (const STRPTR Key)
  42. {
  43.  
  44.     const struct { STRPTR Key; BOOL Value; } Table [] =
  45.     {
  46.  
  47.         "yes",      TRUE,
  48.         "true",     TRUE,
  49.         "on",       TRUE,
  50.         "1",        TRUE,
  51.         "no",       FALSE,
  52.         "false",    FALSE,
  53.         "off",      FALSE,
  54.         "0",        FALSE,
  55.  
  56.         NULL,       -1
  57.  
  58.     };
  59.  
  60.  
  61.     LONG    i;
  62.  
  63.     for (i = 0; i < NumElements (Table); i++)
  64.     {
  65.  
  66.         if (stricmp (Table [i] . Key, Key) == 0)
  67.         {
  68.  
  69.             return (Table [i] . Value);
  70.  
  71.         }
  72.  
  73.     }
  74.  
  75. }
  76.  
  77. ///
  78. /// ErrorRequest ()
  79.  
  80.     /*
  81.      *    FUNCTION    ShowRequest() for errors, playing
  82.      *                also error sound.
  83.      *
  84.      *    NOTE
  85.      *
  86.      *    EXAMPLE     ErrorRequest ("OK|Not OK|Shut Up", "This is my cool text");
  87.      *
  88.      */
  89.  
  90. ULONG
  91. ErrorRequest (const STRPTR Gadgets, const STRPTR Format, ...)
  92. {
  93.  
  94.     UBYTE    Text [1024];
  95.  
  96.     va_list   Args;
  97.     va_start (Args, Format);
  98.     vsprintf (Text, Format, Args);
  99.     va_end   (Args);
  100.  
  101.     PlaySound (Set -> Sound . Error);
  102.  
  103.     return (ShowRequest (Gadgets, Text));
  104.  
  105. }
  106.  
  107. ///
  108. /// Exists ()
  109.  
  110.     /*
  111.      *    FROM        /csh/comm3.c
  112.      *
  113.      *    FUNCTION    Check if specified file or dir
  114.      *                exists.
  115.      *
  116.      *    NOTE
  117.      *
  118.      *    EXAMPLE     Exists ("path:file");
  119.      *
  120.      */
  121.  
  122.  
  123. BOOL
  124. Exists (const STRPTR PathFile)
  125. {
  126.  
  127.     BPTR    lock;
  128.  
  129.     if (lock = Lock (PathFile, ACCESS_READ))
  130.     {
  131.  
  132.         UnLock (lock);
  133.         return (TRUE);
  134.  
  135.     }
  136.  
  137.     return (FALSE);
  138.  
  139. }
  140.  
  141. ///
  142. /// GetFile ()
  143.  
  144.     /*
  145.      *    FUNCTION    Get a file via file
  146.      *                reqtools reqeuster.
  147.      *
  148.      *    NOTE
  149.      *
  150.      *    EXAMPLE     STRPTR File = GetFile ();
  151.      *
  152.      */
  153.  
  154.  
  155. STRPTR
  156. GetFile (VOID)
  157. {
  158.  
  159.     struct    rtFileRequester   *FileReq;
  160.  
  161.     if (FileReq = rtAllocRequestA (RT_FILEREQ, NULL))
  162.     {
  163.  
  164.         UBYTE    File [TT_LEN];
  165.                  File [0] = NULL;
  166.  
  167.         if (rtFileRequest (FileReq, File, LocaleString (MSG_GUI_SELECT_FILE), TAG_END))
  168.         {
  169.  
  170.             UBYTE    Buffer [TT_LEN];
  171.  
  172.             STRCPY  (Buffer, FileReq -> Dir);
  173.             AddPart (Buffer, File, sizeof (Buffer));
  174.  
  175.             return (Buffer);
  176.  
  177.         }
  178.  
  179.         rtFreeRequest (FileReq);
  180.  
  181.     }
  182.  
  183.     return (NULL);
  184.  
  185. }
  186.  
  187. ///
  188. /// ShowRequest ()
  189.  
  190.     /*
  191.      *    FUNCTION    Show a requester with given
  192.      *                text and gadgets.
  193.      *
  194.      *    NOTE
  195.      *
  196.      *    EXAMPLE     ShowRequest ("OK|Not OK", "This is %ld", 120);
  197.      *
  198.      */
  199.  
  200. ULONG
  201. ShowRequest (const STRPTR Gadgets, const STRPTR Format, ...)
  202. {
  203.  
  204.     UBYTE    Text [1024];
  205.  
  206.     va_list   Args;
  207.     va_start (Args, Format);
  208.     vsprintf (Text, Format, Args);
  209.     va_end   (Args);
  210.  
  211.     return (rtEZRequest (Text, Gadgets, NULL, NULL));
  212.  
  213. }
  214.  
  215. ///
  216. /// StrEmpty ()
  217.  
  218.     /*
  219.      *    FUNCTION    Check if a string is empty or not.
  220.      *
  221.      *    NOTE
  222.      *
  223.      *    EXAMPLE     StrEmpty ("  ");
  224.      *
  225.      */
  226.  
  227.  
  228. BOOL
  229. StrEmpty (const STRPTR str)
  230. {
  231.  
  232.     if (str [0] == '\0')
  233.     {
  234.  
  235.         return (TRUE);
  236.  
  237.     }
  238.     else
  239.     {
  240.  
  241.         UWORD    i;
  242.  
  243.         for (i = 0; str [i] != '\0'; i++)
  244.         {
  245.  
  246.             if (str [i] != ' ' && str [i] != '\t')      // non-space found
  247.             {
  248.  
  249.                 return (FALSE);
  250.  
  251.             }
  252.  
  253.         }
  254.  
  255.         return (TRUE);
  256.  
  257.     }
  258.  
  259. }
  260.  
  261. ///
  262.  
  263.