home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_USR08B.LHA / gerlib / examples / Find.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  8.3 KB  |  480 lines

  1. /*-- Rev Header - do NOT edit!
  2.  *
  3.  *  Filename : Find.cc
  4.  *  Purpose  : Find a file in a database
  5.  *
  6.  *  Program  : Find
  7.  *  Author   : Gerhard Müller
  8.  *  Copyright: (c) by Gerhard Müller
  9.  *  Creation : Fri Oct  8 00:50:29 1993
  10.  *
  11.  *  compile  : makefile
  12.  *
  13.  *
  14.  *  Compile version  : 0.1
  15.  *  Ext. Version     : 0.1
  16.  *
  17.  *  REVISION HISTORY
  18.  *
  19.  *  Date       C-Version  E-Version    Comment
  20.  *  ---------  ---------  -----------  -------
  21.  *  12.9.93                            work started
  22.  *
  23.  *
  24.  *
  25.  *-- REV_END --
  26.  */
  27.  
  28.     /*
  29.      * C-Includes, C-Definitionen
  30.      *
  31.      */
  32.  
  33. #define class _class
  34. #define template _template
  35.  
  36. extern "C" {
  37. #include <exec/types.h>
  38. #include <exec/ports.h>
  39. #include <exec/memory.h>
  40. #include <exec/execbase.h>
  41. #include <dos/dos.h>
  42. #include <dos/dosextens.h>
  43. #include <dos/exall.h>
  44. #include <dos/rdargs.h>
  45. #include <clib/alib_protos.h>
  46. #include <clib/alib_stdio_protos.h>
  47. #include <inline/stubs.h>
  48. #include <clib/dos_protos.h>
  49. #ifdef __OPTIMIZE__
  50. #include <inline/exec.h>
  51. #include <inline/dos.h>
  52. #include <inline/utility.h>
  53. #else
  54. #include <clib/exec_protos.h>
  55. #include <clib/dos_protos.h>
  56. #include <clib/utility_protos.h>
  57. #endif
  58.  
  59. int     atexit(void (*)(void));
  60. }
  61.  
  62. #undef template
  63. #undef class
  64.  
  65.  
  66. extern "C" {
  67.  
  68. /* dont mangle our global symbols */
  69.  
  70. Library            *UtilityBase;
  71.  
  72. extern ExecBase    *SysBase;
  73.  
  74. extern BPTR stdin;
  75. extern BPTR stdout;
  76. }
  77.  
  78.  
  79.     /*
  80.      * C++-Includes, C++-Definitionen
  81.      *
  82.      */
  83.  
  84. #include "add/baserel/add.h"        // prototypes for all new C-functions
  85. #include "add/baserel/couta.h"        // defines cout
  86. #include "add/baserel/OwnError.h"    // defines err
  87.  
  88.  
  89.     /*
  90.      * our function-prototypes
  91.      *
  92.      */
  93.  
  94. void    OutputError(void);
  95. void    MyExit();
  96. BOOL    MyInit(int alen, char *aptr);
  97. int        main(int alen, char *aptr);
  98. void    OutputError(void);
  99.  
  100.  
  101. /* Version-String */
  102.  
  103. char *version = "\0$VER: Find 0.01 (" __DATE__ ")";
  104.  
  105.  
  106.  
  107. /* things for commandline-parsing */
  108.  
  109. #define TEMPLATE "STRING/A,EXACT/S"
  110.  
  111. typedef struct { char *string; long exact_match; } ARGS;
  112. #define DEFAULT { NULL, NULL }
  113.  
  114. ARGS            args = DEFAULT;    /* here the arguments are stored */
  115.  
  116. struct RDArgs    *rda = NULL;    /* for CLI-startup */
  117. UBYTE            **ToolTypes;     /* for WB-startup */
  118.  
  119.  
  120.  
  121.  
  122.  
  123.     /*
  124.      * This function gets called by exit(), look im MyInit() for corresponding atexit() call
  125.      * frees all system-depended things
  126.      */
  127.  
  128.  
  129. void MyExit()
  130. {
  131.         /* Free arguments passed by commandline allocated by ReadArgs() */
  132.  
  133.     if( rda )
  134.     {
  135.         FreeArgs( rda );
  136.         FreeDosObject( DOS_RDARGS, rda );
  137.     }
  138.  
  139.  
  140.         /* close all our libraries */
  141.  
  142.     if(UtilityBase)
  143.         CloseLibrary(UtilityBase);
  144.  
  145. }
  146.  
  147.  
  148.     /*
  149.      * Open some libraries and make the commandline-parsing
  150.      *
  151.      */
  152.  
  153. BOOL MyInit(int alen, char *aptr)
  154. {
  155.     BOOL ok=FALSE;
  156.  
  157.     /* Are we running under control of Kickstart 2.0 or higher? */
  158.  
  159.     if(SysBase -> LibNode . lib_Version < 37)
  160.     {
  161.         err.NotifyError("needs Workbench & Kickstart Version 2.04 and above !\n");
  162.         return FALSE;
  163.     }
  164.  
  165.  
  166.     atexit(MyExit);            // the function MyExit should be called at exit()-time
  167.  
  168.  
  169.     /* open some libraries, not really needed in this programm, just to show how to do */
  170.  
  171.     if(UtilityBase = OpenLibrary((UBYTE *)"utility.library",37))
  172.     {
  173.         // we have all our libraries
  174.             ok=TRUE;
  175.     }
  176.  
  177.     if(!ok) return FALSE;
  178.  
  179.  
  180.     /* normal startup-code.... */
  181.  
  182.     ok=FALSE;
  183.  
  184.     if(alen)
  185.     {
  186.         /* start from CLI */
  187.         /* parse arguements */
  188.  
  189.         if(rda = (RDArgs *)AllocDosObject( DOS_RDARGS, NULL ))
  190.         {
  191.             rda->RDA_ExtHelp = (UBYTE *)"This could be an extended help - here you get nothing :-)\n";
  192.  
  193.             if(ReadArgs( (UBYTE *)TEMPLATE, (LONG *) &args, rda ) )
  194.             {
  195.                 /* Args erfolgreich gelesen */
  196.  
  197.                 ok=TRUE;
  198.             }
  199.             else
  200.             {
  201.                 err.DosError(IoErr(), "Error parsing commandline");    /* prints the appropriate err message */
  202.             }
  203.         }
  204.         else
  205.             err.NotifyError("Out of memory error !");
  206.     }
  207.     else
  208.     {
  209.         /* Workbench-Start */
  210.  
  211.         err.NotifyError("Start only from CLI !");
  212.  
  213.         ok=FALSE;
  214.     }
  215.  
  216.     return ok;
  217. }
  218.  
  219.  
  220.  
  221.     /*
  222.      * Here the testprogram starts, uses some misc features
  223.      *
  224.      */
  225.  
  226.  
  227. int main(int alen, char *aptr)
  228. {
  229.     if(MyInit(alen,aptr))        // all things went well
  230.     {
  231.         BPTR fh;
  232.  
  233.         if(fh=Open((STRPTR)"FindDB:find.codes",MODE_OLDFILE))
  234.         {
  235.             int file_len;
  236.  
  237.             if(file_len=GetFileSize(fh))
  238.             {
  239.                 char *file_mem;
  240.  
  241.                 if(file_mem=(char *)AllocVec(file_len+1,MEMF_ANY))
  242.                 {
  243.                     if(Read(fh,file_mem,file_len)==file_len)
  244.                     {
  245.                         // Now the file is in Memory
  246.  
  247.                         // terminate with zero
  248.  
  249.                         file_mem[file_len]=0;
  250.  
  251.                         char *WBuf;
  252.  
  253.                         if(WBuf=(char *)AllocVec(1024,MEMF_ANY))
  254.                         {
  255.                             // We have Memory for WildCard Matching
  256.  
  257.                             char *Dir_String;
  258.                             char *Filename_String;
  259.  
  260.                             if(args.exact_match)
  261.                             {
  262.                                 // The easy way
  263.                             }
  264.                             else
  265.                             {
  266.                                 if(!ParsePatternNoCase((STRPTR)args.string,(STRPTR)WBuf,1024))
  267.                                     args.exact_match=TRUE;
  268.                             }
  269.  
  270.  
  271.                             // Now do the search
  272.  
  273.                             char *s=file_mem;
  274.                             BOOL found=FALSE;
  275.                             BOOL stop=FALSE;
  276.  
  277.                             // first search for directories
  278.  
  279.                             cout << "\nDirectories\n-----------\n";
  280.  
  281.                             while(s < file_mem+file_len && (!stop))
  282.                             {
  283.  
  284.                                 // remeber beginning
  285.  
  286.                                 Dir_String=s;
  287.  
  288.  
  289.                                 // search for 0
  290.  
  291.                                 while(*s) s++;
  292.  
  293.                                 if( (s[-1]=='/') || (s[-1]==':'))
  294.                                 {
  295.                                     // We have a Directory
  296.  
  297.                                     // s[-1]=0;    /* kill '/' or ':' */
  298.  
  299.                                     if(args.exact_match)
  300.                                     {
  301.                                         // do pattern matchhing by Hand...
  302.  
  303.                                         if(Stricmp((STRPTR)args.string, (STRPTR)Dir_String)==0)
  304.                                         {
  305.                                             cout << Dir_String << endl;
  306.                                             found=TRUE;
  307.                                         }
  308.                                     }
  309.                                     else
  310.                                     {
  311.                                         if(MatchPatternNoCase((UBYTE *)WBuf,(UBYTE *)Dir_String))
  312.                                         {
  313.                                             cout << Dir_String << endl;
  314.                                             found=TRUE;
  315.                                         }
  316.                                     }
  317.  
  318.                                     /* skip zero */
  319.                                     s++;
  320.                                 }
  321.                                 else
  322.                                 {
  323.                                     /* skip zero */
  324.                                     s++;
  325.                                 }
  326.  
  327.                                 // if the user wants to abort the output, he can..
  328.  
  329.                                 if(SetSignal(0L,0L) & SIGBREAKF_CTRL_C) /* Hit since last check? */
  330.                                 {
  331.                                      SetSignal(0L,SIGBREAKF_CTRL_C);    /* Clear old status */
  332.                                     cout << "\n*** BREAK\n" << endl;
  333.                                     stop=TRUE;
  334.                                 }
  335.                             }
  336.  
  337.  
  338.                             if(!stop)
  339.                             {
  340.  
  341.                                 if(!found)
  342.                                     cout << "- No matches found -\n";
  343.  
  344.  
  345.                                 // search for files
  346.  
  347.                                 cout << "\nFiles\n-----\n";
  348.  
  349.                                 found=FALSE;
  350.                                 s=file_mem;
  351.                                 char *temp;
  352.  
  353.                                 while(s < file_mem+file_len && (!stop))
  354.                                 {
  355.  
  356.                                     // remeber beginning
  357.  
  358.                                     temp=s;
  359.  
  360.  
  361.                                     // search for 0
  362.  
  363.                                     while(*s) s++;
  364.  
  365.                                     if( (s[-1]=='/') || (s[-1]==':'))
  366.                                     {
  367.                                         // We have a Directory
  368.  
  369.                                         Dir_String=temp;
  370.  
  371.                                         /* skip zero */
  372.                                         s++;
  373.                                     }
  374.                                     else
  375.                                     {
  376.                                         // We have a file
  377.  
  378.                                         if(args.exact_match)
  379.                                         {
  380.                                             // do pattern matchhing by Hand...
  381.  
  382.                                             if(Stricmp((STRPTR)args.string, (STRPTR)temp)==0)
  383.                                             {
  384.                                                 cout << Dir_String
  385.                                                      << temp << endl;
  386.                                                 found=TRUE;
  387.                                             }
  388.                                         }
  389.                                         else
  390.                                         {
  391.                                             if(MatchPatternNoCase((UBYTE *)WBuf,(UBYTE *)temp))
  392.                                             {
  393.                                                 cout << Dir_String
  394.                                                      << temp << endl;
  395.                                                 found=TRUE;
  396.                                             }
  397.                                         }
  398.  
  399.                                         /* skip zero */
  400.                                         s++;
  401.                                     }
  402.  
  403.                                     // if the user wants to abort the output, he can..
  404.  
  405.                                     if(SetSignal(0L,0L) & SIGBREAKF_CTRL_C) /* Hit since last check? */
  406.                                     {
  407.                                          SetSignal(0L,SIGBREAKF_CTRL_C);    /* Clear old status */
  408.                                         cout << "\n*** BREAK\n" << endl;
  409.                                         stop=TRUE;
  410.                                     }
  411.                                 }
  412.  
  413.                                 if( (!found) && (!stop) )
  414.                                     cout << "- No matches found -\n";
  415.  
  416.                                 cout << endl << flush;
  417.                             }
  418.  
  419.                             FreeVec(WBuf);
  420.                         }
  421.                         else
  422.                             err.NotifyError("Out of memory error!");
  423.                     }
  424.                     else
  425.                         err.NotifyError("Read error on FindDB:find.codes!");
  426.  
  427.                     FreeVec(file_mem);
  428.                 }
  429.                 else
  430.                     err.NotifyError("Out of memory error!");
  431.             }
  432.             else
  433.                 err.NotifyError("File FindDB:find.codes is empty!");
  434.  
  435.             Close(fh);
  436.         }
  437.         else
  438.             err.NotifyError("Can't access FindDB:find.codes!");
  439.  
  440.     }    // MyInit()
  441.  
  442.  
  443.     // if an error occurred...
  444.  
  445.     OutputError();
  446.  
  447.     return(0);                // exit is called if we fall through
  448. }
  449.  
  450.  
  451.  
  452.  
  453.     /*
  454.      * Output error-message if we have one
  455.      *
  456.      */
  457.  
  458.  
  459. void OutputError(void)
  460. {
  461.     if(err)
  462.     {
  463.         char *err_mem=0;
  464.         char *es;
  465.         LONG err_nr=err.SetErrorNumber(0);
  466.  
  467.         if(es=err.have_error_string())
  468.         {
  469.             cout <<    es << endl;
  470.         }
  471.         else
  472.         {
  473.             if(err_nr)
  474.             {
  475.                 cout <<    "Error-Number: " << err_nr << endl;
  476.             }
  477.         }
  478.     }
  479. }
  480.