1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/base/versionparsing.h"
12 
13 #include <stdlib.h>
14 
15 namespace rtc {
16 
ParseVersionString(const std::string & version_str,int num_expected_segments,int version[])17 bool ParseVersionString(const std::string& version_str,
18                         int num_expected_segments,
19                         int version[]) {
20   size_t pos = 0;
21   for (int i = 0;;) {
22     size_t dot_pos = version_str.find('.', pos);
23     size_t n;
24     if (dot_pos == std::string::npos) {
25       // npos here is a special value meaning "to the end of the string"
26       n = std::string::npos;
27     } else {
28       n = dot_pos - pos;
29     }
30 
31     version[i] = atoi(version_str.substr(pos, n).c_str());
32 
33     if (++i >= num_expected_segments) break;
34 
35     if (dot_pos == std::string::npos) {
36       // Previous segment was not terminated by a dot, but there's supposed to
37       // be more segments, so that's an error.
38       return false;
39     }
40     pos = dot_pos + 1;
41   }
42   return true;
43 }
44 
CompareVersions(const int version1[],const int version2[],int num_segments)45 int CompareVersions(const int version1[],
46                     const int version2[],
47                     int num_segments) {
48   for (int i = 0; i < num_segments; ++i) {
49     int diff = version1[i] - version2[i];
50     if (diff != 0) {
51       return diff;
52     }
53   }
54   return 0;
55 }
56 
57 }  // namespace rtc
58