[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#89172: apt: [PATCH] use library instead of char[]



I forgot to attatch the patch.

Dan

-- 
Dan Gohman
gohmandj@mrs.umn.edu
diff -ur apt.orig/apt-pkg/acquire-method.cc apt/apt-pkg/acquire-method.cc
--- apt.orig/apt-pkg/acquire-method.cc	Tue Feb 20 01:03:17 2001
+++ apt/apt-pkg/acquire-method.cc	Sun Mar 11 11:14:08 2001
@@ -24,9 +24,16 @@
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/fileutl.h>
 
-#include <stdarg.h>
-#include <stdio.h>
+#include <algorithm>
+#include <functional>
+#include <sstream>
+#include <string>
+#include <cstdarg>
+#include <cstdio>
+
 #include <unistd.h>
+
+using namespace std;
 									/*}}}*/
 
 // AcqMethod::pkgAcqMethod - Constructor				/*{{{*/
@@ -34,32 +41,30 @@
 /* This constructs the initialization text */
 pkgAcqMethod::pkgAcqMethod(const char *Ver,unsigned long Flags)
 {
-   char S[300] = "";
-   char *End = S;
-   strcat(End,"100 Capabilities\n");
-   sprintf(End+strlen(End),"Version: %s\n",Ver);
+   ostringstream S;
+   S << "100 Capabilities\n"
+        "Version: " << Ver << "\n";
 
    if ((Flags & SingleInstance) == SingleInstance)
-      strcat(End,"Single-Instance: true\n");
+      S << "Single-Instance: true\n";
    
    if ((Flags & Pipeline) == Pipeline)
-      strcat(End,"Pipeline: true\n");
+      S << "Pipeline: true\n";
    
    if ((Flags & SendConfig) == SendConfig)
-      strcat(End,"Send-Config: true\n");
+      S << "Send-Config: true\n";
 
    if ((Flags & LocalOnly) == LocalOnly)
-      strcat(End,"Local-Only: true\n");
+      S << "Local-Only: true\n";
 
    if ((Flags & NeedsCleanup) == NeedsCleanup)
-      strcat(End,"Needs-Cleanup: true\n");
+      S << "Needs-Cleanup: true\n";
 
    if ((Flags & Removable) == Removable)
-      strcat(End,"Removable: true\n");
-   strcat(End,"\n");
+      S << "Removable: true\n";
 
-   if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
-      exit(100);
+   S << "\n";
+   Write(S.str());
 
    SetNonBlock(STDIN_FILENO,true);
 
@@ -67,6 +72,18 @@
    QueueBack = 0;
 }
 									/*}}}*/
+// AcqMethod::Write - Write a message to stdout				/*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void pkgAcqMethod::Write(const string &message)
+{
+   ssize_t len = message.length();
+   if (write(STDOUT_FILENO, message.c_str(), len) != len)
+   {
+      exit(100);
+   }
+}
+									/*}}}*/
 // AcqMethod::Fail - A fetch has failed					/*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -85,20 +102,19 @@
 void pkgAcqMethod::Fail(string Err,bool Transient)
 {
    // Strip out junk from the error messages
-   for (char *I = Err.begin(); I != Err.end(); I++)
-   {
-      if (*I == '\r') 
-	 *I = ' ';
-      if (*I == '\n') 
-	 *I = ' ';
-   }
+   replace_if(Err.begin(),
+              Err.end(),
+              compose2(logical_or<bool>(),
+                       bind2nd(equal_to<char>(), '\r'),
+                       bind2nd(equal_to<char>(), '\n')),
+              ' ');
    
-   char S[1024];
+   ostringstream S;
    if (Queue != 0)
    {
-      snprintf(S,sizeof(S)-50,"400 URI Failure\nURI: %s\n"
-	       "Message: %s %s\n",Queue->Uri.c_str(),Err.c_str(),
-	       FailExtra.c_str());
+      S << "400 URI Failure\n"
+           "URI: " << Queue->Uri << "\n"
+           "Message: " << Err << " " << FailExtra << "\n";
 
       // Dequeue
       FetchItem *Tmp = Queue;
@@ -108,18 +124,19 @@
 	 QueueBack = Queue;
    }
    else
-      snprintf(S,sizeof(S)-50,"400 URI Failure\nURI: <UNKNOWN>\n"
-	       "Message: %s %s\n",Err.c_str(),
-	       FailExtra.c_str());
+   {
+      S << "400 URI Failure\n"
+           "URI: <UNKNOWN>\n"
+           "Message: " << Err << " " << FailExtra << "\n";
+   }
       
    // Set the transient flag 
    if (Transient == true)
-      strcat(S,"Transient-Failure: true\n\n");
+      S << "Transient-Failure: true\n\n";
    else
-      strcat(S,"\n");
+      S << "\n";
    
-   if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
-      exit(100);
+   Write(S.str());
 }
 									/*}}}*/
 // AcqMethod::URIStart - Indicate a download is starting		/*{{{*/
@@ -130,24 +147,21 @@
    if (Queue == 0)
       abort();
    
-   char S[1024] = "";
-   char *End = S;
+   ostringstream S;
    
-   End += snprintf(S,sizeof(S),"200 URI Start\nURI: %s\n",Queue->Uri.c_str());
+   S << "200 URI Start\n"
+        "URI: " << Queue->Uri << "\n";
    if (Res.Size != 0)
-      End += snprintf(End,sizeof(S)-4 - (End - S),"Size: %lu\n",Res.Size);
+      S << "Size: " << Res.Size << "\n";
    
    if (Res.LastModified != 0)
-      End += snprintf(End,sizeof(S)-4 - (End - S),"Last-Modified: %s\n",
-		      TimeRFC1123(Res.LastModified).c_str());
+      S << "Last-Modified: " << TimeRFC1123(Res.LastModified) << "\n";
    
    if (Res.ResumePoint != 0)
-      End += snprintf(End,sizeof(S)-4 - (End - S),"Resume-Point: %lu\n",
-		      Res.ResumePoint);
+      S << "Resume-Point: " << Res.ResumePoint << "\n";
       
-   strcat(End,"\n");
-   if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
-      exit(100);
+   S << "\n";
+   Write(S.str());
 }
 									/*}}}*/
 // AcqMethod::URIDone - A URI is finished				/*{{{*/
