[pacman-dev] [PATCH] Extract a write_deps function from local database writing
This reduces a lot of code duplication in the write function, which cleans it up a bit. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/be_local.c | 67 ++++++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 45 deletions(-) diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c index 16c794e..7cce022 100644 --- a/lib/libalpm/be_local.c +++ b/lib/libalpm/be_local.c @@ -749,6 +749,22 @@ int _alpm_local_db_prepare(alpm_db_t *db, alpm_pkg_t *info) return retval; } +void write_deps(FILE *fp, const char *header, alpm_list_t *deplist) +{ + alpm_list_t *lp; + if(!deplist) { + return; + } + fputs(header, fp); + fputc('\n', fp); + for(lp = deplist; lp; lp = lp->next) { + char *depstring = alpm_dep_compute_string(lp->data); + fprintf(fp, "%s\n", depstring); + free(depstring); + } + fputc('\n', fp); +} + int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq) { FILE *fp = NULL; @@ -790,15 +806,6 @@ int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq } fprintf(fp, "\n"); } - if(info->replaces) { - fputs("%REPLACES%\n", fp); - for(lp = info->replaces; lp; lp = lp->next) { - char *depstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", depstring); - free(depstring); - } - fprintf(fp, "\n"); - } if(info->url) { fprintf(fp, "%%URL%%\n" "%s\n\n", info->url); @@ -851,42 +858,12 @@ int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq } fprintf(fp, "\n"); } - if(info->depends) { - fputs("%DEPENDS%\n", fp); - for(lp = info->depends; lp; lp = lp->next) { - char *depstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", depstring); - free(depstring); - } - fprintf(fp, "\n"); - } - if(info->optdepends) { - fputs("%OPTDEPENDS%\n", fp); - for(lp = info->optdepends; lp; lp = lp->next) { - char *optstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", optstring); - free(optstring); - } - fprintf(fp, "\n"); - } - if(info->conflicts) { - fputs("%CONFLICTS%\n", fp); - for(lp = info->conflicts; lp; lp = lp->next) { - char *depstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", depstring); - free(depstring); - } - fprintf(fp, "\n"); - } - if(info->provides) { - fputs("%PROVIDES%\n", fp); - for(lp = info->provides; lp; lp = lp->next) { - char *depstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", depstring); - free(depstring); - } - fprintf(fp, "\n"); - } + + write_deps(fp, "%REPLACES%", info->replaces); + write_deps(fp, "%DEPENDS%", info->depends); + write_deps(fp, "%OPTDEPENDS%", info->optdepends); + write_deps(fp, "%CONFLICTS%", info->conflicts); + write_deps(fp, "%PROVIDES%", info->provides); fclose(fp); fp = NULL; -- 1.7.9.3
On Fri, Mar 09, 2012 at 02:11:08PM -0600, Dan McGee wrote:
This reduces a lot of code duplication in the write function, which cleans it up a bit.
Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/be_local.c | 67 ++++++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 45 deletions(-)
diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c index 16c794e..7cce022 100644 --- a/lib/libalpm/be_local.c +++ b/lib/libalpm/be_local.c @@ -749,6 +749,22 @@ int _alpm_local_db_prepare(alpm_db_t *db, alpm_pkg_t *info) return retval; }
+void write_deps(FILE *fp, const char *header, alpm_list_t *deplist)
static?
+{ + alpm_list_t *lp; + if(!deplist) { + return; + } + fputs(header, fp); + fputc('\n', fp); + for(lp = deplist; lp; lp = lp->next) { + char *depstring = alpm_dep_compute_string(lp->data); + fprintf(fp, "%s\n", depstring);
if you're going to use the fputs/fputc combo above for the header, why not here as well?
+ free(depstring); + } + fputc('\n', fp); +} + int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq) { FILE *fp = NULL; @@ -790,15 +806,6 @@ int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq } fprintf(fp, "\n"); } - if(info->replaces) { - fputs("%REPLACES%\n", fp); - for(lp = info->replaces; lp; lp = lp->next) { - char *depstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", depstring); - free(depstring); - } - fprintf(fp, "\n"); - } if(info->url) { fprintf(fp, "%%URL%%\n" "%s\n\n", info->url); @@ -851,42 +858,12 @@ int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq } fprintf(fp, "\n"); } - if(info->depends) { - fputs("%DEPENDS%\n", fp); - for(lp = info->depends; lp; lp = lp->next) { - char *depstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", depstring); - free(depstring); - } - fprintf(fp, "\n"); - } - if(info->optdepends) { - fputs("%OPTDEPENDS%\n", fp); - for(lp = info->optdepends; lp; lp = lp->next) { - char *optstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", optstring); - free(optstring); - } - fprintf(fp, "\n"); - } - if(info->conflicts) { - fputs("%CONFLICTS%\n", fp); - for(lp = info->conflicts; lp; lp = lp->next) { - char *depstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", depstring); - free(depstring); - } - fprintf(fp, "\n"); - } - if(info->provides) { - fputs("%PROVIDES%\n", fp); - for(lp = info->provides; lp; lp = lp->next) { - char *depstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", depstring); - free(depstring); - } - fprintf(fp, "\n"); - } + + write_deps(fp, "%REPLACES%", info->replaces); + write_deps(fp, "%DEPENDS%", info->depends); + write_deps(fp, "%OPTDEPENDS%", info->optdepends); + write_deps(fp, "%CONFLICTS%", info->conflicts); + write_deps(fp, "%PROVIDES%", info->provides);
fclose(fp); fp = NULL; -- 1.7.9.3
On Fri, Mar 9, 2012 at 2:16 PM, Dave Reisner <d@falconindy.com> wrote:
On Fri, Mar 09, 2012 at 02:11:08PM -0600, Dan McGee wrote:
This reduces a lot of code duplication in the write function, which cleans it up a bit.
Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/be_local.c | 67 ++++++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 45 deletions(-)
diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c index 16c794e..7cce022 100644 --- a/lib/libalpm/be_local.c +++ b/lib/libalpm/be_local.c @@ -749,6 +749,22 @@ int _alpm_local_db_prepare(alpm_db_t *db, alpm_pkg_t *info) return retval; }
+void write_deps(FILE *fp, const char *header, alpm_list_t *deplist)
static? Yes, good catch, thanks.
+{ + alpm_list_t *lp; + if(!deplist) { + return; + } + fputs(header, fp); + fputc('\n', fp); + for(lp = deplist; lp; lp = lp->next) { + char *depstring = alpm_dep_compute_string(lp->data); + fprintf(fp, "%s\n", depstring);
if you're going to use the fputs/fputc combo above for the header, why not here as well? Agreed; I have another patch that converts a number of fprintf() calls to simpler fputs/fputc calls that don't require %% escaping.
-Dan
Using fputs should be faster as no format string parsing is required. It also prevents silly errors related to unescaped '%' signs, and removes the need to double them up in a lot of places. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/be_local.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c index 55d949f..9090c81 100644 --- a/lib/libalpm/be_local.c +++ b/lib/libalpm/be_local.c @@ -759,7 +759,8 @@ static void write_deps(FILE *fp, const char *header, alpm_list_t *deplist) fputc('\n', fp); for(lp = deplist; lp; lp = lp->next) { char *depstring = alpm_dep_compute_string(lp->data); - fprintf(fp, "%s\n", depstring); + fputs(depstring, fp); + fputc('\n', fp); free(depstring); } fputc('\n', fp); @@ -802,9 +803,10 @@ int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq if(info->groups) { fputs("%GROUPS%\n", fp); for(lp = info->groups; lp; lp = lp->next) { - fprintf(fp, "%s\n", (char *)lp->data); + fputs(lp->data, fp); + fputc('\n', fp); } - fprintf(fp, "\n"); + fputc('\n', fp); } if(info->url) { fprintf(fp, "%%URL%%\n" @@ -813,9 +815,10 @@ int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq if(info->licenses) { fputs("%LICENSE%\n", fp); for(lp = info->licenses; lp; lp = lp->next) { - fprintf(fp, "%s\n", (char *)lp->data); + fputs(lp->data, fp); + fputc('\n', fp); } - fprintf(fp, "\n"); + fputc('\n', fp); } if(info->arch) { fprintf(fp, "%%ARCH%%\n" @@ -856,7 +859,7 @@ int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq if(info->validation & ALPM_PKG_VALIDATION_SIGNATURE) { fputs("pgp\n", fp); } - fprintf(fp, "\n"); + fputc('\n', fp); } write_deps(fp, "%REPLACES%", info->replaces); @@ -885,20 +888,21 @@ int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq free(path); if(info->files.count) { size_t i; - fprintf(fp, "%%FILES%%\n"); + fputs("%FILES%\n", fp); for(i = 0; i < info->files.count; i++) { const alpm_file_t *file = info->files.files + i; - fprintf(fp, "%s\n", file->name); + fputs(file->name, fp); + fputc('\n', fp); } - fprintf(fp, "\n"); + fputc('\n', fp); } if(info->backup) { - fprintf(fp, "%%BACKUP%%\n"); + fputs("%BACKUP%\n", fp); for(lp = info->backup; lp; lp = lp->next) { const alpm_backup_t *backup = lp->data; fprintf(fp, "%s\t%s\n", backup->name, backup->hash); } - fprintf(fp, "\n"); + fputc('\n', fp); } fclose(fp); fp = NULL; -- 1.7.9.3
participants (3)
-
Dan McGee
-
Dan McGee
-
Dave Reisner