]> git.ozlabs.org Git - ccan/blob - ccan/version/version.h
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / version / version.h
1 /*****************************************************************************
2  *
3  * version - simple version handling functions for major.minor version
4  * types
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  ****************************************************************************/
25
26 #ifndef CCAN_VERSION_H
27 #define CCAN_VERSION_H
28
29 #include <stdint.h>
30
31 struct version {
32         uint32_t _v; /* major << 16  | minor */
33 };
34
35 /**
36  * version_major - return the major version of the given struct
37  * @v: the version number to obtain the major number from
38  */
39 static inline uint16_t version_major(struct version v) {
40         return (v._v & 0xFFFF0000) >> 16;
41 }
42
43 /**
44  * version_minor - return the minor version of the given struct
45  * @v: the version number to obtain the minor number from
46  */
47 static inline uint16_t version_minor(const struct version v) {
48         return v._v & 0xFFFF;
49 }
50
51 /**
52  * version - create a new version number
53  * @major: major version number
54  * @minor: minor version number
55  */
56 static inline struct version version(uint16_t major, uint16_t minor)
57 {
58         struct version v = { ._v = major << 16 | minor };
59         return v;
60 }
61
62 /**
63  * version_cmp - compare two versions
64  * @a: the first version number
65  * @b: the second version number
66  * @return a number greater, equal, or less than 0 if a is greater, equal or
67  * less than b, respectively
68  *
69  * Example:
70  *      struct version a = version(1, 0);
71  *      struct version b = version(1, 3);
72  *      if (version_cmp(a, b) < 0)
73  *              printf("b is smaller than b\n");
74  */
75 static inline int version_cmp(struct version a, struct version b)
76 {
77         return  (a._v == b._v) ? 0 : (a._v > b._v) ? 1 : - 1;
78 }
79
80 #endif /* CCAN_VERSION_H */