GCC Code Coverage Report


Directory: src/gate/
File: src/gate/versioning.c
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 58 71 81.7%
Functions: 5 6 83.3%
Branches: 19 36 52.8%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, Stefan Meislinger |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/versioning.h"
30 #include "gate/results.h"
31
32 10 gate_intptr_t gate_version_compare(void const* item1, void const* item2)
33 {
34 10 gate_version_t const* src = (gate_version_t const*)item1;
35 10 gate_version_t const* dst = (gate_version_t const*)item2;
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (src->major < dst->major) return -1;
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (src->major > dst->major) return +1;
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (src->minor < dst->minor) return -1;
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (src->minor > dst->minor) return +1;
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (src->patch < dst->patch) return -1;
41
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (src->patch > dst->patch) return +1;
42
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 if (src->build < dst->build) return -1;
43
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (src->build > dst->build) return +1;
44 3 return 0;
45 }
46 1 gate_size_t gate_version_print(gate_version_t const* src, char* dst, gate_size_t dst_len)
47 {
48 1 gate_size_t ret = 0;
49 gate_size_t used;
50 do
51 {
52
53 1 used = gate_str_print(dst, dst_len,
54 GATE_PRINT_UI32, src->major,
55 GATE_PRINT_CHAR, '.',
56 GATE_PRINT_UI32, src->minor,
57 GATE_PRINT_CHAR, '.',
58 GATE_PRINT_UI32, src->patch,
59 GATE_PRINT_END);
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!used) break;
61 1 ret += used;
62
63
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (src->build != 0)
64 {
65 1 dst += used;
66 1 dst_len -= used;
67
68 1 used = gate_str_print(dst, dst_len,
69 GATE_PRINT_CHAR, '.',
70 GATE_PRINT_UI32, src->build,
71 GATE_PRINT_END);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!used) break;
73 1 ret += used;
74 1 dst += used;
75 1 dst_len -= used;
76 }
77
78 } while (0);
79
80 1 return ret;
81 }
82 gate_result_t gate_version_print_string(gate_version_t const* src, gate_string_t* dst)
83 {
84 char buffer[128];
85 gate_size_t used = gate_version_print(src, buffer, sizeof(buffer));
86 if (NULL != gate_string_create(dst, buffer, used))
87 {
88 return GATE_RESULT_OK;
89 }
90 else
91 {
92 return GATE_RESULT_OUTOFMEMORY;
93 }
94 }
95
96 4 static gate_size_t gate_version_parse_value(gate_string_t const* src, gate_uint32_t* value)
97 {
98 4 gate_uint64_t num = 0;
99 4 gate_size_t bytes_parsed = gate_str_parse_uint64(src->str, src->length, &num);
100
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (bytes_parsed)
101 {
102 4 *value = (gate_uint32_t)num;
103 }
104 4 return bytes_parsed;
105 }
106
107 1 gate_size_t gate_version_parse(char const* str, gate_size_t str_len, gate_version_t* dst)
108 {
109 static gate_string_t const separator = GATE_STRING_INIT_STATIC(".");
110 1 gate_size_t ret = 0;
111 gate_size_t parsed;
112 gate_string_t src;
113 1 gate_string_t str_major = GATE_STRING_INIT_EMPTY;
114 1 gate_string_t str_minor = GATE_STRING_INIT_EMPTY;
115 1 gate_string_t str_patch = GATE_STRING_INIT_EMPTY;
116
117 do
118 {
119 1 gate_mem_clear(dst, sizeof(gate_version_t));
120 1 gate_string_create_static_len(&src, str, str_len);
121
122 1 parsed = gate_string_parse(&src, &separator, 0, &str_major, &src, false);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!parsed)
124 {
125 ret += gate_version_parse_value(&src, &dst->major);
126 break;
127 }
128 1 ret += parsed;
129 1 gate_version_parse_value(&str_major, &dst->major);
130
131 1 parsed = gate_string_parse(&src, &separator, 0, &str_minor, &src, false);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!parsed)
133 {
134 ret += gate_version_parse_value(&src, &dst->minor);
135 break;
136 }
137 1 ret += parsed;
138 1 gate_version_parse_value(&str_minor, &dst->minor);
139
140 1 parsed = gate_string_parse(&src, &separator, 0, &str_patch, &src, false);
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!parsed)
142 {
143 ret += gate_version_parse_value(&src, &dst->patch);
144 break;
145 }
146 1 ret += parsed;
147 1 gate_version_parse_value(&str_patch, &dst->patch);
148
149 1 ret += gate_version_parse_value(&src, &dst->build);
150
151 } while (0);
152
153
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (dst->major + dst->minor + dst->patch + dst->build == 0)
155 {
156 /* parsing totally failed */
157 ret = 0;
158 }
159
160 1 return ret;
161 }
162 1 gate_result_t gate_version_parse_string(gate_string_t const* src, gate_version_t* dst)
163 {
164 1 gate_size_t parsed = gate_version_parse(src->str, src->length, dst);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (parsed == 0)
166 {
167 return GATE_RESULT_INVALIDDATA;
168 }
169 else
170 {
171 1 return GATE_RESULT_OK;
172 }
173 }
174