]> git.saurik.com Git - apt.git/blobdiff - cmdline/apt-sortpkgs.cc
tests: use SHA1 checksum only by default in tests
[apt.git] / cmdline / apt-sortpkgs.cc
index f95a434cc1e87ddaba2bd03b93a97794e6671326..cde3069bd20550574c0ea667b7230a63f0b388fb 100644 (file)
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
+#include <config.h>
+
 #include <apt-pkg/tagfile.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/cmndline.h>
 #include <apt-pkg/init.h>
 #include <apt-pkg/strutl.h>
+#include <apt-pkg/fileutl.h>
+#include <apt-pkg/pkgsystem.h>
+
+#include <apt-private/private-cmndline.h>
 
-#include <config.h>
-#include <apti18n.h>
-    
 #include <vector>
 #include <algorithm>
+#include <stdio.h>
+#include <iostream>
+#include <string>
+#include <memory>
 
-#include <locale.h>
-#include <unistd.h>
+#include <apti18n.h>
                                                                        /*}}}*/
 
 using namespace std;
@@ -59,7 +65,7 @@ struct PkgName                                                                /*{{{*/
 // DoIt - Sort a single file                                           /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool DoIt(string InFile)
+static bool DoIt(string InFile)
 {
    FileFd Fd(InFile,FileFd::ReadOnly);
    pkgTagFile Tags(&Fd);
@@ -103,48 +109,36 @@ bool DoIt(string InFile)
    const char **Order = TFRewritePackageOrder;
    if (Source == true)
       Order = TFRewriteSourceOrder;
-   
+
    // Emit
-   unsigned char *Buffer = new unsigned char[Largest+1];
+   FileFd stdoutfd;
+   stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false);
+   auto const Buffer = std::unique_ptr<unsigned char[]>(new unsigned char[Largest+1]);
    for (vector<PkgName>::iterator I = List.begin(); I != List.end(); ++I)
    {
       // Read in the Record.
-      if (Fd.Seek(I->Offset) == false || Fd.Read(Buffer,I->Length) == false)
-      {
-        delete [] Buffer;
+      if (Fd.Seek(I->Offset) == false || Fd.Read(Buffer.get(),I->Length) == false)
         return false;
-      }
-      
-      Buffer[I->Length] = '\n';      
-      if (Section.Scan((char *)Buffer,I->Length+1) == false)
-      {
-        delete [] Buffer;
+
+      Buffer[I->Length] = '\n';
+      if (Section.Scan((char *)Buffer.get(),I->Length+1) == false)
         return _error->Error("Internal error, failed to scan buffer");
-      }
 
       // Sort the section
-      if (TFRewrite(stdout,Section,Order,0) == false)
-      {
-        delete [] Buffer;
+      if (Section.Write(stdoutfd, Order) == false || stdoutfd.Write("\n", 1) == false)
         return _error->Error("Internal error, failed to sort fields");
-      }
-      
-      fputc('\n',stdout);      
    }
-   
-   delete [] Buffer;
    return true;
 }
                                                                        /*}}}*/
 // ShowHelp - Show the help text                                       /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-int ShowHelp()
+static bool ShowHelp(CommandLine &)
 {
-   ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
-           COMMON_ARCH,__DATE__,__TIME__);
+   ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
    if (_config->FindB("version") == true)
-      return 0;
+      return true;
    
    cout <<
     _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
@@ -158,7 +152,7 @@ int ShowHelp()
       "  -c=? Read this configuration file\n"
       "  -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
 
-   return 0;
+   return true;
 }
                                                                        /*}}}*/
 int main(int argc,const char *argv[])                                  /*{{{*/
@@ -176,19 +170,9 @@ int main(int argc,const char *argv[])                                      /*{{{*/
    textdomain(PACKAGE);
 
    // Parse the command line and initialize the package library
-   CommandLine CmdL(Args,_config);
-   if (pkgInitConfig(*_config) == false ||
-       CmdL.Parse(argc,argv) == false ||
-       pkgInitSystem(*_config,_system) == false)
-   {
-      _error->DumpErrors();
-      return 100;
-   }
-
-   // See if the help should be shown
-   if (_config->FindB("help") == true ||
-       CmdL.FileSize() == 0)
-      return ShowHelp();
+   CommandLine::Dispatch Cmds[] = {{NULL, NULL}};
+   CommandLine CmdL;
+   ParseCommandLine(CmdL, Cmds, Args, &_config, &_system, argc, argv, ShowHelp);
 
    // Match the operation
    for (unsigned int I = 0; I != CmdL.FileSize(); I++)