debrecords.cc 6.2 KB

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