]>
git.saurik.com Git - apt.git/blob - cmdline/apt-sortpkgs.cc
c2b11890a1453a0a186e37a3d56036cf438f3118
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>
23 #include <apt-pkg/fileutl.h>
24 #include <apt-pkg/pkgsystem.h>
37 struct PkgName
/*{{{*/
45 inline int Compare3(const PkgName
&x
) const
47 int A
= stringcasecmp(Name
,x
.Name
);
50 A
= stringcasecmp(Ver
,x
.Ver
);
52 A
= stringcasecmp(Arch
,x
.Arch
);
57 bool operator <(const PkgName
&x
) const {return Compare3(x
) < 0;};
58 bool operator >(const PkgName
&x
) const {return Compare3(x
) > 0;};
59 bool operator ==(const PkgName
&x
) const {return Compare3(x
) == 0;};
62 // DoIt - Sort a single file /*{{{*/
63 // ---------------------------------------------------------------------
65 static bool DoIt(string InFile
)
67 FileFd
Fd(InFile
,FileFd::ReadOnly
);
69 if (_error
->PendingError() == true)
74 pkgTagSection Section
;
75 unsigned long Largest
= 0;
76 unsigned long Offset
= Tags
.Offset();
77 bool Source
= _config
->FindB("APT::SortPkgs::Source",false);
78 while (Tags
.Step(Section
) == true)
82 /* Fetch the name, auto-detecting if this is a source file or a
84 Tmp
.Name
= Section
.FindS("Package");
85 Tmp
.Ver
= Section
.FindS("Version");
86 Tmp
.Arch
= Section
.FindS("Architecture");
88 if (Tmp
.Name
.empty() == true)
89 return _error
->Error(_("Unknown package record!"));
92 Tmp
.Length
= Section
.size();
93 if (Largest
< Tmp
.Length
)
98 Offset
= Tags
.Offset();
100 if (_error
->PendingError() == true)
104 sort(List
.begin(),List
.end());
106 const char **Order
= TFRewritePackageOrder
;
108 Order
= TFRewriteSourceOrder
;
111 unsigned char *Buffer
= new unsigned char[Largest
+1];
112 for (vector
<PkgName
>::iterator I
= List
.begin(); I
!= List
.end(); ++I
)
114 // Read in the Record.
115 if (Fd
.Seek(I
->Offset
) == false || Fd
.Read(Buffer
,I
->Length
) == false)
121 Buffer
[I
->Length
] = '\n';
122 if (Section
.Scan((char *)Buffer
,I
->Length
+1) == false)
125 return _error
->Error("Internal error, failed to scan buffer");
129 if (TFRewrite(stdout
,Section
,Order
,0) == false)
132 return _error
->Error("Internal error, failed to sort fields");
142 // ShowHelp - Show the help text /*{{{*/
143 // ---------------------------------------------------------------------
145 static int ShowHelp()
147 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
148 COMMON_ARCH
,__DATE__
,__TIME__
);
149 if (_config
->FindB("version") == true)
153 _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
155 "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n"
156 "to indicate what kind of file it is.\n"
159 " -h This help text\n"
160 " -s Use source file sorting\n"
161 " -c=? Read this configuration file\n"
162 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
167 int main(int argc
,const char *argv
[]) /*{{{*/
169 CommandLine::Args Args
[] = {
170 {'h',"help","help",0},
171 {'v',"version","version",0},
172 {'s',"source","APT::SortPkgs::Source",0},
173 {'c',"config-file",0,CommandLine::ConfigFile
},
174 {'o',"option",0,CommandLine::ArbItem
},
177 // Set up gettext support
178 setlocale(LC_ALL
,"");
181 // Parse the command line and initialize the package library
182 CommandLine
CmdL(Args
,_config
);
183 if (pkgInitConfig(*_config
) == false ||
184 CmdL
.Parse(argc
,argv
) == false ||
185 pkgInitSystem(*_config
,_system
) == false)
187 _error
->DumpErrors();
191 // See if the help should be shown
192 if (_config
->FindB("help") == true ||
193 CmdL
.FileSize() == 0)
196 // Match the operation
197 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
198 if (DoIt(CmdL
.FileList
[I
]) == false)
201 // Print any errors or warnings found during parsing
202 if (_error
->empty() == false)
204 bool Errors
= _error
->PendingError();
205 _error
->DumpErrors();
206 return Errors
== true?100:0;