1 //  store.h
2 //  hosts2zones
3 //
4 //  Created by Dr. Rolf Jansen on 2014-08-01.
5 //  Copyright (c) 2014 projectworld.net. All rights reserved.
6 //
7 //  Redistribution and use in source and binary forms, with or without modification,
8 //  are permitted provided that the following conditions are met:
9 //
10 //  1. Redistributions of source code must retain the above copyright notice,
11 //     this list of conditions and the following disclaimer.
12 //
13 //  2. Redistributions in binary form must reproduce the above copyright notice,
14 //     this list of conditions and the following disclaimer in the documentation
15 //     and/or other materials provided with the distribution.
16 //
17 //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
18 //  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 //  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
20 //  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 //  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 //  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 //  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
24 //  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 
27 #pragma mark ••• AVL Tree •••
28 
29 #pragma mark ••• Value Data Types •••
30 
31 // Data Types in the key/name-value store -- negative values are dynamic.
32 enum
33 {
34    dynamic    = -1,  // multiply kind with -1 if the data has been dynamically allocated
35    Empty      =  0,  // the key is the data
36    Simple     =  1,  // boolean, integer, floating point, etc. values
37    Data       =  2,  // any kind of structured or unstructured data
38    String     =  3,  // a nul terminated string
39    Dictionary =  5,  // a dictionary table, i.e. another key/name-value store
40 };
41 
42 typedef struct
43 {
44    llong   kind;     // negative kinds indicate dynamically allocated data
45    union
46    {
47       bool    b;     // a boolean value
48       llong   i;     // an integer
49       double  d;     // a floating point number
50       time_t  t;     // a time stamp
51 
52       char   *s;     // a string
53       void   *p;     // a pointer to anything
54    };
55 } Value;
56 
57 
58 typedef struct Node
59 {
60    // key
61    char    *name;    // the name is the key
62    ssize_t  naml;    // char length of the name
63 
64    // value
65    Value  value;
66 
67    // house holding
68    int          B;
69    struct Node *L, *R;
70 } Node;
71 
72 
73 // CAUTION: The following recursive functions must not be called with name == NULL.
74 //          For performace reasons no extra error cheking is done.
75 Node *findTreeNode(const char *name, Node  *node);
76 int    addTreeNode(const char *name, ssize_t naml, Value *value, Node **node, Node **passed);
77 int removeTreeNode(const char *name, Node **node);
78 void   releaseTree(Node *node);
79 
80 
81 #pragma mark ••• Hash Table •••
82 
83 Node **createTable(uint n);
84 void  releaseTable(Node *table[]);
85 
86 // Storing and retrieving values by name
87 Node  *findName(Node *table[], const char *name, ssize_t naml);
88 Node *storeName(Node *table[], const char *name, ssize_t naml, Value *value);
89 void removeName(Node *table[], const char *name, ssize_t naml);
90