]> mj.ucw.cz Git - libucw.git/blob - ucw/conf-context.c
tableprinter: code cleanup
[libucw.git] / ucw / conf-context.c
1 /*
2  *      UCW Library -- Configuration files: Contexts
3  *
4  *      (c) 2012 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/conf.h>
12 #include <ucw/conf-internal.h>
13 #include <ucw/threads.h>
14
15 static struct cf_context cf_default_context;
16
17 static void
18 cf_init_context(struct cf_context *cc)
19 {
20   cc->enable_journal = 1;
21   clist_init(&cc->conf_entries);
22 }
23
24 struct cf_context *
25 cf_new_context(void)
26 {
27   struct cf_context *cc = xmalloc_zero(sizeof(*cc));
28   cf_init_context(cc);
29   return cc;
30 }
31
32 void
33 cf_delete_context(struct cf_context *cc)
34 {
35   ASSERT(!cc->is_active);
36   ASSERT(cc != &cf_default_context);
37   struct cf_context *prev = cf_switch_context(cc);
38   cf_revert();
39   cf_switch_context(prev);
40   xfree(cc->parser);
41   xfree(cc);
42 }
43
44 struct cf_context *
45 cf_switch_context(struct cf_context *cc)
46 {
47   struct ucwlib_context *uc = ucwlib_thread_context();
48   struct cf_context *prev = uc->cf_context;
49   if (prev)
50     prev->is_active = 0;
51   if (cc)
52     {
53       ASSERT(!cc->is_active);
54       cc->is_active = 1;
55     }
56   uc->cf_context = cc;
57   return prev;
58 }
59
60 static void CONSTRUCTOR_WITH_PRIORITY(10100)
61 cf_init_default_context(void)
62 {
63   cf_init_context(&cf_default_context);
64   ucwlib_thread_context()->cf_context = &cf_default_context;
65   cf_default_context.is_active = 1;
66 }
67
68 struct cf_context *
69 cf_obtain_context(void)
70 {
71   return cf_get_context();
72 }