Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / lib-config / get.c
index 124d710129c98d4bb601097a01a4aa0d25c241e2..c0f6a3dd2db82b0f60c8e372603fe749638aa8a7 100644 (file)
@@ -13,9 +13,9 @@
     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 this program; if not, write to the Free Software
-    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 #include "module.h"
@@ -38,11 +38,16 @@ CONFIG_NODE *config_node_find(CONFIG_NODE *node, const char *key)
        return NULL;
 }
 
-/* find the section from node - if not found create it unless new_type is -1.
-   you can also specify in new_type if it's NODE_TYPE_LIST or NODE_TYPE_BLOCK */
 CONFIG_NODE *config_node_section(CONFIG_NODE *parent, const char *key, int new_type)
+{
+        return config_node_section_index(parent, key, -1, new_type);
+}
+
+CONFIG_NODE *config_node_section_index(CONFIG_NODE *parent, const char *key,
+                                      int index, int new_type)
 {
        CONFIG_NODE *node;
+        int nindex;
 
        g_return_val_if_fail(parent != NULL, NULL);
        g_return_val_if_fail(is_node_list(parent), NULL);
@@ -50,14 +55,22 @@ CONFIG_NODE *config_node_section(CONFIG_NODE *parent, const char *key, int new_t
        node = key == NULL ? NULL : config_node_find(parent, key);
        if (node != NULL) {
                g_return_val_if_fail(new_type == -1 || new_type == node->type, NULL);
-                return node;
+               nindex = g_slist_index(parent->value, node);
+               if (index >= 0 && nindex != index &&
+                   nindex <= g_slist_length(parent->value)) {
+                       /* move it to wanted position */
+                       parent->value = g_slist_remove(parent->value, node);
+                       parent->value = g_slist_insert(parent->value, node, index);
+               }
+               return node;
        }
 
        if (new_type == -1)
                return NULL;
 
        node = g_new0(CONFIG_NODE, 1);
-       parent->value = g_slist_append(parent->value, node);
+       parent->value = index < 0 ? g_slist_append(parent->value, node) :
+               g_slist_insert(parent->value, node, index);
 
        node->type = new_type;
        node->key = key == NULL ? NULL : g_strdup(key);
@@ -65,8 +78,6 @@ CONFIG_NODE *config_node_section(CONFIG_NODE *parent, const char *key, int new_t
        return node;
 }
 
-/* find the section with the whole path.
-   create the path if necessary `create' is TRUE. */
 CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int create)
 {
        CONFIG_NODE *node;
@@ -156,46 +167,6 @@ int config_get_bool(CONFIG_REC *rec, const char *section, const char *key, int d
         return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y';
 }
 
-/* Return value of key `value_key' from list item where `key' is `value' */
-const char *config_list_find(CONFIG_REC *rec, const char *section, const char *key, const char *value, const char *value_key)
-{
-       CONFIG_NODE *node;
-
-       node = config_list_find_node(rec, section, key, value, value_key);
-       return node != NULL && node->type == NODE_TYPE_KEY ?
-               node->value : NULL;
-}
-
-/* Like config_list_find(), but return node instead of it's value */
-CONFIG_NODE *config_list_find_node(CONFIG_REC *rec, const char *section, const char *key, const char *value, const char *value_key)
-{
-       CONFIG_NODE *node, *keynode;
-       GSList *tmp;
-
-       g_return_val_if_fail(rec != NULL, NULL);
-       g_return_val_if_fail(key != NULL, NULL);
-       g_return_val_if_fail(value_key != NULL, NULL);
-
-       node = config_node_traverse(rec, section, FALSE);
-       if (node == NULL || !is_node_list(node)) return NULL;
-
-       for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
-               node = tmp->data;
-
-               if (node->type != NODE_TYPE_BLOCK)
-                       continue;
-
-               /* key matches value? */
-               keynode = config_node_find(node, key);
-               if (keynode == NULL || keynode->type != NODE_TYPE_KEY ||
-                   g_strcasecmp(keynode->value, value) != 0) continue;
-
-               return config_node_find(node, value_key);
-       }
-
-       return NULL;
-}
-
 char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def)
 {
        CONFIG_NODE *node;
@@ -228,41 +199,6 @@ int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def)
                (i_toupper(*str) == 'O' && i_toupper(str[1]) == 'N');
 }
 
