summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib
diff options
context:
space:
mode:
authorArch Librarian <arch@canonical.com>2004-09-20 16:51:09 +0000
committerArch Librarian <arch@canonical.com>2004-09-20 16:51:09 +0000
commit24231681aa99e84c8a6aae5e54b7e207e19d6087 (patch)
tree65cbe883a45c8a0ca042123c9e2ffb0c993a0903 /apt-pkg/contrib
parent0a8a80e58374771acc225fe1e08ed8e0fe0016cc (diff)
Sync
Author: jgg Date: 1998-10-23 00:49:58 GMT Sync
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r--apt-pkg/contrib/strutl.cc86
-rw-r--r--apt-pkg/contrib/strutl.h3
2 files changed, 83 insertions, 6 deletions
diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc
index 04a3c7bb7..4882af922 100644
--- a/apt-pkg/contrib/strutl.cc
+++ b/apt-pkg/contrib/strutl.cc
@@ -1,14 +1,16 @@
// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: strutl.cc,v 1.6 1998/10/22 04:56:48 jgg Exp $
+// $Id: strutl.cc,v 1.7 1998/10/23 00:49:59 jgg Exp $
/* ######################################################################
String Util - Some usefull string functions.
- strstrip - Remove whitespace from the front and end of a line.
+ These have been collected from here and there to do all sorts of usefull
+ things to strings. They are usefull in file parsers, URI handlers and
+ especially in APT methods.
This source is placed in the Public Domain, do with it what you will
- It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
+ It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
##################################################################### */
/*}}}*/
@@ -195,8 +197,8 @@ string QuoteString(string Str,const char *Bad)
/*}}}*/
// SizeToStr - Convert a long into a human readable size /*{{{*/
// ---------------------------------------------------------------------
-/* A max of 4 digits are shown before conversion to the next highest unit. The
- max length of the string will be 5 chars unless the size is > 10
+/* A max of 4 digits are shown before conversion to the next highest unit.
+ The max length of the string will be 5 chars unless the size is > 10
YottaBytes (E24) */
string SizeToStr(double Size)
{
@@ -543,3 +545,77 @@ bool ReadMessages(int Fd, vector<string> &List)
}
}
/*}}}*/
+// MonthConv - Converts a month string into a number /*{{{*/
+// ---------------------------------------------------------------------
+/* This was lifted from the boa webserver which lifted it from 'wn-v1.07'
+ Made it a bit more robust with a few touppers though. */
+static int MonthConv(char *Month)
+{
+ switch (toupper(*Month))
+ {
+ case 'A':
+ return toupper(Month[1]) == 'P'?3:7;
+ case 'D':
+ return 11;
+ case 'F':
+ return 1;
+ case 'J':
+ if (toupper(Month[1]) == 'A')
+ return 0;
+ return toupper(Month[2]) == 'N'?5:6;
+ case 'M':
+ return toupper(Month[2]) == 'R'?2:4;
+ case 'N':
+ return 10;
+ case 'O':
+ return 9;
+ case 'S':
+ return 8;
+
+ // Pretend it is January..
+ default:
+ return 0;
+ }
+}
+ /*}}}*/
+// StrToTime - Converts a string into a time_t /*{{{*/
+// ---------------------------------------------------------------------
+/* This handles all 3 populare time formats including RFC 1123, RFC 1036
+ and the C library asctime format. It requires the GNU library function
+ 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar
+ reason the C library does not provide any such function :<*/
+bool StrToTime(string Val,time_t &Result)
+{
+ struct tm Tm;
+ char Month[10];
+ const char *I = Val.c_str();
+
+ // Skip the day of the week
+ for (;*I != 0 && *I != ' '; I++);
+
+ // Handle RFC 1123 time
+ if (sscanf(I," %d %3s %d %d:%d:%d GMT",&Tm.tm_mday,Month,&Tm.tm_year,
+ &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6)
+ {
+ // Handle RFC 1036 time
+ if (sscanf(I," %d-%3s-%d %d:%d:%d GMT",&Tm.tm_mday,Month,
+ &Tm.tm_year,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) == 6)
+ Tm.tm_year += 1900;
+ else
+ {
+ // asctime format
+ if (sscanf(I," %3s %d %d:%d:%d %d",Month,&Tm.tm_mday,
+ &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec,&Tm.tm_year) != 6)
+ return false;
+ }
+ }
+
+ Tm.tm_isdst = 0;
+ Tm.tm_mon = MonthConv(Month);
+ Tm.tm_year -= 1900;
+
+ // Convert to local time and then to GMT
+ Result = timegm(&Tm);
+ return true;
+}
+ /*}}}*/
diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h
index fca36fc38..1de2437b9 100644
--- a/apt-pkg/contrib/strutl.h
+++ b/apt-pkg/contrib/strutl.h
@@ -1,6 +1,6 @@
// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: strutl.h,v 1.6 1998/10/22 04:56:49 jgg Exp $
+// $Id: strutl.h,v 1.7 1998/10/23 00:50:00 jgg Exp $
/* ######################################################################
String Util - These are some usefull string functions
@@ -34,6 +34,7 @@ string Base64Encode(string Str);
string URItoFileName(string URI);
string URIAccess(string URI);
string TimeRFC1123(time_t Date);
+bool StrToTime(string Val,time_t &Result);
string LookupTag(string Message,const char *Tag,const char *Default = 0);
int StringToBool(string Text,int Default = -1);
bool ReadMessages(int Fd, vector<string> &List);