srcrecords.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/strutl.h>
  16. #include <apt-pkg/metaindex.h>
  17. #include <apti18n.h>
  18. /*}}}*/
  19. // SrcRecords::pkgSrcRecords - Constructor /*{{{*/
  20. // ---------------------------------------------------------------------
  21. /* Open all the source index files */
  22. pkgSrcRecords::pkgSrcRecords(pkgSourceList &List) : d(NULL), Files(0), Current(0)
  23. {
  24. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); ++I)
  25. {
  26. std::vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
  27. for (std::vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
  28. J != Indexes->end(); ++J)
  29. {
  30. Parser* P = (*J)->CreateSrcParser();
  31. if (_error->PendingError() == true)
  32. return;
  33. if (P != 0)
  34. Files.push_back(P);
  35. }
  36. }
  37. // Doesn't work without any source index files
  38. if (Files.empty() == true)
  39. {
  40. _error->Error(_("You must put some 'source' URIs"
  41. " in your sources.list"));
  42. return;
  43. }
  44. Restart();
  45. }
  46. /*}}}*/
  47. // SrcRecords::~pkgSrcRecords - Destructor /*{{{*/
  48. // ---------------------------------------------------------------------
  49. /* */
  50. pkgSrcRecords::~pkgSrcRecords()
  51. {
  52. // Blow away all the parser objects
  53. for(std::vector<Parser*>::iterator I = Files.begin(); I != Files.end(); ++I)
  54. delete *I;
  55. }
  56. /*}}}*/
  57. // SrcRecords::Restart - Restart the search /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* Return all of the parsers to their starting position */
  60. bool pkgSrcRecords::Restart()
  61. {
  62. Current = Files.begin();
  63. for (std::vector<Parser*>::iterator I = Files.begin();
  64. I != Files.end(); ++I)
  65. if ((*I)->Offset() != 0)
  66. (*I)->Restart();
  67. return true;
  68. }
  69. /*}}}*/
  70. // SrcRecords::Find - Find the first source package with the given name /*{{{*/
  71. // ---------------------------------------------------------------------
  72. /* This searches on both source package names and output binary names and
  73. returns the first found. A 'cursor' like system is used to allow this
  74. function to be called multiple times to get successive entries */
  75. pkgSrcRecords::Parser *pkgSrcRecords::Find(const char *Package,bool const &SrcOnly)
  76. {
  77. if (Current == Files.end())
  78. return 0;
  79. while (true)
  80. {
  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. // IO error somehow
  91. if (_error->PendingError() == true)
  92. return 0;
  93. // Source name hit
  94. if ((*Current)->Package() == Package)
  95. return *Current;
  96. if (SrcOnly == true)
  97. continue;
  98. // Check for a binary hit
  99. const char **I = (*Current)->Binaries();
  100. for (; I != 0 && *I != 0; ++I)
  101. if (strcmp(Package,*I) == 0)
  102. return *Current;
  103. }
  104. }
  105. /*}}}*/
  106. // Parser::BuildDepType - Convert a build dep to a string /*{{{*/
  107. // ---------------------------------------------------------------------
  108. /* */
  109. const char *pkgSrcRecords::Parser::BuildDepType(unsigned char const &Type)
  110. {
  111. const char *fields[] = {"Build-Depends",
  112. "Build-Depends-Indep",
  113. "Build-Conflicts",
  114. "Build-Conflicts-Indep"};
  115. if (unlikely(Type >= sizeof(fields)/sizeof(fields[0])))
  116. return "";
  117. return fields[Type];
  118. }
  119. /*}}}*/