Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Text Editor

![Image of the program in use.](res/text.bmp)

Almost ready to merge upstream, still need cosmetic style changes, and to write the merge request.

Functional lightweight text editor written in C using ncurses.

## How to use:
Expand Down
77 changes: 41 additions & 36 deletions line.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
#include "line.h"


void init_line(LINE *s)
{
s->size = LINE_SIZE;
s->line = (char *)malloc(LINE_SIZE * sizeof(char));
s->line[0] = '\0';
} // init_line

s->line = malloc(LINE_SIZE * sizeof(char));
if(s->line != NULL)
{
s->size = LINE_SIZE;
s->line[0] = '\0';
}
} //init_line

// Insert char into string.
void insert_char(LINE *s, char c, int index)
void add_char(LINE *s, char c)
{
int i;

if(strlen(s->line) >= s->size - 2) expand(s);

for(i = strlen(s->line); i >= index; i--)
s->line[i + 1] = s->line[i];

s->line[index] = c;
} // insert

insert_char(s, c, strlen(s->line));
}

// Insert char into string.
void insert_char(LINE *s, char c, int index){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the curly brace to its own line

char temp[2];
temp[0] = c;
temp[1] = '\0';
insert_string(s, temp, index);
} // insert char using insert_string()

void remove_char(LINE *s, int index)
void insert_string(LINE *s, char *in, int index)
{
int i;
int len = strlen(s->line);
for(i = index; i < len; i++)
s->line[i] = s->line[i + 1];
} // remove_char
int line_ln = strlen(s->line);
int in_ln = strlen(in);
if(line_ln + in_ln >= s->size) expand(s, in_ln);
if(line_ln + in_ln >= s->size) return;

size_t append_ln = line_ln - index + 1;

memmove(s->line + index + in_ln, s->line + index, append_ln * sizeof(char));
memcpy(s->line + index, in, (in_ln) * sizeof(char));
} // insert string

// expands size of line
void expand(LINE *s)
void expand(LINE *s, int ln)
{
int new_size = s->size * 2;
char *temp = (char *)malloc(new_size * sizeof(char));
strcpy(temp, s->line);
free(s->line);
s->line = temp;
s->size = new_size;
} // expand

void add_char(LINE *s, char c)
{
insert_char(s, c, strlen(s->line));
size_t new_size = s->size * 2 + ln;
char *p = realloc(s->line, new_size * sizeof(char));
if(p != NULL)
{
s->size = new_size;
s->line = p;
}
}


void remove_char(LINE *s, int index){
size_t append_ln = strlen(s->line) - index;
memmove(s->line + index, s->line + index + 1, append_ln * sizeof(char));
} // remove_char
12 changes: 7 additions & 5 deletions line.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

typedef struct
{
char *line;
int size; // size of array, not string
char *line;
int size; // size of array, not string
} LINE;

void init_line(LINE *s);
void insert_char(LINE *s, char c, int index); // inserts char to string
void insert_string(LINE *s, char *str, int index);
void remove_char(LINE *s, int index);
void expand(LINE *s); // doubles the size of the line
void add_char(LINE *s, char c); // add to end of line

void add_char(LINE *s, char c); //add to end of line
void expand(LINE *s, int ln);
#endif


141 changes: 70 additions & 71 deletions page.c
Original file line number Diff line number Diff line change
@@ -1,97 +1,96 @@
#include "page.h"


#include "page.h"
void file_too_big(PAGE *p, int size);
void init_page(PAGE *p, char *filename, int size)
{
p->text = (LINE *)malloc(size * sizeof(LINE));

int i;
for(i = 0; i < size; i++)
{
init_line(p->text + i);
}
p->text = malloc(size * sizeof(LINE));
if(p->text == NULL) file_too_big(p, 0);

int i;
for(i = 0; i < size; i++)
{
init_line(p->text + i);
if(p->text + i == NULL) file_too_big(p, i);
}
strcpy(p->filename, filename);
p->numlines = 0;
p->size = size;
p->numlines = size;
p->size = size;


} // init_page

