can properly open empty and create files
This commit is contained in:
parent
b4f6124da0
commit
e85314ba0e
@ -15,4 +15,9 @@ Write a simple text editor written in pure C:
|
|||||||
https://viewsourcecode.org/snaptoken/kilo/
|
https://viewsourcecode.org/snaptoken/kilo/
|
||||||
|
|
||||||
All C keywords:
|
All C keywords:
|
||||||
https://www.programiz.com/c-programming/list-all-keywords-c-language
|
https://www.programiz.com/c-programming/list-all-keywords-c-language
|
||||||
|
|
||||||
|
Nuklear:
|
||||||
|
https://github.com/Immediate-Mode-UI/Nuklear
|
||||||
|
https://github.com/vurtun/nuklear/wiki/Text-Edit
|
||||||
|
https://dexp.in/articles/nuklear-intro/
|
||||||
|
43
src/ste.c
43
src/ste.c
@ -359,26 +359,35 @@ void drawBar (char *s)
|
|||||||
/* Open a file and put it into a buffer line by line */
|
/* Open a file and put it into a buffer line by line */
|
||||||
void fileOpen (char *filename)
|
void fileOpen (char *filename)
|
||||||
{
|
{
|
||||||
FILE *fp = fopen(filename, "r");
|
FILE *fd = fopen(filename, "a+");
|
||||||
if (fp == NULL) die("Cannot open file", BAD_FILE);
|
if (fd == NULL) die("Cannot open file", BAD_FILE);
|
||||||
|
|
||||||
/* Init the linebuffer */
|
/* Check if the file is empty first */
|
||||||
char *line = NULL;
|
fseek (fd, 0, SEEK_END);
|
||||||
/* Set linecap to 0 so getline will atumatically allocate
|
int size = ftell(fd);
|
||||||
* memory for the line buffer*/
|
if (size) {
|
||||||
size_t linecap = 0;
|
rewind(fd);
|
||||||
ssize_t linelen;
|
|
||||||
|
|
||||||
/* getline returns -1 if no new line is present */
|
/* Init the linebuffer */
|
||||||
while ((linelen = getline(&line, &linecap, fp)) != -1) {
|
char *line = NULL;
|
||||||
while (linelen > 0 && (line[linelen - 1] == '\n' || line[linelen - 1] == '\r'))
|
/* Set linecap to 0 so getline will atumatically allocate
|
||||||
linelen--;
|
* memory for the line buffer*/
|
||||||
rowAddLast(&rows, line, linelen);
|
size_t linecap = 0;
|
||||||
|
ssize_t linelen;
|
||||||
|
|
||||||
|
/* getline returns -1 if no new line is present */
|
||||||
|
while ((linelen = getline(&line, &linecap, fd)) != -1) {
|
||||||
|
while (linelen > 0 && (line[linelen - 1] == '\n' || line[linelen - 1] == '\r'))
|
||||||
|
linelen--;
|
||||||
|
rowAddLast(&rows, line, linelen);
|
||||||
|
}
|
||||||
|
/* free the line buffer */
|
||||||
|
free(line);
|
||||||
|
} else {
|
||||||
|
rowAddLast(&rows, " ", 1);
|
||||||
}
|
}
|
||||||
/* free the line buffer */
|
|
||||||
free(line);
|
|
||||||
/* close the file */
|
/* close the file */
|
||||||
fclose(fp);
|
fclose(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------- file operations ----------------------------------*/
|
/*------------------------------------- file operations ----------------------------------*/
|
||||||
@ -581,7 +590,7 @@ void updateInfo (void)
|
|||||||
|
|
||||||
getmaxyx(stdscr, t.dim.y, t.dim.x);
|
getmaxyx(stdscr, t.dim.y, t.dim.x);
|
||||||
t.dim.y -= 1;
|
t.dim.y -= 1;
|
||||||
t.pad = decimalSize(rows.rownum - 1);
|
t.pad = decimalSize(rows.rownum > 1 ? rows.rownum - 1 : 2);
|
||||||
t.dim.x -= t.pad + 1;
|
t.dim.x -= t.pad + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user