debrecords.cc 6.1 KB

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