item_found = search_cdc_entry(tmp_str, &firstcall);
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);
}
18. Функция
list_tracks
— утилита, которая выводит все дорожки для заданного элемента каталога:
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 */
19. Функция
count_all_entries
подсчитывает все дорожки:
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");
}
20. Теперь у вас есть утилита
display_cdc
для вывода элемента каталога:
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);
}
и утилита
display_cdt
для отображения элемента-дорожки:
static void display_cdt(const cdt_entry *cdt_to_show) {
printf("%d: %s\n", cdt_to_show->track_no,
cdt_to_show->track_txt);
}
21. Служебная функция
strip_return
удаляет завершающий строку символ перевода строки. Помните о том, что Linux, как и UNIX, использует один символ перевода строки для обозначения конца строки.
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';
}
22. Функция
command_mode
предназначена для синтаксического анализа аргументов командной строки. Функция
getopt
— хороший способ убедиться в том, что ваша программа принимает аргументы, соответствующие стандартным соглашениям, принятым в системе Linux.
static int command_mode(int argc, char *argv[]) {
int c;
int result = EXIT_SUCCESS;
char *prog_name = argv[0];
/* Эти внешние переменные используются функцией getopt */
extern char *optarg;
extern 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 ':':