[refactor] Organize STRING
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include <fonts/pixel_font.h>
|
||||
#include <graphics/draw.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
void pf_print_char(char c, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color) {
|
||||
for (SUINT32 y = 0; y < 16; y++) {
|
||||
@@ -23,7 +24,7 @@ void pf_print_char(char c, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT
|
||||
}
|
||||
|
||||
void pf_print(const char* text, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color) {
|
||||
for (SUINT32 i = 0; i < strlen(text); i++) {
|
||||
for (SUINT32 i = 0; i < str_len(text); i++) {
|
||||
char c = text[i];
|
||||
pf_print_char(c, basex + i * 8, basey, color); // 只要 字数 * 8 + basex 不爆hr就没事
|
||||
}
|
||||
|
||||
+2
-7
@@ -1,10 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
#define ASM asm volatile
|
||||
|
||||
static SUINT32 strlen(const char* arr) { // 获取string长度
|
||||
SSINT32 i = 0;
|
||||
while (arr[i++] != '\0');
|
||||
return i - 1;
|
||||
}
|
||||
#include <string_utils.h>
|
||||
#define ASM asm volatile
|
||||
@@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <graphics/context.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
void pf_print_char(char c, SUINT32 basex, SUINT32 basey,
|
||||
EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印字符
|
||||
void pf_print(const char* text, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印string
|
||||
void pf_print(String text, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印string
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <efi.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
EFI_STATUS fs_init();
|
||||
void fs_list();
|
||||
EFI_STATUS fs_read(const CHAR16 *Path, void **Buffer, UINTN *Size);
|
||||
EFI_STATUS fs_read(WString Path, void **Buffer, UINTN *Size);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <efi.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
#define LAYER_MAX 32
|
||||
#define LAYER_NAME_LEN 32
|
||||
@@ -29,7 +30,7 @@ typedef struct layer {
|
||||
void layer_init(void);
|
||||
|
||||
// Layer management
|
||||
layer_t* layer_create(const char* name, layer_type_t type, UINT32 w, UINT32 h);
|
||||
layer_t* layer_create(String name, layer_type_t type, UINT32 w, UINT32 h);
|
||||
void layer_destroy(layer_t* layer);
|
||||
void layer_set_z(layer_t* layer, SSINT32 z);
|
||||
void layer_set_pos(layer_t* layer, SSINT32 x, SSINT32 y);
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@
|
||||
#include <efi.h>
|
||||
#include <memory/pmm.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
#define TASK_STACK_SIZE (PAGE_SIZE * 4) // 16 KB kernel stack per task
|
||||
#define TASK_MAX 32
|
||||
@@ -26,7 +27,7 @@ typedef struct task {
|
||||
} task_t;
|
||||
|
||||
// Create a new task. Returns task pointer or NULL on failure.
|
||||
task_t* task_create(const char* name, void (*entry)(void));
|
||||
task_t* task_create(String name, void (*entry)(void));
|
||||
|
||||
// Yield CPU to next ready task (cooperative)
|
||||
void yield(void);
|
||||
|
||||
+2
-1
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <efi.h>
|
||||
#include <efiser.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
struct serial_context { // 串行内容结构体
|
||||
EFI_SERIAL_IO_PROTOCOL *SerialIo;
|
||||
@@ -10,7 +11,7 @@ struct serial_context { // 串行内容结构体
|
||||
extern serial_context g_serial;
|
||||
|
||||
void serial_init(EFI_SERIAL_IO_PROTOCOL *SerialIo); // 初始化串行驱动
|
||||
void serial_write(const char *str); // 往串行写string
|
||||
void serial_write(String str); // 往串行写string
|
||||
void serial_write_char(char c); // 往串行写char(不推荐使用)
|
||||
void serial_write_hex(UINTN val); // 往串行写十六进制数字
|
||||
char serial_read_char(); // 读串行
|
||||
@@ -0,0 +1,91 @@
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
// === Unified string types ===
|
||||
// Use these throughout the kernel instead of raw char*/const char*.
|
||||
|
||||
typedef const char* String; // 8-bit narrow string
|
||||
typedef const uint16_t* WString; // 16-bit wide string (CHAR16)
|
||||
|
||||
// === Narrow string utilities (String) ===
|
||||
|
||||
static inline SUINT32 str_len(String s) {
|
||||
SUINT32 i = 0;
|
||||
while (s[i]) i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
static inline SSINT32 str_cmp(String a, String b) {
|
||||
while (*a && *a == *b) { a++; b++; }
|
||||
return (SUINT8)*a - (SUINT8)*b;
|
||||
}
|
||||
|
||||
static inline bool str_eq(String a, String b) {
|
||||
return str_cmp(a, b) == 0;
|
||||
}
|
||||
|
||||
// Copy up to max-1 chars, always null-terminates.
|
||||
static inline void str_copy(char* dst, String src, SUINT32 max) {
|
||||
if (max == 0) return;
|
||||
SUINT32 i = 0;
|
||||
while (src[i] && i < max - 1) {
|
||||
dst[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dst[i] = '\0';
|
||||
}
|
||||
|
||||
// === Wide string utilities (WString) ===
|
||||
|
||||
static inline SUINT32 wstr_len(WString s) {
|
||||
SUINT32 i = 0;
|
||||
while (s[i]) i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
static inline SSINT32 wstr_cmp(WString a, WString b) {
|
||||
while (*a && *a == *b) { a++; b++; }
|
||||
return (SUINT16)*a - (SUINT16)*b;
|
||||
}
|
||||
|
||||
static inline bool wstr_eq(WString a, WString b) {
|
||||
return wstr_cmp(a, b) == 0;
|
||||
}
|
||||
|
||||
static inline void wstr_copy(uint16_t* dst, WString src, SUINT32 max) {
|
||||
if (max == 0) return;
|
||||
SUINT32 i = 0;
|
||||
while (src[i] && i < max - 1) {
|
||||
dst[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dst[i] = 0;
|
||||
}
|
||||
|
||||
// Convert wide string to ASCII (non-printable -> '?')
|
||||
static inline void wstr_to_ascii(char* dst, WString src, SUINT32 max) {
|
||||
if (max == 0) return;
|
||||
SUINT32 i = 0;
|
||||
while (src[i] && i < max - 1) {
|
||||
if (src[i] >= 0x20 && src[i] <= 0x7E)
|
||||
dst[i] = (char)src[i];
|
||||
else
|
||||
dst[i] = '?';
|
||||
i++;
|
||||
}
|
||||
dst[i] = '\0';
|
||||
}
|
||||
|
||||
// === Memory utilities ===
|
||||
|
||||
static inline void mem_set(void* dst, SUINT8 val, SUINT32 size) {
|
||||
SUINT8* d = (SUINT8*)dst;
|
||||
for (SUINT32 i = 0; i < size; i++) d[i] = val;
|
||||
}
|
||||
|
||||
static inline void mem_copy(void* dst, const void* src, SUINT32 size) {
|
||||
SUINT8* d = (SUINT8*)dst;
|
||||
const SUINT8* s = (const SUINT8*)src;
|
||||
for (SUINT32 i = 0; i < size; i++) d[i] = s[i];
|
||||
}
|
||||
+4
-14
@@ -1,7 +1,7 @@
|
||||
#include <fs.h>
|
||||
#include <serial.h>
|
||||
#include <memory/heap.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
extern EFI_SYSTEM_TABLE *ST;
|
||||
|
||||
@@ -537,14 +537,7 @@ struct list_ctx {
|
||||
};
|
||||
|
||||
static void name_to_ascii(const CHAR16 *Name, char *Ascii, UINTN ascii_sz) {
|
||||
UINTN i = 0;
|
||||
for (; Name[i] && i < ascii_sz - 1; i++) {
|
||||
if (Name[i] >= 0x20 && Name[i] <= 0x7E)
|
||||
Ascii[i] = (char)Name[i];
|
||||
else
|
||||
Ascii[i] = '?';
|
||||
}
|
||||
Ascii[i] = 0;
|
||||
wstr_to_ascii(Ascii, (WString)Name, ascii_sz);
|
||||
}
|
||||
|
||||
static void list_callback(void *ctx, const CHAR16 *Name, UINT8 Attr,
|
||||
@@ -633,10 +626,7 @@ struct find_ctx {
|
||||
};
|
||||
|
||||
static BOOLEAN name_match(const CHAR16 *a, const CHAR16 *b) {
|
||||
UINTN i = 0;
|
||||
for (; a[i] && b[i]; i++)
|
||||
if (a[i] != b[i]) return FALSE;
|
||||
return a[i] == 0 && b[i] == 0;
|
||||
return wstr_eq((WString)a, (WString)b);
|
||||
}
|
||||
|
||||
static void find_callback(void *ctx, const CHAR16 *Name, UINT8 Attr,
|
||||
@@ -651,7 +641,7 @@ static void find_callback(void *ctx, const CHAR16 *Name, UINT8 Attr,
|
||||
}
|
||||
}
|
||||
|
||||
EFI_STATUS fs_read(const CHAR16 *Path, void **Buffer, UINTN *Size) {
|
||||
EFI_STATUS fs_read(WString Path, void **Buffer, UINTN *Size) {
|
||||
if (!g_fs_inited) return EFI_NOT_READY;
|
||||
|
||||
const CHAR16 *p = Path;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <serial.h>
|
||||
#include <idt.h>
|
||||
#include <pic.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
// --- Layer list (sorted by z, lowest first) ---
|
||||
|
||||
@@ -84,12 +84,7 @@ layer_t* layer_create(const char* name, layer_type_t type, UINT32 w, UINT32 h) {
|
||||
layer->visible = true;
|
||||
layer->next = NULL;
|
||||
|
||||
const char* s = name;
|
||||
char* d = layer->name;
|
||||
for (SSINT32 i = 0; i < LAYER_NAME_LEN - 1 && *s; i++) {
|
||||
*d++ = *s++;
|
||||
}
|
||||
*d = '\0';
|
||||
str_copy(layer->name, name, LAYER_NAME_LEN);
|
||||
|
||||
UINTN buf_size = (UINTN)w * h * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
|
||||
layer->buffer = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL*)kmalloc(buf_size);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <gdt.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
#include <serial.h>
|
||||
|
||||
static gdt_entry g_gdt[7]; // 5 segments + TSS (2 entries)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <idt.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
#include <pic.h>
|
||||
#include <serial.h>
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <pic.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
#include <serial.h>
|
||||
|
||||
static inline void outb(UINT16 port, UINT8 val) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <pit.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
#include <pic.h>
|
||||
#include <serial.h>
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <fonts/pixel_font.h>
|
||||
#include <serial.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
#include <memory/pmm.h>
|
||||
#include <memory/heap.h>
|
||||
#include <scheduler.h>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <memory/heap.h>
|
||||
#include <memory/pmm.h>
|
||||
#include <serial.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
struct heap_block {
|
||||
UINTN size; // includes header; bit 0 = 1 used, 0 free
|
||||
@@ -184,10 +185,7 @@ void* kcalloc(UINTN num, UINTN size) {
|
||||
UINTN total = num * size;
|
||||
void* ptr = kmalloc(total);
|
||||
if (ptr) {
|
||||
UINT8* p = (UINT8*)ptr;
|
||||
for (UINTN i = 0; i < total; i++) {
|
||||
p[i] = 0;
|
||||
}
|
||||
mem_set(ptr, 0, total);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
@@ -216,11 +214,7 @@ void* krealloc(void* ptr, UINTN new_size) {
|
||||
|
||||
void* new_ptr = kmalloc(new_size);
|
||||
if (new_ptr) {
|
||||
UINT8* src = (UINT8*)ptr;
|
||||
UINT8* dst = (UINT8*)new_ptr;
|
||||
for (UINTN i = 0; i < old_size; i++) {
|
||||
dst[i] = src[i];
|
||||
}
|
||||
mem_copy(new_ptr, ptr, old_size);
|
||||
kfree(ptr);
|
||||
}
|
||||
return new_ptr;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <memory/pmm.h>
|
||||
#include <common.h>
|
||||
#include <serial.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
extern EFI_SYSTEM_TABLE *ST;
|
||||
|
||||
|
||||
@@ -53,12 +53,7 @@ task_t* task_create(const char* name, void (*entry)(void)) {
|
||||
task->time_slice = TIME_SLICE_DEFAULT;
|
||||
|
||||
// Copy name
|
||||
const char* s = name;
|
||||
char* d = task->name;
|
||||
for (SSINT32 i = 0; i < TASK_NAME_LEN - 1 && *s; i++) {
|
||||
*d++ = *s++;
|
||||
}
|
||||
*d = '\0';
|
||||
str_copy(task->name, name, TASK_NAME_LEN);
|
||||
|
||||
// Set up initial stack for first context_switch into this task.
|
||||
// Stack grows downward. context_switch will pop 6 regs then ret.
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
#include <serial.h>
|
||||
#include <common.h>
|
||||
#include <string_utils.h>
|
||||
|
||||
extern EFI_SYSTEM_TABLE *ST;
|
||||
|
||||
@@ -22,7 +22,7 @@ void serial_write_char(char c) {
|
||||
}
|
||||
}
|
||||
|
||||
void serial_write(const char *str) {
|
||||
void serial_write(String str) {
|
||||
if (!g_serial.SerialIo) {
|
||||
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, (CHAR16*)L"serial: null io\n");
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user