ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/metadata.c
Revision: 296
Committed: Mon Dec 5 20:41:51 2005 UTC (20 years, 8 months ago) by nenolod
Content type: text/x-csrc
File size: 2284 byte(s)
Log Message:
- Add a simple metadata API for extending channel/client/whatever structs
  via adding a single dlink_list to the structure you wish to make extensible.

Reviewed by db.

File Contents

# User Rev Content
1 nenolod 296 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * metadata.c: metadata system
4     *
5     * Copyright (C) 2005 by the past and present ircd coders, and others.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22     * $Id$
23     */
24    
25     #include "stdinc.h"
26     #include "metadata.h"
27    
28     struct Metadata *
29     add_metadata_str(dlink_list *list, char *key, char *strval)
30     {
31     struct Metadata *mdptr;
32    
33     assert(key != NULL);
34     assert(strval != NULL);
35    
36     if (find_metadata(list, key) != NULL)
37     return NULL;
38    
39     mdptr = MyMalloc(sizeof(struct Metadata));
40     memset(mdptr, 0, sizeof(struct Metadata));
41     DupString(key, mdptr->key);
42     DupString(strval, mdptr->u.strval);
43    
44     dlinkAdd(mdptr, make_dlink_node(), list);
45    
46     return mdptr;
47     }
48    
49     struct Metadata *
50     add_metadata_int(dlink_list *list, char *key, int intval)
51     {
52     struct Metadata *mdptr;
53    
54     assert(key != NULL);
55    
56     if (find_metadata(list, key) != NULL)
57     return NULL;
58    
59     mdptr = MyMalloc(sizeof(struct Metadata));
60     memset(mdptr, 0, sizeof(struct Metadata));
61     DupString(key, mdptr->key);
62     mdptr->u.intval = intval;
63    
64     dlinkAdd(mdptr, make_dlink_node(), list);
65    
66     return mdptr;
67     }
68    
69     struct Metadata *
70     find_metadata(dlink_list *list, char *key)
71     {
72     dlink_node *n;
73    
74     assert(key != NULL);
75    
76     DLINK_FOREACH(n, list->head)
77     {
78     struct Metadata *md = (struct Metadata *) n->data;
79    
80     if (!irccmp(key, md->key))
81     return md;
82     }
83    
84     return NULL;
85     }
86    
87     void
88     destroy_metadata(dlink_list *list, char *key)
89     {
90     struct Metadata *md;
91    
92     assert(key != NULL);
93    
94     if ((md = find_metadata(list, key)) == NULL)
95     return;
96    
97     if (md->u.strval != NULL)
98     MyFree(md->u.strval);
99    
100     dlinkFindDelete(list, md);
101    
102     MyFree(md);
103     }