first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / configuration / configuration_list.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : configuration_list                                                *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2001-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "configlet.h"
16 #include "configuration_list.h"
17
18 #include <basis/astring.h>
19 #include <structures/amorph.h>
20
21 #include <typeinfo>
22
23 using namespace basis;
24 using namespace structures;
25
26 namespace configuration {
27
28 class cl_figlet_list : public amorph<configlet> {};
29
30 //////////////
31
32 configuration_list::configuration_list()
33 : _figs(new cl_figlet_list)
34 {
35 }
36
37 configuration_list::~configuration_list()
38 {
39   WHACK(_figs);
40 }
41
42 void configuration_list::reset() { _figs->reset(); }
43
44 void configuration_list::add(const configlet &new_item)
45 {
46   zap(new_item);
47   _figs->append(new_item.duplicate());
48 }
49
50 const configlet *configuration_list::find(const configlet &to_find) const
51 {
52   for (int i = 0; i < _figs->elements(); i++) {
53     configlet &curr = *_figs->borrow(i);
54     if ( (to_find.section() == curr.section())
55         && (to_find.entry() == curr.entry())
56         && (typeid(curr) == typeid(to_find)) ) {
57       return &curr;
58     }
59   }
60   return NIL;
61 }
62
63 bool configuration_list::zap(const configlet &dead_item)
64 {
65   for (int i = 0; i < _figs->elements(); i++) {
66     configlet &curr = *_figs->borrow(i);
67     if ( (dead_item.section() == curr.section())
68         && (dead_item.entry() == curr.entry()) ) {
69       _figs->zap(i, i);
70       return true;
71     }
72   }
73   return false;
74 }
75
76 bool configuration_list::load(configurator &config)
77 {
78   bool to_return = true;
79   for (int i = 0; i < _figs->elements(); i++) {
80     configlet &curr = *_figs->borrow(i);
81     if (!curr.load(config)) to_return = false;  // any failure is bad.
82   }
83   return to_return;
84 }
85
86 bool configuration_list::store(configurator &config) const
87 {
88   bool to_return = true;
89   for (int i = 0; i < _figs->elements(); i++) {
90     configlet &curr = *_figs->borrow(i);
91     if (!curr.store(config)) to_return = false;  // any failure is bad.
92   }
93   return to_return;
94 }
95
96 } //namespace.
97