void remove_cd() { FILE *titles_fp, *temp_fp; char entry[MAX_ENTRY]; int cat_length; if (current_cd[0] == '\0') return; clear_all_screen(); mvprintw(PROMPT_LINE, 0, "About to remove CD %s: %s. ", current_cat, current_cd); if (!get_confirm()) return; cat_length = strlen(current_cat); titles_fp = fopen(title_file, "r"); temp_fp = fopen(temp_file, "w"); while (fgets(entry, MAX_ENTRY, titles_fp)) { if (strncmp(current_cat, entry, cat_length) != 0) fputs(entry, temp_fp); } fclose(titles_fp); fclose(temp_fp); unlink(title_file); rename(temp_file, title_file); remove_tracks(); current_cd[0] = '\0'; }
void update_cd() { FILE *tracks_fp; char track_name[MAX_STRING]; int len; int track = 1; int screen_line = 1; WINDOW *box_window_ptr; WINDOW *sub_window_ptr; clear_all_screen(); mvprintw(PROMPT_LINE, 0, "Re-entering tracks for CD. "); if(!get_confirm()) return; move(PROMPT_LINE, 0); clrtoeol(); remove_tracks(); mvprintw(MESSAGE_LINE, 0, "Enter a blank line to finish"); tracks_fp = fopen(tracks_file, "a"); box_window_ptr = subwin(stdscr, BOXED_LINES + 2, BOXED_ROWS + 2, BOX_LINE_POS - 1, BOX_ROW_POS - 1); if(!box_window_ptr) return; box(box_window_ptr, ACS_VLINE, ACS_HLINE); sub_window_ptr = subwin(stdscr, BOXED_LINES, BOXED_ROWS, BOX_LINE_POS, BOX_ROW_POS); if(!sub_window_ptr) return; scrollok(sub_window_ptr, TRUE); werase(sub_window_ptr); touchwin(stdscr); do { mvwprintw(sub_window_ptr, screen_line++, BOX_ROW_POS + 2, "Track %d: ", track); clrtoeol(); refresh(); wgetnstr(sub_window_ptr, track_name, MAX_STRING); len = strlen(track_name); if(len > 0 && track_name[len - 1] == '\n') track_name[len - 1] = '\0'; if(*track_name) fprintf(tracks_fp, "%s,%d,%s\n", current_cat, track, track_name); track++; if(screen_line > BOXED_LINES - 1) { /* time to start scrolling */ scroll(sub_window_ptr); screen_line--; } } while(*track_name); delwin(sub_window_ptr); fclose(tracks_fp); }
/* getchoice - ask the user to choose passed: greet, an introduction choices, an array of strings, NULL at end */ int getchoice(char *greet, char *choices[]) { static int selected_row = 0; int max_row = 0; int start_screenrow = MESSAGE_LINE, start_screencol = 10; char **option; int selected; int key = 0; option = choices; while (*option) { max_row++; option++; } /* protect against menu getting shorted when CD deleted */ if (selected_row >= max_row) selected_row = 0; clear_all_screen(); mvprintw(start_screenrow - 2, start_screencol, greet); keypad(stdscr, TRUE); cbreak(); noecho(); key = 0; while (key != 'q' && key != KEY_ENTER && key != '\n') { if (key == KEY_UP) { if (selected_row == 0) selected_row = max_row - 1; else selected_row--; } if (key == KEY_DOWN) { if (selected_row == (max_row - 1)) selected_row = 0; else selected_row++; } selected = *choices[selected_row]; draw_menu(choices, selected_row, start_screenrow, start_screencol); key = getch(); } keypad(stdscr, FALSE); nocbreak(); echo(); if (key == 'q') selected = 'q'; return (selected); }
/* remove the currently selected cd */ void remove_cd() { FILE *titles_fp, *temp_fp; char entry[MAX_ENTRY]; int cat_length; // skip if there's no selected cd if (current_cd[0] == '\0') { return; } clear_all_screen(); // prompt for confirmation first mvprintw(PROMPT_LINE, 0, "About to remove CD %s: %s. ", current_cat, current_cd); if (!get_confirm()) { return; } // open files titles_fp = fopen(TITLE_FILE, "r"); if (!titles_fp) return; temp_fp = fopen(TEMP_FILE, "w"); if (!temp_fp) return; // write all entries from exiting file to new file, except the one(s?) // matching the current_cat. cat_length = strlen(current_cat); while (fgets(entry, MAX_ENTRY, titles_fp)) { if (strncmp(current_cat, entry, cat_length) != 0) { fputs(entry, temp_fp); } } // close files and swap the temp file for the tracks file fclose(titles_fp); fclose(temp_fp); unlink(TITLE_FILE); rename(TEMP_FILE, TITLE_FILE); // do pretty much the same operations on the tracks file remove_tracks(); // reset current_cd. // we should probably reset current_cat too to be consistent, but // all the code checks current_cd, so this is ok-ish current_cd[0] = '\0'; }
/* top-level function for adding a record */ void add_record() { char catalog_number[MAX_STRING]; char cd_title[MAX_STRING]; char cd_type[MAX_STRING]; char cd_artist[MAX_STRING]; char cd_entry[MAX_STRING * 4 + 8]; int screenrow = MESSAGE_LINE; int screencol = 10; clear_all_screen(); // heading mvprintw(screenrow, screencol, "Enter new CD details"); screenrow += 2; // print prompt and get each response mvprintw(screenrow, screencol, " Catalog number: "); get_string(catalog_number); screenrow++; mvprintw(screenrow, screencol, " CD title: "); get_string(cd_title); screenrow++; mvprintw(screenrow, screencol, " CD type: "); get_string(cd_type); screenrow++; mvprintw(screenrow, screencol, " CD artist: "); get_string(cd_artist); screenrow++; // make a single entry string with all the data sprintf(cd_entry, "%s, %s, %s, %s", catalog_number, cd_title, cd_type, cd_artist); // get confirmation before saving mvprintw(PROMPT_LINE - 2, 5, "About to add this new entry:"); mvprintw(PROMPT_LINE, 5, cd_entry); refresh(); move(PROMPT_LINE, 0); if (get_confirm()) { // save in database file, and update global constants to reflect the // 'active' cd. insert_entry(cd_entry); strcpy(current_cd, cd_title); strcpy(current_cat, catalog_number); }; }
void add_record() { char catalog_number[MAX_STRING]; char cd_title[MAX_STRING]; char cd_type[MAX_STRING]; char cd_artist[MAX_STRING]; char cd_entry[MAX_STRING]; int screenrow = MESSAGE_LINE; int screencol = 10; clear_all_screen(); mvprintw(screenrow, screencol, "Enter new CD details"); screenrow += 2; mvprintw(screenrow, screencol, "Catalog Number: "); get_string(catalog_number); screenrow++; mvprintw(screenrow, screencol, " CD Title: "); get_string(cd_title); screenrow++; mvprintw(screenrow, screencol, " CD Type: "); get_string(cd_type); screenrow++; mvprintw(screenrow, screencol, " Artist: "); get_string(cd_artist); screenrow++; mvprintw(PROMPT_LINE-2, 5, "About to add this new entry: "); sprintf(cd_entry, "%s, %s, %s, %s", catalog_number, cd_title, cd_type, cd_artist); mvprintw(PROMPT_LINE, 5, "%s", cd_entry); refresh(); move(PROMPT_LINE, 5); refresh(); move(PROMPT_LINE, 0); if (get_confirm()) { insert_title(cd_entry); strcpy(current_cd, cd_title); strcpy(current_cat, catalog_number); } }
void remove_cd() { FILE *titles_fp, *temp_fp; char entry[MAX_ENTRY]; int cat_length; if (current_cd[0] == '\0') { return; } clear_all_screen(); mvprintw(PROMPT_LINE, 0, "About to remove CD %s: %s.", current_cat, current_cd); if (!get_confirm()) { return; } //todo whey current_cd remove all? cat_length = strlen(current_cat); /* Copy the titles file to a tempory, ignoring this CD */ titles_fp = fopen(TITLE_FILE, "r"); temp_fp = fopen(temp_file, "w"); while (fgets(entry, MAX_ENTRY, titles_fp)) { /* Compare catalog number and copy entry if no match */ if (strncmp(current_cat, entry, cat_length) != 0) { fputs(entry, temp_fp); } } fclose(titles_fp); fclose(temp_fp); /* Delete the titles file, and rename the temporary file */ unlink(TITLE_FILE); rename(temp_file, TITLE_FILE); /* Now do the same for the tracks file */ remove_tracks(); /* Reset current CD to 'None' */ current_cd[0] = '\0'; }
static int handle_del(t_elem **list) { t_elem *tmp; int nbr_elem; int current_max_len; current_max_len = list_get_max_len(*list); nbr_elem = (*list)->prev->pos_list; if (nbr_elem == 1) return (1); tmp = get_current_elem(*list); if (tmp->pos_list == 1) (*list) = (*list)->next; tmp->prev->next = tmp->next; tmp->next->prev = tmp->prev; tmp->next->cursor = 1; list_update_pos(*list, tmp->pos_list); if (current_max_len != list_get_max_len(*list)) check_window_size(*list); ft_memdel((void **)&tmp); clear_all_screen(); return (0); }
void list_tracks() { FILE *tracks_fp; char entry[MAX_ENTRY]; int cat_length; int lines_op = 0; WINDOW *track_pad_ptr; int tracks; int key; int first_line = 0; if (current_cd[0] == '\0') { mvprintw(ERROR_LINE, 0, "You must select a CD first. ", stdout); get_return(); return; } clear_all_screen(); cat_length = strlen(current_cat); tracks_fp = fopen(tracks_file, "r"); if (!tracks_fp) return; while (fgets(entry, MAX_ENTRY, tracks_fp)) { if (strncmp(current_cat, entry, cat_length) == 0) tracks++; } fclose(tracks_fp); track_pad_ptr = newpad(tracks + 1 + BOXED_LINES, BOXED_ROWS + 1); if (!track_pad_ptr) return; tracks_fp = fopen(tracks_file, "r"); if (!tracks_fp) return; mvprintw(4, 0, "CD Track Listing\n"); while (fgets(entry, MAX_ENTRY, tracks_fp)) { if (strncmp(current_cat, entry, cat_length) == 0) { mvwprintw(track_pad_ptr, lines_op++, 0, "%s", entry + cat_length + 1); } } fclose(tracks_fp); if (lines_op > BOXED_LINES) { mvprintw(MESSAGE_LINE, 0, "Cursor keys to scroll, RETURN or q to exit"); } else { mvprintw(MESSAGE_LINE, 0, "RETURN or q to exit"); } wrefresh(stdscr); keypad(stdscr, TRUE); cbreak(); noecho(); key = 0; while (key != 'q' && key != KEY_ENTER && key != '\n') { if (key == KEY_UP) { if (first_line > 0) first_line--; } if (key == KEY_DOWN) { if (first_line + BOXED_LINES + 1 < tracks) first_line++; } prefresh(track_pad_ptr, first_line, 0, BOX_LINE_POS, BOX_ROW_POS, BOX_LINE_POS + BOXED_LINES, BOX_ROW_POS + BOXED_ROWS); key = getch(); } delwin(track_pad_ptr); keypad(stdscr, FALSE); nocbreak(); echo(); }
/* list_tracks - list the tracks for the current CD */ void list_tracks() { FILE *tracks_fp; char entry[MAX_ENTRY]; int cat_length; int lines_op = 0; WINDOW *track_pad_ptr; int tracks = 0; int key; int first_line = 0; if (current_cd[0] == '\0') { mvprintw(ERROR_LINE, 0, "You must select a CD first. ", stdout); get_return(); return; } clear_all_screen(); cat_length = strlen(current_cat); /* First count the number of tracks for the current CD */ tracks_fp = fopen(tracks_file, "r"); if (!tracks_fp) return; while (fgets(entry, MAX_ENTRY, tracks_fp)) { if (strncmp(current_cat, entry, cat_length) == 0) tracks++; } fclose(tracks_fp); /* Make a new pad, ensure that even if there is only a single track the PAD is large enough so the later prefresh() is always valid. */ track_pad_ptr = newpad(tracks + 1 + BOXED_LINES, BOXED_ROWS + 1); if (!track_pad_ptr) return; tracks_fp = fopen(tracks_file, "r"); if (!tracks_fp) return; mvprintw(4, 0, "CD Track Listing\n"); /* write the track information into the pad */ while (fgets(entry, MAX_ENTRY, tracks_fp)) { /* Compare catalog number and output rest of entry */ if (strncmp(current_cat, entry, cat_length) == 0) { mvwprintw(track_pad_ptr, lines_op++, 0, "%s", entry + cat_length + 1); } } fclose(tracks_fp); if (lines_op > BOXED_LINES) { mvprintw(MESSAGE_LINE, 0, "Cursor keys to scroll, RETURN or q to exit"); } else { mvprintw(MESSAGE_LINE, 0, "RETURN or q to exit"); } wrefresh(stdscr); keypad(stdscr, TRUE); cbreak(); noecho(); key = 0; while (key != 'q' && key != KEY_ENTER && key != '\n') { if (key == KEY_UP) { if (first_line > 0) first_line--; } if (key == KEY_DOWN) { if (first_line + BOXED_LINES + 1 < tracks) first_line++; } /* now draw the appropriate part of the pad on the screen */ prefresh(track_pad_ptr, first_line, 0, BOX_LINE_POS, BOX_ROW_POS, BOX_LINE_POS + BOXED_LINES, BOX_ROW_POS + BOXED_ROWS); /* wrefresh(stdscr); */ key = getch(); } delwin(track_pad_ptr); keypad(stdscr, FALSE); nocbreak(); echo(); }
// Functions from admin account void book_manage() { int day,sl,size=0,c=0,com,choice,menu_choice; int day_n,com_n,sl_n; char **options,buff[15],*user_id; char* menu[] = {"dDelete booking", "cChange booking", "bBack", 0, }; struct tm tim,today; time_t now= time(0); user_id= malloc(15*sizeof(char)); options= malloc(200*sizeof(char*)); for(c=0;c<100;c++) options[c]= malloc(20*sizeof(char)); today= tim = *(localtime(&now)); for(com=1;com<=MAX_COMP_P;com++) { now= time(0); tim= *(localtime(&now)); for(day=0;day<3;day++) { if(day!=0) now+= 60*60*24; tim= *(localtime(&now)); for(sl=0;sl<5;sl++) { user_id= user_free_compp(day,com,sl); strcpy(options[3*size],"Comp id: "); options[3*size][8]= com+48; strftime(buff,15,"%d/%m/%y" ,&tim); strcpy(options[3*size+1],"Slot no. "); options[3*size+1][8]= sl+49; strcat(options[3*size+1],buff); strcpy(options[3*size+2],"User: "******"What to do you want to do with selected computer",menu); switch(menu_choice){ case 'b': break; case 'd': if(!get_confirm()) return; else update_compp(com_n,"0",day_n,sl_n); break; case 'c': mvprintw(13,10,"Enter user name: "); scanw("%s",user_id); if(!get_confirm()) return; update_compp(com_n,user_id,day_n,sl_n); break; } endwin(); }
void update_cd() { FILE *tracks_fp; char track_name[MAX_STRING]; int len; int track = 1; int screen_line = 1; WINDOW *box_window_ptr; WINDOW *sub_window_ptr; clear_all_screen(); mvprintw(PROMPT_LINE, 0, "Re-entering tracks for CD."); if (!get_confirm()) { return; } move(PROMPT_LINE, 0); clrtoeol(); remove_tracks(); mvprintw(MESSAGE_LINE, 0, "Enter a blank line to finish"); tracks_fp = fopen(TRACKS_FILE, "a"); /* Just to show how, enter the information in a scrolling, boxed, window. The trick is to set-up a sub-window, draw a box around the edge, then add a new, scrolling, sub-window just inside the boxed sub-window. */ box_window_ptr = subwin(stdscr, BOXED_LINES + 2, BOXED_ROWS + 2, BOX_LINE_POS - 1, BOX_ROW_POS - 1); if (!box_window_ptr) { return; } box(box_window_ptr, ACS_VLINE, ACS_HLINE); sub_window_ptr = subwin(stdscr, BOXED_LINES, BOXED_ROWS, BOX_LINE_POS, BOX_ROW_POS); if (!sub_window_ptr) { return; } scrollok(sub_window_ptr, TRUE); werase(sub_window_ptr); touchwin(stdscr); do { //todo mvprintw mvwprintw(sub_window_ptr, screen_line++, BOX_ROW_POS + 2, "Track %d: ", track); clrtoeol(); refresh(); wgetnstr(sub_window_ptr, track_name, MAX_STRING); len = strlen(track_name); if (len > 0 && track_name[len - 1 ] == '\n') { track_name[len - 1] = '\0'; } if (*track_name) { fprintf(tracks_fp, "%s,%d,%s\n", current_cat, track, track_name); } track++; if (screen_line > BOXED_LINES - 1) { /*time to start scrolling*/ scroll(sub_window_ptr); screen_line--; } } while (*track_name); delwin(sub_window_ptr); fclose(tracks_fp); }
/* This function can only be called if a cd has already been selected and * stored in the globals `current_cd` and `current_cat`. */ void list_tracks() { FILE *tracks_fp; char entry[MAX_ENTRY]; int cat_length; int lines_op = 0; WINDOW *track_pad; int ntracks = 0; int key; int first_line = 0; // handle the case of no current cd if (current_cd[0] == '\0') { mvprintw(ERROR_LINE, 0, "You must select a CD before listing tracks"); get_return(); return; } clear_all_screen(); // First, count the number of tracks for the current CD. close the // file when done so we can reopen before listing them. cat_length = strlen(current_cat); tracks_fp = fopen(TRACKS_FILE, "r"); if (!tracks_fp) return; while (fgets(entry, MAX_ENTRY, tracks_fp)) { if (strncmp(current_cat, entry, cat_length) == 0) { ntracks++; } } fclose(tracks_fp); // make a new pad. Make sure it is big enough to fill up the boxed region // even if there are no listings. // NOTE: we don't actually make a box for this operation... the box is // only used in update_cd. But we are reusing the same region. track_pad = newpad(ntracks + 1 + BOXED_LINES, BOXED_COLS + 1); if (!track_pad) return; // reopen the tracks file tracks_fp = fopen(TRACKS_FILE, "r"); if (!tracks_fp) return; // print out all the tracks onto the pad. mvprintw(4, 0, "CD Track Listing\n"); while (fgets(entry, MAX_ENTRY, tracks_fp)) { if (strncmp(current_cat, entry, cat_length) == 0) { // print the track info. Use `cat_length+1` bc of the comma // this actually is kind of strange because we are still // printing a kind of ugly comma-separated track number and // name, but it works. You could make it better as an exercise. mvwprintw(track_pad, lines_op++, 0, "%s", entry + cat_length + 1); } } fclose(tracks_fp); // tell the user what to do if (lines_op > BOXED_LINES) { mvprintw(MESSAGE_LINE, 0, "Arrows to scroll, RETURN or q to exit."); } else { mvprintw(MESSAGE_LINE, 0, "RETURN or q to exit."); } // set up the control keys and such, refresh the main screen wrefresh(stdscr); keypad(stdscr, TRUE); cbreak(); noecho(); // start up a loop over control keys; inside the loop refresh the pad key = 0; while (key != '\n' && key != KEY_ENTER && key != 'q') { if (key == KEY_UP) { if (first_line > 0) { first_line--; } } else if (key == KEY_DOWN) { if (first_line + BOXED_LINES < ntracks - 1) { first_line++; } } prefresh(track_pad, first_line, 0, BOX_LINE_POS, BOX_COL_POS, BOX_LINE_POS + BOXED_LINES, BOX_COL_POS + BOXED_COLS); key = getch(); } }
/* Update the tracks in a cd. Uses a boxed scrolling window, which is worth * a note: if you try to box a window and then sroll, the box itself will * scroll b/c it's inside the bounds. To deal with this, boxed windows that * need to scroll should be implemented as two nested windows. */ void update_cd() { FILE *tracks_fp; char track_name[MAX_STRING]; int len; int track_num = 1; int screen_line = 1; WINDOW *box_window; WINDOW *sub_window; clear_all_screen(); // get confirmation before we delete any old tracks and add new ones. mvprintw(PROMPT_LINE, 0, "Re-entering tracks for CD."); if (!get_confirm()) { return; } move(PROMPT_LINE, 0); clrtoeol(); clear_all_screen(); // go ahead and remove tracks, print prompt, and create the FILE remove_tracks(); mvprintw(MESSAGE_LINE, 0, "Enter a blank line to finish"); tracks_fp = fopen(TRACKS_FILE, "a"); // set up the nexted windows. Note the box has +2 on all dims // also note that the subwindow isn't impmlemented as a sub window of the // box_window, but rather of stdscr. Not sure if this matters. box_window = subwin(stdscr, BOXED_LINES + 2, BOXED_COLS + 2, BOX_LINE_POS - 1, BOX_COL_POS -1); if (!box_window) { // "error handling" lol return; } sub_window = subwin(stdscr, BOXED_LINES, BOXED_COLS, BOX_LINE_POS, BOX_COL_POS); if (!sub_window) { // more "error handling" return; } // initialize the windows box(box_window, ACS_VLINE, ACS_HLINE); scrollok(sub_window, TRUE); werase(sub_window); touchwin(stdscr); do { mvwprintw(sub_window, screen_line++, 3, "Track %d: ", track_num); clrtoeol(); refresh(); // get the track name and strip the '\n'. If they entered a blank line, // the first character will e '\0', which is key in the `ifs` to follow wgetnstr(sub_window, track_name, MAX_STRING); len = strlen(track_name); if (len > 0 && track_name[len - 1] == '\n') { track_name[len - 1] = '\0'; } // if the track name doesn't start with 0, write it to file. Then incr // track_num if (*track_name) { fprintf(tracks_fp, "%s,%d,%s\n", current_cat, track_num, track_name); } track_num++; // scroll down if need be (refresh in the next loop) if (screen_line > BOXED_LINES - 1) { scroll(sub_window); screen_line--; } } while (*track_name); // clean up delwin(sub_window); delwin(box_window); // the authors seem to have forgotten this fclose(tracks_fp); }
int getchoice(char *greet, char *choices[]) { static int selected_row = 0; // static so we can re-highlight choice int max_row = 0; int start_screenrow = MESSAGE_LINE, start_screencol = 10; char **option; int selected; int key = 0; // determine the number of rows option = choices; while (*option) { max_row++; option++; } /* deal with the case where menu gets shorter after a cd is deleted */ if (selected_row >= max_row) { selected_row = 0; } clear_all_screen(); mvprintw(start_screenrow - 2, start_screencol, greet); // turn on keypad, cbreak, noecho keypad(stdscr, TRUE); cbreak(); noecho(); // loop over the menu redraw until the press 'q' or until they have // a choice highlighted and press enter. key = 0; while (key != 'q' && key != KEY_ENTER && key != '\n') { // move the selection up or down. Wrap across line 0. if (key == KEY_UP) { if (selected_row == 0) { selected_row = max_row - 1; } else { selected_row--; } } else if (key == KEY_DOWN) { if (selected_row == max_row - 1) { selected_row = 0; } else { selected_row++; } } // [] has higher precedence than *, so this is the same as // choices[selected_row][0]. selected = *choices[selected_row]; // draw the menu, then get the next key. Note initial case is handled. draw_menu(choices, selected_row, start_screenrow, start_screencol); key = getch(); } // turn off keypad, cbreak, noecho keypad(stdscr, TRUE); nocbreak(); echo(); return selected; }