srcrecords.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: srcrecords.cc,v 1.2 1999/04/07 05:30:18 jgg 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. #ifdef __GNUG__
  12. #pragma implementation "apt-pkg/srcrecords.h"
  13. #endif
  14. #include <apt-pkg/srcrecords.h>
  15. #include <apt-pkg/error.h>
  16. #include <apt-pkg/configuration.h>
  17. #include <apt-pkg/strutl.h>
  18. #include <apt-pkg/debsrcrecords.h>
  19. /*}}}*/
  20. // SrcRecords::pkgSrcRecords - Constructor /*{{{*/
  21. // ---------------------------------------------------------------------
  22. /* Open all the source index files */
  23. pkgSrcRecords::pkgSrcRecords(pkgSourceList &List) : Files(0), Current(0)
  24. {
  25. pkgSourceList::const_iterator I = List.begin();
  26. // Count how many items we will need
  27. unsigned int Count = 0;
  28. for (; I != List.end(); I++)
  29. if (I->Type == pkgSourceList::Item::DebSrc)
  30. Count++;
  31. // Doesnt work without any source index files
  32. if (Count == 0)
  33. {
  34. _error->Error("Sorry, you must put some 'source' uris"
  35. " in your sources.list");
  36. return;
  37. }
  38. Files = new Parser *[Count+1];
  39. memset(Files,0,sizeof(*Files)*(Count+1));
  40. // Create the parser objects
  41. Count = 0;
  42. string Dir = _config->FindDir("Dir::State::lists");
  43. for (I = List.begin(); I != List.end(); I++)
  44. {
  45. if (I->Type != pkgSourceList::Item::DebSrc)
  46. continue;
  47. // Open the file
  48. FileFd *FD = new FileFd(Dir + URItoFileName(I->PackagesURI()),
  49. FileFd::ReadOnly);
  50. if (_error->PendingError() == true)
  51. {
  52. delete FD;
  53. return;
  54. }
  55. Files[Count] = new debSrcRecordParser(FD,I);
  56. Count++;
  57. }
  58. Restart();
  59. }
  60. /*}}}*/
  61. // SrcRecords::~pkgSrcRecords - Destructor /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* */
  64. pkgSrcRecords::~pkgSrcRecords()
  65. {
  66. if (Files == 0)
  67. return;
  68. // Blow away all the parser objects
  69. for (unsigned int Count = 0; Files[Count] != 0; Count++)
  70. delete Files[Count];
  71. }
  72. /*}}}*/
  73. // SrcRecords::Restart - Restart the search /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* Return all of the parsers to their starting position */
  76. bool pkgSrcRecords::Restart()
  77. {
  78. Current = Files;
  79. for (Parser **I = Files; *I != 0; I++)
  80. if ((*I)->Restart() == false)
  81. return false;
  82. return true;
  83. }
  84. /*}}}*/
  85. // SrcRecords::Find - Find the first source package with the given name /*{{{*/
  86. // ---------------------------------------------------------------------
  87. /* This searches on both source package names and output binary names and
  88. returns the first found. A 'cursor' like system is used to allow this
  89. function to be called multiple times to get successive entries */
  90. pkgSrcRecords::Parser *pkgSrcRecords::Find(const char *Package,bool SrcOnly)
  91. {
  92. if (*Current == 0)
  93. return 0;
  94. while (true)
  95. {
  96. // Step to the next record, possibly switching files
  97. while ((*Current)->Step() == false)
  98. {
  99. if (_error->PendingError() == true)
  100. return 0;
  101. Current++;
  102. if (*Current == 0)
  103. return 0;
  104. }
  105. // IO error somehow
  106. if (_error->PendingError() == true)
  107. return 0;
  108. // Source name hit
  109. if ((*Current)->Package() == Package)
  110. return *Current;
  111. if (SrcOnly == true)
  112. continue;
  113. // Check for a binary hit
  114. const char **I = (*Current)->Binaries();
  115. for (; I != 0 && *I != 0; I++)
  116. if (strcmp(Package,*I) == 0)
  117. return *Current;
  118. }
  119. }
  120. /*}}}*/