srcrecords.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: srcrecords.cc,v 1.7.2.2 2003/12/26 16:27:34 mdz Exp $
  4. /* ######################################################################
  5. Source Package Records - Allows access to source package records
  6. Parses and allows access to the list of source records and searching by
  7. source name on that list.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include<config.h>
  12. #include <apt-pkg/srcrecords.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/sourcelist.h>
  15. #include <apt-pkg/metaindex.h>
  16. #include <apt-pkg/indexfile.h>
  17. #include <apt-pkg/macros.h>
  18. #include <string.h>
  19. #include <string>
  20. #include <vector>
  21. #include <apti18n.h>
  22. /*}}}*/
  23. // SrcRecords::pkgSrcRecords - Constructor /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* Open all the source index files */
  26. pkgSrcRecords::pkgSrcRecords(pkgSourceList &List) : d(NULL), Files(0), Current(0)
  27. {
  28. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); ++I)
  29. {
  30. std::vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
  31. for (std::vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
  32. J != Indexes->end(); ++J)
  33. {
  34. Parser* P = (*J)->CreateSrcParser();
  35. if (_error->PendingError() == true)
  36. return;
  37. if (P != 0)
  38. Files.push_back(P);
  39. }
  40. }
  41. // Doesn't work without any source index files
  42. if (Files.empty() == true)
  43. {
  44. _error->Error(_("You must put some 'source' URIs"
  45. " in your sources.list"));
  46. return;
  47. }
  48. Restart();
  49. }
  50. /*}}}*/
  51. // SrcRecords::~pkgSrcRecords - Destructor /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* */
  54. pkgSrcRecords::~pkgSrcRecords()
  55. {
  56. // Blow away all the parser objects
  57. for(std::vector<Parser*>::iterator I = Files.begin(); I != Files.end(); ++I)
  58. delete *I;
  59. }
  60. /*}}}*/
  61. // SrcRecords::Restart - Restart the search /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* Return all of the parsers to their starting position */
  64. bool pkgSrcRecords::Restart()
  65. {
  66. Current = Files.begin();
  67. for (std::vector<Parser*>::iterator I = Files.begin();
  68. I != Files.end(); ++I)
  69. if ((*I)->Offset() != 0)
  70. (*I)->Restart();
  71. return true;
  72. }
  73. /*}}}*/
  74. // SrcRecords::Step - Step to the next Source Record /*{{{*/
  75. // ---------------------------------------------------------------------
  76. /* Step to the next source package record */
  77. const pkgSrcRecords::Parser* pkgSrcRecords::Step()
  78. {
  79. if (Current == Files.end())
  80. return 0;
  81. // Step to the next record, possibly switching files
  82. while ((*Current)->Step() == false)
  83. {
  84. if (_error->PendingError() == true)
  85. return 0;
  86. ++Current;
  87. if (Current == Files.end())
  88. return 0;
  89. }
  90. return *Current;
  91. }
  92. /*}}}*/
  93. // SrcRecords::Find - Find the first source package with the given name /*{{{*/
  94. // ---------------------------------------------------------------------
  95. /* This searches on both source package names and output binary names and
  96. returns the first found. A 'cursor' like system is used to allow this
  97. function to be called multiple times to get successive entries */
  98. pkgSrcRecords::Parser *pkgSrcRecords::Find(const char *Package,bool const &SrcOnly)
  99. {
  100. while (true)
  101. {
  102. if(Step() == 0)
  103. return 0;
  104. // IO error somehow
  105. if (_error->PendingError() == true)
  106. return 0;
  107. // Source name hit
  108. if ((*Current)->Package() == Package)
  109. return *Current;
  110. if (SrcOnly == true)
  111. continue;
  112. // Check for a binary hit
  113. const char **I = (*Current)->Binaries();
  114. for (; I != 0 && *I != 0; ++I)
  115. if (strcmp(Package,*I) == 0)
  116. return *Current;
  117. }
  118. }
  119. /*}}}*/
  120. // Parser::BuildDepType - Convert a build dep to a string /*{{{*/
  121. // ---------------------------------------------------------------------
  122. /* */
  123. const char *pkgSrcRecords::Parser::BuildDepType(unsigned char const &Type)
  124. {
  125. const char *fields[] = {"Build-Depends",
  126. "Build-Depends-Indep",
  127. "Build-Conflicts",
  128. "Build-Conflicts-Indep"};
  129. if (unlikely(Type >= sizeof(fields)/sizeof(fields[0])))
  130. return "";
  131. return fields[Type];
  132. }
  133. /*}}}*/