debrecords.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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(), 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. return Tags.Jump(Section,Ver->Offset);
  39. }
  40. bool debRecordParser::Jump(pkgCache::DescFileIterator const &Desc)
  41. {
  42. return Tags.Jump(Section,Desc->Offset);
  43. }
  44. /*}}}*/
  45. debRecordParser::~debRecordParser() {}
  46. // RecordParserBase::FileName - Return the archive filename on the site /*{{{*/
  47. string debRecordParserBase::FileName()
  48. {
  49. return Section.FindS("Filename");
  50. }
  51. /*}}}*/
  52. // RecordParserBase::Name - Return the package name /*{{{*/
  53. string debRecordParserBase::Name()
  54. {
  55. return Section.FindS("Package");
  56. }
  57. /*}}}*/
  58. // RecordParserBase::Homepage - Return the package homepage /*{{{*/
  59. string debRecordParserBase::Homepage()
  60. {
  61. return Section.FindS("Homepage");
  62. }
  63. /*}}}*/
  64. // RecordParserBase::Hashes - return the available archive hashes /*{{{*/
  65. HashStringList debRecordParserBase::Hashes() const
  66. {
  67. HashStringList hashes;
  68. for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
  69. {
  70. std::string const hash = Section.FindS(*type);
  71. if (hash.empty() == false)
  72. hashes.push_back(HashString(*type, hash));
  73. }
  74. return hashes;
  75. }
  76. /*}}}*/
  77. // RecordParserBase::Maintainer - Return the maintainer email /*{{{*/
  78. string debRecordParserBase::Maintainer()
  79. {
  80. return Section.FindS("Maintainer");
  81. }
  82. /*}}}*/
  83. // RecordParserBase::RecordField - Return the value of an arbitrary field /*{{*/
  84. string debRecordParserBase::RecordField(const char *fieldName)
  85. {
  86. return Section.FindS(fieldName);
  87. }
  88. /*}}}*/
  89. // RecordParserBase::ShortDesc - Return a 1 line description /*{{{*/
  90. string debRecordParserBase::ShortDesc(std::string const &lang)
  91. {
  92. string const Res = LongDesc(lang);
  93. if (Res.empty() == true)
  94. return "";
  95. string::size_type const Pos = Res.find('\n');
  96. if (Pos == string::npos)
  97. return Res;
  98. return string(Res,0,Pos);
  99. }
  100. /*}}}*/
  101. // RecordParserBase::LongDesc - Return a longer description /*{{{*/
  102. string debRecordParserBase::LongDesc(std::string const &lang)
  103. {
  104. string orig;
  105. if (lang.empty() == true)
  106. {
  107. std::vector<string> const lang = APT::Configuration::getLanguages();
  108. for (std::vector<string>::const_iterator l = lang.begin();
  109. l != lang.end(); ++l)
  110. {
  111. std::string const tagname = "Description-" + *l;
  112. orig = Section.FindS(tagname.c_str());
  113. if (orig.empty() == false)
  114. break;
  115. else if (*l == "en")
  116. {
  117. orig = Section.FindS("Description");
  118. if (orig.empty() == false)
  119. break;
  120. }
  121. }
  122. if (orig.empty() == true)
  123. orig = Section.FindS("Description");
  124. }
  125. else
  126. {
  127. std::string const tagname = "Description-" + lang;
  128. orig = Section.FindS(tagname.c_str());
  129. if (orig.empty() == true && lang == "en")
  130. orig = Section.FindS("Description");
  131. }
  132. char const * const codeset = nl_langinfo(CODESET);
  133. if (strcmp(codeset,"UTF-8") != 0) {
  134. string dest;
  135. UTF8ToCodeset(codeset, orig, &dest);
  136. return dest;
  137. }
  138. return orig;
  139. }
  140. /*}}}*/
  141. static const char * const SourceVerSeparators = " ()";
  142. // RecordParserBase::SourcePkg - Return the source package name if any /*{{{*/
  143. string debRecordParserBase::SourcePkg()
  144. {
  145. string Res = Section.FindS("Source");
  146. string::size_type Pos = Res.find_first_of(SourceVerSeparators);
  147. if (Pos == string::npos)
  148. return Res;
  149. return string(Res,0,Pos);
  150. }
  151. /*}}}*/
  152. // RecordParserBase::SourceVer - Return the source version number if present /*{{{*/
  153. string debRecordParserBase::SourceVer()
  154. {
  155. string Pkg = Section.FindS("Source");
  156. string::size_type Pos = Pkg.find_first_of(SourceVerSeparators);
  157. if (Pos == string::npos)
  158. return "";
  159. string::size_type VerStart = Pkg.find_first_not_of(SourceVerSeparators, Pos);
  160. if(VerStart == string::npos)
  161. return "";
  162. string::size_type VerEnd = Pkg.find_first_of(SourceVerSeparators, VerStart);
  163. if(VerEnd == string::npos)
  164. // Corresponds to the case of, e.g., "foo (1.2" without a closing
  165. // paren. Be liberal and guess what it means.
  166. return string(Pkg, VerStart);
  167. else
  168. return string(Pkg, VerStart, VerEnd - VerStart);
  169. }
  170. /*}}}*/
  171. // RecordParserBase::GetRec - Return the whole record /*{{{*/
  172. void debRecordParserBase::GetRec(const char *&Start,const char *&Stop)
  173. {
  174. Section.GetSection(Start,Stop);
  175. }
  176. /*}}}*/
  177. debRecordParserBase::~debRecordParserBase() {}
  178. bool debDebFileRecordParser::LoadContent()
  179. {
  180. // load content only once
  181. if (controlContent.empty() == false)
  182. return true;
  183. std::ostringstream content;
  184. if (debDebPkgFileIndex::GetContent(content, debFileName) == false)
  185. return false;
  186. // add two newlines to make sure the scanner finds the section,
  187. // which is usually done by pkgTagFile automatically if needed.
  188. content << "\n\n";
  189. controlContent = content.str();
  190. if (Section.Scan(controlContent.c_str(), controlContent.length()) == false)
  191. return _error->Error(_("Unable to parse package file %s (%d)"), debFileName.c_str(), 3);
  192. return true;
  193. }