void NcursesControl(Connection *conn, const char *file) { int input; void mysighand(int signum) { if (signum == 2) { waddstr(body, "Catching SIGINT\nClosing DB\n"); PREFRESH; DatabaseClose(conn); exit(1); } } signal(SIGINT, mysighand); // user input do { DisplayMode("Mode: Master"); input = getchar(); switch(input) { case '?': NcursesUsage(); break; case 'c': NcursesConsole(conn, file); break; case 'e': RefreshdbList(conn); if (has_colors() == TRUE) wattron(body, A_REVERSE); PREFRESH; NcursesExamine(conn, file); break; case 'o': ReloadListing(); touchwin(body); wrefresh(body); if (has_colors() == TRUE) wattron(body, A_REVERSE); mvwprintw(body, 0, 0, "%s", lsbuf[0]); PREFRESH; NcursesOtherdb(conn, file); break; case 'r': NcursesResize(conn, file); break; default: break; } } while (input != 'q'); }
void Die(const char* message, Const char* conn) { if(errno) { perror(message); } else { printf("ERROR: %s\n", message); } DatabaseClose(conn); exit(1); }
void NcursesResize(Connection *conn, const char *file) { if (has_colors() == TRUE) { start_color(); init_pair(1, COLOR_WHITE, COLOR_BLUE); init_pair(2, COLOR_BLACK, COLOR_CYAN); init_pair(3, COLOR_WHITE, COLOR_MAGENTA); init_color(COLOR_WHITE, 500, 500, 500); init_pair(4, COLOR_BLACK, COLOR_WHITE); init_pair(5, COLOR_BLACK, COLOR_GREEN); init_pair(6, COLOR_CYAN, COLOR_CYAN); bkgd(COLOR_PAIR(2)); attron(A_REVERSE); } curs_set(0); noecho(); getmaxyx(stdscr, maxy, maxx); halfx = maxx >> 1; halfy = maxy >> 1; mvaddstr(maxy - 2, maxx - 17, "Press ? for help"); /* debug mvprintw(maxy - 4, maxx - 17, "maxy = %d", maxy); mvprintw(maxy - 3, maxx - 17, "maxx = %d", maxx); */ refresh(); // title window title = newwin(3 ,maxx - 2, 1, 1); if (title == NULL) { addstr("Unable to allocate memory for title window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(title, COLOR_PAIR(1)); box(title, '|', '='); NcursesCenter(title, 1, banner); wrefresh(title); // body window (y size has to be tweaked by screen size) //border_body = newwin(((2 * maxy) / 3) - 5, maxx - 2, 5, 1); border_body = newwin(((2 * maxy) / 3) - 2, maxx - 2, 5, 1); if (border_body == NULL) { addstr("Unable to allocate memory for border body window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(border_body, COLOR_PAIR(1)); box(border_body, '|', '='); wrefresh(border_body); body = newpad(maxy * 4, maxx - 2); if (body == NULL) { addstr("Unable to allocate memory for body window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(body, COLOR_PAIR(1)); // usage window //border_usage = newwin((maxy / 2) - 5, maxx / 2, /*border_usage = newwin((maxy / 2) + 1, maxx / 2 + 13, (maxy / 4) - 4, (maxx / 4) - 6); */ border_usage = newwin((maxy / 2) - 1, maxx / 2 + 13, (maxy / 4) - 4, (maxx / 4) - 6); if (border_usage == NULL) { addstr("Unable to allocate memory for border usage window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(border_usage, COLOR_PAIR(3)); box(border_usage, '|', '='); /*usage = newwin((maxy / 2) - 1, (maxx / 2) + 11, (maxy / 4) - 3, (maxx / 4) - 5); */ usage = newwin((maxy / 2) - 3, (maxx / 2) + 11, (maxy / 4) - 3, (maxx / 4) - 5); if (usage == NULL) { addstr("Unable to allocate memory for usage window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(usage, COLOR_PAIR(3)); getmaxyx(usage, usage_maxy, usage_maxx); while (*help_ptr) { getyx(usage, usage_y, usage_x); if (*help_ptr == ' ') if (usage_maxx - usage_x < 12) waddch(usage, '\n'); waddch(usage, *help_ptr); help_ptr++; } // console window border_console = newwin(5, maxx / 3, maxy - 8, maxx / 3); if (border_console == NULL) { addstr("Unable to allocate memory for border console window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(border_console, COLOR_PAIR(5)); console = newwin(3, (maxx / 3) - 2, (maxy - 8 + 1), (maxx / 3) + 1); if (console == NULL) { addstr("Unable to allocate memory for console window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(console, COLOR_PAIR(4)); box(console, '*', '*'); // add window add = SUBWINDOWS; if (add == NULL) { addstr("Unable to allocate memory for add window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(add, COLOR_PAIR(1)); box(add, '|', '='); NcursesCenter(add, 0, "Add Record"); // resize window resize = SUBWINDOWS; if (resize == NULL) { addstr("Unable to allocate memory for resize window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(resize, COLOR_PAIR(1)); box(resize, '|', '='); NcursesCenter(resize, 0, "Resize to"); // find window find = SUBWINDOWS; if (find == NULL) { addstr("Unable to allocate memory for find window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(find, COLOR_PAIR(1)); box(find, '|', '='); NcursesCenter(find, 0, "Find"); // create window create = SUBWINDOWS; if (create == NULL) { addstr("Unable to allocate memory for find window"); DatabaseClose(conn); endwin(); exit(1); } wbkgd(create, COLOR_PAIR(1)); box(create, '|', '='); NcursesCenter(create, 0, "Add New Database"); DatabaseList(conn, body); PREFRESH; refresh(); }
void NcursesOtherdb(Connection *conn, const char *file) { Connection *olddb, *newdb, *createdb; int y = 0, down = 0, selection = 0; int dbsize = 10; int signature, input; char action; char create_buf[MAX_DATA]; char *arg1; FILE *checksig; olddb = conn; getyx(body, gety, getx); do { DisplayMode("Mode: Otherdb"); /* debug getyx(body, gety, getx); mvprintw(maxy - 10, maxx - 30, "\ny = %d\nx = %d\ndown = %d\n", y, x, down); mvprintw(maxy - 7, maxx - 30, "\ngety = %d\ngetx = %d\n", gety, getx); mvprintw(maxy - 10, maxx - 30, "\ny = %d\nx = %d\ndown = %d\n", y, x, down); refresh(); */ prefresh(body, down, 0, 6, 2, (maxy / 2) + 8, maxx - 3); input = getchar(); switch(input) { case 'c': touchwin(create); wrefresh(create); echo(); wmove(create, 1, 1); wrefresh(create); wgetnstr(create, create_buf, MAX_DATA); arg1 = strtok(create_buf, " "); action = 'c'; createdb = DatabaseLoad(arg1, &action); DatabaseCreate(createdb, &dbsize); DatabaseWrite(createdb, arg1); DatabaseClose(createdb); conn = olddb; DisplayError("Database has been successfully created"); noecho(); ClearLine(create); ReloadListing(); break; case 'e': checksig = fopen(lsbuf[selection], "r+"); if (checksig == NULL) { DisplayError("ERROR 622: could not open file"); break; } // check signature fread(&signature, sizeof(int), 1, checksig); if (signature != 53281) { DisplayError("ERROR 127: db file not weno"); break; } DatabaseClose(conn); action = 'C'; newdb = DatabaseLoad(lsbuf[selection], &action); DisplayError("Database has been successfully loaded, exit this mode"); ReloadListing(); break; //case KEY_DOWN: case 'j': if (y != (maxy / 2) + 3) y++; wmove(body, y, 0); selection++; ReloadListing(); if (y == (maxy / 2) + 3) down++; if (selection > (lscount) - 1) { selection = 0; y = 0; down = 0; } OtherdbSelection(&selection); break; //case KEY_UP: case 'k': if (y != 0) y--; selection--; ReloadListing(); if ((y == 0) && (down != 0)) down--; if (selection < 0) { selection = (lscount) - 1; y = (lscount + 1) / 2; down = (lscount) / 2; } OtherdbSelection(&selection); break; default: break; } } while (input != 'q'); RefreshdbList(conn); touchwin(body); wrefresh(body); PREFRESH; }
void DatabaseCreate(struct Connection *conn){ for (int i=0; i < MAX_ROWS; i++){ struct Address addr = {.id = i, .set = 0}; conn->db->rows[i] = addr; } } void DatabaseSet(struct Connection *conn, int id, const char *name, const char *email){ struct Address *addr = &conn->db->rows[id]; if(addr->set) die("Allready set, delete it first"); addr->set = 1; char *res = strncpy(addr->name, name, MAX_DATA); if (!res) die("Name copy fialed"); res = strncpy(addr->email, email, MAX_DATA); if (!res) die("Email copy failed"); } void DatabaseGet(struct Connection *conn, int id){ struct Address *addr = &conn->db->rows[id]; if(addr->set){ AddressPrint(addr); } else { die("ID is not set"); } } void DatabaseDelete(struct Connection *conn, int id){ struct Address addr = {.id = id, .set = 0}; conn->db->rows[id] = addr; } void DatabaseList(struct Connection *conn){ struct Database *db = conn->db; for (int i=0; i<MAX_ROWS; i++){ struct Address *cur = &db->rows[i]; if(cur->set){ AddressPrint(cur); } } } int main(int argc, char *argv[]){ if(argc < 3) die("USAGE: ext17 <dbfile> <action> [action params]"); char *filename = argv[2]; char action = argv[1][0]; struct Connection *conn = DatabaseOpen(filename, action); int id = 0; if(argc > 3) id = atoi(argv[3]); if(id >= MAX_ROWS) die("There is not that many records."); switch(action){ case 'c': DatabaseCreate(conn); DatabaseWrite(conn); break; case 'g': if (argc != 4) die("Need an id to get"); DatabaseGet(conn, id); break; case 's': if(argc != 6) die("Need id, name, email to set."); DatabaseSet(conn, id, argv[4], argv[5]); DatabaseWrite(conn); break; case 'd': if(argc != 4) die("Need id to delete"); DatabaseDelete(conn, id); DatabaseWrite(conn); break; case 'l': DatabaseList(conn); break; default: die("Invalid action:\nc=create\ng=get\ns=set\nd=del\nl=list"); } DatabaseClose(conn); return 0; }
void DatabaseCreate(struct Connection* conn ) { int i = 0; for(i = 0; i < MAX_ROWS; i++) { //make a prototype to initialise it struct Address addr = {.id = i, .set =0}; //then just assign it conn->db->rows[i] = addr; } } void DatabaseSet(struct Connection* conn, int id, const char* name, const char* email) { struct Address* addr = &conn->db->rows[id]; if(addr->set) Die("Already set, deleted it first!", conn); addr->set =1; //WARNING: bug, read the "How to break it" section and fix this char* res = strncpy(addr->name,name,MAX_DATA); //demonstrate the strncpy bug if(!res) Die("Name copy failed", conn); res = strncpy(addr->email,email,MAX_DATA); if(!res) Die("Email copy failed", conn); } void DatabaseGet(struct Connection* conn, int id) { struct Address* addr = &conn->db->rows[id]; if(addr->set) { AddressPrint(addr); } else { Die("Id is not set.", conn); } } void DatabaseDelete(struct Connection* conn, int id) { struct Address addr = {.id = id, .set = 0}; conn->db->rows[id] = addr; } void DatabaseList(struct Connection* conn) { int i = 0; struct Database* db = conn->db; for(i=0;i<MAX_ROWS;i++) { struct Address* cur = &db->rows[i]; if(cur->set) AddressPrint(cur); } } int main(int argc, char* argv[]) { if(argc<3) Die("USAGE: ex17 <dbfile> <action> [action params]", conn); char* filename = argv[1]; char action = argv[2][0]; struct Connection* conn = DatabaseOpen(filename, action); int id =0; if(argc>3) id = atoi(argv[3]); if(id>=MAX_ROWS) Die("There's not that many records.", conn); switch(action) { case 'c': DatabaseCreate(conn); DatabaseWrite(conn); break; case 'g': if(argc != 4) Die("Need an id to get", conn); DatabaseGet(conn, id); break; case 's': if(argc != 6) Die("Need id, name, email to set.", conn); DatabaseSet(conn, id, argv[4], argv[5]); DatabaseWrite(conn); break; case 'd': if(argc != 4) Die("Need id to delete", conn); DatabaseDelete(conn, id); DatabaseWrite(conn); break; case 'l': DatabaseList(conn); break; default: Die("Invalid action, only: c=create, g=get, s=set, d=del, l=list", conn); }//end switch action DatabaseClose(conn); return 0; }//end main