]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - btree_cache.c
Add support to decompress xz files
[thirdparty/sarg.git] / btree_cache.c
index f04bb55d448f93182adb0e2ba9c1fd6dd671f0ec..5f5caeb8ddeb5a8ee5047ae290e964962f76c82a 100644 (file)
@@ -1,10 +1,11 @@
 /*
- * AUTHOR: Pedro Lineu Orso                         pedro.orso@gmail.com
- *                                                            1998, 2009
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ *     http://sourceforge.net/projects/sarg/forums/forum/363374
  * ---------------------------------------------------------------------
  *
  *  This program is free software; you can redistribute it and/or modify
 #define AVL_DOUBLE_LEFT_ROTATION  4
 
 struct bt {
-           char value[64], targetattr[256];
-           struct bt *left, *right;
-           int balanceinfo;
-           // -1 - ÐÅÒÅ×ÅÛÉ×ÁÅÔ ×ÌÅ×Ï
-           // +1 - ÐÅÒÅ×ÅÛÉ×ÁÅÔ ×ÐÒÁ×Ï
+       char value[64], targetattr[256];
+       struct bt *left, *right;
+       int balanceinfo;
 };
 
 struct bt *root_bt = NULL;
@@ -44,12 +43,12 @@ int sizeof_bt;
 
 FILE *dbgfp = NULL;
 
-void set_balance_info(struct bt *node);
-struct bt *get_disbalanced_node(struct bt *node);
-void balance_node(struct bt *node);
+static void set_balance_info(struct bt *node);
+static struct bt *get_disbalanced_node(struct bt *node);
+static void balance_node(struct bt *node);
 
 
-struct bt *insert_node(struct bt *root, char *item, char *value)
+static struct bt *insert_node(struct bt *root, const char *item, const char *value)
 {
        struct bt *new_item_bt = NULL;
        if (!root)
@@ -57,8 +56,8 @@ struct bt *insert_node(struct bt *root, char *item, char *value)
                new_item_bt = malloc(sizeof_bt);
                new_item_bt->left = NULL;
                new_item_bt->right = NULL;
-               strncpy(new_item_bt->value, item, 64);
-               strncpy(new_item_bt->targetattr, value, 256);
+               safe_strcpy(new_item_bt->value, item, sizeof(new_item_bt->value));
+               safe_strcpy(new_item_bt->targetattr, value, sizeof(new_item_bt->targetattr));
                new_item_bt->balanceinfo = 0;
                return new_item_bt;
        }
@@ -88,7 +87,7 @@ struct bt *insert_node(struct bt *root, char *item, char *value)
 
 
 
-void balance_tree(struct bt *node)
+static void balance_tree(struct bt *node)
 {
        struct bt *disbalanced_node = NULL;
        do
@@ -104,7 +103,7 @@ void balance_tree(struct bt *node)
 
 
 
-void delete_tree(struct bt *root)
+static void delete_tree(struct bt *root)
 {
        if (root)
        {
@@ -115,7 +114,7 @@ void delete_tree(struct bt *root)
        }
 }
 
-struct bt *search_item(struct bt *root, char *item)
+static struct bt *search_item(struct bt *root, const char *item)
 {
        int result;
        while (root && (result = strncmp(root->value, item, 64)))
@@ -128,18 +127,17 @@ struct bt *search_item(struct bt *root, char *item)
        return root;
 }
 
-int get_length(struct bt *node, int d)
+static int get_length(struct bt *node, int d)
 {
        int l_depth = d, r_depth = d;
        if (node->left)
                l_depth = get_length(node->left, d+1);
        if (node->right)
                r_depth = get_length(node->right, d+1);
-       // ÷ÏÚ×ÒÁÝÁÅÍ ÂÏÌØÛÅÅ ÉÚ ÚÎÁÞÅÎÉÊ
        return( ( l_depth > r_depth ) ? l_depth : r_depth );
 }
 
-struct bt *get_parent(struct bt *node)
+static struct bt *get_parent(struct bt *node)
 {
        if (node == root_bt)
                return NULL;
@@ -167,7 +165,7 @@ struct bt *get_parent(struct bt *node)
        }
 }
 
-void set_balance_info(struct bt *node)
+static void set_balance_info(struct bt *node)
 {
        int l_depth = 0, r_depth = 0;
        if (node->left)
@@ -183,7 +181,7 @@ void set_balance_info(struct bt *node)
        node->balanceinfo = r_depth - l_depth;
 }
 
-void rotate_right(struct bt *node)
+static void rotate_right(struct bt *node)
 {
        struct bt *left, *right, *tmp, *parent = get_parent(node);
        left = node->left;
@@ -192,7 +190,7 @@ void rotate_right(struct bt *node)
        node = left;
        tmp->left = right;
        node->right = tmp;
-       
+
        if (root_bt == tmp)
                root_bt = node;
        else
@@ -205,7 +203,7 @@ void rotate_right(struct bt *node)
        }
 }
 
-void rotate_left(struct bt *node)
+static void rotate_left(struct bt *node)
 {
        struct bt *left, *right, *tmp, *parent = get_parent(node);
        left = node->right->left;
@@ -225,10 +223,9 @@ void rotate_left(struct bt *node)
                        if (parent->right == tmp)
                                parent->right = node;
        }
-
 }
 
-int get_disbalance_type(struct bt *node)
+static int get_disbalance_type(struct bt *node)
 {
        if (node->balanceinfo < 0)
                if (node->left->balanceinfo > 0)
@@ -242,7 +239,7 @@ int get_disbalance_type(struct bt *node)
                        return AVL_SINGLE_LEFT_ROTATION;
 }
 
-void balance_node(struct bt *node)
+static void balance_node(struct bt *node)
 {
        switch (get_disbalance_type(node))
        {
@@ -260,14 +257,15 @@ void balance_node(struct bt *node)
                        rotate_left(node);
                        rotate_left(node);
                        break;
-               default:
-                       exit(1);
+               default://compiler pacifier: should never happen!
+                       debuga(__FILE__,__LINE__,_("Failed to balance the b-tree cache"));
+                       exit(EXIT_FAILURE);
                        break;
-                       
+
        }
 }
 
-struct bt *get_disbalanced_node(struct bt *node)
+static struct bt *get_disbalanced_node(struct bt *node)
 {
        struct bt *rdn;
        if (fabs(node->balanceinfo) > 1)
@@ -288,20 +286,18 @@ struct bt *get_disbalanced_node(struct bt *node)
                return NULL;
 }
 
-void init_cache()
+void init_cache(void)
 {
        root_bt = NULL;
        sizeof_bt = sizeof(struct bt);
 }
 
 
-int insert_to_cache(char *key, char *value)
+int insert_to_cache(const char *key, const char *value)
 {
-
        struct bt *root = NULL;
-
        char  strict_chars[] = " ~!@^&(){}|<>?:;\"\'\\[]`,\r\n\0", *strict_chars_ptr;
-       
+
        strict_chars_ptr = strict_chars;
        while (*strict_chars_ptr)
        {
@@ -319,10 +315,9 @@ int insert_to_cache(char *key, char *value)
        }
        else
                return 1;
-
 }
 
-char *search_in_cache(char *key)
+char *search_in_cache(const char *key)
 {
        struct bt *node;
        if ((node = search_item(root_bt, key)))
@@ -333,7 +328,7 @@ char *search_in_cache(char *key)
                return NULL;
 }
 
-void destroy_cache()
+void destroy_cache(void)
 {
        delete_tree(root_bt);
        root_bt = NULL;