]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debrecords.cc
Store tags in the cache (they are very useful :/).
[apt.git] / apt-pkg / deb / debrecords.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debrecords.cc,v 1.10 2001/03/13 06:51:46 jgg Exp $
4 /* ######################################################################
5
6 Debian Package Records - Parser for debian package records
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #include <config.h>
12
13 #include <apt-pkg/debrecords.h>
14 #include <apt-pkg/debindexfile.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/aptconfiguration.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/cacheiterators.h>
19 #include <apt-pkg/pkgcache.h>
20 #include <apt-pkg/tagfile.h>
21 #include <apt-pkg/error.h>
22
23 #include <string.h>
24 #include <algorithm>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 #include <langinfo.h>
29
30 #include <apti18n.h>
31 /*}}}*/
32
33 using std::string;
34
35 // RecordParser::debRecordParser - Constructor /*{{{*/
36 debRecordParser::debRecordParser(string FileName,pkgCache &Cache) :
37 debRecordParserBase(), d(NULL), File(FileName, FileFd::ReadOnly, FileFd::Extension),
38 Tags(&File, std::max(Cache.Head().MaxVerFileSize, Cache.Head().MaxDescFileSize) + 200)
39 {
40 }
41 /*}}}*/
42 // RecordParser::Jump - Jump to a specific record /*{{{*/
43 bool debRecordParser::Jump(pkgCache::VerFileIterator const &Ver)
44 {
45 if (Ver.end() == true)
46 return false;
47 return Tags.Jump(Section,Ver->Offset);
48 }
49 bool debRecordParser::Jump(pkgCache::DescFileIterator const &Desc)
50 {
51 if (Desc.end() == true)
52 return false;
53 return Tags.Jump(Section,Desc->Offset);
54 }
55 /*}}}*/
56 debRecordParser::~debRecordParser() {}
57
58 debRecordParserBase::debRecordParserBase() : Parser(), d(NULL) {}
59 // RecordParserBase::FileName - Return the archive filename on the site /*{{{*/
60 string debRecordParserBase::FileName()
61 {
62 return Section.FindS("Filename");
63 }
64 /*}}}*/
65 // RecordParserBase::Name - Return the package name /*{{{*/
66 string debRecordParserBase::Name()
67 {
68 string Result = Section.FindS("Package");
69
70 // Normalize mixed case package names to lower case, like dpkg does
71 // See Bug#807012 for details
72 std::transform(Result.begin(), Result.end(), Result.begin(), tolower_ascii);
73
74 return Result;
75 }
76 /*}}}*/
77 // RecordParserBase::Homepage - Return the package homepage /*{{{*/
78 string debRecordParserBase::Homepage()
79 {
80 return Section.FindS("Homepage");
81 }
82 /*}}}*/
83 // RecordParserBase::Hashes - return the available archive hashes /*{{{*/
84 HashStringList debRecordParserBase::Hashes() const
85 {
86 HashStringList hashes;
87 for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
88 {
89 std::string const hash = Section.FindS(*type);
90 if (hash.empty() == false)
91 hashes.push_back(HashString(*type, hash));
92 }
93 auto const size = Section.FindULL("Size", 0);
94 if (size != 0)
95 hashes.FileSize(size);
96 return hashes;
97 }
98 /*}}}*/
99 // RecordParserBase::Maintainer - Return the maintainer email /*{{{*/
100 string debRecordParserBase::Maintainer()
101 {
102 return Section.FindS("Maintainer");
103 }
104 /*}}}*/
105 // RecordParserBase::RecordField - Return the value of an arbitrary field /*{{*/
106 string debRecordParserBase::RecordField(const char *fieldName)
107 {
108 return Section.FindS(fieldName);
109 }
110 /*}}}*/
111 // RecordParserBase::ShortDesc - Return a 1 line description /*{{{*/
112 string debRecordParserBase::ShortDesc(std::string const &lang)
113 {
114 string const Res = LongDesc(lang);
115 if (Res.empty() == true)
116 return "";
117 string::size_type const Pos = Res.find('\n');
118 if (Pos == string::npos)
119 return Res;
120 return string(Res,0,Pos);
121 }
122 /*}}}*/
123 // RecordParserBase::LongDesc - Return a longer description /*{{{*/
124 string debRecordParserBase::LongDesc(std::string const &lang)
125 {
126 string orig;
127 if (lang.empty() == true)
128 {
129 std::vector<string> const lang = APT::Configuration::getLanguages();
130 for (std::vector<string>::const_iterator l = lang.begin();
131 l != lang.end(); ++l)
132 {
133 std::string const tagname = "Description-" + *l;
134 orig = Section.FindS(tagname.c_str());
135 if (orig.empty() == false)
136 break;
137 else if (*l == "en")
138 {
139 orig = Section.FindS("Description");
140 if (orig.empty() == false)
141 break;
142 }
143 }
144 if (orig.empty() == true)
145 orig = Section.FindS("Description");
146 }
147 else
148 {
149 std::string const tagname = "Description-" + lang;
150 orig = Section.FindS(tagname.c_str());
151 if (orig.empty() == true && lang == "en")
152 orig = Section.FindS("Description");
153 }
154
155 char const * const codeset = nl_langinfo(CODESET);
156 if (strcmp(codeset,"UTF-8") != 0) {
157 string dest;
158 UTF8ToCodeset(codeset, orig, &dest);
159 return dest;
160 }
161
162 return orig;
163 }
164 /*}}}*/
165
166 static const char * const SourceVerSeparators = " ()";
167 // RecordParserBase::SourcePkg - Return the source package name if any /*{{{*/
168 string debRecordParserBase::SourcePkg()
169 {
170 string Res = Section.FindS("Source");
171 string::size_type Pos = Res.find_first_of(SourceVerSeparators);
172 if (Pos == string::npos)
173 return Res;
174 return string(Res,0,Pos);
175 }
176 /*}}}*/
177 // RecordParserBase::SourceVer - Return the source version number if present /*{{{*/
178 string debRecordParserBase::SourceVer()
179 {
180 string Pkg = Section.FindS("Source");
181 string::size_type Pos = Pkg.find_first_of(SourceVerSeparators);
182 if (Pos == string::npos)
183 return "";
184
185 string::size_type VerStart = Pkg.find_first_not_of(SourceVerSeparators, Pos);
186 if(VerStart == string::npos)
187 return "";
188
189 string::size_type VerEnd = Pkg.find_first_of(SourceVerSeparators, VerStart);
190 if(VerEnd == string::npos)
191 // Corresponds to the case of, e.g., "foo (1.2" without a closing
192 // paren. Be liberal and guess what it means.
193 return string(Pkg, VerStart);
194 else
195 return string(Pkg, VerStart, VerEnd - VerStart);
196 }
197 /*}}}*/
198 // RecordParserBase::GetRec - Return the whole record /*{{{*/
199 void debRecordParserBase::GetRec(const char *&Start,const char *&Stop)
200 {
201 Section.GetSection(Start,Stop);
202 }
203 /*}}}*/
204 debRecordParserBase::~debRecordParserBase() {}
205
206 bool debDebFileRecordParser::LoadContent()
207 {
208 // load content only once
209 if (controlContent.empty() == false)
210 return true;
211
212 std::ostringstream content;
213 if (debDebPkgFileIndex::GetContent(content, debFileName) == false)
214 return false;
215 // add two newlines to make sure the scanner finds the section,
216 // which is usually done by pkgTagFile automatically if needed.
217 content << "\n\n";
218
219 controlContent = content.str();
220 if (Section.Scan(controlContent.c_str(), controlContent.length()) == false)
221 return _error->Error(_("Unable to parse package file %s (%d)"), debFileName.c_str(), 3);
222 return true;
223 }
224 bool debDebFileRecordParser::Jump(pkgCache::VerFileIterator const &) { return LoadContent(); }
225 bool debDebFileRecordParser::Jump(pkgCache::DescFileIterator const &) { return LoadContent(); }
226 std::string debDebFileRecordParser::FileName() { return debFileName; }
227
228 debDebFileRecordParser::debDebFileRecordParser(std::string FileName) : debRecordParserBase(), d(NULL), debFileName(FileName) {}
229 debDebFileRecordParser::~debDebFileRecordParser() {}