debrecords.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/debindexfile.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apt-pkg/aptconfiguration.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/cacheiterators.h>
  16. #include <apt-pkg/pkgcache.h>
  17. #include <apt-pkg/tagfile.h>
  18. #include <apt-pkg/error.h>
  19. #include <string.h>
  20. #include <algorithm>
  21. #include <sstream>
  22. #include <string>
  23. #include <vector>
  24. #include <langinfo.h>
  25. #include <apti18n.h>
  26. /*}}}*/
  27. using std::string;
  28. // RecordParser::debRecordParser - Constructor /*{{{*/
  29. debRecordParser::debRecordParser(string FileName,pkgCache &Cache) :
  30. debRecordParserBase(), d(NULL), File(FileName, FileFd::ReadOnly, FileFd::Extension),
  31. Tags(&File, std::max(Cache.Head().MaxVerFileSize, Cache.Head().MaxDescFileSize) + 200)
  32. {
  33. }
  34. /*}}}*/
  35. // RecordParser::Jump - Jump to a specific record /*{{{*/
  36. bool debRecordParser::Jump(pkgCache::VerFileIterator const &Ver)
  37. {
  38. if (Ver.end() == true)
  39. return false;
  40. return Tags.Jump(Section,Ver->Offset);
  41. }
  42. bool debRecordParser::Jump(pkgCache::DescFileIterator const &Desc)
  43. {
  44. if (Desc.end() == true)
  45. return false;
  46. return Tags.Jump(Section,Desc->Offset);
  47. }
  48. /*}}}*/
  49. debRecordParser::~debRecordParser() {}
  50. debRecordParserBase::debRecordParserBase() : Parser(), d(NULL) {}
  51. // RecordParserBase::FileName - Return the archive filename on the site /*{{{*/
  52. string debRecordParserBase::FileName()
  53. {
  54. return Section.FindS("Filename");
  55. }
  56. /*}}}*/
  57. // RecordParserBase::Name - Return the package name /*{{{*/
  58. string debRecordParserBase::Name()
  59. {
  60. string Result = Section.FindS("Package");
  61. // Normalize mixed case package names to lower case, like dpkg does
  62. // See Bug#807012 for details
  63. std::transform(Result.begin(), Result.end(), Result.begin(), tolower_ascii);
  64. return Result;
  65. }
  66. /*}}}*/
  67. // RecordParserBase::Homepage - Return the package homepage /*{{{*/
  68. string debRecordParserBase::Homepage()
  69. {
  70. return Section.FindS("Homepage");
  71. }
  72. /*}}}*/
  73. // RecordParserBase::Hashes - return the available archive hashes /*{{{*/
  74. HashStringList debRecordParserBase::Hashes() const
  75. {
  76. HashStringList hashes;
  77. for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
  78. {
  79. std::string const hash = Section.FindS(*type);
  80. if (hash.empty() == false)
  81. hashes.push_back(HashString(*type, hash));
  82. }
  83. return hashes;
  84. }
  85. /*}}}*/
  86. // RecordParserBase::Maintainer - Return the maintainer email /*{{{*/
  87. string debRecordParserBase::Maintainer()
  88. {
  89. return Section.FindS("Maintainer");
  90. }
  91. /*}}}*/
  92. // RecordParserBase::RecordField - Return the value of an arbitrary field /*{{*/
  93. string debRecordParserBase::RecordField(const char *fieldName)
  94. {
  95. return Section.FindS(fieldName);
  96. }
  97. /*}}}*/
  98. // RecordParserBase::ShortDesc - Return a 1 line description /*{{{*/
  99. string debRecordParserBase::ShortDesc(std::string const &lang)
  100. {
  101. string const Res = LongDesc(lang);
  102. if (Res.empty() == true)
  103. return "";
  104. string::size_type const Pos = Res.find('\n');
  105. if (Pos == string::npos)
  106. return Res;
  107. return string(Res,0,Pos);
  108. }
  109. /*}}}*/
  110. // RecordParserBase::LongDesc - Return a longer description /*{{{*/
  111. string debRecordParserBase::LongDesc(std::string const &lang)
  112. {
  113. string orig;
  114. if (lang.empty() == true)
  115. {
  116. std::vector<string> const lang = APT::Configuration::getLanguages();
  117. for (std::vector<string>::const_iterator l = lang.begin();
  118. l != lang.end(); ++l)
  119. {
  120. std::string const tagname = "Description-" + *l;
  121. orig = Section.FindS(tagname.c_str());
  122. if (orig.empty() == false)
  123. break;
  124. else if (*l == "en")
  125. {
  126. orig = Section.FindS("Description");
  127. if (orig.empty() == false)
  128. break;
  129. }
  130. }
  131. if (orig.empty() == true)
  132. orig = Section.FindS("Description");
  133. }
  134. else
  135. {
  136. std::string const tagname = "Description-" + lang;
  137. orig = Section.FindS(tagname.c_str());
  138. if (orig.empty() == true && lang == "en")
  139. orig = Section.FindS("Description");
  140. }
  141. char const * const codeset = nl_langinfo(CODESET);
  142. if (strcmp(codeset,"UTF-8") != 0) {
  143. string dest;
  144. UTF8ToCodeset(codeset, orig, &dest);
  145. return dest;
  146. }
  147. return orig;
  148. }
  149. /*}}}*/
  150. static const char * const SourceVerSeparators = " ()";
  151. // RecordParserBase::SourcePkg - Return the source package name if any /*{{{*/
  152. string debRecordParserBase::SourcePkg()
  153. {
  154. string Res = Section.FindS("Source");
  155. string::size_type Pos = Res.find_first_of(SourceVerSeparators);
  156. if (Pos == string::npos)
  157. return Res;
  158. return string(Res,0,Pos);
  159. }
  160. /*}}}*/
  161. // RecordParserBase::SourceVer - Return the source version number if present /*{{{*/
  162. string debRecordParserBase::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. // RecordParserBase::GetRec - Return the whole record /*{{{*/
  181. void debRecordParserBase::GetRec(const char *&Start,const char *&Stop)
  182. {
  183. Section.GetSection(Start,Stop);
  184. }
  185. /*}}}*/
  186. debRecordParserBase::~debRecordParserBase() {}
  187. bool debDebFileRecordParser::LoadContent()
  188. {
  189. // load content only once
  190. if (controlContent.empty() == false)
  191. return true;
  192. std::ostringstream content;
  193. if (debDebPkgFileIndex::GetContent(content, debFileName) == false)
  194. return false;
  195. // add two newlines to make sure the scanner finds the section,
  196. // which is usually done by pkgTagFile automatically if needed.
  197. content << "\n\n";
  198. controlContent = content.str();
  199. if (Section.Scan(controlContent.c_str(), controlContent.length()) == false)
  200. return _error->Error(_("Unable to parse package file %s (%d)"), debFileName.c_str(), 3);
  201. return true;
  202. }
  203. bool debDebFileRecordParser::Jump(pkgCache::VerFileIterator const &) { return LoadContent(); }
  204. bool debDebFileRecordParser::Jump(pkgCache::DescFileIterator const &) { return LoadContent(); }
  205. std::string debDebFileRecordParser::FileName() { return debFileName; }
  206. debDebFileRecordParser::debDebFileRecordParser(std::string FileName) : debRecordParserBase(), d(NULL), debFileName(FileName) {}
  207. debDebFileRecordParser::~debDebFileRecordParser() {}