summaryrefslogtreecommitdiffstats
path: root/src/io/stream/uristream.cpp
blob: 734b8fac54b9b3fe591c5b16f2705586ad99c7cd (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Our base String stream classes.  We implement these to
 * be based on Glib::ustring
 *
 * Authors:
 *   Bob Jamison <rjamison@titan.com>
 *
 * Copyright (C) 2004 Inkscape.org
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */


#include "uristream.h"
#include "io/sys.h"


namespace Inkscape
{
namespace IO
{

//#########################################################################
//# F I L E    I N P U T    S T R E A M
//#########################################################################


/**
 *
 */
FileInputStream::FileInputStream(FILE *source)
    : inf(source)
{
    if (!inf) {
        Glib::ustring err = "FileInputStream passed NULL";
        throw StreamException(err);
    }
}

/**
 *
 */
FileInputStream::~FileInputStream()
{
    close();
}

/**
 * Returns the number of bytes that can be read (or skipped over) from
 * this input stream without blocking by the next caller of a method for
 * this input stream.
 */
int FileInputStream::available()
{
    return 0;
}


/**
 *  Closes this input stream and releases any system resources
 *  associated with the stream.
 */
void FileInputStream::close()
{
            if (!inf)
                return;
            fflush(inf);
            fclose(inf);
            inf=nullptr;
}

/**
 * Reads the next byte of data from the input stream.  -1 if EOF
 */
int FileInputStream::get()
{
    int retVal = -1;
                if (!inf || feof(inf))
                {
                    retVal = -1;
                }
                else
                {
                    retVal = fgetc(inf);
                }

    return retVal;
}




//#########################################################################
//#  F I L E    O U T P U T    S T R E A M
//#########################################################################

FileOutputStream::FileOutputStream(FILE *fp)
    : ownsFile(false)
    , outf(fp)
{
    if (!outf) {
        Glib::ustring err = "FileOutputStream given null file ";
        throw StreamException(err);
    }
}

/**
 *
 */
FileOutputStream::~FileOutputStream()
{
    close();
}

/**
 * Closes this output stream and releases any system resources
 * associated with this stream.
 */
void FileOutputStream::close()
{
            if (!outf)
                return;
            fflush(outf);
            if ( ownsFile )
                fclose(outf);
            outf=nullptr;
}

/**
 *  Flushes this output stream and forces any buffered output
 *  bytes to be written out.
 */
void FileOutputStream::flush()
{
            if (!outf)
                return;
            fflush(outf);
}

/**
 * Writes the specified byte to this output stream.
 */
int FileOutputStream::put(char ch)
{
    unsigned char uch;

            if (!outf)
                return -1;
            uch = (unsigned char)(ch & 0xff);
            if (fputc(uch, outf) == EOF) {
                Glib::ustring err = "ERROR writing to file ";
                throw StreamException(err);
            }

    return 1;
}





} // namespace IO
} // namespace Inkscape


//#########################################################################
//# E N D    O F    F I L E
//#########################################################################

// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :