GCC Code Coverage Report


Directory: src/gate/
File: src/gate/versioning.c
Date: 2025-12-12 23:40:09
Exec Total Coverage
Lines: 56 69 81.2%
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 /*
75 dst += used;
76 dst_len -= used;
77 */
78 }
79
80 } while (0);
81
82 1 return ret;
83 }
84 gate_result_t gate_version_print_string(gate_version_t const* src, gate_string_t* dst)
85 {
86 char buffer[128];
87 gate_size_t used = gate_version_print(src, buffer, sizeof(buffer));
88 if (NULL != gate_string_create(dst, buffer, used))
89 {
90 return GATE_RESULT_OK;
91 }
92 else
93 {
94 return GATE_RESULT_OUTOFMEMORY;
95 }
96 }
97
98 4 static gate_size_t gate_version_parse_value(gate_string_t const* src, gate_uint32_t* value)
99 {
100 4 gate_uint64_t num = 0;
101 4 gate_size_t bytes_parsed = gate_str_parse_uint64(src->str, src->length, &num);
102
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (bytes_parsed)
103 {
104 4 *value = (gate_uint32_t)num;
105 }
106 4 return bytes_parsed;
107 }
108
109 1 gate_size_t gate_version_parse(char const* str, gate_size_t str_len, gate_version_t* dst)
110 {
111 static gate_string_t const separator = GATE_STRING_INIT_STATIC(".");
112 1 gate_size_t ret = 0;
113 gate_size_t parsed;
114 gate_string_t src;
115 1 gate_string_t str_major = GATE_STRING_INIT_EMPTY;
116 1 gate_string_t str_minor = GATE_STRING_INIT_EMPTY;
117 1 gate_string_t str_patch = GATE_STRING_INIT_EMPTY;
118
119 do
120 {
121 1 gate_mem_clear(dst, sizeof(gate_version_t));
122 1 gate_string_create_static_len(&src, str, str_len);
123
124 1 parsed = gate_string_parse(&src, &separator, 0, &str_major, &src, false);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!parsed)
126 {
127 ret += gate_version_parse_value(&src, &dst->major);
128 break;
129 }
130 1 ret += parsed;
131 1 gate_version_parse_value(&str_major, &dst->major);
132
133 1 parsed = gate_string_parse(&src, &separator, 0, &str_minor, &src, false);
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!parsed)
135 {
136 ret += gate_version_parse_value(&src, &dst->minor);
137 break;
138 }
139 1 ret += parsed;
140 1 gate_version_parse_value(&str_minor, &dst->minor);
141
142 1 parsed = gate_string_parse(&src, &separator, 0, &str_patch, &src, false);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!parsed)
144 {
145 ret += gate_version_parse_value(&src, &dst->patch);
146 break;
147 }
148 1 ret += parsed;
149 1 gate_version_parse_value(&str_patch, &dst->patch);
150
151 1 ret += gate_version_parse_value(&src, &dst->build);
152
153 } while (0);
154
155
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (dst->major + dst->minor + dst->patch + dst->build == 0)
157 {
158 /* parsing totally failed */
159 ret = 0;
160 }
161
162 1 return ret;
163 }
164 1 gate_result_t gate_version_parse_string(gate_string_t const* src, gate_version_t* dst)
165 {
166 1 gate_size_t parsed = gate_version_parse(src->str, src->length, dst);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (parsed == 0)
168 {
169 return GATE_RESULT_INVALIDDATA;
170 }
171 else
172 {
173 1 return GATE_RESULT_OK;
174 }
175 }
176