1 /*
2 * IO layer : handles for URIs
3 *
4 * Authors:
5 * Johan Ceuppens <jceuppen at easynet dot be>
6 *
7 * Copyright (C) 2004 Johan Ceuppens
8 *
9 * Released under GNU LGPL, read the file 'COPYING.LIB' for more information
10 */
12 #ifndef __STREAM_HANDLES_H_
13 #define __STREAM_HANDLES_H_
15 #include <stdio.h>
16 #include <exception>
18 #include "forward.h"
20 namespace Inkscape {
22 /**
23 * URIHandle (Abstract class)
24 */
26 class URIHandle
27 {
28 public:
29 virtual ~URIHandle() {}
30 virtual int read (void *buf, int buflen) = 0;
31 virtual int write (void const *buf, int buflen) = 0;
32 virtual void close() = 0;
34 protected:
36 virtual int sys_read (void *buf, int buflen) = 0;
37 virtual int sys_write (void const *buf, int buflen) = 0;
38 virtual void sys_close() = 0;
39 virtual void error(char const *errstr) = 0;
41 };
43 /**
44 * FileHandle
45 */
47 class IOException : public std::exception {};
49 class ReadException : public IOException
50 {
51 public:
52 const char *what() const throw() { return "error read"; }
53 };
55 class WriteException : public IOException
56 {
57 public:
58 const char *what() const throw() { return "error write"; }
59 };
61 class FileHandle : public URIHandle
62 {
63 public:
64 FileHandle() : fp(0) {}
65 virtual ~FileHandle() { if (fp) sys_close(); };
66 virtual int open(URI const& uri, char const* mode);
67 virtual void close();
68 virtual int read (void *buf, int buflen);
69 virtual int write (void const *buf, int buflen);
70 virtual int seek (long offset, int whence);
71 protected:
73 virtual FILE *sys_open(URI const& uri, char const* mode);
74 virtual void sys_close();
75 virtual int sys_read(void *buf, int buflen) throw(ReadException);
76 virtual int sys_write(void const *buf, int buflen) throw(WriteException);
77 virtual int sys_seek(long offset, int whence);
78 virtual void error(char const *errstr);
79 FILE *get_fp() { return fp; }
81 private:
82 FILE *fp;
83 };
85 /*
86 class SocketHandle : public URIHandle
87 {
88 // ...
89 };
90 */
91 }
92 #endif
94 /*
95 Local Variables:
96 mode:c++
97 c-file-style:"stroustrup"
98 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
99 indent-tabs-mode:nil
100 fill-column:99
101 End:
102 */
103 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :