summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/AddonVersion.cpp
blob: 10b314e51ce8b014b51c479a7bfd64abf333a802 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "AddonVersion.h"
#include "utils/StringUtils.h"
#include "utils/log.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

namespace
{
// Add-on versions are used e.g. in file names and should
// not have too much freedom in their accepted characters
// Things that should be allowed: e.g. 0.1.0~beta3+git010cab3
// Note that all of these characters are url-safe
const std::string VALID_ADDON_VERSION_CHARACTERS =
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.+_@~";
} // namespace

namespace ADDON
{
CAddonVersion::CAddonVersion(const std::string& version)
  : mEpoch(0), mUpstream(version.empty() ? "0.0.0" : [&version] {
      auto versionLowerCase = std::string(version);
      StringUtils::ToLower(versionLowerCase);
      return versionLowerCase;
    }())
{
  size_t pos = mUpstream.find(':');
  if (pos != std::string::npos)
  {
    mEpoch = strtol(mUpstream.c_str(), nullptr, 10);
    mUpstream.erase(0, pos + 1);
  }

  pos = mUpstream.find('-');
  if (pos != std::string::npos)
  {
    mRevision = mUpstream.substr(pos + 1);
    if (mRevision.find_first_not_of(VALID_ADDON_VERSION_CHARACTERS) != std::string::npos)
    {
      CLog::Log(LOGERROR, "AddonVersion: {} is not a valid revision number", mRevision);
      mRevision = "";
    }
    mUpstream.erase(pos);
  }

  if (mUpstream.find_first_not_of(VALID_ADDON_VERSION_CHARACTERS) != std::string::npos)
  {
    CLog::Log(LOGERROR, "AddonVersion: {} is not a valid version", mUpstream);
    mUpstream = "0.0.0";
  }
}

CAddonVersion::CAddonVersion(const char* version)
  : CAddonVersion(std::string(version ? version : ""))
{
}

/**Compare two components of a Debian-style version.  Return -1, 0, or 1
   * if a is less than, equal to, or greater than b, respectively.
   */
int CAddonVersion::CompareComponent(const char* a, const char* b)
{
  while (*a && *b)
  {
    while (*a && *b && !isdigit(*a) && !isdigit(*b))
    {
      if (*a != *b)
      {
        if (*a == '~')
          return -1;
        if (*b == '~')
          return 1;
        return *a < *b ? -1 : 1;
      }
      a++;
      b++;
    }
    if (*a && *b && (!isdigit(*a) || !isdigit(*b)))
    {
      if (*a == '~')
        return -1;
      if (*b == '~')
        return 1;
      return isdigit(*a) ? -1 : 1;
    }

    char *next_a, *next_b;
    long int num_a = strtol(a, &next_a, 10);
    long int num_b = strtol(b, &next_b, 10);
    if (num_a != num_b)
      return num_a < num_b ? -1 : 1;

    a = next_a;
    b = next_b;
  }
  if (!*a && !*b)
    return 0;
  if (*a)
    return *a == '~' ? -1 : 1;
  else
    return *b == '~' ? 1 : -1;
}

bool CAddonVersion::operator<(const CAddonVersion& other) const
{
  if (mEpoch != other.mEpoch)
    return mEpoch < other.mEpoch;

  int result = CompareComponent(mUpstream.c_str(), other.mUpstream.c_str());
  if (result)
    return (result < 0);

  return (CompareComponent(mRevision.c_str(), other.mRevision.c_str()) < 0);
}

bool CAddonVersion::operator>(const CAddonVersion& other) const
{
  return !(*this <= other);
}

bool CAddonVersion::operator==(const CAddonVersion& other) const
{
  return mEpoch == other.mEpoch &&
         CompareComponent(mUpstream.c_str(), other.mUpstream.c_str()) == 0 &&
         CompareComponent(mRevision.c_str(), other.mRevision.c_str()) == 0;
}

bool CAddonVersion::operator!=(const CAddonVersion& other) const
{
  return !(*this == other);
}

bool CAddonVersion::operator<=(const CAddonVersion& other) const
{
  return *this < other || *this == other;
}

bool CAddonVersion::operator>=(const CAddonVersion& other) const
{
  return !(*this < other);
}

bool CAddonVersion::empty() const
{
  return mEpoch == 0 && mUpstream == "0.0.0" && mRevision.empty();
}

std::string CAddonVersion::asString() const
{
  std::string out;
  if (mEpoch)
    out = StringUtils::Format("{}:", mEpoch);
  out += mUpstream;
  if (!mRevision.empty())
    out += "-" + mRevision;
  return out;
}

bool CAddonVersion::SplitFileName(std::string& ID,
                                  std::string& version,
                                  const std::string& filename)
{
  size_t dpos = filename.rfind('-');
  if (dpos == std::string::npos)
    return false;
  ID = filename.substr(0, dpos);
  version = filename.substr(dpos + 1);
  version = version.substr(0, version.size() - 4);

  return true;
}
} // namespace ADDON