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