-/* Get the value of keys `key' and `key_value' and put them to
-   `ret_key' and `ret_value'. Returns -1 if not found. */
-int config_node_get_keyvalue(CONFIG_NODE *node, const char *key, const char *value_key, char **ret_key, char **ret_value)
-{
-       CONFIG_NODE *keynode, *valuenode;
-       GSList *tmp;
-
-       g_return_val_if_fail(node != NULL, -1);
-       g_return_val_if_fail(key != NULL, -1);
-       g_return_val_if_fail(value_key != NULL, -1);
-       g_return_val_if_fail(ret_key != NULL, -1);
-       g_return_val_if_fail(ret_value != NULL, -1);
-
-       for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
-               node = tmp->data;
-
-               if (node->type != NODE_TYPE_BLOCK)
-                       continue;
-
-               keynode = config_node_find(node, key);
-               if (keynode == NULL || keynode->type != NODE_TYPE_KEY)
-                       continue;
-
-               valuenode = config_node_find(node, value_key);
-
-               *ret_key = keynode->key;
-               *ret_value = valuenode != NULL && valuenode->type == NODE_TYPE_KEY ?
-                       valuenode->value : NULL;
-               return 0;
-       }
-
-       return -1;
-}
-
-/* Return all values from from the list `node' in a g_strsplit() array */
 char **config_node_get_list(CONFIG_NODE *node)
 {
        GString *values;
@@ -278,7 +214,7 @@ char **config_node_get_list(CONFIG_NODE *node)
                node = tmp->data;
 
                if (node->type == NODE_TYPE_VALUE)
-                       g_string_sprintfa(values, "%s ", (char *) node->value);
+                       g_string_append_printf(values, "%s ", (char *) node->value);
        }
 
        /* split the values to **str array */
@@ -293,23 +229,53 @@ char **config_node_get_list(CONFIG_NODE *node)
         return ret;
 }
 
-/* Returns n'th node from list. */
-CONFIG_NODE *config_node_index(CONFIG_NODE *node, int index)
+CONFIG_NODE *config_node_nth(CONFIG_NODE *node, int index)
 {
        GSList *tmp;
 
        g_return_val_if_fail(node != NULL, NULL);
        g_return_val_if_fail(is_node_list(node), NULL);
 
-       for (tmp = node->value; tmp != NULL; tmp = tmp->next, index--) {
-               if (index == 0)
-                        return tmp->data;
+       for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
+               CONFIG_NODE *node = tmp->data;
+
+               if (node->type != NODE_TYPE_COMMENT) {
+                       if (index == 0)
+                               return node;
+                       index--;
+               }
        }
 
        return NULL;
 }
 
-/* Returns the first non-comment node in list */
+int config_node_index(CONFIG_NODE *parent, const char *key)
+{
+       CONFIG_NODE *node;
+       GSList *tmp;
+       int index;
+
+       g_return_val_if_fail(parent != NULL, -1);
+       g_return_val_if_fail(key != NULL, -1);
+
+       node = config_node_find(parent, key);
+       if (node == NULL)
+               return -1;
+
+       index = 0;
+       for (tmp = parent->value; tmp != NULL; tmp = tmp->next) {
+               CONFIG_NODE *tmpnode = tmp->data;
+
+               if (tmpnode == node)
+                       return index;
+
+               if (tmpnode->type != NODE_TYPE_COMMENT)
+                       index++;
+       }
+
+        return -1;
+}
+
 GSList *config_node_first(GSList *list)
 {
        while (list != NULL) {
@@ -322,7 +288,6 @@ GSList *config_node_first(GSList *list)
        return list;
 }
 
-/* Returns the next non-comment node in list */
 GSList *config_node_next(GSList *list)
 {
        list = list->next;