Skip to content

Commit be1777d

Browse files
committed
Cleaned up last bit...added documentation, made sure everything is functional...implemented argv file-open
1 parent 5e08929 commit be1777d

6 files changed

Lines changed: 119 additions & 73 deletions

File tree

edit.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,37 +65,45 @@ struct File
6565
bool saved;
6666
};
6767

68-
struct Change
68+
/*struct Change //Not implemented, possibly later
6969
{
7070
int line; //Line of the change
7171
char *ot; //Old text of the line
7272
char *nt; //New text of the line
7373
struct Change *prev;
7474
struct Change *next;
75-
};
75+
};*/
7676

7777
extern int mode, svdmd;
7878
extern char ln[NAME_MAX];
7979

8080
//Define our functions
81+
82+
//print.c
8183
void initialize_editor(); //Initialize NCurses and color mode
8284
void border_line_print(int, char *, struct Window *); //Print a Cyan line at row "r" with text "t", with restrictions of Window "w"
8385
void content_line_print(int, struct Window *, struct Cursor *); //Print line at row "r" with text "t", with restrictions and contents of Window "w", using cursor position x,y
84-
void print_editor(struct Window *); //Prints the editor sans file contents
8586
void print_contents(struct Window *, struct Cursor *, struct File *); //Prints the inner contents of the editor
87+
void dialog(struct Window *, char *, char *);
88+
bool msg_box(struct Window *, char *);
89+
void run_mode(int, struct Window *, struct Cursor *, struct File *);
90+
91+
//text.c
8692
int get_line_number_pos(int, char *); //Gets the position of the line number 'line' in text 'text'
8793
int get_end_of_line(int, char *);
88-
void get_input(struct Window *, struct Cursor *, struct File *);
89-
void key_left(struct Window *, struct Cursor *);
94+
unsigned long long array_size(int);
9095
void ins_char(int, char, char *);
9196
void del_char(struct Window *, struct Cursor *);
9297
void del_line(int, char *);
93-
void dialog(struct Window *, char *, char *);
94-
bool msg_box(struct Window *, char *);
98+
99+
//input.c
100+
void get_input(struct Window *, struct Cursor *, struct File *);
101+
void key_left(struct Window *, struct Cursor *);
95102
int dialog_input(struct Window *, char *);
103+
104+
//file.c
96105
void open(struct Window *, struct File *);
97106
void save(struct Window *, struct File *);
98107
void new(struct Window *, struct File *, struct Cursor *);
99-
unsigned long long array_size(int);
100108

101109
#endif /* EDIT_H */

file.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
void open(struct Window *w, struct File *f)
2626
/*Opens a file, copies the contents, and
27-
*assesses file permissions*/
27+
*assesses file permissions
28+
*/
2829
{
2930
struct stat f_stat;
3031
f->fp = fopen(f->path, "r");
@@ -50,6 +51,7 @@ void open(struct Window *w, struct File *f)
5051
}
5152

5253
void save(struct Window *w, struct File *f)
54+
//Saves the file. If it cannot - calls SAVE_AS
5355
{
5456
f->fp = fopen(f->path, "w");
5557

@@ -64,6 +66,7 @@ void save(struct Window *w, struct File *f)
6466
}
6567

6668
void new(struct Window *w, struct File *f, struct Cursor *c)
69+
//Creates a blank file
6770
{
6871
f->fp=NULL;
6972
f->ro=true;

input.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,19 @@ void get_input(struct Window *w, struct Cursor *c, struct File *f)
161161
if (k!='\n')
162162
c->x++;
163163
else
164-
c->y++;
164+
{
165+
c->x=0; c->y++;
166+
}
165167
f->saved=false;
166168
break;
167169
}
168170
}
169171

