]>
git.saurik.com Git - apt.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 /*{{{*/
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>
26 #include <apt-private/private-cmndline.h>
39 struct PkgName
/*{{{*/
47 inline int Compare3(const PkgName
&x
) const
49 int A
= stringcasecmp(Name
,x
.Name
);
52 A
= stringcasecmp(Ver
,x
.Ver
);
54 A
= stringcasecmp(Arch
,x
.Arch
);
59 bool operator <(const PkgName
&x
) const {return Compare3(x
) < 0;};
60 bool operator >(const PkgName
&x
) const {return Compare3(x
) > 0;};
61 bool operator ==(const PkgName
&x
) const {return Compare3(x
) == 0;};
64 // DoIt - Sort a single file /*{{{*/
65 // ---------------------------------------------------------------------
67 static bool DoIt(string InFile
)
69 FileFd
Fd(InFile
,FileFd::ReadOnly
);
71 if (_error
->PendingError() == true)
76 pkgTagSection Section
;
77 unsigned long Largest
= 0;
78 unsigned long Offset
= Tags
.Offset();
79 bool Source
= _config
->FindB("APT::SortPkgs::Source",false);
80 while (Tags
.Step(Section
) == true)
84 /* Fetch the name, auto-detecting if this is a source file or a
86 Tmp
.Name
= Section
.FindS("Package");
87 Tmp
.Ver
= Section
.FindS("Version");
88 Tmp
.Arch
= Section
.FindS("Architecture");
90 if (Tmp
.Name
.empty() == true)
91 return _error
->Error(_("Unknown package record!"));
94 Tmp
.Length
= Section
.size();
95 if (Largest
< Tmp
.Length
)
100 Offset
= Tags
.Offset();
102 if (_error
->PendingError() == true)
106 sort(List
.begin(),List
.end());
108 const char **Order
= TFRewritePackageOrder
;
110 Order
= TFRewriteSourceOrder
;
113 unsigned char *Buffer
= new unsigned char[Largest
+1];
114 for (vector
<PkgName
>::iterator I
= List
.begin(); I
!= List
.end(); ++I
)
116 // Read in the Record.
117 if (Fd
.Seek(I
->Offset
) == false || Fd
.Read(Buffer
,I
->Length
) == false)
123 Buffer
[I
->Length
] = '\n';
124 if (Section
.Scan((char *)Buffer
,I
->Length
+1) == false)
127 return _error
->Error("Internal error, failed to scan buffer");
131 if (TFRewrite(stdout
,Section
,Order
,0) == false)
134 return _error
->Error("Internal error, failed to sort fields");
144 // ShowHelp - Show the help text /*{{{*/
145 // ---------------------------------------------------------------------
147 static bool ShowHelp(CommandLine
&)
149 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
150 COMMON_ARCH
,__DATE__
,__TIME__
);
151 if (_config
->FindB("version") == true)
155 _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
157 "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n"
158 "to indicate what kind of file it is.\n"
161 " -h This help text\n"
162 " -s Use source file sorting\n"
163 " -c=? Read this configuration file\n"
164 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
169 int main(int argc
,const char *argv
[]) /*{{{*/
171 CommandLine::Args Args
[] = {
172 {'h',"help","help",0},
173 {'v',"version","version",0},
174 {'s',"source","APT::SortPkgs::Source",0},
175 {'c',"config-file",0,CommandLine::ConfigFile
},
176 {'o',"option",0,CommandLine::ArbItem
},
179 // Set up gettext support
180 setlocale(LC_ALL
,"");
183 // Parse the command line and initialize the package library
184 CommandLine::Dispatch Cmds
[] = {{NULL
, NULL
}};
186 ParseCommandLine(CmdL
, Cmds
, Args
, &_config
, &_system
, argc
, argv
, ShowHelp
);
188 // Match the operation
189 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
190 if (DoIt(CmdL
.FileList
[I
]) == false)
193 // Print any errors or warnings found during parsing
194 if (_error
->empty() == false)
196 bool Errors
= _error
->PendingError();
197 _error
->DumpErrors();
198 return Errors
== true?100:0;