/usr/include/gtextutils/outbuf3.hpp is in libgtextutils-dev 0.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
Gordon's Text-Utilities Library
Copyright (C) 2009-2013 Assaf Gordon (assafgordon@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef __JOSUTTIS_FD_OUTBUF_H__
#define __JOSUTTIS_FD_OUTBUF_H__
/*
* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*
* Added by A. Gordon:
* The file is available as "io/outbuf3.hpp" in the examples tarball at
* http://www.josuttis.com/libbook/
*
* And in the book at Chapter 13, Page 673.
*
* Modifications:
* 1. Larger buffer, with vector<char>
* 2. Accepts output file descriptor in c'tor
*/
#include <cstdio>
#include <streambuf>
#include <vector>
// for write():
#ifdef _MSC_VER
# include <io.h>
#else
# include <unistd.h>
#endif
class josuttis_fd_outbuf : public std::streambuf {
protected:
static const int bufferSize = 32768; // size of data buffer
/*
* Note: The vector is used simply because it will manage the memory
* resource for us. The buffer is used as a regular C buffer.
* see "Effective STL" By Scott Meyer, Page 77,
* Item 17: "Know how to pass vector and string to legacy API"
*/
std::vector<char> buffer_vector ;
char* buffer ;
int output_fd ;
public:
/* constructor
* - initialize data buffer
* - one character less to let the bufferSizeth character
* cause a call of overflow()
*/
josuttis_fd_outbuf ( int _output_fd ) :
buffer_vector(bufferSize),
buffer ( &buffer_vector[0] ),
output_fd ( _output_fd )
{
setp (buffer, buffer+(bufferSize-1));
}
/* destructor
* - flush data buffer
*/
virtual ~josuttis_fd_outbuf () {
sync();
}
protected:
// flush the characters in the buffer
int flushBuffer () {
int num = pptr()-pbase();
if (write (output_fd, buffer, num) != num) {
return EOF;
}
pbump (-num); // reset put pointer accordingly
return num;
}
/* buffer full
* - write c and all previous characters
*/
virtual int_type overflow (int_type c) {
if (c != EOF) {
// insert character into the buffer
*pptr() = c;
pbump(1);
}
// flush the buffer
if (flushBuffer() == EOF) {
// ERROR
return EOF;
}
return c;
}
/* synchronize data with file/destination
* - flush the data in the buffer
*/
virtual int sync () {
if (flushBuffer() == EOF) {
// ERROR
return -1;
}
return 0;
}
};
/*
* An output stream that uses the above outbuf
*
* Based on code example from page 673 (class fdostream)
*/
class josuttis_fdostream : public std::ostream
{
private:
josuttis_fd_outbuf buf ;
public:
josuttis_fdostream ( int fd ) :
std::ostream(0),
buf(fd)
{
rdbuf(&buf) ;
}
};
#endif
|