@@ -158,55 +172,49 @@
    if (Queue == 0)
       abort();
    
-   char S[1024] = "";
-   char *End = S;
+   ostringstream S;
    
-   End += snprintf(S,sizeof(S),"201 URI Done\nURI: %s\n",Queue->Uri.c_str());
+   S << "201 URI Done\n"
+        "URI: " << Queue->Uri << "\n";
 
    if (Res.Filename.empty() == false)
-      End += snprintf(End,sizeof(S)-50 - (End - S),"Filename: %s\n",Res.Filename.c_str());
+      S << "Filename: " << Res.Filename << "\n";
    
    if (Res.Size != 0)
-      End += snprintf(End,sizeof(S)-50 - (End - S),"Size: %lu\n",Res.Size);
+      S << "Size: " << Res.Size << "\n";
    
    if (Res.LastModified != 0)
-      End += snprintf(End,sizeof(S)-50 - (End - S),"Last-Modified: %s\n",
-		      TimeRFC1123(Res.LastModified).c_str());
+      S << "Last-Modified: " << TimeRFC1123(Res.LastModified) << "\n";
 
    if (Res.MD5Sum.empty() == false)
-      End += snprintf(End,sizeof(S)-50 - (End - S),"MD5-Hash: %s\n",Res.MD5Sum.c_str());
+      S << "MD5-Hash: " << Res.MD5Sum << "\n";
 
    if (Res.ResumePoint != 0)
-      End += snprintf(End,sizeof(S)-50 - (End - S),"Resume-Point: %lu\n",
-		      Res.ResumePoint);
+      S << "Resume-Point: " << Res.ResumePoint << "\n";
 
    if (Res.IMSHit == true)
-      strcat(End,"IMS-Hit: true\n");
-   End = S + strlen(S);
+      S << "IMS-Hit: true\n";
    
    if (Alt != 0)
    {
       if (Alt->Filename.empty() == false)
-	 End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-Filename: %s\n",Alt->Filename.c_str());
+	 S << "Alt-Filename: " << Alt->Filename << "\n";
       
       if (Alt->Size != 0)
-	 End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-Size: %lu\n",Alt->Size);
+	 S << "Alt-Size: " << Alt->Size << "\n";
       
       if (Alt->LastModified != 0)
-	 End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-Last-Modified: %s\n",
-			 TimeRFC1123(Alt->LastModified).c_str());
+	 S << "Alt-Last-Modified: " << TimeRFC1123(Alt->LastModified) << "\n";
       
       if (Alt->MD5Sum.empty() == false)
