File: tools.cpp

package info (click to toggle)
vdr-plugin-markad 4.2.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,084 kB
  • sloc: cpp: 22,441; python: 613; makefile: 270; sh: 95
file content (109 lines) | stat: -rw-r--r-- 3,586 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * tools.cpp: A program for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "tools.h"
#include "debug.h"

#include <string>
#include <cstring>
#include <algorithm>
#include <math.h>

cTools::cTools() {
}

cTools::~cTools() {
}


void cTools::LogSeparator(const bool main) {
    if (main) dsyslog("=======================================================================================================================");
    else      dsyslog("-----------------------------------------------------------------------------------------------------------------------");
}


void cTools::StartSection(const char* name) {
    startSectionTime = std::chrono::high_resolution_clock::now();
    dsyslog("<<<<<<<<<< start section: %s <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<", name);
}


int cTools::EndSection(const char* name) const {
    std::chrono::high_resolution_clock::time_point stopSectionTime = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double, std::milli> durationSection = stopSectionTime - startSectionTime;
    int msElapsed = round(durationSection.count());
    dsyslog(">>>>>>>>>> end  section: %s: %5ds %3dms >>>>>>>>>>>>>>>>>", name, static_cast<int>(msElapsed / 1000), msElapsed % 1000);
    return msElapsed;
}


bool cTools::CompareChannelName(const char *nameA, const char *nameB, const int flags) {
    std::string name1(nameA);
    std::string name2(nameB);

    // remove "_HD"
    if (flags & IGNORE_HD) {
        size_t pos = name1.find("_HD");
        if (pos != std::string::npos) name1.replace(pos, 3, "");
        pos = name2.find("_HD");
        if (pos != std::string::npos) name2.replace(pos, 3, "");
    }

    // change to uppercase
    std::transform(name1.begin(), name1.end(), name1.begin(), ::toupper);
    std::transform(name2.begin(), name2.end(), name2.begin(), ::toupper);

    // remove state and country
    if (flags & IGNORE_COUNTRY) {
        for (const char *country : countries) {
            size_t pos    = name1.find(country);
            size_t length = strlen(country);
            if (pos != std::string::npos) name1.replace(pos, length, "");
            pos = name2.find(country);
            if (pos != std::string::npos) name2.replace(pos, length, "");
        }

    }

    // remove cities
    if (flags & IGNORE_CITY) {
        for (const char *city : cities) {
            size_t pos    = name1.find(city);
            size_t length = strlen(city);
            if (pos != std::string::npos) name1.replace(pos, length, "");
            pos = name2.find(city);
            if (pos != std::string::npos) name2.replace(pos, length, "");
        }
    }

    // remove fill character "_"
    while (true) {
        size_t pos = name1.find( "_" );
        if (pos == std::string::npos) break;
        name1.replace(pos, 1, "");
    }
    while (true) {
        size_t pos = name2.find( "_" );
        if (pos == std::string::npos) break;
        name2.replace(pos, 1, "");
    }

    // compare names
    if (name1.compare(name2) == 0) {
#ifdef DEBUG_CHANNEL_NAME
        dsyslog("cTools::CompareChannelName(): identical -> nameA %s, nameB %s, name1 %s, name2 %s, flags %d", nameA, nameB, name1.c_str(), name2.c_str(), flags);
#endif
        return true;  // we have an exact match
    }
    else {
#ifdef DEBUG_CHANNEL_NAME
        dsyslog("cTools::CompareChannelName(): different -> nameA %s, nameB %s, name1 %s, name2 %s, flags %d", nameA, nameB, name1.c_str(), name2.c_str(), flags);
#endif
        return false;
    }
    return false;
}