added loading files
This commit is contained in:
Binary file not shown.
@@ -44,10 +44,13 @@ String_t str_from_slice(const char* string, int len) {
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
String_t str_new() {
|
||||
return str_with_capacity(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int str_dealloc(String_t* self) {
|
||||
free(self->data);
|
||||
return 0;
|
||||
@@ -70,6 +73,36 @@ int str_push(String_t* self, char c) {
|
||||
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) {
|
||||
if (self->size == 0) {
|
||||
return '\0';
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#ifndef DYNSTR_H
|
||||
#define DYNSTR_H
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
@@ -13,6 +14,10 @@ String_t str_from_chars(char* string);
|
||||
|
||||
String_t str_from_slice(char* string, int len);
|
||||
|
||||
String_t str_from_file(FILE* file);
|
||||
|
||||
int str_push_str(String_t* string, char* other);
|
||||
|
||||
String_t str_new();
|
||||
|
||||
String_t* str_lines(String_t* self, int* numlines);
|
||||
|
||||
+4
-5
@@ -11,7 +11,7 @@ typedef struct {
|
||||
String_t* buffer;
|
||||
} Editor;
|
||||
|
||||
Editor editor_from(char* input_string) {
|
||||
Editor editor_from(String_t input_string) {
|
||||
Editor e;
|
||||
e.lines = 0;
|
||||
e.screen_line = 0;
|
||||
@@ -19,10 +19,9 @@ Editor editor_from(char* input_string) {
|
||||
e.editmode = false;
|
||||
|
||||
int linenum = 0;
|
||||
String_t lines = str_from_chars(input_string);
|
||||
e.buffer = str_lines(&lines, &linenum);
|
||||
e.buffer = str_lines(&input_string, &linenum);
|
||||
e.lines = (size_t)linenum;
|
||||
str_dealloc(&lines);
|
||||
str_dealloc(&input_string);
|
||||
|
||||
for (size_t i = 0; i < e.lines; i++) {
|
||||
// adding the line number
|
||||
@@ -38,7 +37,7 @@ Editor editor_from(char* input_string) {
|
||||
}
|
||||
|
||||
Editor new_editor() {
|
||||
return editor_from("");
|
||||
return editor_from(str_new());
|
||||
}
|
||||
|
||||
void move_cursor(Editor* self, int x, int y) {
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ typedef struct {
|
||||
|
||||
Editor new_editor();
|
||||
|
||||
Editor editor_from(char* input_string);
|
||||
Editor editor_from(String_t input_string);
|
||||
|
||||
void move_cursor(Editor* self, int x, int y);
|
||||
void delchar(Editor* self);
|
||||
|
||||
+1
-19
@@ -1,19 +1 @@
|
||||
buffer full
|
||||
size: 25
|
||||
size: 23
|
||||
size: 25
|
||||
size: 0
|
||||
ok!buffer full
|
||||
size: 25
|
||||
size: 23
|
||||
size: 30
|
||||
size: 4
|
||||
size: 0
|
||||
ok!buffer full
|
||||
size: 25
|
||||
size: 23
|
||||
size: 30
|
||||
size: 4
|
||||
size: 1
|
||||
size: 0
|
||||
ok!
|
||||
starting editor openiing file somefile.c
|
||||
+18
-4
@@ -1,6 +1,7 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "editor.h"
|
||||
|
||||
void help() {
|
||||
@@ -14,7 +15,7 @@ void help() {
|
||||
printf(" cmd log // prints a list of all changes made to the file\n");
|
||||
}
|
||||
|
||||
int open_editor() {
|
||||
int open_editor(char* filename) {
|
||||
initscr();
|
||||
raw();
|
||||
noecho();
|
||||
@@ -22,9 +23,14 @@ int open_editor() {
|
||||
int max_y, max_x;
|
||||
getmaxyx(stdscr, max_y, max_x);
|
||||
|
||||
Editor editor = editor_from("this is some text to edit\nthis is the second line\nand this is the third lol");
|
||||
move(0, 5);
|
||||
|
||||
Editor editor;
|
||||
if (strcmp(filename, "") == 0) {
|
||||
editor = new_editor();
|
||||
} else {
|
||||
editor = editor_from(str_from_file(fopen(filename, "r")));
|
||||
}
|
||||
while (true) {
|
||||
refresh();
|
||||
int c = getch();
|
||||
@@ -79,8 +85,16 @@ int open_editor() {
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
open_editor();
|
||||
int main(int argc, char* argv[]) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
// }
|
||||
Reference in New Issue
Block a user