]>
git.saurik.com Git - apt-legacy.git/blob - cmdline/apt-sortpkgs.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-sortpkgs.cc,v 1.5 2003/01/11 07:18:44 jgg Exp $
4 /* ######################################################################
6 APT Sort Packages - Program to sort Package and Source files
8 This program is quite simple, it just sorts the package files by
9 package and sorts the fields inside by the internal APT sort order.
10 Input is taken from a named file and sent to stdout.
12 ##################################################################### */
14 // Include Files /*{{{*/
15 #include <apt-pkg/tagfile.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/cmndline.h>
19 #include <apt-pkg/init.h>
20 #include <apt-pkg/strutl.h>
34 struct PkgName
/*{{{*/
42 inline int Compare3(const PkgName
&x
) const
44 int A
= stringcasecmp(Name
,x
.Name
);
47 A
= stringcasecmp(Ver
,x
.Ver
);
49 A
= stringcasecmp(Arch
,x
.Arch
);
54 bool operator <(const PkgName
&x
) const {return Compare3(x
) < 0;};
55 bool operator >(const PkgName
&x
) const {return Compare3(x
) > 0;};
56 bool operator ==(const PkgName
&x
) const {return Compare3(x
) == 0;};
59 // DoIt - Sort a single file /*{{{*/
60 // ---------------------------------------------------------------------
62 bool DoIt(string InFile
)
64 FileFd
Fd(InFile
,FileFd::ReadOnly
);
66 if (_error
->PendingError() == true)
71 pkgTagSection Section
;
72 unsigned long Largest
= 0;
73 unsigned long Offset
= Tags
.Offset();
74 bool Source
= _config
->FindB("APT::SortPkgs::Source",false);
75 while (Tags
.Step(Section
) == true)
79 /* Fetch the name, auto-detecting if this is a source file or a
81 Tmp
.Name
= Section
.FindS("Package");
82 Tmp
.Ver
= Section
.FindS("Version");
83 Tmp
.Arch
= Section
.FindS("Architecture");
85 if (Tmp
.Name
.empty() == true)
86 return _error
->Error(_("Unknown package record!"));
89 Tmp
.Length
= Section
.size();
90 if (Largest
< Tmp
.Length
)
95 Offset
= Tags
.Offset();
97 if (_error
->PendingError() == true)
101 sort(List
.begin(),List
.end());
103 const char **Order
= TFRewritePackageOrder
;
105 Order
= TFRewriteSourceOrder
;
108 unsigned char *Buffer
= new unsigned char[Largest
+1];
109 for (vector
<PkgName
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
111 // Read in the Record.
112 if (Fd
.Seek(I
->Offset
) == false || Fd
.Read(Buffer
,I
->Length
) == false)
118 Buffer
[I
->Length
] = '\n';
119 if (Section
.Scan((char *)Buffer
,I
->Length
+1) == false)
122 return _error
->Error("Internal error, failed to scan buffer");
126 if (TFRewrite(stdout
,Section
,Order
,0) == false)
129 return _error
->Error("Internal error, failed to sort fields");
139 // ShowHelp - Show the help text /*{{{*/
140 // ---------------------------------------------------------------------
144 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,VERSION
,
145 COMMON_ARCH
,__DATE__
,__TIME__
);
146 if (_config
->FindB("version") == true)
150 _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
152 "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n"
153 "to indicate what kind of file it is.\n"
156 " -h This help text\n"
157 " -s Use source file sorting\n"
158 " -c=? Read this configuration file\n"
159 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
164 int main(int argc
,const char *argv
[]) /*{{{*/
166 CommandLine::Args Args
[] = {
167 {'h',"help","help",0},
168 {'v',"version","version",0},
169 {'s',"source","APT::SortPkgs::Source",0},
170 {'c',"config-file",0,CommandLine::ConfigFile
},
171 {'o',"option",0,CommandLine::ArbItem
},
174 // Set up gettext support
175 setlocale(LC_ALL
,"");
178 // Parse the command line and initialize the package library
179 CommandLine
CmdL(Args
,_config
);
180 if (pkgInitConfig(*_config
) == false ||
181 CmdL
.Parse(argc
,argv
) == false ||
182 pkgInitSystem(*_config
,_system
) == false)
184 _error
->DumpErrors();
188 // See if the help should be shown
189 if (_config
->FindB("help") == true ||
190 CmdL
.FileSize() == 0)
193 // Match the operation
194 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
195 if (DoIt(CmdL
.FileList
[I
]) == false)
198 // Print any errors or warnings found during parsing
199 if (_error
->empty() == false)
201 bool Errors
= _error
->PendingError();
202 _error
->DumpErrors();
203 return Errors
== true?100:0;