170172
int dialog_input(struct Window *w, char *str)
173+
/*When recieving input from a prompt
174+
*as opposed to the regular EDIT_MODE
175+
*use this function instead of get_input
176+
*/
171177
{
172178
int i=strlen(str);
173179
int k;

main.c

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -39,69 +39,18 @@ int main(int argc, char *argv[])
3939
struct File *f = malloc(sizeof(struct File));
4040
f->saved=true;
4141

42-
struct Change *ch = malloc(sizeof(struct Change));
42+
if (argc)
43+
{
44+
strcpy(f->path, argv[1]);
45+
open(w, f);
46+
}
4347

48+
//Initialize curses
4449
initialize_editor();
45-
while(1)
50+
while(1) //Run until user quits application
4651
{
47-
cbreak();
48-
curs_set(1);
4952
getmaxyx(stdscr, w->height, w->width); //Get the dimensions of current screen (stdscr) and place those values into the Window structure
50-
51-
switch (mode)
52-
{
53-
case EDIT_MODE:
54-
dialog(w, "[Esc] Quit [F1] Save [F2] Save as [F3] Open [F4] New [F5] Delete line", "Unnamed Text Editor");
55-
print_contents(w, c, f);
56-
get_input(w, c, f);
57-
break;
58-
case NOT_SAVED:
59-
if(msg_box(w, "File not saved! Do you want to continue?"))
60-
{
61-
strcpy(f->path, "");
62-
mode=svdmd;
63-
}
64-
else
65-
mode=EDIT_MODE;
66-
break;
67-
case OPEN_FILE:
68-
dialog(w, "File path:", "Open File");
69-
if(!dialog_input(w,f->path))
70-
{
71-
open(w,f);
72-
mode=EDIT_MODE;
73-
}
74-
break;
75-
case NEW_FILE:
76-
dialog(w, "File path:", "New File");
77-
if(!dialog_input(w,f->path))
78-
{
79-
new(w,f,c);
80-
if (f->fp = fopen(f->path, "w"))
81-
{
82-
f->ro=false;
83-
fclose(f->fp);
84-
}
85-
mode=EDIT_MODE;
86-
}
87-
break;
88-
case DEL_LINE:
89-
dialog(w, "Line number:", "Delete Line");
90-
if(!dialog_input(w,ln))
91-
{
92-
del_line(atoi(ln)-1, w->contents);
93-
mode=EDIT_MODE;
94-
}
95-
break;
96-
case SAVE_AS:
97-
dialog(w, "File path:", "Save As");
98-
if(!dialog_input(w,f->path))
99-
{
100-
save(w, f);
101-
mode=EDIT_MODE;
102-
}
103-
break;
104-
}
53+
run_mode(mode, w, c, f); //Print to the console according to our mode of operation
10554
refresh();
10655
clear();
10756
}

print.c

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
*/
2323
#include "edit.h"
2424

25-
void initialize_editor() //Initialize NCurses
25+
void initialize_editor()
26+
//Initialize curses
2627
{
2728
initscr(); //Initialize screen and curses mode
2829
if (has_colors() == FALSE) //Check to see if the console supports colored print, if not, then execute the following segment
@@ -35,10 +36,14 @@ void initialize_editor() //Initialize NCurses
3536
init_pair(EDITOR_SCHEME, COLOR_BLACK, COLOR_WHITE); //Initialize color pair of black text on a white background (for editor)
3637
init_pair(BOUNDARY_SCHEME, COLOR_BLACK, COLOR_CYAN); //Initialize color pair of black text on a cyan background (for top and bottom boundaries)
3738
keypad(stdscr, TRUE);
38-
noecho();
39+
cbreak();
40+
curs_set(1);
3941
}
4042

41-
void border_line_print(int r, char *t, struct Window *w) //Print a Cyan line at row "r" with text "t", with restrictions of Window "w"
43+
void border_line_print(int r, char *t, struct Window *w)
44+
/*Print a Cyan line at row "r" with text "t", with
45+
*restrictions of Window "w"
46+
*/
4247
{
4348
attrset(COLOR_PAIR(BOUNDARY_SCHEME)); //Turn on attribute for the boundary color scheme
4449
move(r, 0); //Set cursor to the beginning of the line for the y-position "row"
@@ -50,7 +55,10 @@ void border_line_print(int r, char *t, struct Window *w) //Print a Cyan line at
5055
move(r, 0); //Move the cursor back to the beginning of the line of the first row printed
5156
}
5257

53-
void content_line_print(int r, struct Window *w, struct Cursor *c) //Print line at row "r" with text "t", with restrictions and contents of Window "w", using cursor position x,y
58+
void content_line_print(int r, struct Window *w, struct Cursor *c)
59+
/*Print line at row "r" with text "t", with restrictions and
60+
*contents of Window "w", using cursor position x,y
61+
*/
5462
{
5563
long i = 0, j;
5664

@@ -73,6 +81,7 @@ void content_line_print(int r, struct Window *w, struct Cursor *c) //Print line
7381
}
7482

7583
void print_contents(struct Window *w, struct Cursor *c, struct File *f)
84+
//Used to print all of the regular editor contents
7685
{
7786
int i=0;
7887
while (i<w->height-2)
@@ -84,6 +93,9 @@ void print_contents(struct Window *w, struct Cursor *c, struct File *f)
8493
}
8594

8695
bool msg_box(struct Window *w, char *msg)
96+
/*A very cimple continue/cancel message
97+
*window
98+
*/
8799
{
88100
attrset(COLOR_PAIR(BOUNDARY_SCHEME));
89101
move(0,0);
@@ -104,6 +116,11 @@ bool msg_box(struct Window *w, char *msg)
104116
}
105117

106118
void dialog(struct Window *w, char *prompt, char *msg)
119+
/*Prints a string msg on the top line
120+
*and prompt on the bottom. Used equally
121+
*for drawing the regular editor as well
122+
*as for various prompts
123+
*/
107124
{
108125
attrset(COLOR_PAIR(EDITOR_SCHEME));
109126
move(0, 0);
@@ -114,3 +131,64 @@ void dialog(struct Window *w, char *prompt, char *msg)
114131
move(w->height-1, strlen(prompt));
115132
}
116133

134+
void run_mode(int m, struct Window *w, struct Cursor *c, struct File *f)
135+
/*Prints to the console the coresponding output
136+
*based upon the current given mode of operation
137+
*/
138+
{
139+
switch (m)
140+
{
141+
case EDIT_MODE:
142+
dialog(w, "[Esc] Quit [F1] Save [F2] Save as [F3] Open [F4] New [F5] Delete line", "Unnamed Text Editor");
143+
print_contents(w, c, f);
144+
get_input(w, c, f);
145+
break;
146+
case NOT_SAVED:
147+
if(msg_box(w, "File not saved! Do you want to continue?"))
148+
{
149+
strcpy(f->path, "");
150+
m=svdmd;
151+
}
152+
else
153+
m=EDIT_MODE;
154+
break;
155+
case OPEN_FILE:
156+
dialog(w, "File path:", "Open File");
157+
if(!dialog_input(w,f->path))
158+
{
159+
open(w,f);
160+
m=EDIT_MODE;
161+
}
162+
break;
163+
case NEW_FILE:
164+
dialog(w, "File path:", "New File");
165+
if(!dialog_input(w,f->path))
166+
{
167+
new(w,f,c);
168+
if (f->fp = fopen(f->path, "w"))
169+
{
170+
f->ro=false;
171+
fclose(f->fp);
172+
}
173+
m=EDIT_MODE;
174+
}
175+
break;
176+
case DEL_LINE:
177+
dialog(w, "Line number:", "Delete Line");
178+
if(!dialog_input(w,ln))
179+
{
180+
del_line(atoi(ln)-1, w->contents);
181+
m=EDIT_MODE;
182+
}
183+
break;
184+
case SAVE_AS:
185+
dialog(w, "File path:", "Save As");
186+
if(!dialog_input(w,f->path))
187+
{
188+
save(w, f);
189+
m=EDIT_MODE;
190+
}
191+
break;
192+
}
193+
}
194+

text.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ void ins_char(int pos, char k, char *str)
9494
}
9595

9696
void del_char(struct Window *w, struct Cursor *c)
97+
//Deletes a given character
9798
{
9899
key_left(w,c);
99100
for(int pos=c->abs;w->contents[pos]!='\0'; pos++) w->contents[pos]=w->contents[pos+1];
100101
}
101102

102103
void del_line(int line, char *text)
104+
//Deletes a given line
103105
{
104106
int start = get_line_number_pos(line, text);
105107
int diff = get_end_of_line(start, text)-start+1;

0 commit comments

Comments
 (0)