Skip to content

Commit 41aeb64

Browse files
committed
Refactor parsing utils and GD32 build flags
Moved `Atoi`/`Atof` from `json_parsehelper.h` into shared `common/utils/utils_string.h` and updated JSON parsing to reuse the common helpers, reducing duplication. Also cleaned up parse variable naming for readability. In GD32 makefiles, added FATFS include support via `CONFIG_FS_FATFS` and corrected the time macro define from `HAVETIMEOFDAY` to `HAVE_TIMEOFDAY` for consistency.
1 parent 142bd41 commit 41aeb64

17 files changed

Lines changed: 640 additions & 238 deletions

File tree

common/include/common/utils/utils_string.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,71 @@ constexpr uint32_t ConstStrLen(const char* str) {
3636
}
3737
return len;
3838
}
39+
40+
inline int32_t Atoi(const char* buffer, uint32_t size) {
41+
const char* p = buffer;
42+
int32_t sign = 1;
43+
int32_t result = 0;
44+
45+
if (size == 0) {
46+
return 0;
47+
}
48+
49+
if (*p == '-') {
50+
sign = -1;
51+
p++;
52+
size--;
53+
} else if (*p == '+') {
54+
p++;
55+
size--;
56+
}
57+
58+
for (; (size > 0) && (*p >= '0' && *p <= '9'); size--, p++) {
59+
result = (result * 10) + (*p - '0');
60+
}
61+
62+
return sign * result;
63+
}
64+
65+
inline float Atof(const char* buffer, uint32_t size) {
66+
const char* p = buffer;
67+
float sign = 1.0F;
68+
float result = 0.0F;
69+
70+
if (size == 0) {
71+
return 0.0F;
72+
}
73+
74+
if (*p == '-') {
75+
sign = -1.0F;
76+
++p;
77+
--size;
78+
} else if (*p == '+') {
79+
++p;
80+
--size;
81+
}
82+
83+
while (size > 0 && *p >= '0' && *p <= '9') {
84+
result = result * 10.0F + static_cast<float>(*p - '0');
85+
++p;
86+
--size;
87+
}
88+
89+
if (size > 0 && *p == '.') {
90+
++p;
91+
--size;
92+
93+
float divisor = 10.0F;
94+
while (size > 0 && *p >= '0' && *p <= '9') {
95+
result += static_cast<float>(*p - '0') / divisor;
96+
divisor *= 10.0F;
97+
++p;
98+
--size;
99+
}
100+
}
101+
102+
return sign * result;
103+
}
39104
} // namespace common
40105

41106
#endif // COMMON_UTILS_UTILS_STRING_H_

common/include/json/json_parsehelper.h

Lines changed: 17 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -29,103 +29,41 @@
2929
#include <cstdint>
3030
#include <type_traits>
3131

