home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / ii-37 / pattern.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  6KB  |  173 lines

  1. ;/* Pattern.c.   AmigaMail pattern matching example.  Compiled with SAS/C 6.56:
  2. sc NMINC STRMERGE NOSTKCHK NODEBUG DATA=FAR IGNORE=73 Pattern.c
  3. slink from Pattern.o to Pattern lib lib:amiga.lib
  4. quit
  5. */
  6. /*
  7. Copyright (c) 1991 Commodore-Amiga, Inc.
  8.  
  9. This example is provided in electronic form by Commodore-Amiga,
  10. Inc. for use with the Amiga Mail Volume II technical publication.
  11. Amiga Mail Volume II contains additional information on the correct
  12. usage of the techniques and operating system functions presented in
  13. these examples.  The source and executable code of these examples may
  14. only be distributed in free electronic form, via bulletin board or
  15. as part of a fully non-commercial and freely redistributable
  16. diskette.  Both the source and executable code (including comments)
  17. must be included, without modification, in any copy.  This example
  18. may not be published in printed form or distributed with any
  19. commercial product. However, the programming techniques and support
  20. routines set forth in these examples may be used in the development
  21. of original executable software products for Commodore Amiga
  22. computers.
  23.  
  24. All other rights reserved.
  25.  
  26. This example is provided "as-is" and is subject to change; no
  27. warranties are made.  All use is at your own risk. No liability or
  28. responsibility is assumed.
  29. */
  30.  
  31. #include <exec/types.h>
  32. #include <exec/memory.h>
  33. #include <dos/dos.h>
  34. #include <dos/dosasl.h>
  35. #include <dos/rdargs.h>
  36.  
  37. #include <clib/exec_protos.h>
  38. #include <clib/dos_protos.h>
  39. #include <clib/utility_protos.h>
  40.  
  41. /* define pragmas if you have them
  42. #define PRAGMAS */
  43. #ifdef PRAGMAS
  44. #include <pragmas/exec_pragmas.h>
  45. #include <pragmas/dos_pragmas.h>
  46. #include <pragmas/utility_pragmas.h>
  47. #else
  48. struct ExecBase *SysBase;
  49. struct DosLibrary *DOSBase;
  50. struct Library *UtilityBase;
  51. #endif
  52.  
  53. VOID            main(VOID);
  54. UWORD           StrLen(UBYTE *);
  55.  
  56. VOID main(VOID)
  57. {
  58. #ifdef PRAGMAS
  59.     struct DosLibrary *DOSBase;
  60.     struct Library *UtilityBase;
  61.  
  62. #endif
  63.  
  64.     struct RDArgs  *readargs;
  65.     LONG            rargs[3];
  66.     LONG            vargs[4];
  67.     UBYTE         **strings;
  68.     UBYTE          *pattern, *parsebuffer;
  69.     UWORD           case_sensitive, buffersize;
  70.     LONG            iswild, success;
  71.     COUNT           i;
  72.  
  73. #ifndef PRAGMAS
  74.     /* set up SysBase */
  75.     SysBase = (*((struct Library **) 4));
  76. #endif
  77.  
  78.     /* Fail silently if < 37 */
  79.     if (DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37))
  80.     {
  81.         UtilityBase = DOSBase->dl_UtilityBase;
  82.  
  83.         /* See the DOS Autodocs for more information about ReadArgs() */
  84.         if (readargs = ReadArgs("PATTERN/A,CASE/S,STRINGS/A/M", rargs, NULL))
  85.         {
  86.  
  87.             /* The pattern. */
  88.             pattern = (UBYTE *) (rargs[0]);
  89.  
  90.             /*
  91.              * Case sensitive or not? (default not. Note filename matching
  92.              * should ALWAYS be case insensitive).
  93.              */
  94.             case_sensitive = (UWORD) (rargs[1]);
  95.  
  96.             /* Pointer to array of strings to match */
  97.             strings = (UBYTE **) (rargs[2]);
  98.  
  99.             /* Get a buffer big enough to hold all the tokens */
  100.             buffersize = StrLen(pattern) * 3;
  101.  
  102.             if (parsebuffer = AllocMem(buffersize, MEMF_CLEAR))
  103.             {
  104.  
  105.                 /* Parse the pattern, according to case sensitivity flag */
  106.                 if (case_sensitive)
  107.                     iswild = ParsePattern(pattern, parsebuffer, buffersize);
  108.                 else
  109.                 {
  110.                     /* make pattern uppercase in case of character classes */
  111.                     i = 0;
  112.                     while (pattern[i])
  113.                         pattern[i] = ToUpper(pattern[i++]);
  114.                     iswild = ParsePatternNoCase(pattern, parsebuffer, buffersize);
  115.                 }
  116.  
  117.                 /*
  118.                  * -1 if ParsePattern() failed, 0 for no wildcards, 1 for
  119.                  * wildcards. For this I don't care if the supplied pattern had
  120.                  * wildcards or not.
  121.                  */
  122.                 if (iswild != -1)
  123.                 {
  124.                     /* The array of strings is terminated with a NULL */
  125.                     while (*strings)
  126.                     {
  127.  
  128.                         /*
  129.                          * MatchPattern() returns 1 for a successful match, 0
  130.                          * for no match
  131.                          */
  132.                         if (case_sensitive)
  133.                             success = MatchPattern(parsebuffer, *strings);
  134.                         else
  135.                             success = MatchPatternNoCase(parsebuffer, *strings);
  136.                         if (success)
  137.                         {
  138.                             vargs[0] = (LONG) * strings;
  139.                             VFPrintf(Output(), "Match: %s\n", vargs);
  140.                         }
  141.                         else
  142.                         {
  143.                             if (IoErr() != 0)
  144.                             {
  145.                                 VFPrintf(Output(), "Overflow\n", NULL);
  146.                                 break;
  147.                             }
  148.                         }
  149.                         strings++;
  150.                     }
  151.                 }
  152.                 else
  153.                     PrintFault(ERROR_BAD_TEMPLATE, pattern);
  154.                 FreeMem(parsebuffer, buffersize);
  155.             }
  156.             else
  157.                 PrintFault(ERROR_NO_FREE_STORE, NULL);
  158.             FreeArgs(readargs);
  159.         }
  160.         else
  161.             PrintFault(IoErr(), NULL);
  162.         CloseLibrary((struct Library *) DOSBase);
  163.     }
  164. }
  165.  
  166. UWORD StrLen(UBYTE * string)
  167. {
  168.     UBYTE          *length = string + 1;
  169.  
  170.     while (*string++ != '\0');
  171.     return ((UWORD) (string - length));
  172. }
  173.