moved copy fcts to separate file
authorJean-Philippe Orsini <jeanfi@gmail.com>
Tue, 6 Sep 2011 22:13:23 +0000 (22:13 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Tue, 6 Sep 2011 22:13:23 +0000 (22:13 +0000)
src/Makefile.am
src/Makefile.in
src/html.c
src/io.c [new file with mode: 0644]
src/io.h [new file with mode: 0644]
src/ppastats.1

index bcb15aa..fb3ce06 100644 (file)
@@ -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\
index e164fd8..b8d1898 100644 (file)
@@ -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@
index 7f8a8d3..9eb0bcf 100644 (file)
 #include <json/json.h>
 
 #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 \
 " <div id=\"footer\">Generated by \
 <a href='http://wpitchoune.net/ppastats'>ppastats</a></div>\n\
@@ -152,63 +145,6 @@ enum file_copy_error {
     </div>\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 (file)
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 <stdio.h>
+#include <stdlib.h>
+
+#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 (file)
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
index 763800c..0fbbd7e 100644 (file)
@@ -32,7 +32,7 @@ Report bugs to: jeanfi@gmail.com
 .PP
 ppastats home page: <http://wpitchoune.net/ppastats>
 .SH COPYRIGHT
-Copyright \(co 2010\-2011 jeanfi@gmail.com
+Copyright \(co 2011 jeanfi@gmail.com
 License GPLv2: GNU GPL version 2 or later <http://www.gnu.org/licenses/old\-licenses/gpl\-2.0.html>
 .br
 This is free software: you are free to change and redistribute it.