stdiostream

This code is being modified... It currently has a couple of bugs.

Hey all, I just wanted to play around with this kind of stuff so I thought I'd share.

This is a stdiobuf implementation of my own design.
The purpose is to allow you to wrap a C stdio FILE* with a std::iostream.
I make no claims as to correctness or efficiency. (But I think I did a pretty good job...)

It is sometimes convenient to do this kind of thing and get the full power of C++ while interfacing with C. It won't prevent you from doing particularly stupid things with it though.

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
172
173
174
// stdiostream.hpp
//
// Copyright (c) 2010 Michael Thomas Greer
// Distributed under the Boost Software License, Version 1.0
// (See http://www.boost.org/LICENSE_1_0.txt )
//

#pragma once
#ifndef DUTHOMHAS_STDIOSTREAM_HPP
#define DUTHOMHAS_STDIOSTREAM_HPP

#include <cstdio>
#include <iostream>
#include <streambuf>
#include <string>
#include <vector>

namespace duthomhas
  {

  /* /////////////////////////////////////////////////////////////////////////
    basic_stdiobuf
  ///////////////////////////////////////////////////////////////////////// */

  template <
    typename CharType,
    typename CharTraits = std::char_traits <CharType>
    >
  class basic_stdiobuf: public std::basic_streambuf <CharType, CharTraits>
    {
    //------------------------------------------------------------------------
    public:
    //------------------------------------------------------------------------
      typedef CharType                                char_type;
      typedef CharTraits                              traits_type;
      typedef typename traits_type::int_type          int_type;
      typedef typename traits_type::pos_type          pos_type;
      typedef typename traits_type::off_type          off_type;

      typedef basic_stdiobuf <char_type, traits_type> this_type;

      //......................................................................
      basic_stdiobuf( FILE* fp = NULL ):
        fp( fp )
        { }

      //......................................................................
//BUG 1: Hey! I never get called! (How is that?)
      ~basic_stdiobuf()
        {
        this->close();
        }

      //......................................................................
      bool is_open() const throw()
        {
        return fp != NULL;
        }

      //......................................................................
      this_type* open( const char* filename, std::ios_base::openmode mode )
        {
        if (is_open()) return NULL;

        // Figure out the open mode flags  . . . . . . . . . . . . . . . . . .
        std::string fmode;

        bool is_ate = mode & std::ios_base::ate;
        bool is_bin = mode & std::ios_base::binary;
        mode &= ~(std::ios_base::ate | std::ios_base::binary);

        #define _(flag) std::ios_base::flag
        if      (mode == (         _(in)                    )) fmode = "r";
        else if (mode == (                 _(out) & _(trunc))) fmode = "w";
        else if (mode == (_(app)         & _(out)           )) fmode = "a";
        else if (mode == (         _(in) & _(out)           )) fmode = "r+";
        else if (mode == (         _(in) & _(out) & _(trunc))) fmode = "w+";
        else if (mode == (_(app) & _(in) & _(out)           )) fmode = "a+";
        // I would prefer to throw an exception here,
        // but the standard only wants a NULL result.
        else return NULL;
        #undef _
        if (is_bin) fmode.insert( 1, 1, 'b' );

        // Try opening the file  . . . . . . . . . . . . . . . . . . . . . . .
        fp = std::fopen( filename, fmode.c_str() );
        if (!fp) return NULL;

        // Reposition to EOF if wanted . . . . . . . . . . . . . . . . . . . .
        if (is_ate) std::fseek( fp, 0, SEEK_END );

        return this;
        }

      //......................................................................
      this_type* close()
        {
        if (fp)
          {
          std::fclose( fp );
          fp = NULL;
          }
        pushbacks.clear();
        return this;
        }

      //......................................................................
      FILE* stdiofile() const
        {
        return fp;
        }

    //------------------------------------------------------------------------
    protected:
    //------------------------------------------------------------------------

      //......................................................................
      // Get the CURRENT character without advancing the file pointer
      virtual int_type underflow()
        {
        // Return anything previously pushed-back
        if (pushbacks.size())
          return pushbacks.back();

        // Else do the right thing
        fpos_t pos;
        if (std::fgetpos( fp, &pos ) != 0)
          return traits_type::eof();
          
        int c = std::fgetc( fp );
        std::fsetpos( fp, &pos );

        return maybe_eof( c );
        }

      //......................................................................
      // Get the CURRENT character AND advance the file pointer
      virtual int_type uflow()
        {
        // Return anything previously pushed-back
        if (pushbacks.size())
          {
          int_type c = pushbacks.back();
          pushbacks.pop_back();
          return c;
          }

        // Else do the right thing
        return maybe_eof( std::fgetc( fp ) );
        }

      //......................................................................
      virtual int_type pbackfail( int_type c = traits_type::eof() )
        {
        if (!is_open())
          return traits_type::eof();

        // If the argument c is EOF and the file pointer is not at the
        // beginning of the character sequence, it is decremented by one.
        if (traits_type::eq_int_type( c, traits_type::eof() ))
          {
          pushbacks.clear();
          return std::fseek( fp, -1L, SEEK_CUR )
               ? traits_type::eof()
               : 0;
          }

        // Otherwise, make the argument the next value to be returned by
        // underflow() or uflow()
        pushbacks.push_back( c );
        return c;
        }

      //...................................................................... 

Due to post size, the file must be split. The remainder follows below.
Last edited on
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
      virtual int_type overflow( int_type c = traits_type::eof() )
        {
        pushbacks.clear();

        // Do nothing
        if (traits_type::eq_int_type( c, traits_type::eof() ))
          return 0;

        // Else write a character
        return maybe_eof( std::fputc( c, fp ) );
        }

      //......................................................................
      virtual this_type* setbuf( char* s, std::streamsize n )
        {
        return std::setvbuf( fp, s, (s and n) ? _IOLBF : _IONBF, (size_t)n )
             ? NULL
             : this;
        }

      //......................................................................
      virtual pos_type seekoff(
        off_type                offset,
        std::ios_base::seekdir  direction,
        std::ios_base::openmode which = std::ios_base::in | std::ios_base::out
        ) {
        pushbacks.clear();
        return std::fseek( fp, offset,
          (direction == std::ios_base::beg) ? SEEK_SET :
          (direction == std::ios_base::cur) ? SEEK_CUR :
                                              SEEK_END
          ) ? (-1) : std::ftell( fp );
        }

      //......................................................................
      virtual pos_type seekpos(
        pos_type                position,
        std::ios_base::openmode which = std::ios_base::in | std::ios_base::out
        ) {
        pushbacks.clear();
        return std::fseek( fp, position, SEEK_SET )
             ? (-1)
             : std::ftell( fp );
        }

      //......................................................................
      virtual int sync()
        {
        pushbacks.clear();
        return std::fflush( fp )
             ? traits_type::eof()
             : 0;
        }

    //------------------------------------------------------------------------
    private:
    //------------------------------------------------------------------------
      FILE*                  fp;
      std::vector <int_type> pushbacks;  // we'll treat this like a stack

      //......................................................................
      // Utility function to make sure EOF gets translated to the proper value
      inline int_type maybe_eof( int value ) const
        {
        return (value == EOF) ? traits_type::eof() : value;
        }
    };


  /* /////////////////////////////////////////////////////////////////////////
    basic_stdiostream
  ///////////////////////////////////////////////////////////////////////// */

  template <
    typename CharType,
    typename CharTraits = std::char_traits <CharType>
    >
  struct basic_stdiostream: public std::basic_iostream <CharType, CharTraits>
    {
    typedef CharType                                      char_type;
    typedef CharTraits                                    traits_type;

    typedef basic_stdiobuf      <char_type, traits_type>  sbuf_type;
    typedef basic_stdiostream   <char_type, traits_type>  this_type;
    typedef std::basic_iostream <char_type, traits_type>  base_type;

    //......................................................................
    basic_stdiostream( FILE* fp = NULL ):
      base_type( new sbuf_type( fp ) )
      { }

    //......................................................................
    basic_stdiostream( const char* filename, std::ios_base::openmode mode ):
//BUG 2: Oops! This is a potential memory leak!
      base_type( (new sbuf_type)->open( filename, mode ) )
      { }

    //......................................................................
    void open(
      const char*             filename,
      std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out
      ) {
      sbuf_type* buf = static_cast <sbuf_type*> ( this->rdbuf() );
      if (!(buf->open( filename, mode )))
        this->setstate( std::ios_base::badbit );
      }

    //......................................................................
    void close()
      {
      sbuf_type* buf = static_cast <sbuf_type*> ( this->rdbuf() );
      buf->close();
      }
    };


  /* /////////////////////////////////////////////////////////////////////////
    Useful typedefs
  ///////////////////////////////////////////////////////////////////////// */

  typedef basic_stdiobuf    <char> stdiobuf;
  typedef basic_stdiostream <char> stdiostream;

  } // namespace duthomhas

