* fix problematic use of tolower() when calculating the version
[ntk/apt.git] / apt-pkg / contrib / strutl.cc
index 006452a..3b7a67d 100644 (file)
@@ -331,23 +331,27 @@ string TimeToStr(unsigned long Sec)
    {
       if (Sec > 60*60*24)
       {
-        sprintf(S,"%lid %lih%lim%lis",Sec/60/60/24,(Sec/60/60) % 24,(Sec/60) % 60,Sec % 60);
+        //d means days, h means hours, min means minutes, s means seconds
+        sprintf(S,_("%lid %lih %limin %lis"),Sec/60/60/24,(Sec/60/60) % 24,(Sec/60) % 60,Sec % 60);
         break;
       }
       
       if (Sec > 60*60)
       {
-        sprintf(S,"%lih%lim%lis",Sec/60/60,(Sec/60) % 60,Sec % 60);
+        //h means hours, min means minutes, s means seconds
+        sprintf(S,_("%lih %limin %lis"),Sec/60/60,(Sec/60) % 60,Sec % 60);
         break;
       }
       
       if (Sec > 60)
       {
-        sprintf(S,"%lim%lis",Sec/60,Sec % 60);
+        //min means minutes, s means seconds
+        sprintf(S,_("%limin %lis"),Sec/60,Sec % 60);
         break;
       }
-      
-      sprintf(S,"%lis",Sec);
+
+      //s means seconds
+      sprintf(S,_("%lis"),Sec);
       break;
    }
    
@@ -459,9 +463,9 @@ string Base64Encode(const string &S)
    return Final;
 }
                                                                        /*}}}*/
-// stringcmp - Arbitary string compare                                 /*{{{*/
+// stringcmp - Arbitrary string compare                                        /*{{{*/
 // ---------------------------------------------------------------------
-/* This safely compares two non-null terminated strings of arbitary 
+/* This safely compares two non-null terminated strings of arbitrary 
    length */
 int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
 {
@@ -517,7 +521,7 @@ int stringcmp(string::const_iterator A,string::const_iterator AEnd,
 }
 #endif
                                                                        /*}}}*/
-// stringcasecmp - Arbitary case insensitive string compare            /*{{{*/
+// stringcasecmp - Arbitrary case insensitive string compare           /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
@@ -658,11 +662,24 @@ string TimeRFC1123(time_t Date)
 // ---------------------------------------------------------------------
 /* This pulls full messages from the input FD into the message buffer. 
    It assumes that messages will not pause during transit so no
-   fancy buffering is used. */
+   fancy buffering is used.
+
+   In particular: this reads blocks from the input until it believes
+   that it's run out of input text.  Each block is terminated by a
+   double newline ('\n' followed by '\n').  As noted below, there is a
+   bug in this code: it assumes that all the blocks have been read if
+   it doesn't see additional text in the buffer after the last one is
+   parsed, which will cause it to lose blocks if the last block
+   coincides with the end of the buffer.
+ */
 bool ReadMessages(int Fd, vector<string> &List)
 {
    char Buffer[64000];
    char *End = Buffer;
+   // Represents any left-over from the previous iteration of the
+   // parse loop.  (i.e., if a message is split across the end
+   // of the buffer, it goes here)
+   string PartialMessage;
    
    while (1)
    {
@@ -690,6 +707,7 @@ bool ReadMessages(int Fd, vector<string> &List)
         
         // Pull the message out
         string Message(Buffer,I-Buffer);
+        PartialMessage += Message;
 
         // Fix up the buffer
         for (; I < End && *I == '\n'; I++);
@@ -697,10 +715,32 @@ bool ReadMessages(int Fd, vector<string> &List)
         memmove(Buffer,I,End-Buffer);
         I = Buffer;
         
-        List.push_back(Message);
+        List.push_back(PartialMessage);
+        PartialMessage.clear();
       }
-      if (End == Buffer)
-        return true;
+      if (End != Buffer)
+       {
+         // If there's text left in the buffer, store it
+         // in PartialMessage and throw the rest of the buffer
+         // away.  This allows us to handle messages that
+         // are longer than the static buffer size.
+         PartialMessage += string(Buffer, End);
+         End = Buffer;
+       }
+      else
+       {
+         // BUG ALERT: if a message block happens to end at a
+         // multiple of 64000 characters, this will cause it to
+         // terminate early, leading to a badly formed block and
+         // probably crashing the method.  However, this is the only
+         // way we have to find the end of the message block.  I have
+         // an idea of how to fix this, but it will require changes
+         // to the protocol (essentially to mark the beginning and
+         // end of the block).
+         //
+         //  -- dburrows 2008-04-02
+         return true;
+       }
 
       if (WaitFd(Fd) == false)
         return false;
@@ -996,6 +1036,21 @@ void ioprintf(ostream &out,const char *format,...)
    out << S;
 }
                                                                        /*}}}*/
+// strprintf - C format string outputter to C++ strings                /*{{{*/
+// ---------------------------------------------------------------------
+/* This is used to make the internationalization strings easier to translate
+   and to allow reordering of parameters */
+void strprintf(string &out,const char *format,...) 
+{
+   va_list args;
+   va_start(args,format);
+   
+   // sprintf the description
+   char S[1024];
+   vsnprintf(S,sizeof(S),format,args);
+   out = string(S);
+}
+                                                                       /*}}}*/
 // safe_snprintf - Safer snprintf                                      /*{{{*/
 // ---------------------------------------------------------------------
 /* This is a snprintf that will never (ever) go past 'End' and returns a
@@ -1019,6 +1074,17 @@ char *safe_snprintf(char *Buffer,char *End,const char *Format,...)
 }
                                                                        /*}}}*/
 
+// tolower_ascii - tolower() function that ignores the locale          /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+int tolower_ascii(int c)
+{
+   if (c >= 'A' and c <= 'Z')
+      return c + 32;
+   return c;
+}
+                                                                       /*}}}*/
+
 // CheckDomainList - See if Host is in a , seperate list               /*{{{*/
 // ---------------------------------------------------------------------
 /* The domain list is a comma seperate list of domains that are suffix