]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/configuration.cc
6d937d65716165cad239e915a594a47912dbc3a7
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: configuration.cc,v 1.1 1998/07/07 04:17:10 jgg Exp $
4 /* ######################################################################
8 This class provides a configuration file and command line parser
9 for a tree-oriented configuration environment. All runtime configuration
12 ##################################################################### */
14 // Include files /*{{{*/
16 #pragma implementation "pkglib/configuration.h"
18 #include <pkglib/configuration.h>
23 Configuration
*_config
;
25 // Configuration::Configuration - Constructor /*{{{*/
26 // ---------------------------------------------------------------------
28 Configuration::Configuration()
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 */
37 Configuration::Item
*Configuration::Lookup(Item
*Head
,const char *S
,
38 unsigned long Len
,bool Create
)
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)
53 I
->Value
= string(S
,Len
);
59 // Configuration::Lookup - Lookup a fully scoped item /*{{{*/
60 // ---------------------------------------------------------------------
61 /* This performs a fully scoped lookup of a given name, possibly creating
63 Configuration::Item
*Configuration::Lookup(const char *Name
,bool Create
)
65 const char *Start
= Name
;
66 const char *End
= Start
+ strlen(Name
);
67 const char *TagEnd
= Name
;
69 for (; End
- TagEnd
> 2; TagEnd
++)
71 if (TagEnd
[0] == ':' && TagEnd
[1] == ':')
73 Itm
= Lookup(Itm
,Start
,TagEnd
- Start
,Create
);
76 TagEnd
= Start
= TagEnd
+ 2;
80 Itm
= Lookup(Itm
,Start
,End
- Start
,Create
);
86 // Configuration::Find - Find a value /*{{{*/
87 // ---------------------------------------------------------------------
89 string
Configuration::Find(const char *Name
,const char *Default
)
91 Item
*Itm
= Lookup(Name
,false);
92 if (Itm
== 0 || Itm
->Value
.empty() == true)
97 // Configuration::FindI - Find an integer value /*{{{*/
98 // ---------------------------------------------------------------------
100 int Configuration::FindI(const char *Name
,int Default
)
102 Item
*Itm
= Lookup(Name
,false);
103 if (Itm
== 0 || Itm
->Value
.empty() == true)
107 int Res
= strtol(Itm
->Value
.c_str(),&End
,0);
108 if (End
== Itm
->Value
.c_str())
114 // Configuration::Set - Set a value /*{{{*/
115 // ---------------------------------------------------------------------
117 void Configuration::Set(const char *Name
,string Value
)
119 Item
*Itm
= Lookup(Name
,true);
125 // Configuration::Set - Set an integer value /*{{{*/
126 // ---------------------------------------------------------------------
128 void Configuration::Set(const char *Name
,int Value
)
130 Item
*Itm
= Lookup(Name
,true);
134 snprintf(S
,sizeof(S
),"%i",Value
);