Added skeleton for query charset conversion. Bug #977.
[yaz-moved-to-github.git] / src / zoom-opt.c
index bda3051..e7e77cb 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2000-2004, Index Data
+ * Copyright (C) 1995-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: zoom-opt.c,v 1.2 2004-10-15 00:19:02 adam Exp $
+ * $Id: zoom-opt.c,v 1.6 2007-01-03 08:42:15 adam Exp $
  */
 /**
  * \file zoom-opt.c
@@ -37,10 +37,10 @@ ZOOM_options_create_with_parent2 (ZOOM_options parent1, ZOOM_options parent2)
     opt->entries = 0;
     opt->parent1= parent1;
     if (parent1)
-       (parent1->refcount)++;
+        (parent1->refcount)++;
     opt->parent2= parent2;
     if (parent2)
-       (parent2->refcount)++;
+        (parent2->refcount)++;
     return opt;
 }
 
@@ -69,24 +69,39 @@ ZOOM_API(void)
 ZOOM_options_destroy (ZOOM_options opt)
 {
     if (!opt)
-       return;
+        return;
     (opt->refcount)--;
     if (opt->refcount == 0)
     {
-       struct ZOOM_options_entry *e;
-       
-       ZOOM_options_destroy (opt->parent1);
-       ZOOM_options_destroy (opt->parent2);
-       e = opt->entries;
-       while (e)
-       {
-           struct ZOOM_options_entry *e0 = e;
-           xfree (e->name);
-           xfree (e->value);
-           e = e->next;
-           xfree (e0);
-       }
-       xfree (opt);
+        struct ZOOM_options_entry *e;
+        
+        ZOOM_options_destroy (opt->parent1);
+        ZOOM_options_destroy (opt->parent2);
+        e = opt->entries;
+        while (e)
+        {
+            struct ZOOM_options_entry *e0 = e;
+            xfree (e->name);
+            xfree (e->value);
+            e = e->next;
+            xfree (e0);
+        }
+        xfree (opt);
+    }
+}
+
+/* PRIVATE to ZOOM_options_setl() */
+static void
+_set_value(struct ZOOM_options_entry **e, const char *value, int len)
+{
+    (*e)->value = 0;
+    (*e)->len = 0;
+    if (value)
+    {
+        (*e)->value = (char *) xmalloc (len+1);
+        memcpy ((*e)->value, value, len);
+        (*e)->value[len] = '\0';
+        (*e)->len = len;
     }
 }
 
@@ -99,29 +114,17 @@ ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value,
     e = &opt->entries;
     while (*e)
     {
-       if (!strcmp((*e)->name, name))
-       {
-           xfree ((*e)->value);
-            (*e)->value = 0;
-            if (value)
-            {
-                (*e)->value = (char *) xmalloc (len+1);
-                memcpy ((*e)->value, value, len);
-                (*e)->value[len] = '\0';
-            }
-           return;
-       }
-       e = &(*e)->next;
+        if (!strcmp((*e)->name, name))
+        {
+            xfree ((*e)->value);
+            _set_value(e, value, len);
+            return;
+        }
+        e = &(*e)->next;
     }
     *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e));
     (*e)->name = xstrdup (name);
-    (*e)->value = 0;
-    if (value)
-    {
-        (*e)->value = (char *) xmalloc (len+1);
-        memcpy ((*e)->value, value, len);
-        (*e)->value[len] = '\0';
-    }
+    _set_value(e, value, len);
     (*e)->next = 0;
 }
 
@@ -132,39 +135,47 @@ ZOOM_options_set (ZOOM_options opt, const char *name, const char *value)
 }
 
 ZOOM_API(const char *)
-ZOOM_options_get (ZOOM_options opt, const char *name)
+ZOOM_options_getl (ZOOM_options opt, const char *name, int *lenp)
 {
     const char *v = 0;
     if (!opt)
-       return 0;
+        return 0;
     if (opt->callback_func)
-       v = (*opt->callback_func)(opt->callback_handle, name);
+        v = (*opt->callback_func)(opt->callback_handle, name);
     if (!v)
     {
-       struct ZOOM_options_entry *e;
-       for (e = opt->entries; e; e = e->next)
-           if (!strcmp(e->name, name))
-           {
-               v = e->value;
-               break;
-           }
+        struct ZOOM_options_entry *e;
+        for (e = opt->entries; e; e = e->next)
+            if (!strcmp(e->name, name))
+            {
+                v = e->value;
+                *lenp = e->len;
+                break;
+            }
     }
     if (!v)
-       v = ZOOM_options_get(opt->parent1, name);
+        v = ZOOM_options_getl(opt->parent1, name, lenp);
     if (!v)
-       v = ZOOM_options_get(opt->parent2, name);
+        v = ZOOM_options_getl(opt->parent2, name, lenp);
     return v;
 }
 
+ZOOM_API(const char *)
+ZOOM_options_get (ZOOM_options opt, const char *name)
+{
+    int dummy;
+    return ZOOM_options_getl(opt, name, &dummy);
+}
+
 ZOOM_API(int)
 ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa)
 {
     const char *v = ZOOM_options_get (opt, name);
 
     if (!v)
-       return defa;
+        return defa;
     if (!strcmp (v, "1") || !strcmp(v, "T"))
-       return 1;
+        return 1;
     return 0;
 }
 
@@ -174,7 +185,7 @@ ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa)
     const char *v = ZOOM_options_get (opt, name);
 
     if (!v || !*v)
-       return defa;
+        return defa;
     return atoi(v);
 }
 
@@ -186,3 +197,11 @@ ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
     sprintf (s, "%d", value);
     ZOOM_options_set (opt, name, s);
 }
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+