summaryrefslogtreecommitdiffstats
path: root/src/streams-handles.cpp
blob: 9c6e861f80aeca9dd8fde44ee184461762430177 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * IO layer : handles for URIs
 *
 * Authors:
 *   Johan Ceuppens <jceuppen at easynet dot be>
 *
 * Copyright (C) 2004 Johan Ceuppens
 *
 * Released under GNU LGPL, read the file 'COPYING.LIB' for more information
 */

#include "streams-handles.h"
#include "uri.h"

#include <iostream>

namespace Inkscape {

/**
 * FileHandle
 */

int FileHandle::open(URI const& uri, char const* mode)
{
    if (sys_open(uri, mode) == 0)
	return 0;
    else
	return 1;
}

FILE *FileHandle::sys_open(URI const& uri, char const* mode)
{
    gchar *filename = uri.toNativeFilename();

    if ((fp = std::fopen(filename, mode)) == 0) {
	error("fopen");
    }
#ifdef DEBUG_STREAMS
    std::cout<<"file opened fp="<<fp<<std::endl;
#endif
    return fp;
}

void FileHandle::close()
{
    sys_close();
}

void FileHandle::sys_close()
{
    fclose(fp);
}

int FileHandle::read(void *buf, int buflen)
{
    return sys_read(buf, buflen);
}

int FileHandle::sys_read (void *buf, int buflen) throw(ReadException)
{
    int nbytes = 0;
    if ((nbytes = std::fread(buf, 1, buflen, fp)) < 0) {
	if (ferror(fp)) {
	    error("fread");
	    throw ReadException();
	}
    }
    if (nbytes == 0)
	return EOF;
    else
	return nbytes;
}

int FileHandle::write (void const *buf, int buflen)
{
    return sys_write(buf, buflen);
}

int FileHandle::sys_write (void const *buf, int buflen) throw(WriteException)
{
    int nbytes = 0;
    if ((nbytes = std::fwrite(buf, 1, buflen, fp)) < 0) {
	error("fwrite");
	throw WriteException();
    }

    return nbytes;
}

int FileHandle::seek(long offset, int whence)
{
    return sys_seek(offset, whence);
}

int FileHandle::sys_seek(long offset, int whence)
{
    int result;
    if ((result = fseek(fp, offset, whence)) < 0) {
	error("fseek");
    }
    return result;
}
void FileHandle::error(char const *errstr)
{
    std::cerr<<"error FileHandle: "<<errstr<<std::endl;
}

} // namespace Inkscape

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :