From 6b077ee78bd064474b576e076383503e682a1dd2 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Tue, 6 Sep 2011 22:13:23 +0000 Subject: [PATCH] moved copy fcts to separate file --- src/Makefile.am | 1 + src/Makefile.in | 8 +++--- src/html.c | 66 +------------------------------------------------ src/io.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/io.h | 44 +++++++++++++++++++++++++++++++++ src/ppastats.1 | 2 +- 6 files changed, 129 insertions(+), 69 deletions(-) create mode 100644 src/io.c create mode 100644 src/io.h diff --git a/src/Makefile.am b/src/Makefile.am index bcb15aa..fb3ce06 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,6 +9,7 @@ bin_PROGRAMS = ppastats ppastats_SOURCES = \ cache.h cache.c\ html.h html.c\ + io.h io.c\ list.h list.c\ lp.h lp.c\ lp_json.h lp_json.c\ diff --git a/src/Makefile.in b/src/Makefile.in index e164fd8..b8d1898 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -46,9 +46,9 @@ CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)" PROGRAMS = $(bin_PROGRAMS) -am_ppastats_OBJECTS = cache.$(OBJEXT) html.$(OBJEXT) list.$(OBJEXT) \ - lp.$(OBJEXT) lp_json.$(OBJEXT) lp_ws.$(OBJEXT) main.$(OBJEXT) \ - ppastats.$(OBJEXT) +am_ppastats_OBJECTS = cache.$(OBJEXT) html.$(OBJEXT) io.$(OBJEXT) \ + list.$(OBJEXT) lp.$(OBJEXT) lp_json.$(OBJEXT) lp_ws.$(OBJEXT) \ + main.$(OBJEXT) ppastats.$(OBJEXT) ppastats_OBJECTS = $(am_ppastats_OBJECTS) ppastats_LDADD = $(LDADD) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) @@ -191,6 +191,7 @@ AM_CPPFLAGS = -Wall -std=gnu99 -Werror\ ppastats_SOURCES = \ cache.h cache.c\ html.h html.c\ + io.h io.c\ list.h list.c\ lp.h lp.c\ lp_json.h lp_json.c\ @@ -282,6 +283,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cache.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/html.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/io.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lp.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lp_json.Po@am__quote@ diff --git a/src/html.c b/src/html.c index 7f8a8d3..9eb0bcf 100644 --- a/src/html.c +++ b/src/html.c @@ -27,18 +27,11 @@ #include #include "html.h" +#include "io.h" #include "lp.h" #include "lp_ws.h" #include "ppastats.h" -enum file_copy_error { - FILE_COPY_ERROR_OPEN_SRC = 1, - FILE_COPY_ERROR_OPEN_DST, - FILE_COPY_ERROR_READ, - FILE_COPY_ERROR_WRITE, - FILE_COPY_ERROR_ALLOC_BUFFER -}; - #define HTML_FOOTER \ "
Generated by \ ppastats
\n\ @@ -152,63 +145,6 @@ enum file_copy_error { \n\ %s" -#define FCOPY_BUF_SZ 4096 -static int file_copy(FILE * src, FILE * dst) -{ - int ret = 0; - char *buf = malloc(FCOPY_BUF_SZ); - int n; - - if (!buf) - return FILE_COPY_ERROR_ALLOC_BUFFER; - - while (!ret) { - n = fread(buf, 1, FCOPY_BUF_SZ, src); - if (n) { - if (fwrite(buf, 1, n, dst) != n) - ret = FILE_COPY_ERROR_WRITE; - } else { - if (!feof(src)) - ret = FILE_COPY_ERROR_READ; - else - break; - } - } - - free(buf); - - return ret; -} - -int -fcopy(const char *src, const char *dst) -{ - FILE *fsrc, *fdst; - int ret = 0; - - if (debug) - printf("DEBUG: copy: %s to %s\n", src, dst); - - fsrc = fopen(src, "r"); - - if (fsrc) { - fdst = fopen(dst, "w+"); - - if (fdst) { - ret = file_copy(fsrc, fdst); - fclose(fdst); - } else { - ret = FILE_COPY_ERROR_OPEN_DST; - } - - fclose(fsrc); - } else { - ret = FILE_COPY_ERROR_OPEN_SRC; - } - - return ret; -} - static char *path_new(const char *dir, const char *file, const char *suffixe) { char *path = malloc(strlen(dir)+1+ diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..2cce202 --- /dev/null +++ b/src/io.c @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2011 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include +#include + +#include "io.h" + +#define FCOPY_BUF_SZ 4096 + +static int file_copy(FILE *src, FILE *dst) +{ + int ret = 0; + char *buf = malloc(FCOPY_BUF_SZ); + int n; + + if (!buf) + return FILE_COPY_ERROR_ALLOC_BUFFER; + + while (!ret) { + n = fread(buf, 1, FCOPY_BUF_SZ, src); + if (n) { + if (fwrite(buf, 1, n, dst) != n) + ret = FILE_COPY_ERROR_WRITE; + } else { + if (!feof(src)) + ret = FILE_COPY_ERROR_READ; + else + break; + } + } + + free(buf); + + return ret; +} + +int fcopy(const char *src, const char *dst) +{ + FILE *fsrc, *fdst; + int ret = 0; + + fsrc = fopen(src, "r"); + + if (fsrc) { + fdst = fopen(dst, "w+"); + + if (fdst) { + ret = file_copy(fsrc, fdst); + fclose(fdst); + } else { + ret = FILE_COPY_ERROR_OPEN_DST; + } + + fclose(fsrc); + } else { + ret = FILE_COPY_ERROR_OPEN_SRC; + } + + return ret; +} diff --git a/src/io.h b/src/io.h new file mode 100644 index 0000000..b06709f --- /dev/null +++ b/src/io.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2011 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef _PPASTATS_IO_H_ +#define _PPASTATS_IO_H_ + +/* + * Convenience functions for copying files. + */ + + +enum file_copy_code { + FILE_COPY_OK = 0, + FILE_COPY_ERROR_OPEN_SRC = 1, + FILE_COPY_ERROR_OPEN_DST, + FILE_COPY_ERROR_READ, + FILE_COPY_ERROR_WRITE, + FILE_COPY_ERROR_ALLOC_BUFFER +}; + +/* + * Copies file 'src' to 'dst'. + * + * Returns FILE_COPY_OK on success otherwise error code. + */ +int fcopy(const char *src, const char *dst); + +#endif diff --git a/src/ppastats.1 b/src/ppastats.1 index 763800c..0fbbd7e 100644 --- a/src/ppastats.1 +++ b/src/ppastats.1 @@ -32,7 +32,7 @@ Report bugs to: jeanfi@gmail.com .PP ppastats home page: .SH COPYRIGHT -Copyright \(co 2010\-2011 jeanfi@gmail.com +Copyright \(co 2011 jeanfi@gmail.com License GPLv2: GNU GPL version 2 or later .br This is free software: you are free to change and redistribute it. -- 2.7.4