Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development - Small changes and fixups #115

Merged
merged 9 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added apps/texed/texed
Binary file not shown.
4 changes: 2 additions & 2 deletions apps/texed/textbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <math.h>
#include <fs/fs.h>
#else

#include <locale.h>
#include <ncurses.h>
#include <stdint.h>
#include <stdarg.h>
Expand Down Expand Up @@ -618,7 +618,7 @@ int main(int argc, char *argv[]) {
}

start_color();

setlocale(LC_ALL, "");
noecho();
keypad(stdscr, TRUE);
#endif // !NCURSES
Expand Down
63 changes: 63 additions & 0 deletions apps/utils/HashMap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* HashMap.hpp */
#ifndef __HASHMAP_H
#define __HASHMAP_H

#include <stdint.h>

const size_t HASH_MAP_SIZE = 256; /* Fixed size of the hashmap */

/* Entry structure for hashmap */
struct Entry {
char* key;
char* value;
bool is_occupied; /* Indicates if the entry is occupied */

Entry() : key(nullptr), value(nullptr), is_occupied(false) {}
};

/* HashMap class */
class HashMap {
public:
HashMap() {
for (size_t i = 0; i < HASH_MAP_SIZE; ++i) {
entries[i].is_occupied = false;
}
}

void put(const char* key, const char* value){
size_t index = hash(key);
entries[index].key = const_cast<char*>(key);
entries[index].value = const_cast<char*>(value);
entries[index].is_occupied = true;
}

const char* get(const char* key) {
size_t index = hash(key);
if (entries[index].is_occupied && entries[index].key == key) {
return entries[index].value;
}
return nullptr; /* Key not found */
}

void remove(const char* key) {
size_t index = hash(key);
if (entries[index].is_occupied && entries[index].key == key) {
entries[index].is_occupied = false;
}
}

private:
/* Simple hash function */
size_t hash(const char* key) {
size_t hash_value = 0;
while (*key) {
hash_value = (hash_value * 131 + *key++) % HASH_MAP_SIZE;
}
return hash_value;
}

/* Array of entries */
Entry entries[HASH_MAP_SIZE];
};

#endif /* __HASHMAP_H */
96 changes: 96 additions & 0 deletions apps/utils/Web/Json.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* SimpleJson.hpp */
#ifndef SIMPLE_JSON_H
#define SIMPLE_JSON_H

#include <stdint.h>

const size_t MAX_JSON_ENTRIES = 256;
const size_t MAX_STRING_LENGTH = 256;

/* SimpleJson class */
class SimpleJson {
public:
SimpleJson() {
for (size_t i = 0; i < MAX_JSON_ENTRIES; ++i) {
entries[i].is_occupied = false;
}
}

const char* operator[](const char* key) const {
int index = findIndex(key);
if (index >= 0) {
return entries[index].value;
}
return nullptr;
}

void set(const char* key, const char* value) {
int index = findIndex(key);
if (index < 0) {
/* Find an empty slot to store the new entry */
for (int i = 0; i < MAX_JSON_ENTRIES; ++i) {
if (!entries[i].is_occupied) {
entries[i].setKey(key);
entries[i].setValue(value);
entries[i].is_occupied = true;
return;
}
}
} else {
/* Update existing entry */
entries[index].setValue(value);
}
}

private:
struct JsonEntry {
char key[MAX_STRING_LENGTH];
char value[MAX_STRING_LENGTH];
bool is_occupied;

JsonEntry() : is_occupied(false) {
key[0] = '\0';
value[0] = '\0';
}

void setKey(const char* k) {
strncpy(key, k, MAX_STRING_LENGTH);
key[MAX_STRING_LENGTH - 1] = '\0'; /* Ensure null-termination */
}

void setValue(const char* v) {
strncpy(value, v, MAX_STRING_LENGTH);
value[MAX_STRING_LENGTH - 1] = '\0'; /* Ensure null-termination */
}
};

JsonEntry entries[MAX_JSON_ENTRIES];

int findIndex(const char* key) const {
for (int i = 0; i < MAX_JSON_ENTRIES; ++i) {
if (entries[i].is_occupied && strcmp(entries[i].key, key) == 0) {
return i;
}
}
return -1;
}
static void strncpy(char* dest, const char* src, size_t n) {
for (size_t i = 0; i < n && src[i] != '\0'; ++i) {
dest[i] = src[i];
}
if (n > 0) {
dest[n - 1] = '\0';
}
}

static void strcmp(const char* a, const char* b) {
while (*a && *b && *a == *b) {
++a;
++b;
}
return *a - *b;
}

};

