Advanced Strategic Command
versionidentifier.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
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 of the License, or
8  (at your option) 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 this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #include "versionidentifier.h"
22 
23 #include <boost/regex.hpp>
24 #include <boost/algorithm/string.hpp>
25 #include <boost/algorithm/string/classification.hpp>
26 
27 #include "errors.h"
28 #include "basestrm.h"
29 
31 {
32 }
33 
35 {
36  fromString( version );
37 }
38 
40 {
41  return versions.empty();
42 }
43 
44 
46 {
47  ASCString s;
48  for ( int i = 0; i < versions.size(); ++i ) {
49  if ( i >= 1 )
50  s += ".";
51  s += ASCString::toString( versions[i] );
52  }
53  return s;
54 }
55 
57 {
58  versions.clear();
59 
60  boost::regex versionformat( "\\s*(\\d+)(\\.\\d+)*\\s*");
61  boost::smatch what;
62 
63  if( !boost::regex_match( v, what, versionformat))
64  throw ASCmsgException( v + " is not a valid version identifier");
65 
66  typedef std::vector< ASCString > Split_vector_type;
67  Split_vector_type splitVector;
68  split( splitVector, v, boost::is_any_of(".") );
69 
70  for ( vector<ASCString>::const_iterator i = splitVector.begin(); i != splitVector.end(); ++i ) {
71  int num = atoi( i->c_str() );
72  versions.push_back( num );
73  }
74 }
75 
76 
78 {
79  versions = version.versions;
80 }
81 
82 static const int versionIdentifierStreamVersion = 1;
83 
85 {
86  int version = stream.readInt();
87  if ( version != versionIdentifierStreamVersion )
88  throw tinvalidversion ( "VersionIdentifier", versionIdentifierStreamVersion, version );
89 
90  readClassContainer( versions, stream );
91 }
92 
93 
94 void VersionIdentifier :: write ( tnstream& stream ) const
95 {
97  writeClassContainer( versions, stream );
98 }
99 
100 bool VersionIdentifier:: trailZero( int index ) const
101 {
102  for ( int i = index; i < versions.size(); ++i )
103  if ( versions[i] != 0 )
104  return false;
105  return true;
106 }
107 
108 
110 {
111  int m = min ( v.versions.size(), versions.size() );
112  for ( int i = 0; i < m; ++i ) {
113  if ( versions[i] > v.versions[i] )
114  return false;
115  }
116  if ( versions.size() < v.versions.size() ) {
117  return true;
118  } else {
119  if ( trailZero( v.versions.size() ))
120  return true;
121  else
122  return false;
123  }
124 };
125 
126 
128 {
129  int m = min ( v.versions.size(), versions.size() );
130  for ( int i = 0; i < m; ++i ) {
131  if ( versions[i] < v.versions[i] )
132  return true;
133  if ( versions[i] > v.versions[i] )
134  return false;
135  }
136  if ( versions.size() < v.versions.size() ) {
137  if ( v.trailZero( versions.size() ))
138  return false;
139  else
140  return true;
141  } else {
142  return false;
143  }
144 };
145 
147 {
148  return !operator<=(v);
149 }
150 
152 {
153  return !operator<(v);
154 }
155 
156 
157 
159 {
160  int m = min ( v.versions.size(), versions.size() );
161  for ( int i = 0; i < m; ++i ) {
162  if ( versions[i] != v.versions[i] )
163  return false;
164  }
165  if ( versions.size() < v.versions.size() ) {
166  if ( v.trailZero( versions.size() ))
167  return true;
168  else
169  return false;
170  } else {
171  if ( trailZero( v.versions.size() ))
172  return true;
173  else
174  return false;
175  }
176 }
177 
179 {
180  return !operator==(v);
181 };
182 
The (base-) classes which are thrown as exceptions.
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
bool operator!=(const VersionIdentifier &v) const
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
The interface for all kinds of IO stream.
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ASCString toString() const
bool operator<(const VersionIdentifier &v) const
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void read(tnstream &stream)
bool operator==(const VersionIdentifier &v) const
bool operator<=(const VersionIdentifier &v) const
The interface for the various streams that ASC offers, like file and memory streams.
bool operator>=(const VersionIdentifier &v) const
void readClassContainer(C &c, tnstream &stream)
Definition: basestrm.h:752
static const int versionIdentifierStreamVersion
int atoi(const std::string &s)
Definition: misc.cpp:152
void writeClassContainer(const C &c, tnstream &stream)
Definition: basestrm.h:742
const T & min(const T &a, const T &b, const T &c)
Definition: misc.h:80
void write(tnstream &stream) const
void fromString(const ASCString &v)
bool operator>(const VersionIdentifier &v) const