void dest_page(PAGE *p)
{
int i;
for(i = 0; i < p->numlines; i++)
{
free(p->text[i].line); // maybe replace with dest_line()
}
free(p->text);
int i;
for(i = 0; i < p->numlines; i++)
{
free((p->text + i)->line); // maybe replace with dest_line()
}
free(p->text);
} // dest_page



// WARNING: Expansion function implemented but not tested
void insert_line(PAGE *p, int index)
{
if( p->numlines >= p->size ) expand_page(p);

LINE newline;
init_line(&newline);
newline.line[0] = '\0';

int i;

for(i = p->numlines - 1; i >= index; i--)
p->text[i + 1] = p->text[i];

p->text[index] = newline;
(p->numlines)++;
int insert_line(PAGE *p, int index)
{
if(p->numlines + 1 >= p->size) expand_page(p);
if(p->numlines + 1 >= p->size) return 0;

size_t lines = p->numlines - index;
memmove(&p->text[index + 1].line, &p->text[index].line,
lines * sizeof(LINE));

LINE newline;
init_line(&newline);
p->text[index] = newline;
p->numlines++;
return 1;
} // insert_line



void remove_line(PAGE *p, int index)
{
if( p->numlines > 1 )
{
free(p->text[index].line);

int i;
for(i = index; i < p->numlines - 1; i++)
{
p->text[i] = p->text[i + 1];
}
(p->numlines)--;
}
size_t lines;

free(p->text[index].line);
lines = p->numlines - index - 1;
memmove(&p->text[index].line, &p->text[index + 1].line,
lines * sizeof(LINE));
p->numlines--;
} // remove_line


void expand_page(PAGE *p)
{
int newsize = p->size * 2;
LINE *newline = malloc(newsize * sizeof(LINE));

int i;
for(i = 0; i < p->size; i++) // copy old lines
newline[i] = p->text[i];
for(i = p->size; i < newsize; i++) // init new lines
init_line(newline + i);

p->text = newline;
p->size = newsize;
} // expand_page

int newsize = p->size * 2 + 1;
LINE *newline = realloc(p->text, newsize * sizeof(LINE));

if(newline != NULL)
{
p->size = newsize;
p->text = newline;
}
}

// NOTE: This moves the cursor to the end of the displayed text
void print_page(const PAGE *p, int start, int end)
{
int i, line;
for(i = start, line = 0; i < p->numlines && i < end; i++, line++)
{
move(line, 0);
clrtoeol();
printw(" %s", p->text[i].line);
}
if(start < end)
int i, line;
for(i = start; i <= end; i++)
{
move(line, 0);
clrtoeol(); // if we deleted a line this may be necessary
move(line-1, 1);
line = i - start;
move(line, 0);
clrtoeol();
if(i < p->numlines)
printw(" %s", p->text[i].line);
}
refresh();
refresh();
} // print_page


void file_too_big(PAGE *p, int last_line)
{
p->size = last_line;
dest_page(p);
exit(EXIT_FAILURE);
}

11 changes: 6 additions & 5 deletions page.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
#include <ncurses.h> // might have to move this
#include "line.h"

#define PAGE_SIZE 500 /* Default number of lines */
#define PAGE_SIZE 1 /* Default number of lines */
#define WIN_SIZE (LINES - 2) /* Size of window, making room for bottom prompt */
#define NAME_LIMIT 256 /* Max size of a unix filename + 1 */

typedef struct
{
char filename[NAME_LIMIT];
LINE *text; // lines of text
int numlines;
int size; // size of array
LINE *text; // lines of text
int numlines;
int size; // size of array
} PAGE;

void init_page(PAGE *p, char *filename, int size);
void dest_page(PAGE *p);
void insert_line(PAGE *p, int index);
int insert_line(PAGE *p, int index);
void remove_line(PAGE *p, int index);
void expand_page(PAGE *p);
void print_page(const PAGE *p, int start, int end);

#endif

1 change: 1 addition & 0 deletions prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ int prompt_yesno(const char *message)
return !choice; // yes is 0
}


Loading