Merge pull request #3819 from arnova/subtitles_for_stacks
[vuplus_xbmc] / xbmc / addons / AddonVersion.cpp
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 "guilib/LocalizeStrings.h"
28 #include "utils/StringUtils.h"
29
30 namespace ADDON
31 {
32   AddonVersion::AddonVersion(const CStdString& version)
33   {
34     m_originalVersion = version;
35     if (m_originalVersion.empty())
36       m_originalVersion = "0.0.0";
37     const char *epoch_end = strchr(m_originalVersion.c_str(), ':');
38     if (epoch_end != NULL)
39       mEpoch = atoi(m_originalVersion.c_str());
40     else
41       mEpoch = 0;
42
43     const char *upstream_start;
44     if (epoch_end)
45       upstream_start = epoch_end + 1;
46     else
47       upstream_start = m_originalVersion.c_str();
48
49     const char *upstream_end = strrchr(upstream_start, '-');
50     size_t upstream_size;
51     if (upstream_end == NULL)
52       upstream_size = strlen(upstream_start);
53     else
54       upstream_size = upstream_end - upstream_start;
55
56     mUpstream = (char*) malloc(upstream_size + 1);
57     strncpy(mUpstream, upstream_start, upstream_size);
58     mUpstream[upstream_size] = '\0';
59
60     if (upstream_end == NULL)
61       mRevision = strdup("0");
62     else
63       mRevision = strdup(upstream_end + 1);
64   }
65
66   /**Compare two components of a Debian-style version.  Return -1, 0, or 1
67    * if a is less than, equal to, or greater than b, respectively.
68    */
69   int AddonVersion::CompareComponent(const char *a, const char *b)
70   {
71     while (*a && *b)
72     {
73       while (*a && *b && !isdigit(*a) && !isdigit(*b))
74       {
75         if (*a != *b)
76         {
77           if (*a == '~') return -1;
78           if (*b == '~') return 1;
79           return *a < *b ? -1 : 1;
80         }
81         a++;
82         b++;
83       }
84       if (*a && *b && (!isdigit(*a) || !isdigit(*b)))
85       {
86         if (*a == '~') return -1;
87         if (*b == '~') return 1;
88         return isdigit(*a) ? -1 : 1;
89       }
90
91       char *next_a, *next_b;
92       long int num_a = strtol(a, &next_a, 10);
93       long int num_b = strtol(b, &next_b, 10);
94       if (num_a != num_b)
95         return num_a < num_b ? -1 : 1;
96
97       a = next_a;
98       b = next_b;
99     }
100     if (!*a && !*b)
101       return 0;
102     if (*a)
103       return *a == '~' ? -1 : 1;
104     else
105       return *b == '~' ? 1 : -1;
106   }
107
108   bool AddonVersion::operator<(const AddonVersion& other) const
109   {
110     if (Epoch() != other.Epoch())
111       return Epoch() < other.Epoch();
112
113     int result = CompareComponent(Upstream(), other.Upstream());
114     if (result)
115       return (result < 0);
116
117     return (CompareComponent(Revision(), other.Revision()) < 0);
118   }
119
120   bool AddonVersion::operator==(const AddonVersion& other) const
121   {
122     return Epoch() == other.Epoch()
123       && CompareComponent(Upstream(), other.Upstream()) == 0
124       && CompareComponent(Revision(), other.Revision()) == 0;
125   }
126
127   bool AddonVersion::empty() const
128   {
129     return m_originalVersion.empty() || m_originalVersion == "0.0.0";
130   }
131
132   CStdString AddonVersion::Print() const
133   {
134     return StringUtils::Format("%s %s", g_localizeStrings.Get(24051).c_str(), m_originalVersion.c_str());
135   }
136
137   bool AddonVersion::SplitFileName(CStdString& ID, CStdString& version,
138                                    const CStdString& filename)
139   {
140     size_t dpos = filename.rfind("-");
141     if (dpos == std::string::npos)
142       return false;
143     ID = filename.substr(0, dpos);
144     version = filename.substr(dpos + 1);
145     version = version.substr(0, version.size() - 4);
146
147     return true;
148   }
149
150   bool AddonVersion::Test()
151   {
152     AddonVersion v1_0("1.0");
153     AddonVersion v1_00("1.00");
154     AddonVersion v1_0_0("1.0.0");
155     AddonVersion v1_1("1.1");
156     AddonVersion v1_01("1.01");
157     AddonVersion v1_0_1("1.0.1");
158
159     bool ret = false;
160
161     // These are totally sane
162     ret = (v1_0 < v1_1) && (v1_0 < v1_01) && (v1_0 < v1_0_1) &&
163           (v1_1 > v1_0_1) && (v1_01 > v1_0_1);
164
165     // These are rather sane
166     ret &= (v1_0 != v1_0_0) && (v1_0 < v1_0_0) && (v1_0_0 > v1_0) &&
167            (v1_00 != v1_0_0) && (v1_00 < v1_0_0) && (v1_0_0 > v1_00);
168
169     ret &= (v1_0 == v1_00) && !(v1_0 < v1_00) && !(v1_0 > v1_00);
170     ret &= (v1_1 == v1_01) && !(v1_1 < v1_01) && !(v1_1 > v1_01);
171
172     return ret;
173   }
174 }