]> git.neil.brown.name Git - TABS.git/blob - database/names_db.c
Terminal Alocation and Booking System - June 2006
[TABS.git] / database / names_db.c
1 #include        "db_header.h"
2
3     
4 /* this database records the mappings from names to numbers
5  * and numbers to names
6  */
7 book_changeres set_namenum(name_mapping * mapping)
8 {
9     static book_changeres result;
10     char key[20];
11     item k;
12     static map_value oldvalue;
13     static int oldkey;
14     extern char * value_key();
15
16     /* look for map(keyint) => old_string */
17     result = read_db(k = key_int(key, C_NAMES + mapping->type, mapping->key),
18                      &oldvalue, sizeof(oldvalue),
19                      (xdrproc_t)xdr_map_value, CONFIG);
20
21     if (result == C_OK)
22     {
23         /* found  map(keyint) => old_string
24          * remove map(old_string) => keyint
25          */
26         k = key_string(C_NUMBERS + mapping->type, oldvalue);
27         result = delete_from_db(k, CONFIG);
28         free(k.item_val);
29     }
30
31     if (mapping->value && mapping->value[0])
32     {
33         /* look for map(new_string) => otherkey */
34         result = read_db(k= key_string(C_NUMBERS + mapping->type, mapping->value),
35                          &oldkey, sizeof(oldkey),
36                          (xdrproc_t)xdr_int, CONFIG);
37         free(k.item_val);
38         if (result == C_OK)
39         {
40             /* found  map(new_string) => otherkey
41              * remove map(otherkey) => new_string
42              */
43             result = delete_from_db(key_int(key, C_NAMES + mapping->type, oldkey),
44                            CONFIG);
45         }
46
47         /* now write map(keyint) => newstring */
48         result = write_db(key_int(key, C_NAMES + mapping->type, mapping->key),
49                           &mapping->value, 
50                           (xdrproc_t)xdr_map_value, CONFIG);
51
52         if (result == C_OK)
53         {
54             /* write map(newstring) => keyint  */
55             result = write_db(k = key_string(C_NUMBERS + mapping->type, mapping->value),
56                               &mapping->key, (xdrproc_t)xdr_int, CONFIG);
57             free(k.item_val);
58         }
59     }
60     else
61     {
62         /* remove map(keyint) => oldstr */
63         result = delete_from_db(key_int(key, C_NAMES + mapping->type, mapping->key),
64                                 CONFIG);
65     }
66     return result;
67 }