1
1
#pragma once
2
2
3
+ /* Compact C standard library
4
+ *
5
+ * Provides essential C library functions for embedded systems including
6
+ * string manipulation, memory management, character classification,
7
+ * and basic I/O operations.
8
+ */
9
+
3
10
#include <hal.h>
4
11
12
+ /* Basic Type Definitions */
5
13
#ifndef NULL
6
14
#define NULL ((void *) 0) /* Standard NULL definition */
7
15
#endif
@@ -10,25 +18,31 @@ typedef _Bool bool;
10
18
#define false ((bool) 0)
11
19
#define true ((bool) 1)
12
20
13
- /* Character classification macros */
14
- #define isprint (c ) (' ' <= (c) && (c) <= '~')
15
- #define isspace (c ) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
16
- #define isdigit (c ) ('0' <= (c) && (c) <= '9')
17
- #define islower (c ) ('a' <= (c) && (c) <= 'z')
18
- #define isupper (c ) ('A' <= (c) && (c) <= 'Z')
19
- #define isalpha (c ) (islower(c) || isupper(c))
20
- #define isalnum (c ) (isalpha(c) || isdigit(c))
21
+ /* Utility Macros */
21
22
#define min (a , b ) ((a) < (b) ? (a) : (b))
22
23
#define max (a , b ) ((a) > (b) ? (a) : (b))
23
24
24
- /* Endianness conversion macros (standard network to host byte order) */
25
+ /* Character Classification Macros */
26
+ #define isprint (c ) (' ' <= (c) && (c) <= '~') /* Printable character */
27
+ #define isspace (c ) \
28
+ ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r') /* Whitespace */
29
+ #define isdigit (c ) ('0' <= (c) && (c) <= '9') /* Decimal digit */
30
+ #define islower (c ) ('a' <= (c) && (c) <= 'z') /* Lowercase letter */
31
+ #define isupper (c ) ('A' <= (c) && (c) <= 'Z') /* Uppercase letter */
32
+ #define isalpha (c ) (islower(c) || isupper(c)) /* Alphabetic character */
33
+ #define isalnum (c ) (isalpha(c) || isdigit(c)) /* Alphanumeric character */
34
+
35
+ /* Endianness Conversion Macros */
25
36
#if __BYTE_ORDER == __BIG_ENDIAN
37
+ /* Big-endian: no conversion needed */
26
38
#define htons (n ) (n)
27
39
#define ntohs (n ) (n)
28
40
#define htonl (n ) (n)
29
41
#define ntohl (n ) (n)
30
42
31
43
#else /* __LITTLE_ENDIAN */
44
+ /* Little-endian: byte swapping required */
45
+
32
46
/* 16-bit byte swap */
33
47
#define htons (n ) \
34
48
(((((uint16_t) (n) & 0x00FF)) << 8) | (((uint16_t) (n) & 0xFF00) >> 8))
@@ -54,6 +68,7 @@ typedef _Bool bool;
54
68
? (x) \
55
69
: (((uint64_t) htonl((uint32_t) ((x) & 0xFFFFFFFFULL))) << 32) | \
56
70
htonl((uint32_t) (((x) >> 32) & 0xFFFFFFFFULL)))
71
+
57
72
#define ntohll (x ) \
58
73
((1 == ntohl(1)) \
59
74
? (x) \
@@ -62,53 +77,76 @@ typedef _Bool bool;
62
77
63
78
#endif /* __BYTE_ORDER */
64
79
65
- /* String manipulation functions */
80
+ /* String Manipulation Functions */
81
+
82
+ /* String copying and concatenation */
66
83
char * strcpy (char * s1 , const char * s2 );
67
84
char * strncpy (char * s1 , const char * s2 , int32_t n );
68
85
char * strcat (char * s1 , const char * s2 );
69
86
char * strncat (char * s1 , const char * s2 , int32_t n );
87
+
88
+ /* String comparison */
70
89
int32_t strcmp (const char * s1 , const char * s2 );
71
90
int32_t strncmp (const char * s1 , const char * s2 , int32_t n );
91
+
92
+ /* String searching */
72
93
char * strstr (const char * s1 , const char * s2 );
73
- size_t strlen (const char * s1 );
74
94
char * strchr (const char * s1 , int32_t c );
75
95
char * strpbrk (const char * s1 , const char * s2 );
96
+
97
+ /* String length */
98
+ size_t strlen (const char * s1 );
99
+
100
+ /* String tokenization */
76
101
char * strsep (char * * pp , const char * delim );
77
102
char * strtok (char * s , const char * delim );
78
103
char * strtok_r (char * s , const char * delim , char * * holder );
79
104
80
- /* Memory manipulation functions */
105
+ /* Memory Manipulation Functions */
106
+
107
+ /* Memory copying and moving */
81
108
void * memcpy (void * dst , const void * src , uint32_t n );
82
109
void * memmove (void * dst , const void * src , uint32_t n );
110
+
111
+ /* Memory comparison and initialization */
83
112
int32_t memcmp (const void * cs , const void * ct , uint32_t n );
84
113
void * memset (void * s , int32_t c , uint32_t n );
114
+
115
+ /* Mathematical Functions */
85
116
int32_t abs (int32_t n ); /* Absolute value */
86
117
87
- /* Character classification and conversion functions */
118
+ /* String to Number Conversion */
88
119
int32_t strtol (const char * s , char * * end , int32_t base );
89
120
int32_t atoi (const char * s );
90
121
void itoa (int32_t i , char * s , int32_t base ); /* Integer to ASCII conversion */
91
122
92
- /* Random number generation */
123
+ /* Random Number Generation */
93
124
94
125
#ifndef RAND_MAX
95
126
#define RAND_MAX 32767U /* Default max value for random() */
96
127
#endif
97
128
129
+ /* Simple random number generator */
98
130
int32_t random (void );
99
131
void srand (uint32_t seed );
100
132
101
- /* POSIX-style opaque container for re-entrant random number generator state */
133
+ /* POSIX-style re-entrant random number generator */
102
134
struct random_data {
103
- uint32_t state ; /* state must never be zero */
135
+ uint32_t state ; /* State must never be zero */
104
136
};
105
137
int random_r (struct random_data * buf , int32_t * result );
106
138
107
- /* Input/Output functions */
139
+ /* Input/Output Functions */
140
+
141
+ /* Character and string output */
108
142
int32_t puts (const char * str );
143
+
144
+ /* Character and string input */
109
145
int getchar (void );
110
146
char * gets (char * s );
111
147
char * fgets (char * s , int n , void * f );
112
148
char * getline (char * s );
149
+
150
+ /* Formatted output */
113
151
int32_t printf (const char * fmt , ...);
114
152
int32_t sprintf (char * out , const char * fmt , ...);
0 commit comments