debrecords.cc 6.1 KB

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