Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / intervals.cc @ 636cbdd5

History | View | Annotate | Download (2.33 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  INTERVALS.CC - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Levente Meszaros
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17
18
#include "opp_ctype.h"
19
#include "envirbase.h"
20
#include "stringtokenizer.h"
21
#include "intervals.h"
22
23
USING_NAMESPACE
24
25
Intervals::Intervals()
26
{
27
    intervals = NULL;
28
}
29
30
Intervals::~Intervals()
31
{
32
    delete [] intervals;
33
}
34
35
void Intervals::parse(const char *text)
36
{
37
    std::vector<Interval> parsedIntervals;
38
    StringTokenizer tokenizer(text, ",");
39
    while (tokenizer.hasMoreTokens())
40
    {
41
        // parse interval string
42
        const char *s = tokenizer.nextToken();
43
        const char *ellipsis = strstr(s, "..");
44
        if (!ellipsis)
45
            throw cRuntimeError("Wrong syntax in interval %s=%s", text, s);
46
47
        const char *startstr = s;
48
        const char *stopstr = ellipsis+2;
49
        while (opp_isspace(*startstr)) startstr++;
50
        while (opp_isspace(*stopstr)) stopstr++;
51
52
        // add to vector
53
        Interval interval;
54
        if (startstr!=ellipsis)
55
            interval.startTime = STR_SIMTIME(std::string(startstr, ellipsis-startstr).c_str());
56
        if (*stopstr)
57
            interval.stopTime = STR_SIMTIME(stopstr);
58
        parsedIntervals.push_back(interval);
59
    }
60
61
    // return as plain C++ array
62
    intervals = new Interval[parsedIntervals.size()+1]; // +1: terminating (0,0)
63
    for (int i=0; i<(int)parsedIntervals.size(); i++)
64
        intervals[i] = parsedIntervals[i];
65
}
66
67
bool Intervals::contains(simtime_t t) const
68
{
69
    // empty contains all t values
70
    if (!intervals)
71
        return true;
72
73
    for (Interval *i = intervals; i->startTime!=0 || i->stopTime!=0; i++)
74
        if (i->startTime <= t && (i->stopTime == 0 || t <= i->stopTime))
75
            return true;
76
77
    return false;
78
}
79
80
bool Intervals::empty() const
81
{
82
    return !intervals || (intervals[0].startTime==0 && intervals[0].stopTime==0);
83
}