~ubuntu-branches/ubuntu/breezy/aqsis/breezy

« back to all changes in this revision

Viewing changes to libri2rib/outstream.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Will Newton
  • Date: 2004-12-07 20:06:49 UTC
  • Revision ID: james.westby@ubuntu.com-20041207200649-fccswkrvp4oc8lmn
Tags: upstream-0.9.3
Import upstream version 0.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright � 1997 - 2001, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.com
 
5
//
 
6
// This library is free software; you can redistribute it and/or
 
7
// modify it under the terms of the GNU Lesser General Public
 
8
// License as published by the Free Software Foundation; either
 
9
// version 2.1 of the License, or (at your option) any later version.
 
10
//
 
11
// This library is distributed in the hope that it will be useful,
 
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
// General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public
 
17
// License along with this library; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
/** \file
 
21
 *  \brief Fstream and Gzip output
 
22
 *  \author Lionel J. Lacour (intuition01@online.fr)
 
23
 */
 
24
 
 
25
#ifdef  WIN32
 
26
#include        <io.h>
 
27
#endif
 
28
#include "outstream.h"
 
29
#include "errno.h"
 
30
#include "string.h"
 
31
#include "error.h"
 
32
#include <unistd.h>
 
33
 
 
34
USING_NAMESPACE( libri2rib );
 
35
 
 
36
 
 
37
void CqStreamGzip::error()
 
38
{
 
39
    int * e = 0;
 
40
    const char *cp = gzerror( gzf, e );
 
41
 
 
42
    if ( *e == Z_ERRNO )
 
43
    {
 
44
        throw CqError ( RIE_SYSTEM, RIE_ERROR, strerror( errno ), TqFalse );
 
45
    }
 
46
    else
 
47
    {
 
48
        throw CqError( RIE_SYSTEM, RIE_ERROR, cp, TqFalse );
 
49
    }
 
50
}
 
51
 
 
52
CqStream & CqStreamGzip::operator<< ( int i )
 
53
{
 
54
    if ( gzprintf( gzf, "%i", i ) == 0 ) error();
 
55
    return *this;
 
56
}
 
57
 
 
58
CqStream & CqStreamGzip::operator<< ( float f )
 
59
{
 
60
    if ( gzprintf( gzf, "%f", f ) == 0 ) error();
 
61
    return *this;
 
62
}
 
63
 
 
64
CqStream & CqStreamGzip::operator<< ( std::string s )
 
65
{
 
66
    if ( gzputs( gzf, s.c_str() ) == -1 ) error();
 
67
    return *this;
 
68
}
 
69
 
 
70
CqStream & CqStreamGzip::operator<< ( char c )
 
71
{
 
72
    if ( gzputc( gzf, c ) == -1 ) error();
 
73
    return *this;
 
74
}
 
75
 
 
76
void CqStreamGzip::openFile( const char *name )
 
77
{
 
78
    gzf = gzopen( name, "wb" );
 
79
    if ( gzf == NULL )
 
80
    {
 
81
        throw CqError( RIE_NOFILE, RIE_ERROR, "Unable to open file ", name, "", TqFalse );
 
82
    }
 
83
    gzsetparams( gzf, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY );
 
84
}
 
85
 
 
86
void CqStreamGzip::openFile( int fdesc )
 
87
{
 
88
    gzf = gzdopen( dup( fdesc ), "wb" );
 
89
    if ( gzf == NULL )
 
90
    {
 
91
        char c[ 100 ];
 
92
        sprintf( c, "%u", fdesc );
 
93
        throw CqError( RIE_NOFILE, RIE_ERROR, "Unable to open file with descriptor=", c, "", TqFalse );
 
94
    }
 
95
}
 
96
 
 
97
void CqStreamGzip::closeFile()
 
98
{
 
99
    if ( gzf )
 
100
        gzclose( gzf );
 
101
}
 
102
 
 
103
void CqStreamGzip::flushFile()
 
104
{
 
105
    //  At the end of a procedural RunProgram the
 
106
    // gzip internal state must be reset.
 
107
    if ( gzf )
 
108
    {
 
109
        gzflush( gzf, Z_FINISH );
 
110
    }
 
111
}
 
112
 
 
113
 
 
114
 
 
115
 
 
116
void CqStreamFDesc::error()
 
117
{
 
118
    throw CqError ( RIE_SYSTEM, RIE_ERROR, strerror( errno ), TqFalse );
 
119
}
 
120
 
 
121
CqStream & CqStreamFDesc::operator<< ( int i )
 
122
{
 
123
    if ( fprintf( fstr, "%i", i ) < 0 ) error();
 
124
    return *this;
 
125
}
 
126
 
 
127
CqStream & CqStreamFDesc::operator<< ( float f )
 
128
{
 
129
    if ( fprintf( fstr, "%f", f ) < 0 ) error();
 
130
    return *this;
 
131
}
 
132
 
 
133
CqStream & CqStreamFDesc::operator<< ( std::string s )
 
134
{
 
135
    if ( fputs( s.c_str(), fstr ) == EOF ) error();
 
136
    return *this;
 
137
}
 
138
 
 
139
CqStream & CqStreamFDesc::operator<< ( char c )
 
140
{
 
141
    if ( fputc( c, fstr ) == EOF ) error();
 
142
    return *this;
 
143
}
 
144
 
 
145
void CqStreamFDesc::openFile( const char *name )
 
146
{
 
147
    fstr = fopen( name, "wb" );
 
148
    if ( fstr == NULL )
 
149
    {
 
150
        throw CqError( RIE_NOFILE, RIE_ERROR, "Unable to open file ", name, "", TqFalse );
 
151
    }
 
152
}
 
153
 
 
154
void CqStreamFDesc::openFile ( int fdesc )
 
155
{
 
156
    fstr = fdopen ( dup( fdesc ), "wb" );
 
157
    if ( fstr == NULL )
 
158
    {
 
159
        char c[ 100 ];
 
160
        sprintf( c, "%u", fdesc );
 
161
        throw CqError( RIE_NOFILE, RIE_ERROR, "Unable to open file with descriptor=", c, "", TqFalse );
 
162
    }
 
163
}
 
164
 
 
165
void CqStreamFDesc::closeFile()
 
166
{
 
167
    if ( fstr )
 
168
        fclose( fstr );
 
169
}
 
170
 
 
171
void CqStreamFDesc::flushFile()
 
172
{
 
173
    if ( fstr )
 
174
        fflush( fstr );
 
175
}