Skip to content

Commit

Permalink
More code cleanup
Browse files Browse the repository at this point in the history
- Made "char*" arguments "const char*" where applicable
- Made arguments for "struct task" and "struct tasklist" pointers where applicable
- Made sure everything worked right after the changes
  • Loading branch information
pjpollina committed Oct 1, 2018
1 parent 3bf49c9 commit 198f108
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
20 changes: 10 additions & 10 deletions src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,30 @@ struct tasklist {
struct task tasks[TASK_LIST_MAX];
};

struct task init_task(char*, unsigned short);
struct tasklist init_tasklist(char*);
bool task_complete(struct task);
bool tasklist_full(struct tasklist);
struct task init_task(const char*, unsigned short);
struct tasklist init_tasklist(const char*);
bool task_complete(struct task*);
bool tasklist_full(struct tasklist*);
int add_task_to_list(struct tasklist*, struct task);
struct task remove_task_from_list(struct tasklist*, int);
int move_task_up(struct tasklist*, int);
int move_task_down(struct tasklist*, int);

int tasklist_menu(struct tasklist*, char*);

int render_header(WINDOW*, struct tasklist);
int render_task(WINDOW*, struct task, int, int);
int render_tasklist(WINDOW*, struct tasklist, int);
int render_header(WINDOW*, struct tasklist*);
int render_task(WINDOW*, struct task*, int, int);
int render_tasklist(WINDOW*, struct tasklist*, int);
int render_ntprompt(WINDOW*, int, int);
int render_line_wipeout(int);

int line_edit_prompt(char*, int, int);
int new_task_prompt(struct tasklist*, int);
int save_prompt(struct tasklist*, int, char*);
int rename_list_prompt(struct tasklist*, int);
bool confirmation_prompt(int, char*);
bool confirmation_prompt(int, const char*);

int write_listfile(struct tasklist*, char*);
int read_listfile(struct tasklist*, char*);
int write_listfile(struct tasklist*, const char*);
int read_listfile(struct tasklist*, const char*);

#endif
10 changes: 5 additions & 5 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "defs.h"