-	 End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-MD5-Hash: %s\n",
-			 Alt->MD5Sum.c_str());
+	 S << "Alt-MD5-Hash: " << Alt->MD5Sum << "\n";
       
       if (Alt->IMSHit == true)
-	 strcat(End,"Alt-IMS-Hit: true\n");
+	 S << "Alt-IMS-Hit: true\n";
    }
    
-   strcat(End,"\n");
-   if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
-      exit(100);
+   S << "\n";
+   Write(S.str());
 
    // Dequeue
    FetchItem *Tmp = Queue;
@@ -222,12 +230,12 @@
    to be ackd */
 bool pkgAcqMethod::MediaFail(string Required,string Drive)
 {
-   char S[1024];
-   snprintf(S,sizeof(S),"403 Media Failure\nMedia: %s\nDrive: %s\n\n",
-	    Required.c_str(),Drive.c_str());
+   ostringstream S;
+   S << "403 Media Failure\n"
+        "Media: " << Required << "\n"
+        "Drive: " << Drive << "\n\n";
 
-   if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
-      exit(100);
+   Write(S.str());
    
    vector<string> MyMessages;
    
@@ -273,14 +281,14 @@
 // ---------------------------------------------------------------------
 /* This parses each configuration entry and puts it into the _config 
    Configuration class. */
-bool pkgAcqMethod::Configuration(string Message)
+bool pkgAcqMethod::Configuration(const string &Message)
 {
    ::Configuration &Cnf = *_config;
    
-   const char *I = Message.begin();
+   string::const_iterator I = Message.begin();
    
-   unsigned int Length = strlen("Config-Item");
-   for (; I + Length < Message.end(); I++)
+   string::size_type Length = strlen("Config-Item");
+   for (; I + Length < Message.end(); ++I)
    {
       // Not a config item
       if (I[Length] != ':' || stringcasecmp(I,I+Length,"Config-Item") != 0)
@@ -288,11 +296,11 @@
       
       I += Length + 1;
       
-      for (; I < Message.end() && *I == ' '; I++);
-      const char *Equals = I;
-      for (; Equals < Message.end() && *Equals != '='; Equals++);
-      const char *End = Equals;
-      for (; End < Message.end() && *End != '\n'; End++);
+      for (; I < Message.end() && *I == ' '; ++I);
+      string::const_iterator Equals = I;
+      for (; Equals < Message.end() && *Equals != '='; ++Equals);
+      string::const_iterator End = Equals;
+      for (; End < Message.end() && *End != '\n'; ++End);
       if (End == Equals)
 	 return false;
       
@@ -397,8 +405,7 @@
    vsnprintf(S+Len,sizeof(S)-4-Len,Format,args);
    strcat(S,"\n\n");
    
-   if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
-      exit(100);
+   Write(S);
 }
 									/*}}}*/
 // AcqMethod::Status - Send a status message				/*{{{*/
@@ -421,8 +428,7 @@
    vsnprintf(S+Len,sizeof(S)-4-Len,Format,args);
    strcat(S,"\n\n");
    
-   if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
-      exit(100);
+   Write(S);
 }
 									/*}}}*/
 
diff -ur apt.orig/apt-pkg/acquire-method.h apt/apt-pkg/acquire-method.h
--- apt.orig/apt-pkg/acquire-method.h	Tue Feb 20 01:03:17 2001
+++ apt/apt-pkg/acquire-method.h	Sun Mar 11 11:16:25 2001
@@ -52,10 +52,11 @@
    string FailExtra;
    
    // Handlers for messages
-   virtual bool Configuration(string Message);
+   virtual bool Configuration(const string &Message);
    virtual bool Fetch(FetchItem * /*Item*/) {return true;};
    
    // Outgoing messages
+   void Write(const string &message);
    void Fail(bool Transient = false);
    inline void Fail(const char *Why, bool Transient = false) {Fail(string(Why),Transient);};
    void Fail(string Why, bool Transient = false);

Reply to: