Sync
[ntk/apt.git] / apt-pkg / contrib / configuration.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: configuration.cc,v 1.1 1998/07/07 04:17:10 jgg Exp $
4/* ######################################################################
5
6 Configuration Class
7
8 This class provides a configuration file and command line parser
9 for a tree-oriented configuration environment. All runtime configuration
10 is stored in here.
11
12 ##################################################################### */
13 /*}}}*/
14// Include files /*{{{*/
15#ifdef __GNUG__
16#pragma implementation "pkglib/configuration.h"
17#endif
18#include <pkglib/configuration.h>
19#include <strutl.h>
20
21#include <stdio.h>
22 /*}}}*/
23Configuration *_config;
24
25// Configuration::Configuration - Constructor /*{{{*/
26// ---------------------------------------------------------------------
27/* */
28Configuration::Configuration()
29{
30 Root = new Item;
31}
32 /*}}}*/
33// Configuration::Lookup - Lookup a single item /*{{{*/
34// ---------------------------------------------------------------------
35/* This will lookup a single item by name below another item. It is a
36 helper function for the main lookup function */
37Configuration::Item *Configuration::Lookup(Item *Head,const char *S,
38 unsigned long Len,bool Create)
39{
40 int Res = 1;
41 Item *I = Head->Child;
42 Item **Last = &Head->Child;
43 for (; I != 0; Last = &I->Next, I = I->Next)
44 if ((Res = stringcasecmp(I->Value.begin(),I->Value.end(),S,S + Len)) == 0)
45 break;
46
47 if (Res == 0)
48 return I;
49 if (Create == false)
50 return 0;
51
52 I = new Item;
53 I->Value = string(S,Len);
54 I->Next = *Last;
55 *Last = I;
56 return I;
57}
58 /*}}}*/
59// Configuration::Lookup - Lookup a fully scoped item /*{{{*/
60// ---------------------------------------------------------------------
61/* This performs a fully scoped lookup of a given name, possibly creating
62 new items */
63Configuration::Item *Configuration::Lookup(const char *Name,bool Create)
64{
65 const char *Start = Name;
66 const char *End = Start + strlen(Name);
67 const char *TagEnd = Name;
68 Item *Itm = Root;
69 for (; End - TagEnd > 2; TagEnd++)
70 {
71 if (TagEnd[0] == ':' && TagEnd[1] == ':')
72 {
73 Itm = Lookup(Itm,Start,TagEnd - Start,Create);
74 if (Itm == 0)
75 return 0;
76 TagEnd = Start = TagEnd + 2;
77 }
78 }
79
80 Itm = Lookup(Itm,Start,End - Start,Create);
81 if (Itm == 0)
82 return 0;
83 return Itm;
84}
85 /*}}}*/
86// Configuration::Find - Find a value /*{{{*/
87// ---------------------------------------------------------------------
88/* */
89string Configuration::Find(const char *Name,const char *Default)
90{
91 Item *Itm = Lookup(Name,false);
92 if (Itm == 0 || Itm->Value.empty() == true)
93 return Default;
94 return Itm->Value;
95}
96 /*}}}*/
97// Configuration::FindI - Find an integer value /*{{{*/
98// ---------------------------------------------------------------------
99/* */
100int Configuration::FindI(const char *Name,int Default)
101{
102 Item *Itm = Lookup(Name,false);
103 if (Itm == 0 || Itm->Value.empty() == true)
104 return Default;
105
106 char *End;
107 int Res = strtol(Itm->Value.c_str(),&End,0);
108 if (End == Itm->Value.c_str())
109 return Default;
110
111 return Res;
112}
113 /*}}}*/
114// Configuration::Set - Set a value /*{{{*/
115// ---------------------------------------------------------------------
116/* */
117void Configuration::Set(const char *Name,string Value)
118{
119 Item *Itm = Lookup(Name,true);
120 if (Itm == 0)
121 return;
122 Itm->Value = Value;
123}
124 /*}}}*/
125// Configuration::Set - Set an integer value /*{{{*/
126// ---------------------------------------------------------------------
127/* */
128void Configuration::Set(const char *Name,int Value)
129{
130 Item *Itm = Lookup(Name,true);
131 if (Itm == 0)
132 return;
133 char S[300];
134 snprintf(S,sizeof(S),"%i",Value);
135 Itm->Value = S;
136}
137 /*}}}*/