]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/debrecords.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: debrecords.cc,v 1.10 2001/03/13 06:51:46 jgg Exp $
4 /* ######################################################################
6 Debian Package Records - Parser for debian package records
8 ##################################################################### */
10 // Include Files /*{{{*/
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>
35 // RecordParser::debRecordParser - Constructor /*{{{*/
36 debRecordParser::debRecordParser(string FileName
,pkgCache
&Cache
) :
37 debRecordParserBase(), d(NULL
), File(FileName
, FileFd::ReadOnly
, FileFd::Extension
),
42 // RecordParser::Jump - Jump to a specific record /*{{{*/
43 bool debRecordParser::Jump(pkgCache::VerFileIterator
const &Ver
)
45 if (Ver
.end() == true)
47 return Tags
.Jump(Section
,Ver
->Offset
);
49 bool debRecordParser::Jump(pkgCache::DescFileIterator
const &Desc
)
51 if (Desc
.end() == true)
53 return Tags
.Jump(Section
,Desc
->Offset
);
56 debRecordParser::~debRecordParser() {}
58 debRecordParserBase::debRecordParserBase() : Parser(), d(NULL
) {}
59 // RecordParserBase::FileName - Return the archive filename on the site /*{{{*/
60 string
debRecordParserBase::FileName()
62 return Section
.FindS("Filename");
65 // RecordParserBase::Name - Return the package name /*{{{*/
66 string
debRecordParserBase::Name()
68 string Result
= Section
.FindS("Package");
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
);
77 // RecordParserBase::Display - Return the package homepage /*{{{*/
78 string
debRecordParserBase::Display()
80 string
display(Section
.FindS("Name"));
82 display
= Section
.FindS("Maemo-Display-Name");
86 // RecordParserBase::Homepage - Return the package homepage /*{{{*/
87 string
debRecordParserBase::Homepage()
89 return Section
.FindS("Homepage");
92 // RecordParserBase::Hashes - return the available archive hashes /*{{{*/
93 HashStringList
debRecordParserBase::Hashes() const
95 HashStringList hashes
;
96 for (char const * const * type
= HashString::SupportedHashes(); *type
!= NULL
; ++type
)
98 std::string
const hash
= Section
.FindS(*type
);
99 if (hash
.empty() == false)
100 hashes
.push_back(HashString(*type
, hash
));
102 auto const size
= Section
.FindULL("Size", 0);
104 hashes
.FileSize(size
);
108 // RecordParserBase::Maintainer - Return the maintainer email /*{{{*/
109 string
debRecordParserBase::Maintainer()
111 return Section
.FindS("Maintainer");
114 // RecordParserBase::RecordField - Return the value of an arbitrary field /*{{*/
115 string
debRecordParserBase::RecordField(const char *fieldName
)
117 return Section
.FindS(fieldName
);
120 // RecordParserBase::ShortDesc - Return a 1 line description /*{{{*/
121 string
debRecordParserBase::ShortDesc(std::string
const &lang
)
123 string
const Res
= LongDesc(lang
);
124 if (Res
.empty() == true)
126 string::size_type
const Pos
= Res
.find('\n');
127 if (Pos
== string::npos
)
129 return string(Res
,0,Pos
);
132 // RecordParserBase::LongDesc - Return a longer description /*{{{*/
133 string
debRecordParserBase::LongDesc(std::string
const &lang
)
136 if (lang
.empty() == true)
138 std::vector
<string
> const lang
= APT::Configuration::getLanguages();
139 for (std::vector
<string
>::const_iterator l
= lang
.begin();
140 l
!= lang
.end(); ++l
)
142 std::string
const tagname
= "Description-" + *l
;
143 orig
= Section
.FindS(tagname
.c_str());
144 if (orig
.empty() == false)
148 orig
= Section
.FindS("Description");
149 if (orig
.empty() == false)
153 if (orig
.empty() == true)
154 orig
= Section
.FindS("Description");
158 std::string
const tagname
= "Description-" + lang
;
159 orig
= Section
.FindS(tagname
.c_str());
160 if (orig
.empty() == true && lang
== "en")
161 orig
= Section
.FindS("Description");
164 char const * const codeset
= nl_langinfo(CODESET
);
165 if (strcmp(codeset
,"US-ASCII") != 0 && strcmp(codeset
,"UTF-8") != 0) {
167 UTF8ToCodeset(codeset
, orig
, &dest
);
175 static const char * const SourceVerSeparators
= " ()";
176 // RecordParserBase::SourcePkg - Return the source package name if any /*{{{*/
177 string
debRecordParserBase::SourcePkg()
179 string Res
= Section
.FindS("Source");
180 string::size_type Pos
= Res
.find_first_of(SourceVerSeparators
);
181 if (Pos
== string::npos
)
183 return string(Res
,0,Pos
);
186 // RecordParserBase::SourceVer - Return the source version number if present /*{{{*/
187 string
debRecordParserBase::SourceVer()
189 string Pkg
= Section
.FindS("Source");
190 string::size_type Pos
= Pkg
.find_first_of(SourceVerSeparators
);
191 if (Pos
== string::npos
)
194 string::size_type VerStart
= Pkg
.find_first_not_of(SourceVerSeparators
, Pos
);
195 if(VerStart
== string::npos
)
198 string::size_type VerEnd
= Pkg
.find_first_of(SourceVerSeparators
, VerStart
);
199 if(VerEnd
== string::npos
)
200 // Corresponds to the case of, e.g., "foo (1.2" without a closing
201 // paren. Be liberal and guess what it means.
202 return string(Pkg
, VerStart
);
204 return string(Pkg
, VerStart
, VerEnd
- VerStart
);
207 // RecordParserBase::GetRec - Return the whole record /*{{{*/
208 void debRecordParserBase::GetRec(const char *&Start
,const char *&Stop
)
210 Section
.GetSection(Start
,Stop
);
213 // RecordParserBase::Find - Locate a tag /*{{{*/
214 bool debRecordParserBase::Find(const char *Tag
,const char *&Start
, const char *&End
)
216 return Section
.Find(Tag
,Start
,End
);
219 debRecordParserBase::~debRecordParserBase() {}
221 bool debDebFileRecordParser::LoadContent()
223 // load content only once
224 if (controlContent
.empty() == false)
227 std::ostringstream content
;
228 if (debDebPkgFileIndex::GetContent(content
, debFileName
) == false)
230 // add two newlines to make sure the scanner finds the section,
231 // which is usually done by pkgTagFile automatically if needed.
234 controlContent
= content
.str();
235 if (Section
.Scan(controlContent
.c_str(), controlContent
.length(), false) == false)
236 return _error
->Error(_("Unable to parse package file %s (%d)"), debFileName
.c_str(), 3);
239 bool debDebFileRecordParser::Jump(pkgCache::VerFileIterator
const &) { return LoadContent(); }
240 bool debDebFileRecordParser::Jump(pkgCache::DescFileIterator
const &) { return LoadContent(); }
241 std::string
debDebFileRecordParser::FileName() { return debFileName
; }
243 debDebFileRecordParser::debDebFileRecordParser(std::string FileName
) : debRecordParserBase(), d(NULL
), debFileName(FileName
) {}
244 debDebFileRecordParser::~debDebFileRecordParser() {}