Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / cenvir.cc @ a3be1d55

History | View | Annotate | Download (3.08 KB)

1
//=========================================================================
2
//  CENVIR.CC - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga
8
//
9
//=========================================================================
10

    
11
/*--------------------------------------------------------------*
12
  Copyright (C) 1992-2008 Andras Varga
13
  Copyright (C) 2006-2008 OpenSim Ltd.
14

15
  This file is distributed WITHOUT ANY WARRANTY. See the file
16
  `license' for details on this and other legal matters.
17
*--------------------------------------------------------------*/
18

    
19
#include "cenvir.h"
20
#include "commonutil.h"
21
#include "cconfiguration.h"
22
#include "csimulation.h"
23

    
24

    
25
/**
26
 * The std::streambuf class used by cEnvir's ostream. It redirects writes to
27
 * cEnvir::sputn(s,n). Flush is done at the end of each line, meanwhile
28
 * writes are buffered in a stringbuf.
29
 */
30
class evbuf : public std::basic_stringbuf<char>
31
{
32
  public:
33
    evbuf() {}
34
    // gcc>=3.4 needs either this-> or std::basic_stringbuf<E,T>:: in front of pptr()/pbase()
35
    // note: this method is needed because pptr() and pbase() are protected
36
    bool isempty() {return this->pptr()==this->pbase();}
37
  protected:
38
    virtual int sync()  {
39
        ev.sputn(this->pbase(), this->pptr()-this->pbase());
40
        setp(this->pbase(),this->epptr());
41
        return 0;
42
    }
43
    virtual std::streamsize xsputn(const char *s, std::streamsize n) {
44
        std::streamsize r = std::basic_stringbuf<char>::xsputn(s,n);
45
        for(;n>0;n--,s++)
46
            if (*s=='\n')
47
               {sync();break;}
48
        return r;
49
    }
50
};
51

    
52
//----
53

    
54
cEnvir::cEnvir() : out(new evbuf())
55
{
56
    disable_tracing = false;
57
    debug_on_errors = false;
58
    suppress_notifications = false; //FIXME set to true when not needed!
59
}
60

    
61
cEnvir::~cEnvir()
62
{
63
}
64

    
65
cConfigurationEx *cEnvir::getConfigEx()
66
{
67
    cConfigurationEx *cfg = dynamic_cast<cConfigurationEx *>(getConfig());
68
    if (!cfg)
69
        throw cRuntimeError("Illegal call to ev.getConfigEx(): configuration object is not subclassed from cConfigurationEx");
70
    return cfg;
71
}
72

    
73
// note: exploits the fact that evbuf does sync() on "\n"'s
74
void cEnvir::flushLastLine()
75
{
76
    evbuf *buf = (evbuf *)out.rdbuf();
77
    if (!buf->isempty())
78
        buf->sputn("\n",1);
79
}
80

    
81
//
82
// Temp buffer for vararg functions below.
83
// Note: using a static buffer reduces stack usage of activity() modules;
84
// it also makes the following functions non-reentrant, but we don't need
85
// them to be reentrant anyway.
86
//
87
#define BUFLEN 1024
88
static char staticbuf[BUFLEN];
89

    
90
void cEnvir::printfmsg(const char *fmt,...)
91
{
92
    VSNPRINTF(staticbuf, BUFLEN, fmt);
93
    putsmsg(staticbuf);
94
}
95

    
96
int cEnvir::printf(const char *fmt,...)
97
{
98
    if (disable_tracing)
99
        return 0;
100

    
101
    va_list va;
102
    va_start(va, fmt);
103
    int len = vsnprintf(staticbuf, BUFLEN, fmt, va);
104
    staticbuf[BUFLEN-1] = '\0';
105
    va_end(va);
106

    
107
    // route it through streambuf to preserve ordering
108
    out.rdbuf()->sputn(staticbuf, len);
109
    return len;
110
}
111

    
112
bool cEnvir::askYesNo(const char *fmt,...)
113
{
114
    VSNPRINTF(staticbuf, BUFLEN, fmt);
115
    return askyesno(staticbuf);
116
}
117

    
118