]>
git.saurik.com Git - apt.git/blob - cmdline/apt-sortpkgs.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-sortpkgs.cc,v 1.2 2001/02/20 07:03:17 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>
39 inline int Compare3(const PkgName
&x
) const
41 int A
= stringcasecmp(Name
,x
.Name
);
44 A
= stringcasecmp(Ver
,x
.Ver
);
46 A
= stringcasecmp(Arch
,x
.Arch
);
51 bool operator <(const PkgName
&x
) const {return Compare3(x
) < 0;};
52 bool operator >(const PkgName
&x
) const {return Compare3(x
) > 0;};
53 bool operator ==(const PkgName
&x
) const {return Compare3(x
) == 0;};
56 // DoIt - Sort a single file /*{{{*/
57 // ---------------------------------------------------------------------
59 bool DoIt(string InFile
)
61 FileFd
Fd(InFile
,FileFd::ReadOnly
);
63 if (_error
->PendingError() == true)
68 pkgTagSection Section
;
69 unsigned long Largest
= 0;
70 unsigned long Offset
= Tags
.Offset();
71 bool Source
= _config
->FindB("APT::SortPkgs::Source",false);
72 while (Tags
.Step(Section
) == true)
76 /* Fetch the name, auto-detecting if this is a source file or a
78 Tmp
.Name
= Section
.FindS("Package");
79 Tmp
.Ver
= Section
.FindS("Version");
80 Tmp
.Arch
= Section
.FindS("Architecture");
82 if (Tmp
.Name
.empty() == true)
83 return _error
->Error(_("Unknown package record!"));
86 Tmp
.Length
= Section
.size();
87 if (Largest
< Tmp
.Length
)
92 Offset
= Tags
.Offset();
94 if (_error
->PendingError() == true)
98 sort(List
.begin(),List
.end());
100 const char **Order
= TFRewritePackageOrder
;
102 Order
= TFRewriteSourceOrder
;
105 unsigned char *Buffer
= new unsigned char[Largest
+1];
106 for (vector
<PkgName
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
108 // Read in the Record.
109 if (Fd
.Seek(I
->Offset
) == false || Fd
.Read(Buffer
,I
->Length
) == false)
115 Buffer
[I
->Length
] = '\n';
116 if (Section
.Scan((char *)Buffer
,I
->Length
+1) == false)
119 return _error
->Error("Internal error, failed to scan buffer");
123 if (TFRewrite(stdout
,Section
,Order
,0) == false)
126 return _error
->Error("Internal error, failed to sort fields");
136 // ShowHelp - Show the help text /*{{{*/
137 // ---------------------------------------------------------------------
141 ioprintf(cout
,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE
,VERSION
,
142 COMMON_OS
,COMMON_CPU
,__DATE__
,__TIME__
);
143 if (_config
->FindB("version") == true)
147 _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
149 "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n"
150 "to indicate what kind of file it is.\n"
153 " -h This help text\n"
154 " -s Use source file sorting\n"
155 " -c=? Read this configuration file\n"
156 " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n");
162 int main(unsigned int argc
,const char *argv
[])
164 CommandLine::Args Args
[] = {
165 {'h',"help","help",0},
166 {'v',"version","version",0},
167 {'s',"source","APT::SortPkgs::Source",0},
168 {'c',"config-file",0,CommandLine::ConfigFile
},
169 {'o',"option",0,CommandLine::ArbItem
},
172 // Parse the command line and initialize the package library
173 CommandLine
CmdL(Args
,_config
);
174 if (pkgInitConfig(*_config
) == false ||
175 CmdL
.Parse(argc
,argv
) == false ||
176 pkgInitSystem(*_config
,_system
) == false)
178 _error
->DumpErrors();
182 // See if the help should be shown
183 if (_config
->FindB("help") == true ||
184 CmdL
.FileSize() == 0)
187 // Match the operation
188 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
189 if (DoIt(CmdL
.FileList
[I
]) == false)
192 // Print any errors or warnings found during parsing
193 if (_error
->empty() == false)
195 bool Errors
= _error
->PendingError();
196 _error
->DumpErrors();
197 return Errors
== true?100:0;