diff options
| author | manuel <manuel@mausz.at> | 2015-03-03 16:53:59 +0100 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2015-03-03 16:53:59 +0100 |
| commit | ffca21f2743a7b367fa212799c6e2fea6190dd5d (patch) | |
| tree | 0608ea3a29cf644ec9ab204e2b4bb9bfaae1c381 /xbmc/addons/AddonVersion.cpp | |
| download | kodi-pvr-build-ffca21f2743a7b367fa212799c6e2fea6190dd5d.tar.gz kodi-pvr-build-ffca21f2743a7b367fa212799c6e2fea6190dd5d.tar.bz2 kodi-pvr-build-ffca21f2743a7b367fa212799c6e2fea6190dd5d.zip | |
initial commit for kodi master
Diffstat (limited to 'xbmc/addons/AddonVersion.cpp')
| -rw-r--r-- | xbmc/addons/AddonVersion.cpp | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/xbmc/addons/AddonVersion.cpp b/xbmc/addons/AddonVersion.cpp new file mode 100644 index 0000000..9d5a112 --- /dev/null +++ b/xbmc/addons/AddonVersion.cpp | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2005-2013 Team XBMC | ||
| 3 | * http://xbmc.org | ||
| 4 | * | ||
| 5 | * This Program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2, or (at your option) | ||
| 8 | * any later version. | ||
| 9 | * | ||
| 10 | * This Program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with XBMC; see the file COPYING. If not, see | ||
| 17 | * <http://www.gnu.org/licenses/>. | ||
| 18 | * | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <stdio.h> | ||
| 22 | #include <string.h> | ||
| 23 | #include <stdlib.h> | ||
| 24 | #include <ctype.h> | ||
| 25 | |||
| 26 | #include "AddonVersion.h" | ||
| 27 | #include "utils/StringUtils.h" | ||
| 28 | |||
| 29 | namespace ADDON | ||
| 30 | { | ||
| 31 | AddonVersion::AddonVersion(const std::string& version) | ||
| 32 | : mEpoch(0), mUpstream(version.empty() ? "0.0.0" : version) | ||
| 33 | { | ||
| 34 | size_t pos = mUpstream.find(':'); | ||
| 35 | if (pos != std::string::npos) | ||
| 36 | { | ||
| 37 | mEpoch = strtol(mUpstream.c_str(), NULL, 10); | ||
| 38 | mUpstream.erase(0, pos+1); | ||
| 39 | } | ||
| 40 | |||
| 41 | pos = mUpstream.find('-'); | ||
| 42 | if (pos != std::string::npos) | ||
| 43 | { | ||
| 44 | mRevision = mUpstream.substr(pos+1); | ||
| 45 | mUpstream.erase(pos); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | /**Compare two components of a Debian-style version. Return -1, 0, or 1 | ||
| 50 | * if a is less than, equal to, or greater than b, respectively. | ||
| 51 | */ | ||
| 52 | int AddonVersion::CompareComponent(const char *a, const char *b) | ||
| 53 | { | ||
| 54 | while (*a && *b) | ||
| 55 | { | ||
| 56 | while (*a && *b && !isdigit(*a) && !isdigit(*b)) | ||
| 57 | { | ||
| 58 | if (*a != *b) | ||
| 59 | { | ||
| 60 | if (*a == '~') return -1; | ||
| 61 | if (*b == '~') return 1; | ||
| 62 | return *a < *b ? -1 : 1; | ||
| 63 | } | ||
| 64 | a++; | ||
| 65 | b++; | ||
| 66 | } | ||
| 67 | if (*a && *b && (!isdigit(*a) || !isdigit(*b))) | ||
| 68 | { | ||
| 69 | if (*a == '~') return -1; | ||
| 70 | if (*b == '~') return 1; | ||
| 71 | return isdigit(*a) ? -1 : 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | char *next_a, *next_b; | ||
| 75 | long int num_a = strtol(a, &next_a, 10); | ||
| 76 | long int num_b = strtol(b, &next_b, 10); | ||
| 77 | if (num_a != num_b) | ||
| 78 | return num_a < num_b ? -1 : 1; | ||
| 79 | |||
| 80 | a = next_a; | ||
| 81 | b = next_b; | ||
| 82 | } | ||
| 83 | if (!*a && !*b) | ||
| 84 | return 0; | ||
| 85 | if (*a) | ||
| 86 | return *a == '~' ? -1 : 1; | ||
| 87 | else | ||
| 88 | return *b == '~' ? 1 : -1; | ||
| 89 | } | ||
| 90 | |||
| 91 | bool AddonVersion::operator<(const AddonVersion& other) const | ||
| 92 | { | ||
| 93 | if (mEpoch != other.mEpoch) | ||
| 94 | return mEpoch < other.mEpoch; | ||
| 95 | |||
| 96 | int result = CompareComponent(mUpstream.c_str(), other.mUpstream.c_str()); | ||
| 97 | if (result) | ||
| 98 | return (result < 0); | ||
| 99 | |||
| 100 | return (CompareComponent(mRevision.c_str(), other.mRevision.c_str()) < 0); | ||
| 101 | } | ||
| 102 | |||
| 103 | bool AddonVersion::operator==(const AddonVersion& other) const | ||
| 104 | { | ||
| 105 | return mEpoch == other.mEpoch | ||
| 106 | && CompareComponent(mUpstream.c_str(), other.mUpstream.c_str()) == 0 | ||
| 107 | && CompareComponent(mRevision.c_str(), other.mRevision.c_str()) == 0; | ||
| 108 | } | ||
| 109 | |||
| 110 | bool AddonVersion::empty() const | ||
| 111 | { | ||
| 112 | return mEpoch == 0 && mUpstream == "0.0.0" && mRevision.empty(); | ||
| 113 | } | ||
| 114 | |||
| 115 | std::string AddonVersion::asString() const | ||
| 116 | { | ||
| 117 | std::string out; | ||
| 118 | if (mEpoch) | ||
| 119 | out = StringUtils::Format("%i:", mEpoch); | ||
| 120 | out += mUpstream; | ||
| 121 | if (!mRevision.empty()) | ||
| 122 | out += "-" + mRevision; | ||
| 123 | return out; | ||
| 124 | } | ||
| 125 | |||
| 126 | bool AddonVersion::SplitFileName(std::string& ID, std::string& version, | ||
| 127 | const std::string& filename) | ||
| 128 | { | ||
| 129 | size_t dpos = filename.rfind("-"); | ||
| 130 | if (dpos == std::string::npos) | ||
| 131 | return false; | ||
| 132 | ID = filename.substr(0, dpos); | ||
| 133 | version = filename.substr(dpos + 1); | ||
| 134 | version = version.substr(0, version.size() - 4); | ||
| 135 | |||
| 136 | return true; | ||
| 137 | } | ||
| 138 | } | ||
