File: fileutil.cxx

package info (click to toggle)
swath 0.3.0.cvs20030404
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,564 kB
  • ctags: 545
  • sloc: sh: 6,926; cpp: 3,643; makefile: 151
file content (46 lines) | stat: -rw-r--r-- 893 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
 * fileutil.cpp - file utility functions
 * Created : 11 Jul 1996
 * Author:  Theppitak Karoonboonyanan
 */

#include "misc/fileutil.h"

#define FILEBUFFSIZE 1024

static char buf[FILEBUFFSIZE];

int copyfile(const char* from, const char* to)
{
    FILE *fFrom = fopen(from, "rb");
    FILE *fTo   = fopen(to, "wb+");
    size_t  nRead;

    if (fFrom == NULL)  { return -1; }
    if (fTo   == NULL)  { return -1; }

    while ((nRead = fread(buf, 1, sizeof buf, fFrom)) > 0) {
        if (fwrite(buf, 1, nRead, fTo) != nRead)  { return -1; }
    }

    fclose(fTo);
    fclose(fFrom);

    return 0;
}

int movefile(const char* from, const char* to)
{
    /* try renaming first, if fail, make a copy */
    if (rename(from, to) == 0) {
        return 0;
    } else {
        if (copyfile(from, to) == 0) {
            remove(from);
            return 0;
        }
    }

    return -1;
}