summaryrefslogtreecommitdiff
path: root/extdata.c
blob: f71f410829bbbc5423f0f24f1f7651913ecf9e1f (plain)
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
/*
 * GraphLCD plugin for the Video Disk Recorder
 *
 * extdata.c - external data sent via SVDRP
 *
 * This file is released under the GNU General Public License. Refer
 * to the COPYING file distributed with this package.
 *
 * (c) 2011      Wolfgang Astleitner <mrwastl AT users sourceforge net>
 */

#include "extdata.h"

#include <vdr/tools.h>

#include <stdio.h>
cExtData * cExtData::mExtDataInstance = NULL;

cExtData * cExtData::GetExtData(void) {
    if (mExtDataInstance == NULL) {
        mExtDataInstance = new cExtData();
    }
    return mExtDataInstance;
}


void cExtData::ReleaseExtData(void) {
    delete mExtDataInstance;
    mExtDataInstance = NULL;
}


cExtData::~cExtData(void) {
    data.clear();
    expData.clear();
}


bool cExtData::Set(const std::string & key, const std::string & value, const std::string & scope, uint32_t expire ) {
    if (mExtDataInstance == NULL) return false; // paranoia check

    std::string compkey = scope; compkey += ":"; compkey += key; // <scope>:<key>,  <scope> can be "" (== global)
    data[compkey] = value;
    
    if (expire > 0) {
        expData[compkey] = cTimeMs::Now() + (expire * 1000);
    } else {
        expData.erase(compkey); // just in case of an old expiration entry for compkey         
    }
    return true;
}


bool cExtData::Unset(const std::string & key, const std::string & scope ) {
    if (mExtDataInstance == NULL) return false; // paranoia check

    std::string compkey = scope; compkey += ":"; compkey += key; // <scope>:<key>,  <scope> can be "" (== global)
    expData.erase(compkey); // ignore result;
    return ( (data.erase(compkey) > 0) ? true : false );
}


bool cExtData::IsSet(const std::string & key, const std::string & scope ) {
    std::string ret = Get(key, scope);
    return ( (ret != "") ? true : false );
}


std::string cExtData::Get(const std::string & key, const std::string & scope ) {
    if (mExtDataInstance == NULL) return ""; // paranoia check

    std::string compkey;
    int l = 0;
    int loops = (scope == "") ? 1 : 2;
    while ( l < loops) {
        compkey = (l == 0) ? scope : ""; compkey += ":"; compkey += key; // 1st loop: display-scope, 2nd loop: global
        it = data.find(compkey);
        if ( it != data.end() ) {
            expDataIt = expData.find(compkey);
            if ( expDataIt != expData.end() ) {
                uint64_t expts = (*expDataIt).second;
                if ( cTimeMs::Now() > expts ) {
                    expData.erase(compkey);
                    data.erase(compkey);
                    // expired, thus no result here, but maybe there'll be a hit in global scope, so don't return here
                } else {
                    return (*it).second;
                }
            } else {
                return (*it).second;
            }
        }
        l++;
    }
    return "";
}