srcrecords.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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::Find - Find the first source package with the given name /*{{{*/
  75. // ---------------------------------------------------------------------
  76. /* This searches on both source package names and output binary names and
  77. returns the first found. A 'cursor' like system is used to allow this
  78. function to be called multiple times to get successive entries */
  79. pkgSrcRecords::Parser *pkgSrcRecords::Find(const char *Package,bool const &SrcOnly)
  80. {
  81. if (Current == Files.end())
  82. return 0;
  83. while (true)
  84. {
  85. // Step to the next record, possibly switching files
  86. while ((*Current)->Step() == false)
  87. {
  88. if (_error->PendingError() == true)
  89. return 0;
  90. ++Current;
  91. if (Current == Files.end())
  92. return 0;
  93. }
  94. // IO error somehow
  95. if (_error->PendingError() == true)
  96. return 0;
  97. // Source name hit
  98. if ((*Current)->Package() == Package)
  99. return *Current;
  100. if (SrcOnly == true)
  101. continue;
  102. // Check for a binary hit
  103. const char **I = (*Current)->Binaries();
  104. for (; I != 0 && *I != 0; ++I)
  105. if (strcmp(Package,*I) == 0)
  106. return *Current;
  107. }
  108. }
  109. /*}}}*/
  110. // Parser::BuildDepType - Convert a build dep to a string /*{{{*/
  111. // ---------------------------------------------------------------------
  112. /* */
  113. const char *pkgSrcRecords::Parser::BuildDepType(unsigned char const &Type)
  114. {
  115. const char *fields[] = {"Build-Depends",
  116. "Build-Depends-Indep",
  117. "Build-Conflicts",
  118. "Build-Conflicts-Indep"};
  119. if (unlikely(Type >= sizeof(fields)/sizeof(fields[0])))
  120. return "";
  121. return fields[Type];
  122. }
  123. /*}}}*/