#endif

// end stdiostream.hpp 

You can also use it much like you use a normal std::fstream, except it works through a FILE*, of course...

It is most convenient, however, if you have an open FILE* that you wish to perform some C++ manipulations on...

All commentary and feedback welcome.

--Michael
Last edited on
closed account (S6k9GNh0)
Excellent job! When would I want to use this?

btw, your namespace is pretty awesome.
Thanks! :-)

Uses
Use it whenever you must use a FILE* but still want C++ super powers.
Typical instances are when interfacing with a C library and when playing with tmpfile() and the like. For example:

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
// Play with a tmpfile()
//
// BTW, the tmpfile() function is not a very good choice on Windows. The following
// code may not work for you. (If it does it will probably stick the temporary in the root
// directory of the current drive, so make sure you have access permissions for it first.)
//
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>

#include "stdiostream.hpp"

using namespace std;
using namespace duthomhas;

int main()
  {

  // Open a temporary file to play with
  stdiostream tf( tmpfile() );
  if (!tf)
    {
    cerr << "Fooey!\n";
    return 1;
    }

  // Write some stuff to it...
  tf << "3.";
  tf << "141592\n";
  tf << "Hello ";
  tf << "world!\n";

  // rewind
  tf.seekg( 0, ios::beg );

  // Get the stuff we wrote..
  double pi;
  tf >> pi;
  tf.ignore( numeric_limits <streamsize> ::max(), '\n' );

  string greeting;
  getline( tf, greeting );

  // Show whether suceessful:
  cout << greeting << endl;
  cout << "pi = " << pi << endl;

  // Play with the user
  cout << "\nNow is the time to check that the temporary file actually exists.\n"
          "Press ENTER once done.\n";
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  // All done.
  cout << "\nOK, now it should be gone...\n";
  return 0;
  }

