From 4bc6ed56aa48784c8caf56c3a6fb1a3c972d221c Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Fri, 7 Jan 2011 20:35:43 -0600 Subject: Refactor old date parsing into single method We've managed to duplicate this four times at this point, so make it a method in util.c instead. Signed-off-by: Dan McGee --- lib/libalpm/be_local.c | 25 ++----------------------- lib/libalpm/be_package.c | 12 +----------- lib/libalpm/be_sync.c | 13 +------------ lib/libalpm/util.c | 13 +++++++++++++ lib/libalpm/util.h | 1 + 5 files changed, 18 insertions(+), 46 deletions(-) (limited to 'lib') diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c index 4c8e0e9c..109edf49 100644 --- a/lib/libalpm/be_local.c +++ b/lib/libalpm/be_local.c @@ -28,7 +28,6 @@ #include /* intmax_t */ #include #include -#include #include #include /* PATH_MAX */ #include /* setlocale */ @@ -570,33 +569,13 @@ int _alpm_local_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq) goto error; } _alpm_strtrim(line); - - char first = tolower((unsigned char)line[0]); - if(first > 'a' && first < 'z') { - struct tm tmp_tm = {0}; /* initialize to null in case of failure */ - setlocale(LC_TIME, "C"); - strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm); - info->builddate = mktime(&tmp_tm); - setlocale(LC_TIME, ""); - } else { - info->builddate = atol(line); - } + info->builddate = _alpm_parsedate(line); } else if(strcmp(line, "%INSTALLDATE%") == 0) { if(fgets(line, sizeof(line), fp) == NULL) { goto error; } _alpm_strtrim(line); - - char first = tolower((unsigned char)line[0]); - if(first > 'a' && first < 'z') { - struct tm tmp_tm = {0}; /* initialize to null in case of failure */ - setlocale(LC_TIME, "C"); - strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm); - info->installdate = mktime(&tmp_tm); - setlocale(LC_TIME, ""); - } else { - info->installdate = atol(line); - } + info->installdate = _alpm_parsedate(line); } else if(strcmp(line, "%PACKAGER%") == 0) { if(fgets(line, sizeof(line), fp) == NULL) { goto error; diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c index ae9b9533..4ea3eba8 100644 --- a/lib/libalpm/be_package.c +++ b/lib/libalpm/be_package.c @@ -24,7 +24,6 @@ #include #include #include -#include #include /* setlocale */ #include @@ -203,16 +202,7 @@ static int parse_descfile(struct archive *a, pmpkg_t *newpkg) } else if(strcmp(key, "license") == 0) { newpkg->licenses = alpm_list_add(newpkg->licenses, strdup(ptr)); } else if(strcmp(key, "builddate") == 0) { - char first = tolower((unsigned char)ptr[0]); - if(first > 'a' && first < 'z') { - struct tm tmp_tm = {0}; /* initialize to null in case of failure */ - setlocale(LC_TIME, "C"); - strptime(ptr, "%a %b %e %H:%M:%S %Y", &tmp_tm); - newpkg->builddate = mktime(&tmp_tm); - setlocale(LC_TIME, ""); - } else { - newpkg->builddate = atol(ptr); - } + newpkg->builddate = _alpm_parsedate(ptr); } else if(strcmp(key, "packager") == 0) { STRDUP(newpkg->packager, ptr, RET_ERR(PM_ERR_MEMORY, -1)); } else if(strcmp(key, "arch") == 0) { diff --git a/lib/libalpm/be_sync.c b/lib/libalpm/be_sync.c index c7d8e045..81f3e46e 100644 --- a/lib/libalpm/be_sync.c +++ b/lib/libalpm/be_sync.c @@ -21,7 +21,6 @@ #include "config.h" #include -#include #include #include @@ -313,17 +312,7 @@ static int sync_db_read(pmdb_t *db, struct archive *archive, struct archive_entr READ_AND_STORE(pkg->arch); } else if(strcmp(line, "%BUILDDATE%") == 0) { READ_NEXT(line); - char first = tolower((unsigned char)line[0]); - if(first > 'a' && first < 'z') { - /* initialize to null in case of failure */ - struct tm tmp_tm = {0}; - setlocale(LC_TIME, "C"); - strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm); - pkg->builddate = mktime(&tmp_tm); - setlocale(LC_TIME, ""); - } else { - pkg->builddate = atol(line); - } + pkg->builddate = _alpm_parsedate(line); } else if(strcmp(line, "%PACKAGER%") == 0) { READ_AND_STORE(pkg->packager); } else if(strcmp(line, "%CSIZE%") == 0) { diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 18245646..5f7512fb 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -923,4 +923,17 @@ unsigned long _alpm_hash_sdbm(const char *str) return(hash); } +long _alpm_parsedate(const char *line) +{ + if(isalpha((unsigned char)line[0])) { + /* initialize to null in case of failure */ + struct tm tmp_tm = { 0 }; + setlocale(LC_TIME, "C"); + strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm); + setlocale(LC_TIME, ""); + return(mktime(&tmp_tm)); + } + return(atol(line)); +} + /* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index 10f367fd..be5c1d9b 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -95,6 +95,7 @@ int _alpm_test_md5sum(const char *filepath, const char *md5sum); int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b); int _alpm_splitname(const char *target, pmpkg_t *pkg); unsigned long _alpm_hash_sdbm(const char *str); +long _alpm_parsedate(const char *line); #ifndef HAVE_STRSEP char *strsep(char **, const char *); -- cgit v1.2.3-70-g09d2