static int path_expansion(char* output, char* filepath) {
static int path_expansion(char* output, const char* filepath) {
if(filepath[0] == '~' && filepath[1] == '/') {
char buffer[4096];
strcpy(buffer, getenv("HOME"));
Expand All @@ -17,7 +17,7 @@ static int path_expansion(char* output, char* filepath) {
return EXIT_SUCCESS;
}

static int get_parent_directory(char* output, char* filepath) {
static int get_parent_directory(char* output, const char* filepath) {
char buffer[4096];
strcpy(buffer, filepath);
int i = strlen(buffer) - 1;
Expand All @@ -31,13 +31,13 @@ static int get_parent_directory(char* output, char* filepath) {
return EXIT_SUCCESS;
}

static int make_parent_directory(char* filepath) {
static int make_parent_directory(const char* filepath) {
char pd[4096];
get_parent_directory(pd, filepath);
return mkdir(pd, 0777);
}

int write_listfile(struct tasklist* tl, char* filepath) {
int write_listfile(struct tasklist* tl, const char* filepath) {
// get full filepath
char full_filepath[4096];
path_expansion(full_filepath, filepath);
Expand All @@ -64,7 +64,7 @@ int write_listfile(struct tasklist* tl, char* filepath) {
return EXIT_SUCCESS;
}

int read_listfile(struct tasklist* tl, char* filepath) {
int read_listfile(struct tasklist* tl, const char* filepath) {
// get full filepath
char full_filepath[4096];
path_expansion(full_filepath, filepath);
Expand Down
6 changes: 3 additions & 3 deletions src/menus.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ int tasklist_menu(struct tasklist* tl, char* filepath) {
int height = getmaxy(body);

// rendering
render_header(header, tl[0]);
render_header(header, tl);
wclear(body);
render_tasklist(body, tl[0], pos);
render_tasklist(body, tl, pos);
wrefresh(body);

// input
Expand Down Expand Up @@ -72,7 +72,7 @@ int tasklist_menu(struct tasklist* tl, char* filepath) {
}
break;
case KEY_RIGHT:
if(!task_complete(tl->tasks[pos])) {
if(!task_complete(&tl->tasks[pos])) {
tl->tasks[pos].reached++;
changed_since_save = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/prompts.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "defs.h"

static int insert_into_string(char* str, char new, int pos) {
static int insert_into_string(char* str, const char new, int pos) {
if(strlen(str) == pos) {
str[pos] = new;
str[pos + 1] = '\0';
Expand Down Expand Up @@ -238,7 +238,7 @@ int rename_list_prompt(struct tasklist* tl, int line) {
return EXIT_SUCCESS;
}

bool confirmation_prompt(int line, char* mesg) {
bool confirmation_prompt(int line, const char* mesg) {
// set up prompt window
WINDOW* prompt = newwin(1, strlen(mesg)+10, line+1, 0);
keypad(prompt, true);
Expand Down
20 changes: 10 additions & 10 deletions src/rendering.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

#include "defs.h"

int render_header(WINDOW* w, struct tasklist tl) {
int render_header(WINDOW* w, struct tasklist* tl) {
// clear window
wclear(w);

// get center x position of window
int mx = getmaxx(w);
int lot = strlen(tl.name);
int lot = strlen(tl->name);
int cpos = (mx / 2) - (lot / 2);

// turn on attributes
wattron(w, A_REVERSE);

// render header
mvwhline(w, 0, 0, ' ', mx);
mvwprintw(w, 0, cpos, "%s", tl.name);
mvwprintw(w, 0, cpos, "%s", tl->name);

// turn off attributes
wattroff(w, A_REVERSE);
Expand All @@ -28,7 +28,7 @@ int render_header(WINDOW* w, struct tasklist tl) {
return EXIT_SUCCESS;
}

int render_task(WINDOW* w, struct task t, int line, int highlight) {
int render_task(WINDOW* w, struct task* t, int line, int highlight) {
// turn on relevant attributes
if(line == highlight) {
wattron(w, A_REVERSE);
Expand All @@ -38,10 +38,10 @@ int render_task(WINDOW* w, struct task t, int line, int highlight) {
}

// render the task appropriately
if(t.goal == 1) {
mvwprintw(w, line, 0, " - %s [%c]", t.desc, (task_complete(t)) ? 'x' : ' ');
if(t->goal == 1) {
mvwprintw(w, line, 0, " - %s [%c]", t->desc, (task_complete(t)) ? 'x' : ' ');
} else {
mvwprintw(w, line, 0, " - %s [%d/%d]", t.desc, t.reached, t.goal);
mvwprintw(w, line, 0, " - %s [%d/%d]", t->desc, t->reached, t->goal);
}

// turn off attributes
Expand All @@ -50,18 +50,18 @@ int render_task(WINDOW* w, struct task t, int line, int highlight) {
return EXIT_SUCCESS;
}

int render_tasklist(WINDOW* w, struct tasklist tl, int pos) {
int render_tasklist(WINDOW* w, struct tasklist* tl, int pos) {
// initialize local variables
int rows = getmaxy(w);
int page = pos / rows;

// render list
for(int i = 0; i < rows; i++) {
if(i+(rows*page) == tl.task_count) {
if(i+(rows*page) == tl->task_count) {
render_ntprompt(w, i, pos % rows);
break;
}
render_task(w, tl.tasks[i+(rows*page)], i, pos % rows);
render_task(w, &tl->tasks[i+(rows*page)], i, pos % rows);
}
return EXIT_SUCCESS;
}
Expand Down
12 changes: 6 additions & 6 deletions src/structs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

#include "defs.h"

struct task init_task(char* desc, unsigned short goal) {
struct task init_task(const char* desc, unsigned short goal) {
struct task t = { .goal = goal, .reached = 0 };
strcpy(t.desc, desc);
return t;
}

struct tasklist init_tasklist(char* name) {
struct tasklist init_tasklist(const char* name) {
struct tasklist tl = { .task_count = 0 };
strcpy(tl.name, name);
return tl;
}

bool task_complete(struct task t) {
return t.reached == t.goal;
bool task_complete(struct task* t) {
return t->reached == t->goal;
}

bool tasklist_full(struct tasklist tl) {
return tl.task_count == TASK_LIST_MAX;
bool tasklist_full(struct tasklist* tl) {
return tl->task_count == TASK_LIST_MAX;
}

int add_task_to_list(struct tasklist* tl, struct task t) {
Expand Down

0 comments on commit 198f108

Please sign in to comment.