📄 app_ui.c
字号:
printf(" a single d to delete this and remaining tracks,\n"); printf(" or new track description\n"); while(1) {/* First, we must check whether a track already exists with the current track number. Depending on what we find, we change the prompt. */ memset(&new_track, '\0', sizeof(new_track)); existing_track = get_cdt_entry(entry_to_add_to->catalog, track_no); if (existing_track.catalog[0]) { printf("\tTrack %d: %s\n", track_no, existing_track.track_txt); printf("\tNew text: "); } else { printf("\tTrack %d description: ", track_no); } fgets(tmp_str, TMP_STRING_LEN, stdin); strip_return(tmp_str);/* If there was no existing entry for this track and the user hasn't added one, we assume that there are no more tracks to be added. */ if (strlen(tmp_str) == 0) { if (existing_track.catalog[0] == '\0') { /* no existing entry, so finished adding */ break; } else { /* leave existing entry, jump to next track */ track_no++; continue; } }/* If the user enters a single d character, this deletes the current and any higher numbered tracks. The del_cdt_entry function will return false if it couldn't find a track to delete. */ if ((strlen(tmp_str) == 1) && tmp_str[0] == 'd') { /* delete this and remaining tracks */ while (del_cdt_entry(entry_to_add_to->catalog, track_no)) { track_no++; } break; }/* Here we get to the code for adding a new track, or updating an existing one. We construct the cdt_entry structure new_track, then call the database function add_cdt_entry to add it to the database. */ strncpy(new_track.track_txt, tmp_str, TRACK_TTEXT_LEN - 1); strcpy(new_track.catalog, entry_to_add_to->catalog); new_track.track_no = track_no; if (!add_cdt_entry(new_track)) { fprintf(stderr, "Failed to add new track\n"); break; } track_no++; } /* while */}/* The function del_cat_entry deletes a catalog entry. We never allow tracks for a non-existent catalog entry to exist. */static void del_cat_entry(const cdc_entry *entry_to_delete){ int track_no = 1; int delete_ok; display_cdc(entry_to_delete); if (get_confirm("Delete this entry and all it's tracks? ")) { do { delete_ok = del_cdt_entry(entry_to_delete->catalog, track_no); track_no++; } while(delete_ok); if (!del_cdc_entry(entry_to_delete->catalog)) { fprintf(stderr, "Failed to delete entry\n"); } }}/* The next function is a utility for deleting all the tracks for a catalog. */static void del_track_entries(const cdc_entry *entry_to_delete){ int track_no = 1; int delete_ok; display_cdc(entry_to_delete); if (get_confirm("Delete tracks for this entry? ")) { do { delete_ok = del_cdt_entry(entry_to_delete->catalog, track_no); track_no++; } while(delete_ok); }}/* Next, we create a very simple catalog search facility. We allow the user to enter a string, then check for catalog entries that contain the string. Since there could be multiple entries that match, we simply offer the user each match in turn. */static cdc_entry find_cat(void){ cdc_entry item_found; char tmp_str[TMP_STRING_LEN + 1]; int first_call = 1; int any_entry_found = 0; int string_ok; int entry_selected = 0; do { string_ok = 1; printf("Enter string to search for in catalog entry: "); fgets(tmp_str, TMP_STRING_LEN, stdin); strip_return(tmp_str); if (strlen(tmp_str) > CAT_CAT_LEN) { fprintf(stderr, "Sorry, string too long, maximum %d \ characters\n", CAT_CAT_LEN); string_ok = 0; } } while (!string_ok); while (!entry_selected) { item_found = search_cdc_entry(tmp_str, &first_call); if (item_found.catalog[0] != '\0') { any_entry_found = 1; printf("\n"); display_cdc(&item_found); if (get_confirm("This entry? ")) { entry_selected = 1; } } else { if (any_entry_found) printf("Sorry, no more matches found\n"); else printf("Sorry, nothing found\n"); break; } } return(item_found);}/* list_tracks is a utility function that prints out all the tracks for a given catalog entry. */static void list_tracks(const cdc_entry *entry_to_use){ int track_no = 1; cdt_entry entry_found; display_cdc(entry_to_use); printf("\nTracks\n"); do { entry_found = get_cdt_entry(entry_to_use->catalog, track_no); if (entry_found.catalog[0]) { display_cdt(&entry_found); track_no++; } } while(entry_found.catalog[0]); (void)get_confirm("Press return");} /* list_tracks *//* The count_all_entries function counts all the tracks. */static void count_all_entries(void){ int cd_entries_found = 0; int track_entries_found = 0; cdc_entry cdc_found; cdt_entry cdt_found; int track_no = 1; int first_time = 1; char *search_string = ""; do { cdc_found = search_cdc_entry(search_string, &first_time); if (cdc_found.catalog[0]) { cd_entries_found++; track_no = 1; do { cdt_found = get_cdt_entry(cdc_found.catalog, track_no); if (cdt_found.catalog[0]) { track_entries_found++; track_no++; } } while (cdt_found.catalog[0]); } } while (cdc_found.catalog[0]); printf("Found %d CDs, with a total of %d tracks\n", cd_entries_found, track_entries_found); (void)get_confirm("Press return");}/* Now we have display_cdc, a utility for displaying a catalog entry. */static void display_cdc(const cdc_entry *cdc_to_show){ printf("Catalog: %s\n", cdc_to_show->catalog); printf("\ttitle: %s\n", cdc_to_show->title); printf("\ttype: %s\n", cdc_to_show->type); printf("\tartist: %s\n", cdc_to_show->artist);}/* and display_cdt, for displaying a single track entry. */static void display_cdt(const cdt_entry *cdt_to_show){ printf("%d: %s\n", cdt_to_show->track_no, cdt_to_show->track_txt);}/* The utility function strip_return removes a trailing linefeed character from a string. Remember that UNIX uses a single linefeed to indicate end of line. */static void strip_return(char *string_to_strip){ int len; len = strlen(string_to_strip); if (string_to_strip[len - 1] == '\n') string_to_strip[len - 1] = '\0';}/* command_mode is a function for parsing the command line arguments. The getopt function is a good way of ensuring your program accepts arguments conforming to the standard UNIX conventions. */static int command_mode(int argc, char *argv[]){ int c; int result = EXIT_SUCCESS; char *prog_name = argv[0]; /* these externals used by getopt */ extern char *optarg; extern int optind, opterr, optopt; while ((c = getopt(argc, argv, ":i")) != -1) { switch(c) { case 'i': if (!database_initialize(1)) { result = EXIT_FAILURE; fprintf(stderr, "Failed to initialize database\n"); } break; case ':': case '?': default: fprintf(stderr, "Usage: %s [-i]\n", prog_name); result = EXIT_FAILURE; break; } /* switch */ } /* while */ return(result);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -