v0.0.6
[ppastats.git] / trunk / src / io.c
diff --git a/trunk/src/io.c b/trunk/src/io.c
new file mode 100644 (file)
index 0000000..a7c46af
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * 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 <string.h>
+#include <sys/stat.h>
+
+#include "io.h"
+
+/* Directory separator is \ when cross-compiling for MS Windows
+   systems */
+#if defined(__MINGW32__)
+#define DIRSEP '\\'
+#else
+#define DIRSEP '/'
+#endif
+
+#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;
+}
+
+char *path_append(const char *odir, const char *name)
+{
+       char *dir;
+
+       dir = malloc(strlen(odir)+1+strlen(name)+1);
+
+       sprintf(dir, "%s/%s", odir, name);
+
+       return dir;
+}
+
+void mkdirs(const char *dirs, mode_t mode)
+{
+       char *c = (char *)dirs;
+       char *dir = malloc(strlen(dirs) + 1);
+
+       int i = 0;
+       while (*c) {
+               if ((*c == DIRSEP || *c == '\0') && c != dirs) {
+                       strncpy(dir, dirs, i);
+                       dir[i] = '\0';
+                       mkdir(dir, mode);
+               }
+
+               c++;
+               i++;
+       }
+
+       mkdir(dirs, mode);
+
+       free(dir);
+}