File: message-stream.cc

package info (click to toggle)
crawl 2%3A0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,188 kB
  • sloc: cpp: 363,709; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (119 lines) | stat: -rw-r--r-- 2,903 bytes parent folder | download | duplicates (6)
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
#include "AppHdr.h"

#include "libutil.h" // deleteAll
#include "message.h"

#include <iostream>
#include <streambuf>

namespace msg
{
    mpr_stream_buf msbuf(MSGCH_PLAIN);
    ostream stream(&msbuf);
    vector<ostream*> stream_ptrs;
    vector<mpr_stream_buf*> stream_buffers;
    capitalisation cap(true), nocap(false);

    ostream& streams(msg_channel_type chan)
    {
        ASSERT(chan >= 0
               && static_cast<unsigned int>(chan) < stream_ptrs.size());
        return *stream_ptrs[chan];
    }

    void initialise_mpr_streams()
    {
        for (int i = 0; i < NUM_MESSAGE_CHANNELS; ++i)
        {
            mpr_stream_buf* pmsb =
                new mpr_stream_buf(static_cast<msg_channel_type>(i));
            ostream* pos = new ostream(pmsb);
            (*pos) << nounitbuf;
            stream_ptrs.push_back(pos);
            stream_buffers.push_back(pmsb);
        }
        stream << nounitbuf;
    }

    void deinitialise_mpr_streams()
    {
        deleteAll(stream_ptrs);
        deleteAll(stream_buffers);
    }

    setparam::setparam(int param)
        : m_param(param)
    {}

    capitalisation::capitalisation(bool capital)
        : m_cap(capital)
    {}

    mpr_stream_buf::mpr_stream_buf(msg_channel_type chan) :
        internal_count(0), param(0), muted(false), capitalise(true),
        channel(chan)
    {}

    void mpr_stream_buf::set_param(int p)
    {
        param = p;
    }

    void mpr_stream_buf::set_muted(bool m)
    {
        muted = m;
    }

    void mpr_stream_buf::set_capitalise(bool c)
    {
        capitalise = c;
    }

    // again, can be improved
    int mpr_stream_buf::overflow(int c)
    {
        if (muted)
            return 0;

        if (c == '\n')
        {
            // null-terminate and print the string
            internal_buf[internal_count] = 0;
            if (capitalise)
                mprf(channel, param, "%s", internal_buf);
            else
                mprf_nocap(channel, param, "%s", internal_buf);

            internal_count = 0;

            // reset to defaults (param changing and capitalisation aren't
            // sticky, but muting is).
            set_param(0);
            set_capitalise(true);
        }
        else
            internal_buf[internal_count++] = c;

        if (internal_count + 3 > INTERNAL_LENGTH)
        {
            mprf(MSGCH_ERROR, "oops, hit overflow");
            internal_count = 0;
            return streambuf::traits_type::eof();
        }
        return 0;
    }
}

ostream& operator<<(ostream& os, const msg::setparam& sp)
{
    msg::mpr_stream_buf* ps = dynamic_cast<msg::mpr_stream_buf*>(os.rdbuf());
    ps->set_param(sp.m_param);
    return os;
}

ostream& operator<<(ostream& os, const msg::capitalisation& cap)
{
    msg::mpr_stream_buf* ps = dynamic_cast<msg::mpr_stream_buf*>(os.rdbuf());
    ps->set_capitalise(cap.m_cap);
    return os;
}