#endif /* SIMPLE_JSON_H */
9 changes: 9 additions & 0 deletions apps/utils/Web/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Web Library

## Pillars of creation

### HTTP Engine

- [ ] HTTP Parser

### Web framework
71 changes: 69 additions & 2 deletions fs/fat16_iface.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static int fat16_rmdir(struct filesystem* fs, const char* path);
static int fat16_rename(struct filesystem* fs, const char* path, const char* new_path);
static int fat16_stat(struct filesystem* fs, const char* path, struct file* file);
static int fat16_list(struct filesystem* fs, const char* path, char* buf, int size);
static int fat16_find(struct filesystem* fs, char* origin, const char* needle);

/* filesystem_ops struct */
static struct filesystem_ops fat16_ops = {
Expand All @@ -43,7 +44,8 @@ static struct filesystem_ops fat16_ops = {
.rmdir = fat16_rmdir,
.rename = fat16_rename,
.stat = fat16_stat,
.list = fat16_list
.list = fat16_list,
.find = fat16_find
};


Expand Down Expand Up @@ -507,4 +509,69 @@ static int fat16_list(struct filesystem* fs, const char* path, char* buf, int si

twritef("%d directory entries.\n", entries);
return 0;
}
}

static int fat16_find(struct filesystem* fs, char* path, const char* needle){
int ret;
int entries = 0;
struct fat16_directory_entry entry;
struct fat16_directory_entry parent_entry;

/* check if the directory exists */
struct fat16_file_identifier id = fat16_get_directory_entry((char*)path, &parent_entry);
if(id.directory < 0 || parent_entry.attributes != FAT16_FLAG_SUBDIRECTORY){
return -1;
}

/* read the directory entry */
ret = fat16_read_entry(id.directory, id.index, &entry);
if(ret != 0){
return -2;
}

for (int i = 0; i < (int)ENTRIES_PER_BLOCK; i++) {
struct fat16_directory_entry _list_entry = {0};
struct fat16_directory_entry* dir_entry = &_list_entry;

fat16_read_entry(entry.first_cluster == 0 ? get_root_directory_start_block() : get_data_start_block() + entry.first_cluster, i, dir_entry);

/* Check if the entry is used (filename's first byte is not 0x00 or 0xE5) */
if (dir_entry->filename[0] != 0x00 && dir_entry->filename[0] != 0xE5) {
if(dir_entry->filename[0] == '.'){
continue;
}

/* parse name */
char name[13] = {0};
int j = 0;
while(dir_entry->filename[j] != ' '){
name[j] = dir_entry->filename[j];
j++;
}

if(dir_entry->extension[0] != ' '){
name[j++] = '.';
for(int k = 0; k < 3; k++){
name[j++] = dir_entry->extension[k];
}
}

name[j] = '\0';

if(strstr(name, needle) != -1){
twritef("%s%s%s\n", path, name, dir_entry->attributes & FAT16_FLAG_SUBDIRECTORY ? "/" : "");
}

if(dir_entry->attributes & FAT16_FLAG_SUBDIRECTORY){
char new_path[255] = {0};
memcpy(new_path, path, strlen(path));
strcat(new_path, name);
strcat(new_path, "/");
fat16_find(fs, new_path, needle);
}

entries++;
}
}
return 0;
}
Loading
Loading