]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/configuration.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: configuration.cc,v 1.3 1998/07/12 23:58:44 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 "apt-pkg/configuration.h"
18 #include <apt-pkg/configuration.h>
24 Configuration
*_config
= new Configuration
;
26 // Configuration::Configuration - Constructor /*{{{*/
27 // ---------------------------------------------------------------------
29 Configuration::Configuration()
34 // Configuration::Lookup - Lookup a single item /*{{{*/
35 // ---------------------------------------------------------------------
36 /* This will lookup a single item by name below another item. It is a
37 helper function for the main lookup function */
38 Configuration::Item
*Configuration::Lookup(Item
*Head
,const char *S
,
39 unsigned long Len
,bool Create
)
42 Item
*I
= Head
->Child
;
43 Item
**Last
= &Head
->Child
;
44 for (; I
!= 0; Last
= &I
->Next
, I
= I
->Next
)
45 if ((Res
= stringcasecmp(I
->Tag
.begin(),I
->Tag
.end(),S
,S
+ Len
)) == 0)
54 I
->Tag
= string(S
,Len
);
61 // Configuration::Lookup - Lookup a fully scoped item /*{{{*/
62 // ---------------------------------------------------------------------
63 /* This performs a fully scoped lookup of a given name, possibly creating
65 Configuration::Item
*Configuration::Lookup(const char *Name
,bool Create
)
67 const char *Start
= Name
;
68 const char *End
= Start
+ strlen(Name
);
69 const char *TagEnd
= Name
;
71 for (; End
- TagEnd
> 2; TagEnd
++)
73 if (TagEnd
[0] == ':' && TagEnd
[1] == ':')
75 Itm
= Lookup(Itm
,Start
,TagEnd
- Start
,Create
);
78 TagEnd
= Start
= TagEnd
+ 2;
82 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)
103 // Configuration::FindDir - Find a directory /*{{{*/
104 // ---------------------------------------------------------------------
105 /* Directories are stored as the base dir in the Parent node and the
107 string
Configuration::FindDir(const char *Name
,const char *Default
= 0)
109 Item
*Itm
= Lookup(Name
,false);
110 if (Itm
== 0 || Itm
->Value
.empty() == true)
118 if (Itm
->Value
[0] == '/' || Itm
->Parent
== 0)
120 if (Itm
->Parent
->Value
.end()[-1] == '/')
121 return Itm
->Parent
->Value
+ Itm
->Value
;
123 return Itm
->Parent
->Value
+ '/' + Itm
->Value
;
126 // Configuration::FindI - Find an integer value /*{{{*/
127 // ---------------------------------------------------------------------
129 int Configuration::FindI(const char *Name
,int Default
)
131 Item
*Itm
= Lookup(Name
,false);
132 if (Itm
== 0 || Itm
->Value
.empty() == true)
136 int Res
= strtol(Itm
->Value
.c_str(),&End
,0);
137 if (End
== Itm
->Value
.c_str())
143 // Configuration::Set - Set a value /*{{{*/
144 // ---------------------------------------------------------------------
146 void Configuration::Set(const char *Name
,string Value
)
148 Item
*Itm
= Lookup(Name
,true);
154 // Configuration::Set - Set an integer value /*{{{*/
155 // ---------------------------------------------------------------------
157 void Configuration::Set(const char *Name
,int Value
)
159 Item
*Itm
= Lookup(Name
,true);
163 snprintf(S
,sizeof(S
),"%i",Value
);