… | |
… | |
3 | * Rights to this code are documented in doc/pod/license.pod. |
3 | * Rights to this code are documented in doc/pod/license.pod. |
4 | * |
4 | * |
5 | * NOTE: This is a work in progress and will probably change considerably |
5 | * NOTE: This is a work in progress and will probably change considerably |
6 | * later on. |
6 | * later on. |
7 | * |
7 | * |
8 | * Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
8 | * Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
9 | */ |
9 | */ |
10 | |
10 | |
11 | static char const rcsid[] = "$Id: table.C,v 1.3 2007/07/21 13:23:22 pippijn Exp $"; |
11 | static char const rcsid[] = "$Id: table.C,v 1.4 2007/08/28 17:08:12 pippijn Exp $"; |
12 | |
12 | |
13 | #include "atheme.h" |
13 | #include "atheme.h" |
14 | |
14 | |
15 | static void |
15 | static void |
16 | table_destroy (void *obj) |
16 | table_destroy (void *obj) |
… | |
… | |
29 | |
29 | |
30 | LIST_FOREACH_SAFE (n2, tn2, r->cells.head) |
30 | LIST_FOREACH_SAFE (n2, tn2, r->cells.head) |
31 | { |
31 | { |
32 | table_cell_t *c = (table_cell_t *) n2->data; |
32 | table_cell_t *c = (table_cell_t *) n2->data; |
33 | |
33 | |
34 | free (c->name); |
34 | sfree (c->name); |
35 | free (c->value); |
35 | sfree (c->value); |
36 | free (c); |
36 | delete c; |
37 | node_del (n2, &r->cells); |
37 | node_del (n2, &r->cells); |
38 | node_free (n2); |
38 | node_free (n2); |
39 | } |
39 | } |
40 | |
40 | |
41 | free (r); |
41 | delete r; |
42 | |
42 | |
43 | node_del (n, &table->rows); |
43 | node_del (n, &table->rows); |
44 | node_free (n); |
44 | node_free (n); |
45 | } |
45 | } |
46 | |
46 | |
47 | free (table); |
47 | delete table; |
48 | } |
48 | } |
49 | |
49 | |
50 | /* |
50 | /* |
51 | * table_new(const char *fmt, ...) |
51 | * table_new(char const * const fmt, ...) |
52 | * |
52 | * |
53 | * Table constructor. |
53 | * Table constructor. |
54 | * |
54 | * |
55 | * Inputs: |
55 | * Inputs: |
56 | * - printf-style string to name the table with. |
56 | * - printf-style string to name the table with. |
… | |
… | |
60 | * |
60 | * |
61 | * Side Effects: |
61 | * Side Effects: |
62 | * - none |
62 | * - none |
63 | */ |
63 | */ |
64 | table_t * |
64 | table_t * |
65 | table_new (const char *fmt, ...) |
65 | table_new (char const * const fmt, ...) |
66 | { |
66 | { |
67 | va_list vl; |
67 | va_list vl; |
68 | char *buf; |
68 | char *buf; |
69 | table_t *out; |
69 | table_t *out; |
70 | |
70 | |
… | |
… | |
72 | |
72 | |
73 | va_start (vl, fmt); |
73 | va_start (vl, fmt); |
74 | vasprintf (&buf, fmt, vl); |
74 | vasprintf (&buf, fmt, vl); |
75 | va_end (vl); |
75 | va_end (vl); |
76 | |
76 | |
77 | out = static_cast<table_t *> (scalloc (sizeof (table_t), 1)); |
77 | out = new table_t; |
78 | object_init (&out->parent, buf, table_destroy); |
78 | object_init (&out->parent, buf, table_destroy); |
79 | free (buf); |
79 | sfree (buf); |
80 | |
80 | |
81 | return out; |
81 | return out; |
82 | } |
82 | } |
83 | |
83 | |
84 | /* |
84 | /* |
… | |
… | |
100 | { |
100 | { |
101 | table_row_t *out; |
101 | table_row_t *out; |
102 | |
102 | |
103 | return_val_if_fail (t != NULL, NULL); |
103 | return_val_if_fail (t != NULL, NULL); |
104 | |
104 | |
105 | out = static_cast<table_row_t *> (scalloc (sizeof (table_row_t), 1)); |
105 | out = new table_row_t; |
106 | |
106 | |
107 | node_add (out, node_create (), &t->rows); |
107 | node_add (out, node_create (), &t->rows); |
108 | |
108 | |
109 | return out; |
109 | return out; |
110 | } |
110 | } |
… | |
… | |
122 | * |
122 | * |
123 | * Side Effects: |
123 | * Side Effects: |
124 | * - none |
124 | * - none |
125 | */ |
125 | */ |
126 | void |
126 | void |
127 | table_cell_associate (table_row_t * r, const char *name, const char *value) |
127 | table_cell_associate (table_row_t * r, char const * const name, char const * const value) |
128 | { |
128 | { |
129 | table_cell_t *c; |
129 | table_cell_t *c; |
130 | |
130 | |
131 | return_if_fail (r != NULL); |
131 | return_if_fail (r != NULL); |
132 | return_if_fail (name != NULL); |
132 | return_if_fail (name != NULL); |
133 | return_if_fail (value != NULL); |
133 | return_if_fail (value != NULL); |
134 | |
134 | |
135 | c = static_cast<table_cell_t *> (scalloc (sizeof (table_cell_t), 1)); |
135 | c = new table_cell_t; |
136 | |
136 | |
137 | c->name = sstrdup (name); |
137 | c->name = sstrdup (name); |
138 | c->value = sstrdup (value); |
138 | c->value = sstrdup (value); |
139 | |
139 | |
140 | node_add (c, node_create (), &r->cells); |
140 | node_add (c, node_create (), &r->cells); |
… | |
… | |
156 | * |
156 | * |
157 | * Side Effects: |
157 | * Side Effects: |
158 | * - a callback function is called for each row of the table. |
158 | * - a callback function is called for each row of the table. |
159 | */ |
159 | */ |
160 | void |
160 | void |
161 | table_render (table_t * t, void (*callback) (const char *line, void *data), void *data) |
161 | table_render (table_t * t, void (*callback) (char const * const line, void *data), void *data) |
162 | { |
162 | { |
163 | node_t *n; |
163 | node_t *n; |
164 | table_row_t *f; |
164 | table_row_t *f; |
165 | size_t bufsz = 0; |
165 | size_t bufsz = 0; |
166 | char *buf = NULL; |
166 | char *buf = NULL; |
… | |
… | |
199 | { |
199 | { |
200 | table_cell_t *c = (table_cell_t *) n->data; |
200 | table_cell_t *c = (table_cell_t *) n->data; |
201 | bufsz += c->width + 1; |
201 | bufsz += c->width + 1; |
202 | } |
202 | } |
203 | |
203 | |
204 | buf = static_cast<char *> (smalloc (bufsz)); |
204 | buf = alloc<char> (bufsz); |
205 | *buf = '\0'; |
205 | *buf = '\0'; |
206 | |
206 | |
207 | /* start outputting the header. */ |
207 | /* start outputting the header. */ |
208 | callback (asobject (t)->name, data); |
208 | callback (asobject (t)->name, data); |
209 | LIST_FOREACH (n, f->cells.head) |
209 | LIST_FOREACH (n, f->cells.head) |