]>
git.saurik.com Git - apt.git/blob - cmdline/apt-sortpkgs.cc
44b74cf6c0901fcd3af204e2cb5fb1b07cddb425
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 /*{{{*/
17 #include <apt-pkg/tagfile.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/cmndline.h>
21 #include <apt-pkg/init.h>
22 #include <apt-pkg/strutl.h>
35 struct PkgName
/*{{{*/
43 inline int Compare3(const PkgName
&x
) const
45 int A
= stringcasecmp(Name
,x
.Name
);
48 A
= stringcasecmp(Ver
,x
.Ver
);
50 A
= stringcasecmp(Arch
,x
.Arch
);
55 bool operator <(const PkgName
&x
) const {return Compare3(x
) < 0;};
56 bool operator >(const PkgName
&x
) const {return Compare3(x
) > 0;};
57 bool operator ==(const PkgName
&x
) const {return Compare3(x
) == 0;};
60 // DoIt - Sort a single file /*{{{*/
61 // ---------------------------------------------------------------------
63 bool DoIt(string InFile
)
65 FileFd
Fd(InFile
,FileFd::ReadOnly
);
67 if (_error
->PendingError() == true)
72 pkgTagSection Section
;
73 unsigned long Largest
= 0;
74 unsigned long Offset
= Tags
.Offset();
75 bool Source
= _config
->FindB("APT::SortPkgs::Source",false);
76 while (Tags
.Step(Section
) == true)
80 /* Fetch the name, auto-detecting if this is a source file or a
82 Tmp
.Name
= Section
.FindS("Package");
83 Tmp
.Ver
= Section
.FindS("Version");
84 Tmp
.Arch
= Section
.FindS("Architecture");
86 if (Tmp
.Name
.empty() == true)
87 return _error
->Error(_("Unknown package record!"));
90 Tmp
.Length
= Section
.size();
91 if (Largest
< Tmp
.Length
)
96 Offset
= Tags
.Offset();
98 if (_error
->PendingError() == true)
102 sort(List
.begin(),List
.end());
104 const char **Order
= TFRewritePackageOrder
;
106 Order
= TFRewriteSourceOrder
;
109 unsigned char *Buffer
= new unsigned char[Largest
+1];
110 for (vector
<PkgName
>::iterator I
= List
.begin(); I
!= List
.end(); ++I
)
112 // Read in the Record.
113 if (Fd
.Seek(I
->Offset
) == false || Fd
.Read(Buffer
,I
->Length
) == false)
119 Buffer
[I
->Length
] = '\n';
120 if (Section
.Scan((char *)Buffer
,I
->Length
+1) == false)
123 return _error
->Error("Internal error, failed to scan buffer");
127 if (TFRewrite(stdout
,Section
,Order
,0) == false)
130 return _error
->Error("Internal error, failed to sort fields");
140 // ShowHelp - Show the help text /*{{{*/
141 // ---------------------------------------------------------------------
145 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,VERSION
,
146 COMMON_ARCH
,__DATE__
,__TIME__
);
147 if (_config
->FindB("version") == true)
151 _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
153 "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n"
154 "to indicate what kind of file it is.\n"
157 " -h This help text\n"
158 " -s Use source file sorting\n"
159 " -c=? Read this configuration file\n"
160 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
165 int main(int argc
,const char *argv
[]) /*{{{*/
167 CommandLine::Args Args
[] = {
168 {'h',"help","help",0},
169 {'v',"version","version",0},
170 {'s',"source","APT::SortPkgs::Source",0},
171 {'c',"config-file",0,CommandLine::ConfigFile
},
172 {'o',"option",0,CommandLine::ArbItem
},
175 // Set up gettext support
176 setlocale(LC_ALL
,"");
179 // Parse the command line and initialize the package library
180 CommandLine
CmdL(Args
,_config
);
181 if (pkgInitConfig(*_config
) == false ||
182 CmdL
.Parse(argc
,argv
) == false ||
183 pkgInitSystem(*_config
,_system
) == false)
185 _error
->DumpErrors();
189 // See if the help should be shown
190 if (_config
->FindB("help") == true ||
191 CmdL
.FileSize() == 0)
194 // Match the operation
195 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
196 if (DoIt(CmdL
.FileList
[I
]) == false)
199 // Print any errors or warnings found during parsing
200 if (_error
->empty() == false)
202 bool Errors
= _error
->PendingError();
203 _error
->DumpErrors();
204 return Errors
== true?100:0;