Other Issues
An unresolved issue is whether the destructor should endeavor to automatically close the file or not, or whether this should be a flaggable option.

This is what I am currently most concerned with.
I'd like to figure out what I want to do before this page archives (and becomes non-editable).

Namespace
Thanks. "Duoas" is a phonetic writing to help people (of any language) properly pronounce "dúthomhas", which is Gaelic for "enigma".

Which... having explained it it's not much of a mystery anymore...

Alas. At least it's unique...
:-]
IMO, it should close it (if only to make it seem more like an fstream).
That was my first thought too.

Here's the scenario I am wondering about: a foreign library gives me a FILE* to read/write data, but I only want to manipulate the stream for a short bit -- said library will manipulate the stream further after I am done with it. If I close it, that is a problem for the interfacing code.

I am pretty sure I will update it to make closing an option.

My current thought is that it should only automatically close the FILE if it opened the file to begin with. There will be an optional argument to modify this behavior, as well as an "unlink" or "disassociate" method or something...

Hmm, I like that. I think I'll do that now...
1
2
 stdiostream tf( tmpfile() );
  if (!tf)


Why does this work? You did not supply any operators to convert to either int or bool or anything else...
It is inherited from the std::basic_iostream.

I still need to fix this. There are still a couple of bugs to fix, and as this is just a fun hack I had to put it on the back burner for a while. I'll try to fix it sooner than later though (before this topic gets archived...)
I've seen that it derived from that. but how does std::basc_iostream know that you have successfully opened a file?
because the !operator has been internally overloaded to do a tf.fail() or tf.bad()[Or what it has been - I don´t remember, but it checks for the error flags]
Yes, the ! operator and the void* operator are overloaded to correspond to fail() and good(), respectively.
http://www.cplusplus.com/reference/iostream/ios/operatornot/
http://www.cplusplus.com/reference/iostream/ios/operator_voidpt/
"Enigma", you are epic.
Well i don't want to be nit-picky but you don't provide implementations of neither of those. All i could find was pbackfail();

I want to know this to better understand how the iostream class is setup.
That´s because
Duoas wrote:
It is inherited from the std::basic_iostream.
closed account (z05DSL3A)
Duoas,

I thought you might be interested, just run your code through Visual Studio 2010 with my standard 'release' settings (but without treat warnings as errors), including:
+ warning level 4 [-Wall just spat the dummy with 127 warnings in un/semi-related code]
+ Code Analysis enabled (all rules)

» VC had a problem the and on line 186 (of stdiostream.hpp).1
» warning C4244: 'argument' : conversion from '__int64' to 'long', possible loss of data on line 202
» warning C4100: 'which' : unreferenced formal parameter on line 195
» warning C4244: 'argument' : conversion from 'std::streamoff' to 'long', possible loss of data on line 211
» warning C4100: 'which' : unreferenced formal parameter on line 208
» warning C6031: Return value ignored: 'fsetpos' on line 131

1 Turned off language extensions to get around this. Why support for and, or, et al is removed with language extensions turned on I will never understand.


Ah, that's because I forgot to #include <ciso646>.
Thanks for the feedback. I'll probably work on this today...
Topic archived. No new replies allowed.