/* Print out a summary giving the number of catalog entries
 * and total number of tracks across all of them */
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 = "";

    // loop over all cdc entries. The `first_time` is a flag which should
    // be 1 ("first time = true") on the initial call; it gets set to 0 (note
    // it's passed via ptr), and internally the database code tracks the
    // progress of the search from that point forward
    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);
    get_confirm("Press Enter");
}
示例#2
0
文件: app_ui.c 项目: Afaren/reference
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");
}
/* Finds a catalog entry. It handles multiple matches by letting the user pick
 * one of them. If there are no matches, search_cdc_entry will set item_found
 * to be all 0 bytes and we will wind up with no active catalog entry. */
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;

    // get a search string, checking that the string isn't too long
    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_LEN) {
            fprintf(stderr, "Sorry, string too long, maximum %d characters",
                    CAT_LEN);
            string_ok = 0;
        }
    } while (!string_ok);

    // loop over all cdc entries. The `first_time` is a flag which should
    // be 1 ("first time = true") on the initial call; it gets set to 0 (note
    // it's passed via ptr), and internally the database code tracks the
    // progress of the search from that point forward
    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 {
            // we can get here either because the user didn't select any
            // of the matches, or because there weren't any.
            if (any_entry_found) {
                printf("Sorry, no more matches found\n");
            } else {
                printf("Sorry, no matches found\n");
            }
            break;
        }
    }
    return item_found;
}
/* A 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) {
        // the first_call flag starts as 1, and is set to 0 inside of the
        // search_cdc_entry function, which uses module-level variables to
        // handle the "curser" over results.
        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);
}
示例#5
0
文件: app_ui.c 项目: linq/unix_learn
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: \n");
    fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    if (strlen(tmp_str) > CAT_CAT_LEN) {
      fprintf(stderr, "Sorry, string too long, maxmium %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;
}