Merge pull request #4314 from MartijnKaijser/beta1
[vuplus_xbmc] / xbmc / addons / AddonVersion.h
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 <stdlib.h>
22 #include <string.h>
23 #include <iostream>
24 #include <boost/operators.hpp>
25 #include "utils/StdString.h"
26
27 namespace ADDON
28 {
29   /* \brief Addon versioning using the debian versioning scheme
30
31     AddonVersion uses debian versioning, which means in the each section of the period
32     separated version string, numbers are compared numerically rather than lexicographically,
33     thus any preceding zeros are ignored.
34
35     i.e. 1.00 is considered the same as 1.0, and 1.01 is considered the same as 1.1.
36
37     Further, 1.0 < 1.0.0
38
39     See here for more info: http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
40     */
41   class AddonVersion : public boost::totally_ordered<AddonVersion> {
42   public:
43     AddonVersion(const AddonVersion& other) : mUpstream(NULL), mRevision(NULL) { *this = other; }
44     explicit AddonVersion(const CStdString& version);
45     ~AddonVersion();
46
47     int Epoch() const { return mEpoch; }
48     const char *Upstream() const { return mUpstream; }
49     const char *Revision() const { return mRevision; }
50
51     AddonVersion& operator=(const AddonVersion& other);
52     bool operator<(const AddonVersion& other) const;
53     bool operator==(const AddonVersion& other) const;
54     CStdString Print() const;
55     const char *c_str() const { return m_originalVersion.c_str(); };
56     bool empty() const;
57
58     static bool SplitFileName(CStdString& ID, CStdString& version,
59                               const CStdString& filename);
60
61     static bool Test();
62   protected:
63     CStdString m_originalVersion;
64     int mEpoch;
65     char *mUpstream;
66     char *mRevision;
67
68     static int CompareComponent(const char *a, const char *b);
69   };
70
71   inline AddonVersion::~AddonVersion()
72   {
73     free(mUpstream);
74     free(mRevision);
75   }
76
77   inline AddonVersion& AddonVersion::operator=(const AddonVersion& other)
78   {
79     free(mUpstream);
80     free(mRevision);
81     mEpoch = other.Epoch();
82     mUpstream = strdup(other.Upstream());
83     mRevision = strdup(other.Revision());
84     m_originalVersion = other.m_originalVersion;
85     return *this;
86   }
87 }