gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r15364 - gauger/web


From: gnunet
Subject: [GNUnet-SVN] r15364 - gauger/web
Date: Tue, 31 May 2011 05:03:20 +0200

Author: bartpolot
Date: 2011-05-31 05:03:20 +0200 (Tue, 31 May 2011)
New Revision: 15364

Modified:
   gauger/web/io.php
Log:
Improved and fixed storing algorithm


Modified: gauger/web/io.php
===================================================================
--- gauger/web/io.php   2011-05-31 03:02:00 UTC (rev 15363)
+++ gauger/web/io.php   2011-05-31 03:03:20 UTC (rev 15364)
@@ -280,7 +280,7 @@
 }
 
 /**
- * previous_line: Situates the file cursor at the beginning of the line. In 
case
+ * previous_line: Places the file cursor at the beginning of the line. In case
  *                the cursor is already at the beginning of a line, it goes 
back
  *                to the beginning of the next one. (most usual case)
  * @param $f the file handle
@@ -293,7 +293,7 @@
 }
 
 /**
- * fpeek: Reads the current line and situates the cursor back at the beginning
+ * fpeek: Reads the current line and places the cursor back at the beginning
  *        of it.
  * @param $f the handle to the file
  * @return the line read at the position of the cursor
@@ -306,7 +306,7 @@
 }
 
 /**
- * get_line: Reads the current line and situates the cursor back at the
+ * get_line: Reads the current line and places the cursor back at the
  *           beginning of it. Returns the resulting as an array [rev, value]
  * @param $f the handle to the file
  * @return array containing the revision and the value on the line
@@ -345,6 +345,28 @@
 }
 
 /**
+ * jump_to_postion: sets the cursor in the file at the beginning of a line,
+ * jumping either backwards or forwards. Guarantees to move the cursor as long
+ * as the new position is different from the current one.
+ * @param $f file descriptor
+ * @param $current current cursor position
+ * @param $pos float representing the new position for the cursor
+ */
+function jump_to_position($f, $current, $pos) {
+//     echo "JUMP $current->$pos # ";
+    fseek($f, 0, SEEK_END);
+    $size = ftell($f);
+    if ($pos > $size) $pos = $size;
+    if ($pos < 0) $pos = 0;
+    fseek($f, $pos, SEEK_SET);
+    line_beginning($f);
+    if (ftell($f) == $current) {
+        fgets($f, 512);
+    }
+//     echo ftell($f) . "\n";
+}
+
+/**
  * find_position: finds the position in a file where a given index should go
  * @param $f    the handler to the file
  * @param $size the size of the file
@@ -352,19 +374,19 @@
  * @return the position in the file where the data could be
  */
 function find_position($f, $size, $rev) {
-    $pos = $gap = (int)($size/2);
-    $lastpos = 0;
-    fseek($f, $pos, SEEK_SET);
-    while ($gap > 0.5) { // max log2(size) steps
-        $gap /= 2;
-        $pos = ftell($f);
+    fseek($f, $size/2, SEEK_SET);
+    line_beginning($f);
+    $old = 0;
+    $pos = ftell($f);
+
+    do {
         $l = get_line($f);
 
         if((int)$rev < (int)$l[0]) {
-            if(ftell($f) == 0) {
+            if($pos == 0) {
                 return 0;
             }
-            fseek($f, $pos - $gap, SEEK_SET);
+            jump_to_position($f, $pos, $pos-abs(($pos-$old)/2));
         } else if((int)$rev > (int)$l[0]) {
             fgets($f, 512);
             if(ftell($f) == $size) {
@@ -374,11 +396,13 @@
             if((int)$rev <= (int)$l[0]) {
                 break;
             }
-            fseek($f, min($pos + $gap, $size-1), SEEK_SET);
+            jump_to_position($f, $pos, $pos+abs(($pos-$old)/2));
         } else {
             break;
         }
-    }
+        $old = $pos;
+        $pos = ftell($f);
+    } while ($pos != $old);
 
     return ftell($f);
 }




reply via email to

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