Merge remote-tracking branch 'refs/remotes/origin/main'

This commit is contained in:
FantasyPvP
2024-11-08 10:42:52 +00:00
4 changed files with 278 additions and 29 deletions
+33
View File
@@ -44,10 +44,13 @@ String_t str_from_slice(const char* string, int len) {
return s; return s;
} }
String_t str_new() { String_t str_new() {
return str_with_capacity(1); return str_with_capacity(1);
} }
int str_dealloc(String_t* self) { int str_dealloc(String_t* self) {
free(self->data); free(self->data);
return 0; return 0;
@@ -70,6 +73,36 @@ int str_push(String_t* self, char c) {
return 0; return 0;
} }
int str_push_str(String_t* self, char* c) {
if (self->size + strlen(c) >= self->capacity) {
self->capacity = ( self->size + strlen(c) ) * sizeof(char);
self->data = (char*)realloc(self->data, self->capacity);
for (int i = 0; i < strlen(c); i++) {
self->data[self->size + i] = c[i];
}
self->size = self->capacity;
}
}
String_t str_from_file(FILE* file) {
if (file == NULL) {
return str_new();
}
char buffer[64];
String_t string = str_new();
while (fgets(buffer, sizeof(buffer), file) != NULL) {
str_push_str(&string, buffer);
}
fclose(file);
return string;
}
char str_pop(String_t* self) { char str_pop(String_t* self) {
if (self->size == 0) { if (self->size == 0) {
return '\0'; return '\0';
+5
View File
@@ -1,5 +1,6 @@
#ifndef DYNSTR_H #ifndef DYNSTR_H
#define DYNSTR_H #define DYNSTR_H
#include <stdio.h>
typedef struct { typedef struct {
int size; int size;
@@ -15,6 +16,10 @@ String_t str_from_slice(char* string, int len);
String_t str_merge(String_t* s1, String_t* s2); String_t str_merge(String_t* s1, String_t* s2);
String_t str_from_file(FILE* file);
int str_push_str(String_t* string, char* other);
String_t str_new(); String_t str_new();
String_t* str_lines(String_t* self, int* numlines); String_t* str_lines(String_t* self, int* numlines);
+19 -29
View File
@@ -1,6 +1,7 @@
#include <ncurses.h> #include <ncurses.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "editor.h" #include "editor.h"
void help() { void help() {
@@ -14,7 +15,7 @@ void help() {
printf(" cmd log // prints a list of all changes made to the file\n"); printf(" cmd log // prints a list of all changes made to the file\n");
} }
int open_editor() { int open_editor(char* filename) {
initscr(); initscr();
raw(); raw();
noecho(); noecho();
@@ -22,33 +23,14 @@ int open_editor() {
int max_y, max_x; int max_y, max_x;
getmaxyx(stdscr, max_y, max_x); getmaxyx(stdscr, max_y, max_x);
FILE* file = fopen("somecode.c", "r");
if (file == NULL) {
endwin();
printf("Failed to open file\n");
return 1;
}
String_t text = str_new();
// read from file char by char
while (true) {
char line[1024];
if (fgets(line, 1024, file) == NULL) {
break;
}
String_t new_text = str_from_chars(line);
text = str_merge(&text, &new_text);
str_dealloc(&new_text);
}
fclose(file);
Editor editor = editor_from(text);
move(0, 5); move(0, 5);
Editor editor;
if (strcmp(filename, "") == 0) {
editor = new_editor();
} else {
editor = editor_from(str_from_file(fopen(filename, "r")));
}
while (true) { while (true) {
refresh(); refresh();
int c = getch(); int c = getch();
@@ -102,8 +84,16 @@ int open_editor() {
} }
} }
int main() { int main(int argc, char* argv[]) {
open_editor(); if (argc == 1) {
Editor e = new_editor();
open_editor("");
} else if (argc == 3) {
if (strcmp(argv[1], "open") == 0) {
fprintf(stderr, "starting editor openiing file %s", argv[2]);
open_editor(argv[2]);
}
}
return 0; return 0;
} }
+221
View File
@@ -0,0 +1,221 @@
/// dynamic array class
/// written by: Harry Irving
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
typedef struct {
uint32_t size, capacity;
char* data;
} String_t;
String_t str_with_capacity(int capacity) {
String_t s;
/// allocate memory for 'capacity' chars
s.data = (char*)calloc(capacity, sizeof(char));
s.size = 0;
s.capacity = capacity;
return s;
}
String_t str_from_chars(const char* string) {
String_t s;
s.data = (char*)calloc(strlen(string), sizeof(char));
strcpy(s.data, string);
s.size = strlen(string);
s.capacity = strlen(string);
return s;
}
String_t str_from_slice(const char* string, int len) {
String_t s;
s.data = (char*)calloc(len, sizeof(char));
strncpy(s.data, string, len);
s.size = len;
s.capacity = len;
return s;
}
String_t str_new() {
return str_with_capacity(1);
}
int str_dealloc(String_t* self) {
free(self->data);
return 0;
}
int str_push(String_t* self, char c) {
// check size < capacity
if (self->size < self->capacity) {
self->data[self->size] = c;
self->size++;
return 0;
}
// reallocate to add capacity for an extra char
int newcap = self->capacity + sizeof(char);
self->data = (char*)realloc(self->data, newcap);
self->data[self->size] = c;
self->size++;
self->capacity = newcap;
return 0;
}
int str_push_str(String_t* self, char* c) {
if (self->size + strlen(c) >= self->capacity) {
self->capacity = ( self->size + strlen(c) ) * sizeof(char);
self->data = (char*)realloc(self->data, self->capacity);
for (int i = 0; i < strlen(c); i++) {
self->data[self->size + i] = c[i];
}
self->size = self->capacity;
}
}
String_t str_from_file(FILE* file) {
if (file == NULL) {
return str_new();
}
char buffer[1024];
String_t string = str_new();
while (fgets(buffer, sizeof(buffer), file) != NULL) {
str_push_str(&string, buffer);
}
fclose(file);
return string;
}
char str_pop(String_t* self) {
if (self->size == 0) {
return '\0';
}
self->size--;
char c = self->data[self->size];
return c;
}
int str_insert(String_t* self, int index, char c) {
if (index > self->size) {
return -1;
}
self->size++;
for (int i = self->size - 1; i > index; i--) {
self->data[i] = self->data[i - 1];
}
self->data[index] = c;
return 0;
}
int str_remove(String_t* self, int index) {
if (index >= self->size) {
return -1;
}
self->size--;
for (int i = index; i < self->size; i++) {
self->data[i] = self->data[i + 1];
}
return 0;
}
/// splits a string into an array of strings based on a delimiter
String_t* str_split(String_t* self, int* res_len, char c) {
char* string = self->data;
String_t* elements = NULL;
// find the number of lines in the file
bool flag = false;
*res_len = 0;
for (int i = 0; i < strlen(string); i++) {
if (string[i] == c) {
if (flag) {
(*res_len)++;
flag = false;
}
} else {
flag = true;
}
}
if (flag) {
(*res_len)++;
}
// allocate memory for an array of pointers to each line
elements = (String_t*)malloc((*res_len) * sizeof(String_t));
int i = 0;
flag = false;
char* start = string;
char* end = string;
while (*end != '\0') {
if (*end == c) {
if (flag) {
elements[i] = str_from_slice(start, end - start);
i++;
}
end++;
start = end;
flag = false;
} else {
end++;
flag = true;
}
} if (flag) {
elements[i] = str_from_slice(start, end - start);
}
// returns an array of String_t
return elements;
}
String_t* str_lines(String_t* self, int* numlines) {
return str_split(self, numlines, '\n');
}
int str_len(String_t* s) {
return s->size;
}
char* to_chars(String_t* s) {
return s->data;
}
// int main() {
// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n");
// str_remove(&s, 10);
// str_insert(&s, 10, 'h');
// printf("%s\n", to_chars(&s));
// int linen = 0;
// String_t* lines = str_lines(&s, &linen);
// for (int i=0; i < linen; i++) {
// printf("%s", to_chars(&lines[i]));
// }
// str_dealloc(&s);
// return 0;
// }