debrecords.cc 6.3 KB

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