summaryrefslogtreecommitdiff
path: root/zend/streambuf.cpp
blob: 11d928d1b5730174735251763ad47a31353a3996 (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
/**
 *  StreamBuf.cpp
 *
 *  Implementation file for the StreamBuf class
 *
 *  @see http://www.mr-edd.co.uk/blog/beginners_guide_streambuf
 * 
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2014 Copernica BV
 */
#include "includes.h"

/**
 *  Set up namespace
 */
namespace Php {

/**
 *  Called when the internal buffer should be synchronized
 *  @return int
 */
int StreamBuf::sync()
{
    // current buffer size
    size_t size = pptr() - pbase();
    
    // is this the error stream or the regular output stream?
    if (_error)
    {
        // write to error (the zend_error() method is a varargs function, 
        // which means that we have to include a printf() like format as first
        // parameter. We can not specify pbase() directly, because (1) it is
        // not null terminated and (2) it could contain % signs and allow all
        // sorts of buffer overflows.
        zend_error(_error, "%.*s", (int)size, pbase());
    }
    else
    {
        // write to zend
        zend_write(pbase(), size);
    }
    
    // reset the buffer
    pbump(-size);
    
    // done
    return 0;
}

/**
 *  End namespace
 */
}