WKern
Loading...
Searching...
No Matches
strings.c
Go to the documentation of this file.
1/*
2WKern - A Bare Metal OS / Kernel I am maKing (For Fun)
3Copyright (C) 2025 Wdboyes13
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#include <global.h>
20#include <io/kio.h>
21#include <utils/util.h>
27int Kstrcmp(const char *a, const char *b) {
28 while (*a && (*a == *b)) {
29 a++;
30 b++;
31 }
32 return (unsigned char)*a - (unsigned char)*b;
33}
34
38void Kshcmp() {
39
40 Kprintf("\nSTR1>");
41 char str1[256];
42 Kgetstr(str1, sizeof(str1));
43 Kflush();
44 Kprintf("\nSTR2>");
45 char str2[256];
46 Kgetstr(str2, sizeof(str2));
47 Kflush();
48 if (Kstrcmp(str1, str2) == 0) {
49 Kprintf("\nEqual");
50 } else {
51 Kprintf("\nNot Equal");
52 }
53}
54
60u32 Kmstrlen(const char *str) {
61 u32 len = 0;
62 while (str[len]) {
63 len++;
64 }
65 return len;
66}
67
74char *Kstrchr(const char *str, int c) {
75 while (*str) {
76 if (*str == (char)c) {
77 return (char *)str;
78 }
79 str++;
80 }
81 if ((char)c == '\0') {
82 return (char *)str;
83 }
84 return NULL;
85}
86
92int Ktolower(int c) {
93 if (c >= 'A' && c <= 'Z') {
94 return c + ('a' - 'A'); // Shift from uppercase to lowercase
95 }
96 return c; // Not uppercase, just return as is
97}
98
99// ChecK if the char is a digit (0-9)
100int Kisdigit(int c) { return (c >= '0' && c <= '9'); }
101
107int Kisspace(int c) {
108 return (c == ' ' || // space
109 c == '\t' || // horizontal tab
110 c == '\n' || // newline
111 c == '\v' || // vertical tab
112 c == '\f' || // form feed
113 c == '\r'); // carriage return
114}
115
121size_t Kstrlen(const char *str) {
122 size_t len = 0;
123 while (str[len] != '\0') {
124 len++;
125 }
126 return len;
127}
128
134int Kisxdigit(int c) {
135 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
136 (c >= 'A' && c <= 'F');
137}
138
146int Split(char *input, char **argv, int max_args) {
147 int argc = 0;
148
149 while (*input && argc < max_args) {
150 // SKip leading spaces
151 while (*input == ' ') {
152 input++;
153 }
154
155 if (*input == '\0') {
156 break;
157 }
158
159 // MarK the beginning of the argument
160 argv[argc++] = input;
161
162 // Find the end of the argument
163 while (*input && *input != ' ') {
164 input++;
165 }
166
167 if (*input == ' ') {
168 *input = '\0'; // Null-terminate the string
169 input++; // Move to the next character
170 }
171 }
172
173 return argc;
174}
#define NULL
Definition global.h:39
void Kgetstr(char *str, int length)
Reads a line of input from the keyboard (blocking).
Definition keyin.c:186
void Kflush()
Flushes the keyboard controller input buffer.
Definition keyin.c:219
void Kprintf(const char *fmt,...)
Formatted output to the screen.
Definition printer.c:152
unsigned int u32
32-Bit Unsigned Int
Definition nums.h:30
int Kisspace(int c)
Check if character is whitespace.
Definition strings.c:107
int Split(char *input, char **argv, int max_args)
Split into ARGC.
Definition strings.c:146
char * Kstrchr(const char *str, int c)
Search for character in string.
Definition strings.c:74
void Kshcmp()
Interactive string comparison.
Definition strings.c:38
int Ktolower(int c)
Convert character to lowercase.
Definition strings.c:92
u32 Kmstrlen(const char *str)
Get string length.
Definition strings.c:60
int Kisxdigit(int c)
Checks if input is a Hexadecimal Diget.
Definition strings.c:134
int Kisdigit(int c)
Definition strings.c:100
int Kstrcmp(const char *a, const char *b)
Compare Strings - Similar to standard libc strcmp.
Definition strings.c:27
size_t Kstrlen(const char *str)
Get string length.
Definition strings.c:121