[pacman-dev] [PATCH] Refactor old date parsing into single method
Dan McGee
dan at archlinux.org
Fri Jan 7 21:36:21 EST 2011
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 <dan at archlinux.org>
---
lib/libalpm/be_local.c | 24 ++----------------------
lib/libalpm/be_package.c | 11 +----------
lib/libalpm/be_sync.c | 12 +-----------
lib/libalpm/util.c | 14 ++++++++++++++
lib/libalpm/util.h | 1 +
5 files changed, 19 insertions(+), 43 deletions(-)
diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c
index 4c8e0e9..70675e4 100644
--- a/lib/libalpm/be_local.c
+++ b/lib/libalpm/be_local.c
@@ -570,33 +570,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 ae9b953..5d31eaf 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -203,16 +203,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 c7d8e04..568130f 100644
--- a/lib/libalpm/be_sync.c
+++ b/lib/libalpm/be_sync.c
@@ -313,17 +313,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 1824564..1ce2214 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -923,4 +923,18 @@ unsigned long _alpm_hash_sdbm(const char *str)
return(hash);
}
+long _alpm_parsedate(const char *line)
+{
+ char first = (char)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);
+ 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 10f367f..be5c1d9 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 *);
--
1.7.3.5
More information about the pacman-dev
mailing list