32-
namespace json {
33-
inline int32_t Atoi(const char* buffer, uint32_t size) {
34-
const char* p = buffer;
35-
int32_t sign = 1;
36-
int32_t res = 0;
37-
38-
if (size == 0) {
39-
return 0;
40-
}
41-
42-
if (*p == '-') {
43-
sign = -1;
44-
p++;
45-
size--;
46-
} else if (*p == '+') {
47-
p++;
48-
size--;
49-
}
50-
51-
for (; (size > 0) && (*p >= '0' && *p <= '9'); size--, p++) {
52-
res = res * 10 + (*p - '0');
53-
}
54-
55-
return sign * res;
56-
}
57-
58-
inline float Atof(const char* buffer, uint32_t size) {
59-
const char* p = buffer;
60-
float sign = 1.0f;
61-
float result = 0.0f;
62-
63-
if (size == 0) {
64-
return 0.0f;
65-
}
32+
#include "common/utils/utils_string.h"
6633

67-
if (*p == '-') {
68-
sign = -1.0f;
69-
++p;
70-
--size;
71-
} else if (*p == '+') {
72-
++p;
73-
--size;
74-
}
75-
76-
// Parse integer part
77-
while (size > 0 && *p >= '0' && *p <= '9') {
78-
result = result * 10.0f + static_cast<float>(*p - '0');
79-
++p;
80-
--size;
81-
}
82-
83-
// Parse fractional part
84-
if (size > 0 && *p == '.') {
85-
++p;
86-
--size;
87-
88-
float divisor = 10.0f;
89-
while (size > 0 && *p >= '0' && *p <= '9') {
90-
result += static_cast<float>(*p - '0') / divisor;
91-
divisor *= 10.0f;
92-
++p;
93-
--size;
94-
}
95-
}
96-
97-
return sign * result;
98-
}
99-
100-
template <typename T> T ParseValue(const char* val, uint32_t len) {
101-
int32_t v = Atoi(val, len);
34+
namespace json {
35+
template <typename T>
36+
T ParseValue(const char* val, uint32_t len) {
37+
int32_t value = common::Atoi(val, len);
10238
if constexpr (std::is_unsigned_v<T>) {
103-
if (v < 0) {
39+
if (value < 0) {
10440
return 0; // or handle error
10541
}
10642
}
107-
return static_cast<T>(v);
43+
return static_cast<T>(value);
10844
}
10945

110-
template <typename T> bool ParseInRange(const char* val, uint32_t len, T min, T max, T* out) {
111-
const auto kV = ParseValue<T>(val, len);
46+
template <typename T>
47+
bool ParseInRange(const char* val, uint32_t len, T min, T max, T* out) {
48+
const auto kValue = ParseValue<T>(val, len);
11249

113-
if ((kV < min) || (kV > max)) {
50+
if ((kValue < min) || (kValue > max)) {
11451
return false;
11552
}
11653

117-
*out = kV;
54+
*out = kValue;
11855
return true;
11956
}
12057

121-
template <typename ParseT, typename StoreT> bool ParseInRange(const char* val, uint32_t len, ParseT min, ParseT max, StoreT* out) {
122-
const auto kV = ParseValue<ParseT>(val, len);
58+
template <typename ParseT, typename StoreT>
59+
bool ParseInRange(const char* val, uint32_t len, ParseT min, ParseT max, StoreT* out) {
60+
const auto kValue = ParseValue<ParseT>(val, len);
12361

124-
if ((kV < min) || (kV > max)) {
62+
if ((kValue < min) || (kValue > max)) {
12563
return false;
12664
}
12765

128-
*out = static_cast<StoreT>(kV);
66+
*out = static_cast<StoreT>(kValue);
12967
return true;
13068
}
13169
} // namespace json

common/make/gd32/Includes.mk

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ $(info $$ALL_FLAGS [${ALL_FLAGS}])
1919
set_if_present = $(eval $(2) := $(if $(filter $(1) -D$(1),$(ALL_FLAGS)),1,))
2020

2121
$(call set_if_present,USE_FREE_RTOS, FREE_RTOS)
22+
23+
$(call set_if_present,CONFIG_FS_FATFS, USE_FATFS)
24+
2225
$(call set_if_present,ENABLE_USB_HOST,USB_HOST)
2326
$(call set_if_present,CONFIG_USB_HOST_MSC,USB_HOST_MSC)
2427
$(call set_if_present,ENABLE_USB_DEVICE,USB_DEVICE)
@@ -105,5 +108,8 @@ ifdef USB_HOST_MSC
105108
INCLUDES+=-I../lib-fatfs
106109
endif
107110

108-
#INCLUDES:= $(strip -I../${PROJECT}/include $(sort $(INCLUDES)))
111+
ifdef USE_FATFS
112+
INCLUDES+=-I../lib-fatfs
113+
endif
114+
109115
$(info $$INCLUDES [${INCLUDES}])

common/make/gd32/Validate.mk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ else
3333
DEFINES+=-DCONFIG_EMAC_HASH_MULTICAST_FILTER
3434
endif
3535
ifeq ($(findstring CONFIG_NET_ENABLE_PTP,$(FLAGS)),CONFIG_NET_ENABLE_PTP)
36-
DEFINES+=-DHAVETIMEOFDAY
36+
DEFINES+=-DHAVE_TIMEOFDAY
3737
else
3838
ifeq ($(findstring CONFIG_TIME_USE_SYSTICK,$(FLAGS)),CONFIG_TIME_USE_SYSTICK)
39-
DEFINES+=-DHAVETIMEOFDAY
39+
DEFINES+=-DHAVE_TIMEOFDAY
4040
else
4141
DEFINES+=-DCONFIG_TIME_USE_TIMER
42-
DEFINES+=-DHAVETIMEOFDAY
42+
DEFINES+=-DHAVE_TIMEOFDAY
4343
endif
4444
endif
4545
endif

include/sys/stat.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @file stat.h
3+
*
4+
*/
5+
/* Copyright (C) 2026 by Arjan van Vught mailto:info@gd32-dmx.org
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
#ifndef SYS_STAT_H_
27+
#define SYS_STAT_H_
28+
29+
#include <time.h>
30+
#include <sys/types.h>
31+
32+
struct stat { // NOLINT
33+
mode_t st_mode;
34+
off_t st_size;
35+
// time_t st_atime;
36+
time_t st_mtime;
37+
// time_t st_ctime;
38+
};
39+
40+
#define S_IFMT 0170000 ///< These bits determine file type.
41+
#define S_IFDIR 0040000 ///< Directory.
42+
#define S_IFCHR 0020000 ///< Character device.
43+
#define S_IFBLK 0060000 ///< Block device
44+
#define S_IFREG 0100000 ///< Regular file.
45+
#define S_IFIFO 0010000 ///< FIFO. */
46+
#define S_IFLNK 0120000 ///< Symbolic link.
47+
#define S_IFSOCK 0140000 ///< Socket.
48+
#define S_IREAD 0400 ///< Read by owner.
49+
#define S_IWRITE 0200 ///< Write by owner.
50+
#define S_IEXEC 0100 ///< Execute by owner.
51+
52+
#define S_IRUSR S_IREAD ///< Read by owner.
53+
#define S_IWUSR S_IWRITE ///< Write by owner.
54+
#define S_IXUSR S_IEXEC ///< Execute by owner.
55+
#define S_IRWXU (S_IREAD | S_IWRITE | S_IEXEC) ///< Read,Write,Execute by owner
56+
57+
#define S_IRGRP (S_IRUSR >> 3) ///< Read by group.
58+
#define S_IWGRP (S_IWUSR >> 3) ///< Write by group.
59+
#define S_IXGRP (S_IXUSR >> 3) ///< Execute by group.
60+
#define S_IRWXG (S_IRWXU >> 3) ///< Read,Write,Execute by user
61+
62+
#define S_IROTH (S_IRGRP >> 3) ///< Read by others.
63+
#define S_IWOTH (S_IWGRP >> 3) ///< Write by others.
64+
#define S_IXOTH (S_IXGRP >> 3) ///< Execute by others.
65+
#define S_IRWXO (S_IRWXG >> 3) ///< Read,Write,Execute by other
66+
67+
#pragma GCC diagnostic push
68+
#pragma GCC diagnostic ignored "-Wshadow"
69+
70+
int stat(const char* path, struct stat* buf); // NOLINT
71+
72+
#pragma GCC diagnostic pop
73+
74+
#endif // SYS_STAT_H_
Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* @file timerfd.h
2+
* @file types.h
33
*
44
*/
5-
/* Copyright (C) 2020 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -22,33 +22,13 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
26-
#ifndef SYS_TIMERFD_H_
27-
#define SYS_TIMERFD_H_
28-
29-
#include <time.h>
30-
31-
/* Bits to be set in the FLAGS parameter of `timerfd_settime'. */
32-
enum {
33-
TFD_TIMER_ABSTIME = (1U << 0)
34-
#define TFD_TIMER_ABSTIME TFD_TIMER_ABSTIME
35-
};
36-
37-
struct itimerspec {
38-
struct timespec it_interval; /* Interval for periodic timer */
39-
struct timespec it_value; /* Initial expiration */
40-
};
41-
42-
#ifdef __cplusplus
43-
extern "C" {
44-
#endif
45-
46-
int timerfd_create(int clockid, int flags);
47-
int timerfd_settime(int fd, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);
48-
int timerfd_gettime(int fd, struct itimerspec *curr_value);
49-
50-
#ifdef __cplusplus
51-
}
52-
#endif
53-
54-
#endif /* SYS_TIMERFD_H_ */
25+
26+
#ifndef SYS_TYPES_H_
27+
#define SYS_TYPES_H_
28+
29+
#include <stdint.h>
30+
31+
typedef uint32_t mode_t;
32+
typedef uint32_t off_t;
33+
34+
#endif // TYPES_H_

lib-remoteconfig/http/content/content.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <cstdint>
55
#include "httpd/httpd.h"
6+
#include "storage_index.js.h"
67
#if defined(NODE_ARTNET) || defined(NODE_ARTNET_MULTI)
78
#include "config_artnet.js.h"
89
#endif // defined(NODE_ARTNET) || defined(NODE_ARTNET_MULTI)
@@ -72,6 +73,7 @@
7273
#if defined (NODE_SHOWFILE)
7374
#include "config_showfile.js.h"
7475
#endif // (NODE_SHOWFILE)
76+
#include "storage_index.html.h"
7577
#if !defined (CONFIG_HTTP_HTML_NO_DMX) && (defined(OUTPUT_DMX_SEND) || defined(OUTPUT_DMX_SEND_MULTI))
7678
#include "config_dmxsend.js.h"
7779
#endif // !defined (CONFIG_HTTP_HTML_NO_DMX) && (defined(OUTPUT_DMX_SEND) || defined(OUTPUT_DMX_SEND_MULTI))
@@ -95,6 +97,7 @@ struct FilesContent {
9597
};
9698

9799
inline constexpr struct FilesContent kHttpContent[] = {
100+
{ 3991234706,"storage_index.js", storage_index_js_gz, 899, static_cast<http::ContentTypes>(2), true },
98101
#if defined(NODE_ARTNET) || defined(NODE_ARTNET_MULTI)
99102
{ 2110753961,"config_artnet.js", config_artnet_js_gz, 1034, static_cast<http::ContentTypes>(2), true },
100103
#endif // defined(NODE_ARTNET) || defined(NODE_ARTNET_MULTI)
@@ -164,6 +167,7 @@ inline constexpr struct FilesContent kHttpContent[] = {
164167
#if defined (NODE_SHOWFILE)
165168
{ 1821390800,"config_showfile.js", config_showfile_js_gz, 535, static_cast<http::ContentTypes>(2), true },
166169
#endif // (NODE_SHOWFILE)
170+
{ 2148610488,"storage_index.html", storage_index_html_gz, 513, static_cast<http::ContentTypes>(0), true },
167171
#if !defined (CONFIG_HTTP_HTML_NO_DMX) && (defined(OUTPUT_DMX_SEND) || defined(OUTPUT_DMX_SEND_MULTI))
168172
{ 381868932,"config_dmxsend.js", config_dmxsend_js_gz, 662, static_cast<http::ContentTypes>(2), true },
169173
#endif // !defined (CONFIG_HTTP_HTML_NO_DMX) && (defined(OUTPUT_DMX_SEND) || defined(OUTPUT_DMX_SEND_MULTI))

0 commit comments

Comments
 (0)