fixed some bugs in c versiosn
This commit is contained in:
Vendored
+28
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"type": "cppbuild",
|
||||||
|
"label": "C/C++: gcc build active file",
|
||||||
|
"command": "/usr/bin/gcc",
|
||||||
|
"args": [
|
||||||
|
"-fdiagnostics-color=always",
|
||||||
|
"-g",
|
||||||
|
"${file}",
|
||||||
|
"-o",
|
||||||
|
"${fileDirname}/${fileBasenameNoExtension}"
|
||||||
|
],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${fileDirname}"
|
||||||
|
},
|
||||||
|
"problemMatcher": [
|
||||||
|
"$gcc"
|
||||||
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
},
|
||||||
|
"detail": "Task generated by Debugger."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": "2.0.0"
|
||||||
|
}
|
||||||
Binary file not shown.
+38
-50
@@ -5,15 +5,15 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int size;
|
uint32_t size, capacity;
|
||||||
int capacity;
|
|
||||||
char* data;
|
char* data;
|
||||||
} String;
|
} String_t;
|
||||||
|
|
||||||
String str_with_capacity(int capacity) {
|
String_t str_with_capacity(int capacity) {
|
||||||
String s;
|
String_t s;
|
||||||
|
|
||||||
/// allocate memory for 'capacity' chars
|
/// allocate memory for 'capacity' chars
|
||||||
s.data = (char*)calloc(capacity, sizeof(char));
|
s.data = (char*)calloc(capacity, sizeof(char));
|
||||||
@@ -24,8 +24,8 @@ String str_with_capacity(int capacity) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
String str_from_chars(char* string) {
|
String_t str_from_chars(const char* string) {
|
||||||
String s;
|
String_t s;
|
||||||
|
|
||||||
s.data = (char*)calloc(strlen(string), sizeof(char));
|
s.data = (char*)calloc(strlen(string), sizeof(char));
|
||||||
strcpy(s.data, string);
|
strcpy(s.data, string);
|
||||||
@@ -34,8 +34,8 @@ String str_from_chars(char* string) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
String str_from_slice(char* string, int len) {
|
String_t str_from_slice(const char* string, int len) {
|
||||||
String s;
|
String_t s;
|
||||||
|
|
||||||
s.data = (char*)calloc(len, sizeof(char));
|
s.data = (char*)calloc(len, sizeof(char));
|
||||||
strncpy(s.data, string, len);
|
strncpy(s.data, string, len);
|
||||||
@@ -44,22 +44,21 @@ String str_from_slice(char* string, int len) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String_t str_new() {
|
||||||
String str_new() {
|
|
||||||
return str_with_capacity(1);
|
return str_with_capacity(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int str_dealloc(String* self) {
|
int str_dealloc(String_t* self) {
|
||||||
free(self->data);
|
free(self->data);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void str_push(String* self, char c) {
|
int str_push(String_t* self, char c) {
|
||||||
// check size < capacity
|
// check size < capacity
|
||||||
if (self->size < self->capacity) {
|
if (self->size < self->capacity) {
|
||||||
self->data[self->size] = c;
|
self->data[self->size] = c;
|
||||||
self->size++;
|
self->size++;
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// reallocate to add capacity for an extra char
|
// reallocate to add capacity for an extra char
|
||||||
@@ -68,10 +67,10 @@ void str_push(String* self, char c) {
|
|||||||
self->data[self->size] = c;
|
self->data[self->size] = c;
|
||||||
self->size++;
|
self->size++;
|
||||||
self->capacity = newcap;
|
self->capacity = newcap;
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char str_pop(String* self) {
|
char str_pop(String_t* self) {
|
||||||
if (self->size == 0) {
|
if (self->size == 0) {
|
||||||
return '\0';
|
return '\0';
|
||||||
}
|
}
|
||||||
@@ -80,31 +79,33 @@ char str_pop(String* self) {
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void str_insert(String* self, int index, char c) {
|
int str_insert(String_t* self, int index, char c) {
|
||||||
if (index > self->size) {
|
if (index > self->size) {
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
self->size++;
|
self->size++;
|
||||||
for (int i = self->size - 1; i > index; i--) {
|
for (int i = self->size - 1; i > index; i--) {
|
||||||
self->data[i] = self->data[i - 1];
|
self->data[i] = self->data[i - 1];
|
||||||
}
|
}
|
||||||
self->data[index] = c;
|
self->data[index] = c;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void str_remove(String* self, int index) {
|
int str_remove(String_t* self, int index) {
|
||||||
if (index >= self->size) {
|
if (index >= self->size) {
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
self->size--;
|
self->size--;
|
||||||
for (int i = index; i < self->size; i++) {
|
for (int i = index; i < self->size; i++) {
|
||||||
self->data[i] = self->data[i + 1];
|
self->data[i] = self->data[i + 1];
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
String* str_lines(String* self, int* numlines) {
|
String_t* str_lines(String_t* self, int* numlines) {
|
||||||
|
|
||||||
char* string = self->data;
|
char* string = self->data;
|
||||||
String* lines = NULL;
|
String_t* lines = NULL;
|
||||||
|
|
||||||
// find the number of lines in the file
|
// find the number of lines in the file
|
||||||
*numlines = 0;
|
*numlines = 0;
|
||||||
@@ -121,7 +122,7 @@ String* str_lines(String* self, int* numlines) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// allocate memory for an array of pointers to each line
|
// allocate memory for an array of pointers to each line
|
||||||
lines = (String*)malloc((*numlines + 1) * sizeof(String));
|
lines = (String_t*)malloc((*numlines + 1) * sizeof(String_t));
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char* start = string;
|
char* start = string;
|
||||||
@@ -139,15 +140,15 @@ String* str_lines(String* self, int* numlines) {
|
|||||||
end++;
|
end++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// returns an array of String
|
// returns an array of String_t
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// splits a string into an array of strings based on a delimiter
|
/// splits a string into an array of strings based on a delimiter
|
||||||
String* str_split(String* self, int* res_len, char c) {
|
String_t* str_split(String_t* self, int* res_len, char c) {
|
||||||
|
|
||||||
char* string = self->data;
|
char* string = self->data;
|
||||||
String* elements = NULL;
|
String_t* elements = NULL;
|
||||||
|
|
||||||
// find the number of lines in the file
|
// find the number of lines in the file
|
||||||
|
|
||||||
@@ -168,7 +169,7 @@ String* str_split(String* self, int* res_len, char c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// allocate memory for an array of pointers to each line
|
// allocate memory for an array of pointers to each line
|
||||||
elements = (String*)malloc((*res_len) * sizeof(String));
|
elements = (String_t*)malloc((*res_len) * sizeof(String_t));
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
flag = false;
|
flag = false;
|
||||||
@@ -193,40 +194,27 @@ String* str_split(String* self, int* res_len, char c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// returns an array of String
|
// returns an array of String_t
|
||||||
return elements;
|
return elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
int str_len(String* s) {
|
int str_len(String_t* s) {
|
||||||
return s->size;
|
return s->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* to_chars(String* s) {
|
char* to_chars(String_t* s) {
|
||||||
return s->data;
|
return s->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// int main() {
|
// int main() {
|
||||||
// String s = str_from_chars("hello\nworld\neeeee\notherline\n\0");
|
// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n\0");
|
||||||
|
|
||||||
|
// str_remove(&s, 10);
|
||||||
|
// str_insert(&s, 10, 'h');
|
||||||
|
|
||||||
|
// printf("%s\n", to_chars(&s));
|
||||||
|
|
||||||
// int numlines = 0;
|
|
||||||
// String* lns = str_lines(&s, &numlines);
|
|
||||||
// str_dealloc(&s);
|
// str_dealloc(&s);
|
||||||
|
|
||||||
// for (int i = 0; i < numlines; i++) {
|
// return 0;
|
||||||
// printf("%s\n", lns[i].data);
|
|
||||||
// str_dealloc(&lns[i]);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// String s2 = str_from_chars("$this$needs$to$be$split");
|
|
||||||
|
|
||||||
// int elements = 0;
|
|
||||||
// String* split = str_split(&s2, &elements, '$');
|
|
||||||
// str_dealloc(&s2);
|
|
||||||
|
|
||||||
// printf("%d elements\n", elements);
|
|
||||||
// for (int i = 0; i < elements; i++) {
|
|
||||||
// printf("%s\n", split[i].data);
|
|
||||||
// str_dealloc(&split[i]);
|
|
||||||
// printf("(dealloced)\n");
|
|
||||||
// }
|
|
||||||
// }
|
// }
|
||||||
+14
-14
@@ -5,32 +5,32 @@ typedef struct {
|
|||||||
int size;
|
int size;
|
||||||
int capacity;
|
int capacity;
|
||||||
char* data;
|
char* data;
|
||||||
} String;
|
} String_t;
|
||||||
|
|
||||||
String str_with_capacity(int capacity);
|
String_t str_with_capacity(int capacity);
|
||||||
|
|
||||||
String str_from_chars(char* string);
|
String_t str_from_chars(char* string);
|
||||||
|
|
||||||
String str_from_slice(char* string, int len);
|
String_t str_from_slice(char* string, int len);
|
||||||
|
|
||||||
String str_new();
|
String_t str_new();
|
||||||
|
|
||||||
String* str_lines(String* self, int* numlines);
|
String_t* str_lines(String_t* self, int* numlines);
|
||||||
|
|
||||||
// String* str_split(String* self, int* res_len, char c);
|
// String_t* str_split(String_t* self, int* res_len, char c);
|
||||||
|
|
||||||
void str_push(String* s, char c);
|
void str_push(String_t* s, char c);
|
||||||
|
|
||||||
char str_pop(String* s);
|
char str_pop(String_t* s);
|
||||||
|
|
||||||
void str_insert(String* s, int index, char c);
|
void str_insert(String_t* s, int index, char c);
|
||||||
|
|
||||||
void str_remove(String* s, int index);
|
void str_remove(String_t* s, int index);
|
||||||
|
|
||||||
int str_dealloc(String* s);
|
int str_dealloc(String_t* s);
|
||||||
|
|
||||||
char* to_chars(String* s);
|
char* to_chars(String_t* s);
|
||||||
|
|
||||||
int str_len(String* s);
|
int str_len(String_t* s);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+19
-6
@@ -9,7 +9,7 @@ typedef struct {
|
|||||||
int screen_line;
|
int screen_line;
|
||||||
int screen_col;
|
int screen_col;
|
||||||
bool editmode;
|
bool editmode;
|
||||||
String* buffer;
|
String_t* buffer;
|
||||||
} Editor;
|
} Editor;
|
||||||
|
|
||||||
Editor new_editor() {
|
Editor new_editor() {
|
||||||
@@ -18,7 +18,13 @@ Editor new_editor() {
|
|||||||
e.screen_line = 0;
|
e.screen_line = 0;
|
||||||
e.screen_col = 0;
|
e.screen_col = 0;
|
||||||
e.editmode = false;
|
e.editmode = false;
|
||||||
e.buffer = NULL;
|
|
||||||
|
String_t lines = str_from_chars("");
|
||||||
|
e.buffer = str_lines(&lines, &e.lines);
|
||||||
|
str_dealloc(&lines);
|
||||||
|
|
||||||
|
addstr("");
|
||||||
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,12 +35,18 @@ Editor editor_from(char* input_string) {
|
|||||||
e.screen_line = 0;
|
e.screen_line = 0;
|
||||||
e.screen_col = 0;
|
e.screen_col = 0;
|
||||||
e.editmode = false;
|
e.editmode = false;
|
||||||
e.buffer = NULL;
|
|
||||||
|
String_t lines = str_from_chars(input_string);
|
||||||
|
e.buffer = str_lines(&lines, &e.lines);
|
||||||
|
str_dealloc(&lines);
|
||||||
|
|
||||||
|
addstr(input_string);
|
||||||
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_cursor(Editor* self, int x, int y) {
|
void move_cursor(Editor* self, int x, int y) {
|
||||||
if (x != 0
|
if (y != 0
|
||||||
&& self->screen_line + y >= 0
|
&& self->screen_line + y >= 0
|
||||||
&& self->screen_line + y <= self->lines)
|
&& self->screen_line + y <= self->lines)
|
||||||
{
|
{
|
||||||
@@ -45,6 +57,7 @@ void move_cursor(Editor* self, int x, int y) {
|
|||||||
}
|
}
|
||||||
} else if (self->screen_col + x < 0) {
|
} else if (self->screen_col + x < 0) {
|
||||||
if (self->screen_line - 1 >= 0) {
|
if (self->screen_line - 1 >= 0) {
|
||||||
|
self->screen_line -= 1;
|
||||||
self->screen_col = str_len(&self->buffer[self->screen_line]);
|
self->screen_col = str_len(&self->buffer[self->screen_line]);
|
||||||
}
|
}
|
||||||
} else if (self->screen_col + x > str_len(&self->buffer[self->screen_line])) {
|
} else if (self->screen_col + x > str_len(&self->buffer[self->screen_line])) {
|
||||||
@@ -70,13 +83,13 @@ void addchar(Editor* self, char c) {
|
|||||||
if (self->screen_line == self->lines) {
|
if (self->screen_line == self->lines) {
|
||||||
// reallocate self->buffer to be 1 larger
|
// reallocate self->buffer to be 1 larger
|
||||||
self->lines++;
|
self->lines++;
|
||||||
self->buffer = realloc(self->buffer, sizeof(String) * (self->lines + 1));
|
self->buffer = realloc(self->buffer, sizeof(String_t) * (self->lines + 1));
|
||||||
}
|
}
|
||||||
str_insert(&self->buffer[self->screen_line], self->screen_col, c);
|
str_insert(&self->buffer[self->screen_line], self->screen_col, c);
|
||||||
move_cursor(self, 1, 0);
|
move_cursor(self, 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
String* to_string(Editor* self) {
|
String_t* to_string(Editor* self) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -4,7 +4,7 @@ typedef struct {
|
|||||||
int screen_line;
|
int screen_line;
|
||||||
int screen_col;
|
int screen_col;
|
||||||
bool editmode;
|
bool editmode;
|
||||||
String* buffer;
|
String_t* buffer;
|
||||||
} Editor;
|
} Editor;
|
||||||
|
|
||||||
Editor new_editor();
|
Editor new_editor();
|
||||||
@@ -15,4 +15,4 @@ void move_cursor(Editor* self, int x, int y);
|
|||||||
void delchar(Editor* self);
|
void delchar(Editor* self);
|
||||||
void addchar(Editor* self, char c);
|
void addchar(Editor* self, char c);
|
||||||
|
|
||||||
String* to_string(Editor* self);
|
String_t* to_string(Editor* self);
|
||||||
+19
-2
@@ -24,7 +24,7 @@ int open_editor() {
|
|||||||
|
|
||||||
move(0, 0);
|
move(0, 0);
|
||||||
|
|
||||||
Editor editor = new_editor();
|
Editor editor = editor_from("this is some text to edit\nthis is the second line\nand this is the third lol");
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
refresh();
|
refresh();
|
||||||
@@ -35,10 +35,26 @@ int open_editor() {
|
|||||||
editor.editmode = false;
|
editor.editmode = false;
|
||||||
break;
|
break;
|
||||||
case KEY_BACKSPACE:
|
case KEY_BACKSPACE:
|
||||||
|
delchar(&editor);
|
||||||
|
move_cursor(&editor, -1, 0);
|
||||||
|
break;
|
||||||
|
case KEY_DC:
|
||||||
delchar(&editor);
|
delchar(&editor);
|
||||||
break;
|
break;
|
||||||
case KEY_ENTER:
|
case KEY_ENTER:
|
||||||
editor.editmode = false;
|
//TODO: next line
|
||||||
|
break;
|
||||||
|
case KEY_UP:
|
||||||
|
move_cursor(&editor, 0, -1);
|
||||||
|
break;
|
||||||
|
case KEY_DOWN:
|
||||||
|
move_cursor(&editor, 0, 1);
|
||||||
|
break;
|
||||||
|
case KEY_LEFT:
|
||||||
|
move_cursor(&editor, -1, 0);
|
||||||
|
break;
|
||||||
|
case KEY_RIGHT:
|
||||||
|
move_cursor(&editor, 1, 0);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
addchar(&editor, c);
|
addchar(&editor, c);
|
||||||
@@ -47,6 +63,7 @@ int open_editor() {
|
|||||||
} else {
|
} else {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'q':
|
case 'q':
|
||||||
|
endwin();
|
||||||
return 0;
|
return 0;
|
||||||
case 'i':
|
case 'i':
|
||||||
editor.editmode = true;
|
editor.editmode = true;
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+12
-2
@@ -7,6 +7,16 @@ use std::{
|
|||||||
io::Write,
|
io::Write,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
fn somefunc() {
|
||||||
|
let c = "hello";
|
||||||
|
|
||||||
|
println!("{}", c);
|
||||||
|
|
||||||
|
let c2 = String::from("hello world");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
struct EditorData {
|
struct EditorData {
|
||||||
buffer: Vec<Vec<char>>, // outer vec is the line, inner is col;
|
buffer: Vec<Vec<char>>, // outer vec is the line, inner is col;
|
||||||
file_line: i32,
|
file_line: i32,
|
||||||
@@ -92,14 +102,14 @@ impl EditorData {
|
|||||||
/// Insert a character at the cursor,
|
/// Insert a character at the cursor,
|
||||||
/// then update the cursor position.
|
/// then update the cursor position.
|
||||||
fn addchar(&mut self, c: char) {
|
fn addchar(&mut self, c: char) {
|
||||||
ncurses::insch(keystroke as u32);
|
ncurses::insch(c as u32);
|
||||||
if let Some(line) = self.buffer.get_mut(self.screen_line as usize) {
|
if let Some(line) = self.buffer.get_mut(self.screen_line as usize) {
|
||||||
line.insert(self.screen_col as usize, c);
|
line.insert(self.screen_col as usize, c);
|
||||||
} else {
|
} else {
|
||||||
self.buffer.push(Vec::new());
|
self.buffer.push(Vec::new());
|
||||||
self.buffer.get_mut(self.screen_line as usize).unwrap().push(c);
|
self.buffer.get_mut(self.screen_line as usize).unwrap().push(c);
|
||||||
}
|
}
|
||||||
data.mv_cursor(0, 1);
|
self.mv_cursor(0, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user