bug-findutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Patch] Locate: Move counting and limit-checking into visitors.


From: Bas van Gompel
Subject: [Patch] Locate: Move counting and limit-checking into visitors.
Date: Wed, 15 Jun 2005 22:10:55 +0200 (MET DST)
User-agent: slrn/0.9.8.1 (Win32) Hamster/2.0.6.0 KorrNews/4.2

Hi,

Following patch moves limit-checking and counting into visitors.
When neither is done, a visitor is added which will increment
items_accepted once, then remove itself from the list.


Possible ChangeLog-entry:

2005-06-15  Bas van Gompel  <address@hidden>

        * locate/locate.c: Move checking limits and counting into visitors.
        Bail out early when limit is reached.


diff --exclude='*~' -Ndrup curr/findutils/locate/locate.c 
mine2/findutils/locate/locate.c
--- findutils/locate/locate.c   2005-06-13 01:12:56.000000000 +0200
+++ findutils/locate/locate.c   2005-06-15 21:14:16.000000000 +0200
@@ -748,6 +748,51 @@ visit_stats(struct process_data *procdat
 }
 
 
+static int
+visit_limit(struct process_data *procdata, void *context)
+{
+  struct locate_limits *p = context;
+
+  (void) procdata;
+
+  if (++p->items_accepted >= p->limit)
+    return VISIT_ABORT;
+  else
+    return VISIT_CONTINUE;
+}
+
+static int
+visit_count(struct process_data *procdata, void *context)
+{
+  struct locate_limits *p = context;
+
+  (void) procdata;
+
+  ++p->items_accepted;
+  return VISIT_CONTINUE;
+}
+
+static int
+visit_found(struct process_data *procdata, void *context)
+{
+  struct locate_limits *p = context;
+  struct visitor *pv = inspectors, *pp = NULL;
+
+  (void) procdata;
+
+  ++p->items_accepted;
+
+  /* Remove this (last) visitor from the list. */
+  while (pv->next) {
+    pp = pv;
+    pv = pv->next;
+  }
+  pp->next = NULL;
+  lastinspector = pp;
+
+  return VISIT_CONTINUE;
+}
+
 /* Emit the statistics.
  */
 static void
@@ -786,7 +831,8 @@ print_stats(int argc, size_t database_fi
 
 
 /* Print the entries in DBFILE that match shell globbing patterns in ARGV.
-   Return the number of entries printed.  */
+   Return the number of entries matched, or 1 when no limit is in effect, we
+   are not counting and matches were found. */
 
 static unsigned long
 locate (int argc,
@@ -798,6 +844,7 @@ locate (int argc,
        int use_limit,
        struct locate_limits *plimit,
        int stats,
+       int do_count,
        int op_and,
        int regex)
 {
@@ -989,7 +1036,15 @@ locate (int argc,
       else
        add_visitor(visit_justprint_unquoted, NULL);
     }
-  
+
+
+  if (use_limit)
+    add_visitor(visit_limit, plimit);
+  else if (do_count)
+    add_visitor(visit_count, plimit);
+  else
+    add_visitor(visit_found, plimit);
+
 
   if (argc > 1)
     {
@@ -1008,24 +1063,17 @@ locate (int argc,
               procdata.dbfile,
               old_format ? _("old") : "LOCATE02");
     }
-  
+
+
   procdata.c = getc (procdata.fp);
-  while ( (procdata.c != EOF) && (!use_limit || (plimit->limit > 0)) )
-    {
+  /* If we are searching for filename patterns, the inspector list 
+   * will contain an entry for each pattern for which we are searching.
+   */
+  while ( (procdata.c != EOF) &&
+          (VISIT_ABORT != (mainprocessor)(&procdata)) )
+    /* Do nothing */ ;
 
-      /* If we are searching for filename patterns, the inspector list 
-       * will contain an entry for each pattern for which we are searching.
-       */
-      if ((VISIT_ACCEPTED | VISIT_CONTINUE) & (mainprocessor)(&procdata))
-       {
-         if ((++plimit->items_accepted >= plimit->limit) && use_limit)
-           {
-             break;
-           }
-       }
-    }
 
-      
   if (stats)
     {
       print_stats(argc, st.st_size);
@@ -1251,7 +1299,10 @@ main (int argc, char **argv)
     stdout_is_a_tty = false;
 
   next_element (dbpath, 0);    /* Initialize.  */
-  while ((e = next_element ((char *) NULL, 0)) != NULL)
+
+  /* Bail out early if limit already reached. */
+  while ((e = next_element ((char *) NULL, 0)) != NULL  &&
+        (!use_limit || limits.limit > limits.items_accepted))
     {
       statistics.compressed_bytes = 
       statistics.total_filename_count = 
@@ -1269,7 +1320,7 @@ main (int argc, char **argv)
          e = LOCATE_DB;
        }
          
-      found = locate (argc - optind, &argv[optind], e, ignore_case, print, 
basename_only, use_limit, &limits, stats, op_and, regex);
+      found = locate (argc - optind, &argv[optind], e, ignore_case, print, 
basename_only, use_limit, &limits, stats, just_count, op_and, regex);
     }
   
   if (just_count)


L8r,

Buzz.
-- 
  ) |  | ---/ ---/  Yes, this | This message consists of true | I do not
--  |  |   /    /   really is |   and false bits entirely.    | mail for
  ) |  |  /    /    a 72 by 4 +-------------------------------+ any1 but
--  \--| /--- /---  .sigfile. |   |perl -pe "s.u(z)\1.as."    | me. 4^re




reply via email to

[Prev in Thread] Current Thread [Next in Thread]