debrecords.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debrecords.cc,v 1.10 2001/03/13 06:51:46 jgg Exp $
  4. /* ######################################################################
  5. Debian Package Records - Parser for debian package records
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include <config.h>
  10. #include <apt-pkg/debrecords.h>
  11. #include <apt-pkg/strutl.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/aptconfiguration.h>
  14. #include <langinfo.h>
  15. /*}}}*/
  16. using std::string;
  17. // RecordParser::debRecordParser - Constructor /*{{{*/
  18. // ---------------------------------------------------------------------
  19. /* */
  20. debRecordParser::debRecordParser(string FileName,pkgCache &Cache) :
  21. File(FileName,FileFd::ReadOnlyGzip),
  22. Tags(&File, std::max(Cache.Head().MaxVerFileSize,
  23. Cache.Head().MaxDescFileSize) + 200)
  24. {
  25. }
  26. /*}}}*/
  27. // RecordParser::Jump - Jump to a specific record /*{{{*/
  28. // ---------------------------------------------------------------------
  29. /* */
  30. bool debRecordParser::Jump(pkgCache::VerFileIterator const &Ver)
  31. {
  32. return Tags.Jump(Section,Ver->Offset);
  33. }
  34. bool debRecordParser::Jump(pkgCache::DescFileIterator const &Desc)
  35. {
  36. return Tags.Jump(Section,Desc->Offset);
  37. }
  38. /*}}}*/
  39. // RecordParser::FileName - Return the archive filename on the site /*{{{*/
  40. // ---------------------------------------------------------------------
  41. /* */
  42. string debRecordParser::FileName()
  43. {
  44. return Section.FindS("Filename");
  45. }
  46. /*}}}*/
  47. // RecordParser::Name - Return the package name /*{{{*/
  48. // ---------------------------------------------------------------------
  49. /* */
  50. string debRecordParser::Name()
  51. {
  52. return Section.FindS("Package");
  53. }
  54. /*}}}*/
  55. // RecordParser::Homepage - Return the package homepage /*{{{*/
  56. // ---------------------------------------------------------------------
  57. /* */
  58. string debRecordParser::Homepage()
  59. {
  60. return Section.FindS("Homepage");
  61. }
  62. /*}}}*/
  63. // RecordParser::MD5Hash - Return the archive hash /*{{{*/
  64. // ---------------------------------------------------------------------
  65. /* */
  66. string debRecordParser::MD5Hash()
  67. {
  68. return Section.FindS("MD5Sum");
  69. }
  70. /*}}}*/
  71. // RecordParser::SHA1Hash - Return the archive hash /*{{{*/
  72. // ---------------------------------------------------------------------
  73. /* */
  74. string debRecordParser::SHA1Hash()
  75. {
  76. return Section.FindS("SHA1");
  77. }
  78. /*}}}*/
  79. // RecordParser::SHA256Hash - Return the archive hash /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* */
  82. string debRecordParser::SHA256Hash()
  83. {
  84. return Section.FindS("SHA256");
  85. }
  86. /*}}}*/
  87. // RecordParser::SHA512Hash - Return the archive hash /*{{{*/
  88. // ---------------------------------------------------------------------
  89. /* */
  90. string debRecordParser::SHA512Hash()
  91. {
  92. return Section.FindS("SHA512");
  93. }
  94. /*}}}*/
  95. // RecordParser::Maintainer - Return the maintainer email /*{{{*/
  96. // ---------------------------------------------------------------------
  97. /* */
  98. string debRecordParser::Maintainer()
  99. {
  100. return Section.FindS("Maintainer");
  101. }
  102. /*}}}*/
  103. // RecordParser::RecordField - Return the value of an arbitrary field /*{{*/
  104. // ---------------------------------------------------------------------
  105. /* */
  106. string debRecordParser::RecordField(const char *fieldName)
  107. {
  108. return Section.FindS(fieldName);
  109. }
  110. /*}}}*/
  111. // RecordParser::ShortDesc - Return a 1 line description /*{{{*/
  112. // ---------------------------------------------------------------------
  113. /* */
  114. string debRecordParser::ShortDesc()
  115. {
  116. string Res = LongDesc();
  117. string::size_type Pos = Res.find('\n');
  118. if (Pos == string::npos)
  119. return Res;
  120. return string(Res,0,Pos);
  121. }
  122. /*}}}*/
  123. // RecordParser::LongDesc - Return a longer description /*{{{*/
  124. // ---------------------------------------------------------------------
  125. /* */
  126. string debRecordParser::LongDesc()
  127. {
  128. string orig, dest;
  129. if (!Section.FindS("Description").empty())
  130. orig = Section.FindS("Description").c_str();
  131. else
  132. {
  133. std::vector<string> const lang = APT::Configuration::getLanguages();
  134. for (std::vector<string>::const_iterator l = lang.begin();
  135. orig.empty() && l != lang.end(); ++l)
  136. orig = Section.FindS(string("Description-").append(*l).c_str());
  137. }
  138. char const * const codeset = nl_langinfo(CODESET);
  139. if (strcmp(codeset,"UTF-8") != 0) {
  140. UTF8ToCodeset(codeset, orig, &dest);
  141. orig = dest;
  142. }
  143. return orig;
  144. }
  145. /*}}}*/
  146. static const char *SourceVerSeparators = " ()";
  147. // RecordParser::SourcePkg - Return the source package name if any /*{{{*/
  148. // ---------------------------------------------------------------------
  149. /* */
  150. string debRecordParser::SourcePkg()
  151. {
  152. string Res = Section.FindS("Source");
  153. string::size_type Pos = Res.find_first_of(SourceVerSeparators);
  154. if (Pos == string::npos)
  155. return Res;
  156. return string(Res,0,Pos);
  157. }
  158. /*}}}*/
  159. // RecordParser::SourceVer - Return the source version number if present /*{{{*/
  160. // ---------------------------------------------------------------------
  161. /* */
  162. string debRecordParser::SourceVer()
  163. {
  164. string Pkg = Section.FindS("Source");
  165. string::size_type Pos = Pkg.find_first_of(SourceVerSeparators);
  166. if (Pos == string::npos)
  167. return "";
  168. string::size_type VerStart = Pkg.find_first_not_of(SourceVerSeparators, Pos);
  169. if(VerStart == string::npos)
  170. return "";
  171. string::size_type VerEnd = Pkg.find_first_of(SourceVerSeparators, VerStart);
  172. if(VerEnd == string::npos)
  173. // Corresponds to the case of, e.g., "foo (1.2" without a closing
  174. // paren. Be liberal and guess what it means.
  175. return string(Pkg, VerStart);
  176. else
  177. return string(Pkg, VerStart, VerEnd - VerStart);
  178. }
  179. /*}}}*/
  180. // RecordParser::GetRec - Return the whole record /*{{{*/
  181. // ---------------------------------------------------------------------
  182. /* */
  183. void debRecordParser::GetRec(const char *&Start,const char *&Stop)
  184. {
  185. Section.GetSection(Start,Stop);
  186. }
  187. /*}}}*/