[511] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[D-O-H] r631 - in trunk: . owl

daemon@ATHENA.MIT.EDU (nelhage@MIT.EDU)
Thu Oct 29 18:06:57 2009

Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
To: dirty-owl-hackers@mit.edu
From: nelhage@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Wed, 28 Feb 2007 21:07:23 -0500 (EST)

Author: nelhage
Date: 2007-02-28 21:07:22 -0500 (Wed, 28 Feb 2007)
New Revision: 631

Added:
   trunk/owl/obarray.c
Modified:
   trunk/
   trunk/owl/Makefile.in
   trunk/owl/owl.c
   trunk/owl/owl.h
   trunk/owl/tester.c
Log:
 r19146@phanatique:  nelhage | 2007-02-27 23:38:42 -0500
 Adding owl_obarray for interning strings



Property changes on: trunk
___________________________________________________________________
Name: svk:merge
   - bb873fd7-8e23-0410-944a-99ec44c633eb:/branches/owl/filter-rewrite:15925
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/d-o-h/trunk:19145
   + bb873fd7-8e23-0410-944a-99ec44c633eb:/branches/owl/filter-rewrite:15925
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/d-o-h/trunk:19146

Modified: trunk/owl/Makefile.in
===================================================================
--- trunk/owl/Makefile.in	2007-03-01 02:07:15 UTC (rev 630)
+++ trunk/owl/Makefile.in	2007-03-01 02:07:22 UTC (rev 631)
@@ -24,7 +24,7 @@
      regex.c history.c view.c dict.c variable.c filterelement.c pair.c \
      keypress.c keymap.c keybinding.c cmd.c context.c zcrypt.c \
      aim.c buddy.c buddylist.c timer.c style.c stylefunc.c errqueue.c \
-     zbuddylist.c muxevents.c popexec.c
+     zbuddylist.c muxevents.c popexec.c obarray.c
 OWL_SRC = owl.c
 TESTER_SRC = tester.c
 

Added: trunk/owl/obarray.c
===================================================================
--- trunk/owl/obarray.c	                        (rev 0)
+++ trunk/owl/obarray.c	2007-03-01 02:07:22 UTC (rev 631)
@@ -0,0 +1,96 @@
+#include <stdlib.h>
+#include <string.h>
+#include "owl.h"
+
+// Lookup a key in the obarray. If the key exists, return its index,
+// and the interned value in *val. Otherwise, return the index it
+// should be inserted at.
+int owl_obarray_lookup(owl_obarray *oa, char * key, char ** val) /*noproto*/
+{
+  int first, last, mid;
+  char * str;
+  int cmp;
+
+  mid = 0;
+  first = 0;
+  last = owl_list_get_size(&(oa->strings)) - 1;
+  while(first <= last) {
+    mid = first + (last - first)/2;
+    str = (char*)owl_list_get_element(&(oa->strings), mid);
+    cmp = strcmp(key, str);
+    if(cmp == 0) {
+      *val = str;
+      return mid;
+    } else if(cmp < 0) {
+      first = mid + 1;
+    } else {
+      last = mid - 1;
+    }
+  }
+  *val = NULL;
+  return mid;
+}
+
+// Returns NULL if the string doesn't exist in the obarray
+char * owl_obarray_find(owl_obarray *oa, char * string)
+{
+  char *v;
+  owl_obarray_lookup(oa, string, &v);
+  return v;
+}
+
+// Inserts the string into the obarray if it doesn't exist
+char * owl_obarray_insert(owl_obarray *oa, char * string)
+{
+  char *v;
+  int i;
+  i = owl_obarray_lookup(oa, string, &v);
+  if(!v) {
+    v = owl_strdup(string);
+    owl_list_insert_element(&(oa->strings), i, v);
+  }
+  return v;
+}
+
+void owl_obarray_init(owl_obarray *oa)
+{
+  owl_list_create(&(oa->strings));
+}
+
+/**************************************************************************/
+/************************* REGRESSION TESTS *******************************/
+/**************************************************************************/
+
+#ifdef OWL_INCLUDE_REG_TESTS
+
+#include "test.h"
+
+int owl_obarray_regtest(void) {
+  int numfailed = 0;
+  char *p,*p2;
+
+  owl_obarray oa;
+  owl_obarray_init(&oa);
+
+  printf("BEGIN testing owl_obarray\n");
+
+  p = owl_obarray_insert(&oa, "test");
+  FAIL_UNLESS("returned string is equal", p && !strcmp(p, "test"));
+  p2 = owl_obarray_insert(&oa, "test");
+  FAIL_UNLESS("returned string is equal", p2 && !strcmp(p2, "test"));
+  FAIL_UNLESS("returned the same string", p2 && p == p2);
+
+  p = owl_obarray_insert(&oa, "test2");
+  FAIL_UNLESS("returned string is equal", p && !strcmp(p, "test2"));
+  p2 = owl_obarray_find(&oa, "test2");
+  FAIL_UNLESS("returned the same string", p2 && !strcmp(p2, "test2"));
+
+  p = owl_obarray_find(&oa, "nothere");
+  FAIL_UNLESS("Didn't find a string that isn't there", p == NULL);
+
+  printf("END testing owl_obarray (%d failures)\n", numfailed);
+
+  return numfailed;
+}
+
+#endif /* OWL_INCLUDE_REG_TESTS */

Modified: trunk/owl/owl.c
===================================================================
--- trunk/owl/owl.c	2007-03-01 02:07:15 UTC (rev 630)
+++ trunk/owl/owl.c	2007-03-01 02:07:22 UTC (rev 631)
@@ -341,7 +341,7 @@
   /* welcome message */
   owl_function_debugmsg("startup: creating splash message");
   strcpy(startupmsg, "-----------------------------------------------------------------------\n");
-  sprintf(buff,      "Welcome to Owl version %s.  Press 'h' for on-line help. \n", OWL_VERSION_STRING);
+  sprintf(buff,      "Welcome to barnowl version %s.  Press 'h' for on-line help. \n", OWL_VERSION_STRING);
   strcat(startupmsg, buff);
   strcat(startupmsg, "                                                                       \n");
   strcat(startupmsg, "This is an UNOFFICIAL DEVELOPMENT BUILD of owl. If you are using this  \n");

Modified: trunk/owl/owl.h
===================================================================
--- trunk/owl/owl.h	2007-03-01 02:07:15 UTC (rev 630)
+++ trunk/owl/owl.h	2007-03-01 02:07:22 UTC (rev 631)
@@ -511,6 +511,10 @@
   int **pairs;
 } owl_colorpair_mgr;
 
+typedef struct _owl_obarray {
+  owl_list strings;
+} owl_obarray;
+
 typedef struct _owl_global {
   owl_mainwin mw;
   owl_popwin pw;

Modified: trunk/owl/tester.c
===================================================================
--- trunk/owl/tester.c	2007-03-01 02:07:15 UTC (rev 630)
+++ trunk/owl/tester.c	2007-03-01 02:07:22 UTC (rev 631)
@@ -144,12 +144,15 @@
 
 int main(int argc, char **argv, char **env)
 {
+  owl_errqueue_init(owl_global_get_errqueue(&g));
+
   int numfailures=0;
   if (argc==2 && 0==strcmp(argv[1],"reg")) {
     numfailures += owl_util_regtest();
     numfailures += owl_dict_regtest();
     numfailures += owl_variable_regtest();
     numfailures += owl_filter_regtest();
+    numfailures += owl_obarray_regtest();
     if (numfailures) {
       fprintf(stderr, "*** WARNING: %d failures total\n", numfailures);
     }


home help back first fref pref prev next nref lref last post