configuration.cc 4.5 KB

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