Moving certain aspects into their own static library
Problem: As our kernel grows we need more complex datastructures and functions these would come from the standard C/C++ library with normal programs. The kernel is a freestanding programme and has no access to standard libraries. Solution: We build a mini version of the standard C/C++ library which will contain the datastructures and functions we want. This library can then be statically linked into our kernel binary. Making it a statically linked library also gives more structure to the project. Keeping these random functions and datastructures in the kernel just clutters the kernel source code with less relevant source code.
This commit is contained in:
@ -1,83 +0,0 @@
|
||||
//
|
||||
// Created by nigel on 19/02/23.
|
||||
//
|
||||
#include "Path.h"
|
||||
#include "../memory/KernelHeap.h"
|
||||
String::String(char* characters)
|
||||
: chars(characters)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
char* String::str(){
|
||||
return chars;
|
||||
}
|
||||
|
||||
unsigned int String::length ()
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while ( chars[i] != '\0'){
|
||||
i++;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
// Returns a null character if size exceeds limits
|
||||
char String::operator[] (size_t idx)
|
||||
{
|
||||
if( idx > this->length())
|
||||
return '\0';
|
||||
|
||||
return chars[idx];
|
||||
}
|
||||
|
||||
const char String::operator[](size_t idx) const {
|
||||
return (const char) chars[idx];
|
||||
}
|
||||
|
||||
StringView::StringView(String &string, unsigned int start, unsigned int end)
|
||||
: String(string), begin(start), end(end)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
char* StringView::str(){
|
||||
char* str = (char*) malloc((this->length() * sizeof(char))+1);
|
||||
|
||||
int index = 0;
|
||||
for ( int i = begin; i < end ; i++){
|
||||
str[index] = chars[i];
|
||||
index++;
|
||||
}
|
||||
chars[index+1] ='\0';
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
Path::Path(char *path)
|
||||
: path(String(path))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Path::Path(String &path)
|
||||
: path(path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
StringView Path::getbasename()
|
||||
{
|
||||
unsigned int path_length = path.length();
|
||||
int i = path_length;
|
||||
while (path[i] != '/')
|
||||
i--;
|
||||
|
||||
return StringView(path,i +1, path_length);
|
||||
}
|
||||
|
||||
char* Path::str() {
|
||||
return path.str();
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
//
|
||||
// Created by nigel on 19/02/23.
|
||||
//
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
|
||||
class String {
|
||||
public:
|
||||
String(char* characters);
|
||||
String(String&) = default;
|
||||
unsigned int length();
|
||||
|
||||
char* str ();
|
||||
char operator[](size_t index) ;
|
||||
const char operator[](size_t idx) const;
|
||||
|
||||
protected:
|
||||
char* chars;
|
||||
|
||||
|
||||
};
|
||||
|
||||
class StringView : String {
|
||||
public:
|
||||
StringView(String& string, unsigned int start, unsigned int end );
|
||||
char* str ();
|
||||
private:
|
||||
unsigned int begin;
|
||||
unsigned int end;
|
||||
};
|
||||
|
||||
class Path{
|
||||
public:
|
||||
Path(String& path);
|
||||
Path(char* path);
|
||||
|
||||
StringView getbasename();
|
||||
|
||||
char* str();
|
||||
|
||||
private:
|
||||
String path;
|
||||
|
||||
};
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "../drivers/ata/ataDevice.h"
|
||||
#include "../partitiontable/mbr/MasterBootRecord.h"
|
||||
#include "../memory/KernelHeap.h"
|
||||
#include "Path.h"
|
||||
#include "../../CoreLib/Memory.h"
|
||||
#include "../filesystem/FAT/DirectoryEntry.h"
|
||||
|
||||
MOUNT_INFO mountInfo;
|
||||
@ -389,6 +389,8 @@ char* FindNextEntryName (char* path )
|
||||
|
||||
void FileSystem::ResolvePath(Path &path)
|
||||
{
|
||||
// See reference material (1) https://man7.org/linux/man-pages/man7/path_resolution.7.html
|
||||
|
||||
char* string_path = path.str();
|
||||
void* cpy = string_path;
|
||||
|
||||
@ -410,9 +412,6 @@ void FileSystem::ResolvePath(Path &path)
|
||||
skip = strlen(entry_name);
|
||||
free(entry_name);
|
||||
|
||||
|
||||
|
||||
|
||||
free(cpy);
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "Path.h"
|
||||
#include "../../CoreLib/Path.h"
|
||||
|
||||
#define FS_FILE 0
|
||||
#define FS_DIRECTORY 1
|
||||
|
Reference in New Issue
Block a user