srcrecords.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: srcrecords.cc,v 1.3 1999/10/18 04:15:24 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. (*I)->Restart();
  81. return true;
  82. }
  83. /*}}}*/
  84. // SrcRecords::Find - Find the first source package with the given name /*{{{*/
  85. // ---------------------------------------------------------------------
  86. /* This searches on both source package names and output binary names and
  87. returns the first found. A 'cursor' like system is used to allow this
  88. function to be called multiple times to get successive entries */
  89. pkgSrcRecords::Parser *pkgSrcRecords::Find(const char *Package,bool SrcOnly)
  90. {
  91. if (*Current == 0)
  92. return 0;
  93. while (true)
  94. {
  95. // Step to the next record, possibly switching files
  96. while ((*Current)->Step() == false)
  97. {
  98. if (_error->PendingError() == true)
  99. return 0;
  100. Current++;
  101. if (*Current == 0)
  102. return 0;
  103. }
  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. /*}}}*/