Refactored: created move functions, separate files for line and page structs, and added a makefile
This commit is contained in:
parent
99353ed222
commit
4f8851bafd
37
line.c
Normal file
37
line.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include "line.h"
|
||||||
|
|
||||||
|
// Insert char into string.
|
||||||
|
void insert_char(LINE *s, char c, int index)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void remove_char(LINE *s, int index)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int len = strlen(s->line);
|
||||||
|
for(i = index; i < len; i++)
|
||||||
|
s->line[i] = s->line[i + 1];
|
||||||
|
} // remove_char
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// expands size of line
|
||||||
|
void expand(LINE *s)
|
||||||
|
{
|
||||||
|
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
|
18
line.h
Normal file
18
line.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef LINE_H
|
||||||
|
#define LINE_H
|
||||||
|
/* LINE struct and functions */
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *line;
|
||||||
|
int size; // size of array, not string
|
||||||
|
} LINE;
|
||||||
|
|
||||||
|
void insert_char(LINE *s, char c, int index); // inserts char to string
|
||||||
|
void remove_char(LINE *s, int index);
|
||||||
|
void expand(LINE *s); // doubles the size of the line
|
||||||
|
|
||||||
|
#endif
|
19
makefile
Normal file
19
makefile
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# makefile for text.c
|
||||||
|
|
||||||
|
CXX=gcc
|
||||||
|
CFLAGS=-Wall -g
|
||||||
|
OBJS=line.o page.o
|
||||||
|
LIBS=-lncurses
|
||||||
|
|
||||||
|
text: text.c text.h $(OBJS)
|
||||||
|
$(CXX) $(CFLAGS) -o text text.c $(OBJS) $(LIBS)
|
||||||
|
|
||||||
|
line.o: line.c line.h
|
||||||
|
$(CXX) $(CFLAGS) -c line.c
|
||||||
|
|
||||||
|
page.o: page.c page.h line.h
|
||||||
|
$(CXX) $(CFLAGS) -c page.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJS) text
|
||||||
|
|
71
page.c
Normal file
71
page.c
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#include "page.h"
|
||||||
|
|
||||||
|
void init_page(PAGE *p)
|
||||||
|
{
|
||||||
|
p->text = (LINE *)malloc(PAGE_SIZE * sizeof(LINE));
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for(i = 0; i < PAGE_SIZE; i++)
|
||||||
|
{
|
||||||
|
p->text[i].line = (char *)malloc(LINE_SIZE * sizeof(char));
|
||||||
|
p->text[i].size = LINE_SIZE;
|
||||||
|
}
|
||||||
|
p->numlines = 0;
|
||||||
|
} // init_page
|
||||||
|
|
||||||
|
void dest_page(PAGE *p)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for(i = 0; i < p->numlines; i++)
|
||||||
|
{
|
||||||
|
free(p->text[i].line);
|
||||||
|
}
|
||||||
|
free(p->text);
|
||||||
|
} // dest_page
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// WARNING: Will not expand once the limit of 500 lines reached
|
||||||
|
void insert_line(PAGE *p, int index)
|
||||||
|
{
|
||||||
|
LINE newline;
|
||||||
|
newline.line = (char *)malloc(LINE_SIZE * sizeof(char));
|
||||||
|
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;
|
||||||
|
} // insert_line
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void remove_line(PAGE *p, int index)
|
||||||
|
{
|
||||||
|
if( p->numlines > 0 )
|
||||||
|
{
|
||||||
|
free(p->text[index].line);
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for(i = index; i < p->numlines - 1; i++)
|
||||||
|
{
|
||||||
|
p->text[i] = p->text[i + 1];
|
||||||
|
}
|
||||||
|
(p->numlines)--;
|
||||||
|
}
|
||||||
|
} // remove_line
|
||||||
|
|
||||||
|
void print_page(const PAGE *p, int start, int end)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for(i = start; i < p->numlines && i < end; i++)
|
||||||
|
{
|
||||||
|
move(i, 1);
|
||||||
|
clrtoeol();
|
||||||
|
printw("%s", p->text[i].line);
|
||||||
|
}
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
24
page.h
Normal file
24
page.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#ifndef PAGE_H
|
||||||
|
#define PAGE_H
|
||||||
|
/* PAGE struct definition and related functions */
|
||||||
|
|
||||||
|
#include <ncurses.h> // might have to move this
|
||||||
|
#include "line.h"
|
||||||
|
|
||||||
|
#define PAGE_SIZE 500 /* Max number of lines. Currently not expandable. */
|
||||||
|
#define LINE_SIZE 128 /* Max characters in a line. Expandable */
|
||||||
|
#define WIN_SIZE LINES - 2 /* Size of window, making room for bottom prompt */
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
LINE *text; // lines of text
|
||||||
|
int numlines;
|
||||||
|
} PAGE;
|
||||||
|
|
||||||
|
void init_page(PAGE *p);
|
||||||
|
void dest_page(PAGE *p);
|
||||||
|
void insert_line(PAGE *p, int index);
|
||||||
|
void remove_line(PAGE *p, int index);
|
||||||
|
void print_page(const PAGE *p, int start, int end);
|
||||||
|
|
||||||
|
#endif
|
152
text.c
152
text.c
@ -8,6 +8,8 @@
|
|||||||
* -Loading
|
* -Loading
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
// TODO: Create move_up, down, left, right functions
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
PAGE page;
|
PAGE page;
|
||||||
@ -17,6 +19,11 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
load_file(argc, argv, &page);
|
load_file(argc, argv, &page);
|
||||||
}
|
}
|
||||||
|
else // initialize
|
||||||
|
{
|
||||||
|
page.text[0].line[0] = '\0';
|
||||||
|
page.numlines = 1;
|
||||||
|
}
|
||||||
|
|
||||||
/* curses interface */
|
/* curses interface */
|
||||||
initscr();
|
initscr();
|
||||||
@ -31,6 +38,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
int y, x;
|
int y, x;
|
||||||
getyx(stdscr, y, x);
|
getyx(stdscr, y, x);
|
||||||
|
//int yoffset = 0; // offset to account for screen scroll
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
@ -40,25 +48,20 @@ int main(int argc, char *argv[])
|
|||||||
case KEY_F(4):
|
case KEY_F(4):
|
||||||
goto end;
|
goto end;
|
||||||
break;
|
break;
|
||||||
|
case KEY_F(5):
|
||||||
|
save_file(argc, argv, &page);
|
||||||
|
break;
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
if( y > 0 ) --y;
|
move_up(&page, &x, &y);
|
||||||
if( x > strlen(page.text[y].line) + 1 ) // cursor adjusts to smaller
|
|
||||||
x = strlen(page.text[y].line) + 1; // lines
|
|
||||||
move(y, x);
|
|
||||||
break;
|
break;
|
||||||
case KEY_DOWN:
|
case KEY_DOWN:
|
||||||
if( y < WIN_SIZE ) ++y;
|
move_down(&page, &x, &y);
|
||||||
if( x > strlen(page.text[y].line) + 1 )
|
|
||||||
x = strlen(page.text[y].line) + 1;
|
|
||||||
move(y, x);
|
|
||||||
break;
|
break;
|
||||||
case KEY_LEFT:
|
case KEY_LEFT:
|
||||||
if(x - 1 > 0)
|
move_left(&x, &y);
|
||||||
move(y, --x);
|
|
||||||
break;
|
break;
|
||||||
case KEY_RIGHT:
|
case KEY_RIGHT:
|
||||||
if(x <= strlen(page.text[y].line))
|
move_right(&page, &x, &y);
|
||||||
move(y, ++x);
|
|
||||||
break;
|
break;
|
||||||
case KEY_DC:
|
case KEY_DC:
|
||||||
case 127: // backspace key...
|
case 127: // backspace key...
|
||||||
@ -66,26 +69,27 @@ int main(int argc, char *argv[])
|
|||||||
if(page.text[y].line[x - 2] == '\0')
|
if(page.text[y].line[x - 2] == '\0')
|
||||||
{
|
{
|
||||||
remove_line(&page, y);
|
remove_line(&page, y);
|
||||||
if( y > 0 ) --y;
|
move_up(&page, &x, &y);
|
||||||
if( x > strlen(page.text[y].line) + 1 )
|
|
||||||
x = strlen(page.text[y].line) + 1;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
remove_char(&page.text[y], x - 2); // why 2?
|
remove_char(&page.text[y], x - 2); // why 2?
|
||||||
if( x > 0 ) x--;
|
move_left(&x, &y);
|
||||||
}
|
}
|
||||||
print_page(&page, 0, WIN_SIZE);
|
print_page(&page, 0, WIN_SIZE);
|
||||||
move(y, x);
|
move(y, x);
|
||||||
refresh();
|
break;
|
||||||
|
case '\n': // newline
|
||||||
|
insert_line(&page, y + 1);
|
||||||
|
print_page(&page, 0, WIN_SIZE);
|
||||||
|
move_down(&page, &x, &y);
|
||||||
break;
|
break;
|
||||||
default: // all other chars
|
default: // all other chars
|
||||||
if( isprint(ch) )
|
if( isprint(ch) )
|
||||||
{
|
{
|
||||||
insert(&page.text[y], ch, x - 1);
|
insert_char(&page.text[y], ch, x - 1);
|
||||||
print_page(&page, 0, WIN_SIZE);
|
print_page(&page, 0, WIN_SIZE);
|
||||||
move(y, ++x);
|
move_right(&page, &x, &y);
|
||||||
refresh();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,6 +100,31 @@ end: endwin();
|
|||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
} // main
|
} // main
|
||||||
|
|
||||||
|
void move_left(int *x, int *y)
|
||||||
|
{
|
||||||
|
if(*x - 1 > 0) move(*y, --(*x));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void move_right(PAGE *p, int *x, int *y)
|
||||||
|
{
|
||||||
|
if(*x <= strlen(p->text[*y].line)) move(*y, ++(*x));
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_up(PAGE *p, int *x, int *y)
|
||||||
|
{
|
||||||
|
if( *y > 0 ) --(*y);
|
||||||
|
if( *x > strlen(p->text[*y].line) + 1 ) // cursor adjusts
|
||||||
|
*x = strlen(p->text[*y].line) + 1; // to smaller lines
|
||||||
|
move(*y, *x);
|
||||||
|
}
|
||||||
|
void move_down(PAGE *p, int *x, int *y)
|
||||||
|
{
|
||||||
|
if( *y < WIN_SIZE ) ++(*y);
|
||||||
|
if( *x > strlen(p->text[*y].line) + 1 )
|
||||||
|
*x = strlen(p->text[*y].line) + 1;
|
||||||
|
move(*y, *x);
|
||||||
|
};
|
||||||
|
|
||||||
void load_file(int argc, char **argv, PAGE *p)
|
void load_file(int argc, char **argv, PAGE *p)
|
||||||
{
|
{
|
||||||
@ -128,6 +157,7 @@ void save_file(int argc, char **argv, PAGE *p)
|
|||||||
while(p->text[line].line[col] != '\0')
|
while(p->text[line].line[col] != '\0')
|
||||||
{
|
{
|
||||||
fputc(p->text[line].line[col], fp);
|
fputc(p->text[line].line[col], fp);
|
||||||
|
col++;
|
||||||
}
|
}
|
||||||
fputc('\n', fp);
|
fputc('\n', fp);
|
||||||
}
|
}
|
||||||
@ -136,85 +166,3 @@ void save_file(int argc, char **argv, PAGE *p)
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
} // save_file
|
} // save_file
|
||||||
|
|
||||||
void init_page(PAGE *p)
|
|
||||||
{
|
|
||||||
p->text = (LINE *)malloc(PAGE_SIZE * sizeof(LINE));
|
|
||||||
|
|
||||||
int i;
|
|
||||||
for(i = 0; i < PAGE_SIZE; i++)
|
|
||||||
{
|
|
||||||
p->text[i].line = (char *)malloc(LINE_SIZE * sizeof(char));
|
|
||||||
p->text[i].size = LINE_SIZE;
|
|
||||||
}
|
|
||||||
p->numlines = 0;
|
|
||||||
} // init_page
|
|
||||||
|
|
||||||
void dest_page(PAGE *p)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
for(i = 0; i < p->numlines; i++)
|
|
||||||
{
|
|
||||||
free(p->text[i].line);
|
|
||||||
}
|
|
||||||
free(p->text);
|
|
||||||
} // dest_page
|
|
||||||
|
|
||||||
// Insert char into string.
|
|
||||||
void insert(LINE *s, char c, int index)
|
|
||||||
{
|
|
||||||
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
|
|
||||||
|
|
||||||
// removes a char from line, if it's the last char '\0'
|
|
||||||
// then it calls remove_line
|
|
||||||
void remove_char(LINE *s, int index)
|
|
||||||
{
|
|
||||||
//if(s->line[index] != '\0')
|
|
||||||
//{
|
|
||||||
int i;
|
|
||||||
int len = strlen(s->line);
|
|
||||||
for(i = index; i < len; i++)
|
|
||||||
s->line[i] = s->line[i + 1];
|
|
||||||
//}
|
|
||||||
} // remove_char
|
|
||||||
|
|
||||||
// expands size of line
|
|
||||||
void expand(LINE *s)
|
|
||||||
{
|
|
||||||
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 remove_line(PAGE *p, int index)
|
|
||||||
{
|
|
||||||
free(p->text[index].line);
|
|
||||||
int i;
|
|
||||||
for(i = index; i < p->numlines - 1; i++)
|
|
||||||
{
|
|
||||||
p->text[i] = p->text[i + 1];
|
|
||||||
}
|
|
||||||
(p->numlines)--;
|
|
||||||
} // remove_line
|
|
||||||
|
|
||||||
void print_page(const PAGE *p, int start, int end)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
for(i = start; i < p->numlines && i < end; i++)
|
|
||||||
{
|
|
||||||
move(i, 1);
|
|
||||||
clrtoeol();
|
|
||||||
printw("%s ", p->text[i].line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
36
text.h
36
text.h
@ -2,35 +2,21 @@
|
|||||||
#define TEXT_H
|
#define TEXT_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h> // declared in line.h
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <string.h>
|
#include <string.h> // declared in line.h
|
||||||
#include <ncurses.h> // -lncurses
|
#include <ncurses.h> // -lncurses. declared in page.h
|
||||||
|
|
||||||
#define PAGE_SIZE 500
|
#include "page.h"
|
||||||
#define LINE_SIZE 128
|
|
||||||
#define WIN_SIZE LINES - 2
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
char *line;
|
|
||||||
int size;
|
|
||||||
} LINE;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
LINE *text; // lines of text
|
|
||||||
int numlines;
|
|
||||||
} PAGE;
|
|
||||||
|
|
||||||
void load_file(int argc, char **argv, PAGE *p);
|
void load_file(int argc, char **argv, PAGE *p);
|
||||||
void save_file(int argc, char **argv, PAGE *p);
|
void save_file(int argc, char **argv, PAGE *p);
|
||||||
void init_page(PAGE *p);
|
|
||||||
void dest_page(PAGE *p);
|
void move_left(int *x, int *y);
|
||||||
void insert(LINE *s, char c, int index); // inserts to string
|
void move_right(PAGE *p, int *x, int *y);
|
||||||
void remove_char(LINE *s, int index);
|
void move_up(PAGE *p, int *x, int *y);
|
||||||
void remove_line(PAGE *p, int index);
|
void move_down(PAGE *p, int *x, int *y);
|
||||||
void expand(LINE *s);
|
|
||||||
void print_page(const PAGE *p, int start, int end);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user