Project

General

Profile

Statistics
| Branch: | Revision:

root / src / scave / scaveutils.cc @ a3be1d55

History | View | Annotate | Download (3.16 KB)

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

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#include <stdlib.h>
18
#include <string.h>
19
#include <utility>
20
#include <locale.h>
21
#include "platmisc.h"
22
#include "scaveutils.h"
23
24
25
NAMESPACE_BEGIN
26
27
bool parseInt(const char *s, int &dest)
28
{
29
    char *e;
30
    dest = (int)strtol(s,&e,10);
31
    return !*e;
32
}
33
34
bool parseLong(const char *s, long &dest)
35
{
36
    char *e;
37
    dest = strtol(s,&e,10);
38
    return !*e;
39
}
40
41
bool parseInt64(const char *s, int64 &dest)
42
{
43
    char *e;
44
    dest = strtoll(s,&e,10);
45
    return !*e;
46
}
47
48
bool parseDouble(const char *s, double& dest)
49
{
50
    char *e;
51
    setlocale(LC_NUMERIC, "C");
52
    dest = strtod(s,&e);
53
    if (!*e)
54
    {
55
        return true;
56
    }
57
    if (strncasecmp(s, "inf", 3) == 0 ||
58
                (*s && strncasecmp(s+1, "inf", 3) == 0))
59
    {
60
        dest = POSITIVE_INFINITY;  // +INF or -INF
61
        if (*s=='-') dest = -dest;
62
        return true;
63
    }
64
    if (strstr(s, "IND") || strcasecmp(s,"nan")==0)
65
    {
66
        dest = NaN;
67
        return true;
68
    }
69
70
    return false;
71
}
72
73
bool parseSimtime(const char *s, simultime_t &dest)
74
{
75
    const char *e;
76
    simultime_t t;
77
    double d;
78
79
    try {
80
        t = BigDecimal::parse(s, e);
81
        switch (*e)
82
        {
83
        case '\0': dest = t; return true;
84
        // simtime might be given in scientific notation
85
        // in older vector files, try to parse as double
86
        case 'e': case 'E': if (parseDouble(s, d)) { dest = BigDecimal(d); return true; }
87
        }
88
    } catch (std::exception &e) {
89
        // overflow
90
    }
91
92
    return false;
93
}
94
95
std::string unquoteString(const char *str)
96
{
97
    std::string result;
98
    if (str[0] == '"')
99
    {
100
        const char *p = str+1;
101
        for (; *p; ++p)
102
        {
103
            if (*p == '\\' && (*(p+1) == '\\' || *(p+1) == '"'))
104
                result.push_back(*(++p));
105
            else if (*p == '"' && *(p+1) == '\0')
106
                break;
107
            else
108
                result.push_back(*p);
109
        }
110
        if (*p != '"')
111
            throw opp_runtime_error("Missing closing quote\n");
112
    }
113
    else
114
        result = str;
115
    return result;
116
}
117
118
const std::string *StringPool::insert(const std::string& str)
119
{
120
        if (!lastInsertedPtr || *lastInsertedPtr!=str)
121
        {
122
                std::pair<std::set<std::string>::iterator,bool> p = pool.insert(str);
123
                lastInsertedPtr = &(*p.first);
124
        }
125
    return lastInsertedPtr;
126
}
127
128
const std::string *StringPool::find(const std::string& str) const
129
{
130
        if (lastInsertedPtr && *lastInsertedPtr==str)
131
                return lastInsertedPtr;
132
133
        std::set<std::string>::const_iterator it = pool.find(str);
134
        return it != pool.end() ? &(*it) : NULL;
135
}
136
137
NAMESPACE_END