]>
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 AL |
44 | { |
45 | return Tags.Jump(Section,Ver->Offset); | |
a52f938b OS |
46 | } |
47 | bool debRecordParser::Jump(pkgCache::DescFileIterator const &Desc) | |
48 | { | |
49 | return Tags.Jump(Section,Desc->Offset); | |
f55ece0e AL |
50 | } |
51 | /*}}}*/ | |
2f6a2fbb DK |
52 | debRecordParser::~debRecordParser() {} |
53 | ||
6c55f07a | 54 | debRecordParserBase::debRecordParserBase() : Parser(), d(NULL) {} |
2f6a2fbb DK |
55 | // RecordParserBase::FileName - Return the archive filename on the site /*{{{*/ |
56 | string debRecordParserBase::FileName() | |
7e798dd7 | 57 | { |
7974b907 | 58 | return Section.FindS("Filename"); |
7e798dd7 AL |
59 | } |
60 | /*}}}*/ | |
2f6a2fbb DK |
61 | // RecordParserBase::Name - Return the package name /*{{{*/ |
62 | string debRecordParserBase::Name() | |
b2e465d6 AL |
63 | { |
64 | return Section.FindS("Package"); | |
65 | } | |
66 | /*}}}*/ | |
2f6a2fbb DK |
67 | // RecordParserBase::Homepage - Return the package homepage /*{{{*/ |
68 | string debRecordParserBase::Homepage() | |
f27b4a70 OS |
69 | { |
70 | return Section.FindS("Homepage"); | |
71 | } | |
72 | /*}}}*/ | |
2f6a2fbb DK |
73 | // RecordParserBase::Hashes - return the available archive hashes /*{{{*/ |
74 | HashStringList debRecordParserBase::Hashes() const | |
d9b9e9e2 | 75 | { |
b3501edb DK |
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; | |
d9b9e9e2 MV |
84 | } |
85 | /*}}}*/ | |
2f6a2fbb DK |
86 | // RecordParserBase::Maintainer - Return the maintainer email /*{{{*/ |
87 | string debRecordParserBase::Maintainer() | |
7e798dd7 | 88 | { |
7974b907 | 89 | return Section.FindS("Maintainer"); |
7e798dd7 AL |
90 | } |
91 | /*}}}*/ | |
2f6a2fbb DK |
92 | // RecordParserBase::RecordField - Return the value of an arbitrary field /*{{*/ |
93 | string debRecordParserBase::RecordField(const char *fieldName) | |
75bda619 MV |
94 | { |
95 | return Section.FindS(fieldName); | |
96 | } | |
2f6a2fbb DK |
97 | /*}}}*/ |
98 | // RecordParserBase::ShortDesc - Return a 1 line description /*{{{*/ | |
99 | string debRecordParserBase::ShortDesc(std::string const &lang) | |
7e798dd7 | 100 | { |
ffe3c68e DK |
101 | string const Res = LongDesc(lang); |
102 | if (Res.empty() == true) | |
103 | return ""; | |
104 | string::size_type const Pos = Res.find('\n'); | |
7e798dd7 AL |
105 | if (Pos == string::npos) |
106 | return Res; | |
107 | return string(Res,0,Pos); | |
108 | } | |
109 | /*}}}*/ | |
2f6a2fbb DK |
110 | // RecordParserBase::LongDesc - Return a longer description /*{{{*/ |
111 | string debRecordParserBase::LongDesc(std::string const &lang) | |
7e798dd7 | 112 | { |
ffe3c68e DK |
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 | } | |
a52f938b | 141 | |
ffe3c68e DK |
142 | char const * const codeset = nl_langinfo(CODESET); |
143 | if (strcmp(codeset,"UTF-8") != 0) { | |
144 | string dest; | |
145 | UTF8ToCodeset(codeset, orig, &dest); | |
146 | return dest; | |
147 | } | |
a52f938b | 148 | |
a52f938b | 149 | return orig; |
7e798dd7 AL |
150 | } |
151 | /*}}}*/ | |
c2f2b862 | 152 | |
2f6a2fbb DK |
153 | static const char * const SourceVerSeparators = " ()"; |
154 | // RecordParserBase::SourcePkg - Return the source package name if any /*{{{*/ | |
155 | string debRecordParserBase::SourcePkg() | |
36375005 | 156 | { |
04f232fc | 157 | string Res = Section.FindS("Source"); |
c2f2b862 | 158 | string::size_type Pos = Res.find_first_of(SourceVerSeparators); |
04f232fc AL |
159 | if (Pos == string::npos) |
160 | return Res; | |
161 | return string(Res,0,Pos); | |
36375005 AL |
162 | } |
163 | /*}}}*/ | |
2f6a2fbb DK |
164 | // RecordParserBase::SourceVer - Return the source version number if present /*{{{*/ |
165 | string debRecordParserBase::SourceVer() | |
c2f2b862 MV |
166 | { |
167 | string Pkg = Section.FindS("Source"); | |
168 | string::size_type Pos = Pkg.find_first_of(SourceVerSeparators); | |
169 | if (Pos == string::npos) | |
170 | return ""; | |
171 | ||
172 | string::size_type VerStart = Pkg.find_first_not_of(SourceVerSeparators, Pos); | |
173 | if(VerStart == string::npos) | |
174 | return ""; | |
175 | ||
176 | string::size_type VerEnd = Pkg.find_first_of(SourceVerSeparators, VerStart); | |
177 | if(VerEnd == string::npos) | |
178 | // Corresponds to the case of, e.g., "foo (1.2" without a closing | |
179 | // paren. Be liberal and guess what it means. | |
180 | return string(Pkg, VerStart); | |
181 | else | |
182 | return string(Pkg, VerStart, VerEnd - VerStart); | |
183 | } | |
184 | /*}}}*/ | |
2f6a2fbb DK |
185 | // RecordParserBase::GetRec - Return the whole record /*{{{*/ |
186 | void debRecordParserBase::GetRec(const char *&Start,const char *&Stop) | |
b2e465d6 AL |
187 | { |
188 | Section.GetSection(Start,Stop); | |
189 | } | |
190 | /*}}}*/ | |
2f6a2fbb | 191 | debRecordParserBase::~debRecordParserBase() {} |
862bafea | 192 | |
2f6a2fbb DK |
193 | bool debDebFileRecordParser::LoadContent() |
194 | { | |
195 | // load content only once | |
196 | if (controlContent.empty() == false) | |
197 | return true; | |
198 | ||
199 | std::ostringstream content; | |
200 | if (debDebPkgFileIndex::GetContent(content, debFileName) == false) | |
201 | return false; | |
202 | // add two newlines to make sure the scanner finds the section, | |
203 | // which is usually done by pkgTagFile automatically if needed. | |
204 | content << "\n\n"; | |
205 | ||
206 | controlContent = content.str(); | |
207 | if (Section.Scan(controlContent.c_str(), controlContent.length()) == false) | |
208 | return _error->Error(_("Unable to parse package file %s (%d)"), debFileName.c_str(), 3); | |
209 | return true; | |
210 | } | |
c8a4ce6c DK |
211 | bool debDebFileRecordParser::Jump(pkgCache::VerFileIterator const &) { return LoadContent(); } |
212 | bool debDebFileRecordParser::Jump(pkgCache::DescFileIterator const &) { return LoadContent(); } | |
213 | std::string debDebFileRecordParser::FileName() { return debFileName; } | |
214 | ||
6c55f07a | 215 | debDebFileRecordParser::debDebFileRecordParser(std::string FileName) : debRecordParserBase(), d(NULL), debFileName(FileName) {} |
c8a4ce6c | 216 | debDebFileRecordParser::~debDebFileRecordParser() {} |