debrecords.cc 6.8 KB

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