Fixed problem with 'encoding' directive for .chr files. Added \LXXXX
[idzebra-moved-to-github.git] / util / res.c
index a65e506..e221dc2 100644 (file)
@@ -1,10 +1,26 @@
-/*
- * Copyright (C) 1994-2002, Index Data
- * All rights reserved.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Id: res.c,v 1.29 2002-04-04 14:14:14 adam Exp $
- */
+/* $Id: res.c,v 1.34 2004-01-22 11:27:22 adam Exp $
+   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
+   Index Data Aps
+
+This file is part of the Zebra server.
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Zebra; see the file LICENSE.zebra.  If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+*/
+
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -29,6 +45,7 @@ struct res_struct {
     char *name;
     int  init;
     Res def_res;
+    Res over_res;
 };
 
 static struct res_entry *add_entry (Res r)
@@ -62,10 +79,13 @@ static void reread (Res r)
 
     val_buf = (char*) xmalloc (val_max);
 
+    if (!r->name)
+       return; 
+
     fr = fopen (r->name, "r");
     if (!fr)
     {
-        logf (LOG_WARN|LOG_ERRNO, "Cannot open %s", r->name);
+        logf (LOG_WARN|LOG_ERRNO, "Cannot open `%s'", r->name);
        return ;
     }
     while (1)
@@ -156,23 +176,31 @@ static void reread (Res r)
     fclose (fr);
 }
 
-Res res_open (const char *name, Res def_res)
+Res res_open (const char *name, Res def_res, Res over_res)
 {
     Res r;
+
+    if (name)
+    {
 #ifdef WIN32
-    if (access (name, 4))
+        if (access (name, 4))
 #else
-    if (access (name, R_OK))
+        if (access (name, R_OK))
 #endif
-    {
-        logf (LOG_LOG|LOG_ERRNO, "Cannot access resource file `%s'", name);
-       return 0;
+        {
+            logf (LOG_WARN|LOG_ERRNO, "Cannot open `%s'", name);
+           return 0;
+        }
     }
     r = (Res) xmalloc (sizeof(*r));
     r->init = 0;
     r->first = r->last = NULL;
-    r->name = xstrdup (name);
+    if (name)
+        r->name = xstrdup (name);
+    else
+       r->name=0;
     r->def_res = def_res;
+    r->over_res = over_res;
     return r;
 }
 
@@ -197,12 +225,40 @@ void res_close (Res r)
     xfree (r);
 }
 
-char *res_get (Res r, const char *name)
+const char *res_get_prefix (Res r, const char *name, const char *prefix,
+                           const char *def)
+{
+    const char *v = 0;;
+    if (prefix)
+    {
+       char rname[128];
+       
+       if (strlen(name) + strlen(prefix) >= (sizeof(rname)-2))
+           return 0;
+       strcpy(rname, prefix);
+       strcat(rname, ".");
+       strcat(rname, name);
+       v = res_get(r, rname);
+    }
+    if (!v)
+       v = res_get(r, name);
+    if (!v)
+       v = def;
+    return v;
+}
+
+const char *res_get (Res r, const char *name)
 {
     struct res_entry *re;
+    const char *v;
 
     if (!r)
        return 0;
+    
+    v = res_get(r->over_res, name);
+    if (v)
+       return v;
+
     if (!r->init)
         reread (r);
     for (re = r->first; re; re=re->next)
@@ -212,9 +268,9 @@ char *res_get (Res r, const char *name)
     return res_get (r->def_res, name);
 }
 
-char *res_get_def (Res r, const char *name, char *def)
+const char *res_get_def (Res r, const char *name, const char *def)
 {
-    char *t;
+    const char *t;
 
     if (!(t = res_get (r, name)))
     {
@@ -236,7 +292,7 @@ int res_get_match (Res r, const char *name, const char *value, const char *s)
     return 0;
 }
 
-void res_put (Res r, const char *name, const char *value)
+void res_set (Res r, const char *name, const char *value)
 {
     struct res_entry *re;
     assert (r);
@@ -289,10 +345,12 @@ int res_write (Res r)
     assert (r);
     if (!r->init)
         reread (r);
+    if (!r->name)
+       return 0; /* ok, this was not from a file */
     fr = fopen (r->name, "w");
     if (!fr)
     {
-        logf (LOG_FATAL|LOG_ERRNO, "Cannot create %s", r->name);
+        logf (LOG_FATAL|LOG_ERRNO, "Cannot create `%s'", r->name);
         exit (1);
     }