merged from lp:~donkult/apt/experimental
[ntk/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/strutl.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/aptconfiguration.h>
17 #include <langinfo.h>
18 /*}}}*/
19
20 // RecordParser::debRecordParser - Constructor /*{{{*/
21 // ---------------------------------------------------------------------
22 /* */
23 debRecordParser::debRecordParser(string FileName,pkgCache &Cache) :
24 File(FileName,FileFd::ReadOnlyGzip),
25 Tags(&File, std::max(Cache.Head().MaxVerFileSize,
26 Cache.Head().MaxDescFileSize) + 200)
27 {
28 }
29 /*}}}*/
30 // RecordParser::Jump - Jump to a specific record /*{{{*/
31 // ---------------------------------------------------------------------
32 /* */
33 bool debRecordParser::Jump(pkgCache::VerFileIterator const &Ver)
34 {
35 return Tags.Jump(Section,Ver->Offset);
36 }
37 bool debRecordParser::Jump(pkgCache::DescFileIterator const &Desc)
38 {
39 return Tags.Jump(Section,Desc->Offset);
40 }
41 /*}}}*/
42 // RecordParser::FileName - Return the archive filename on the site /*{{{*/
43 // ---------------------------------------------------------------------
44 /* */
45 string debRecordParser::FileName()
46 {
47 return Section.FindS("Filename");
48 }
49 /*}}}*/
50 // RecordParser::Name - Return the package name /*{{{*/
51 // ---------------------------------------------------------------------
52 /* */
53 string debRecordParser::Name()
54 {
55 return Section.FindS("Package");
56 }
57 /*}}}*/
58 // RecordParser::Homepage - Return the package homepage /*{{{*/
59 // ---------------------------------------------------------------------
60 /* */
61 string debRecordParser::Homepage()
62 {
63 return Section.FindS("Homepage");
64 }
65 /*}}}*/
66 // RecordParser::MD5Hash - Return the archive hash /*{{{*/
67 // ---------------------------------------------------------------------
68 /* */
69 string debRecordParser::MD5Hash()
70 {
71 return Section.FindS("MD5Sum");
72 }
73 /*}}}*/
74 // RecordParser::SHA1Hash - Return the archive hash /*{{{*/
75 // ---------------------------------------------------------------------
76 /* */
77 string debRecordParser::SHA1Hash()
78 {
79 return Section.FindS("SHA1");
80 }
81 /*}}}*/
82 // RecordParser::SHA256Hash - Return the archive hash /*{{{*/
83 // ---------------------------------------------------------------------
84 /* */
85 string debRecordParser::SHA256Hash()
86 {
87 return Section.FindS("SHA256");
88 }
89 /*}}}*/
90 // RecordParser::SHA512Hash - Return the archive hash /*{{{*/
91 // ---------------------------------------------------------------------
92 /* */
93 string debRecordParser::SHA512Hash()
94 {
95 return Section.FindS("SHA512");
96 }
97 /*}}}*/
98 // RecordParser::Maintainer - Return the maintainer email /*{{{*/
99 // ---------------------------------------------------------------------
100 /* */
101 string debRecordParser::Maintainer()
102 {
103 return Section.FindS("Maintainer");
104 }
105 /*}}}*/
106 // RecordParser::RecordField - Return the value of an arbitrary field /*{{*/
107 // ---------------------------------------------------------------------
108 /* */
109 string debRecordParser::RecordField(const char *fieldName)
110 {
111 return Section.FindS(fieldName);
112 }
113
114 /*}}}*/
115 // RecordParser::ShortDesc - Return a 1 line description /*{{{*/
116 // ---------------------------------------------------------------------
117 /* */
118 string debRecordParser::ShortDesc()
119 {
120 string Res = LongDesc();
121 string::size_type Pos = Res.find('\n');
122 if (Pos == string::npos)
123 return Res;
124 return string(Res,0,Pos);
125 }
126 /*}}}*/
127 // RecordParser::LongDesc - Return a longer description /*{{{*/
128 // ---------------------------------------------------------------------
129 /* */
130 string debRecordParser::LongDesc()
131 {
132 string orig, dest;
133
134 if (!Section.FindS("Description").empty())
135 orig = Section.FindS("Description").c_str();
136 else
137 {
138 vector<string> const lang = APT::Configuration::getLanguages();
139 for (vector<string>::const_iterator l = lang.begin();
140 orig.empty() && l != lang.end(); ++l)
141 orig = Section.FindS(string("Description-").append(*l).c_str());
142 }
143
144 char const * const codeset = nl_langinfo(CODESET);
145 if (strcmp(codeset,"UTF-8") != 0) {
146 UTF8ToCodeset(codeset, orig, &dest);
147 orig = dest;
148 }
149
150 return orig;
151 }
152 /*}}}*/
153
154 static const char *SourceVerSeparators = " ()";
155
156 // RecordParser::SourcePkg - Return the source package name if any /*{{{*/
157 // ---------------------------------------------------------------------
158 /* */
159 string debRecordParser::SourcePkg()
160 {
161 string Res = Section.FindS("Source");
162 string::size_type Pos = Res.find_first_of(SourceVerSeparators);
163 if (Pos == string::npos)
164 return Res;
165 return string(Res,0,Pos);
166 }
167 /*}}}*/
168 // RecordParser::SourceVer - Return the source version number if present /*{{{*/
169 // ---------------------------------------------------------------------
170 /* */
171 string debRecordParser::SourceVer()
172 {
173 string Pkg = Section.FindS("Source");
174 string::size_type Pos = Pkg.find_first_of(SourceVerSeparators);
175 if (Pos == string::npos)
176 return "";
177
178 string::size_type VerStart = Pkg.find_first_not_of(SourceVerSeparators, Pos);
179 if(VerStart == string::npos)
180 return "";
181
182 string::size_type VerEnd = Pkg.find_first_of(SourceVerSeparators, VerStart);
183 if(VerEnd == string::npos)
184 // Corresponds to the case of, e.g., "foo (1.2" without a closing
185 // paren. Be liberal and guess what it means.
186 return string(Pkg, VerStart);
187 else
188 return string(Pkg, VerStart, VerEnd - VerStart);
189 }
190 /*}}}*/
191 // RecordParser::GetRec - Return the whole record /*{{{*/
192 // ---------------------------------------------------------------------
193 /* */
194 void debRecordParser::GetRec(const char *&Start,const char *&Stop)
195 {
196 Section.GetSection(Start,Stop);
197 }
198 /*}}}*/