File: fallback.cpp

package info (click to toggle)
openmw 0.47.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,276 kB
  • sloc: cpp: 249,935; xml: 1,978; sh: 1,327; python: 63; makefile: 26
file content (85 lines) | stat: -rw-r--r-- 2,241 bytes parent folder | download | duplicates (3)
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
#include "fallback.hpp"

#include <sstream>

#include <components/debug/debuglog.hpp>

namespace Fallback
{
    std::map<std::string,std::string> Map::mFallbackMap;

    void Map::init(const std::map<std::string,std::string>& fallback)
    {
        mFallbackMap = fallback;
    }

    std::string Map::getString(const std::string& fall)
    {
        std::map<std::string,std::string>::const_iterator it;
        if ((it = mFallbackMap.find(fall)) == mFallbackMap.end())
        {
            return std::string();
        }
        return it->second;
    }

    float Map::getFloat(const std::string& fall)
    {
        const std::string& fallback = getString(fall);
        if (!fallback.empty())
        {
            std::stringstream stream(fallback);
            float number = 0.f;
            stream >> number;
            return number;
        }

        return 0;
    }

    int Map::getInt(const std::string& fall)
    {
        const std::string& fallback = getString(fall);
        if (!fallback.empty())
        {
            std::stringstream stream(fallback);
            int number = 0;
            stream >> number;
            return number;
        }

        return 0;
    }

    bool Map::getBool(const std::string& fall)
    {
        const std::string& fallback = getString(fall);
        return !fallback.empty() && fallback != "0";
    }

    osg::Vec4f Map::getColour(const std::string& fall)
    {
        const std::string& sum = getString(fall);
        if (!sum.empty())
        {
            try
            {
                std::string ret[3];
                unsigned int j = 0;
                for (unsigned int i = 0; i < sum.length(); ++i)
                {
                    if(sum[i]==',') j++;
                    else if (sum[i] != ' ') ret[j]+=sum[i];
                }
                return osg::Vec4f(std::stoi(ret[0])/255.f,std::stoi(ret[1])/255.f,std::stoi(ret[2])/255.f, 1.f);    
            }
            catch (const std::invalid_argument&)
            {
                Log(Debug::Error) << "Error: '" << fall << "' setting value (" << sum << ") is not a valid color, using middle gray as a fallback";
            }
        }

        return osg::Vec4f(0.5f,0.5f,0.5f,1.f);
    }

}