예제 #1
0
파일: app_ui.c 프로젝트: Afaren/reference
static int enter_new_cat_entry(cdc_entry *entry_to_update)
{
    cdc_entry new_entry;
    char tmp_str[TMP_STRING_LEN + 1];

    memset(&new_entry, '\0', sizeof(new_entry));

    printf("Enter catalog entry: ");
    (void)fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.catalog, tmp_str, CAT_CAT_LEN - 1);

    printf("Enter title: ");
    (void)fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.title, tmp_str, CAT_TITLE_LEN - 1);

    printf("Enter type: ");
    (void)fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.type, tmp_str, CAT_TYPE_LEN - 1);

    printf("Enter artist: ");
    (void)fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.artist, tmp_str, CAT_ARTIST_LEN - 1);

    printf("\nNew catalog entry entry is :-\n");
    display_cdc(&new_entry);
    if (get_confirm("Add this entry ?")) {
        memcpy(entry_to_update, &new_entry, sizeof(new_entry));
        return(1);
    }
    return(0);
}
예제 #2
0
/* delete all track entries for the active catalog */
static void del_track_entries(const cdc_entry *entry_to_delete) {
    int track_no = 1, delete_ok;
    // don't delete without confirmation
    display_cdc(entry_to_delete);
    if (get_confirm("Delete tracks for this entry? ")) {
        do {
            // remember, del_cdt_entry returns 0 if the track didn't exist
            delete_ok = del_cdt_entry(entry_to_delete->catalog, track_no);
            track_no++;
        } while (delete_ok);
    }
}
예제 #3
0
/* 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);
    }
}
예제 #4
0
파일: app_ui.c 프로젝트: linq/unix_learn
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");
    }
  }
}
예제 #5
0
/* prints out all 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]);
	get_confirm("Press return");
} /* list tracks */
예제 #6
0
/* 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;
}
예제 #7
0
/* list tracks for the active catalog */
static void list_tracks(const cdc_entry *entry_to_use) {
    int track_no = 1;
    cdt_entry entry_found;

    // first display the current catalog
    display_cdc(entry_to_use);

    // loop over entries and display them
    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]);
    // let the user look before returning to menu
    get_confirm("Press return");
}
예제 #8
0
/* 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);
}
예제 #9
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;
}
예제 #10
0
/* Gets a new cdc_entry from the user. If all goes well, saves the
 * data to the location pointed to by `entry_to_update` 
 *   returns 1 if update occurs, 0 if the user aborts */
static int enter_new_cat_entry(cdc_entry *entry_to_update) {
    cdc_entry new_entry;
    char tmp_str[TMP_STRING_LEN + 1];

    memset(&new_entry, '\0', sizeof(new_entry)); // remember to do this!

    // the code that follows is kind of copy-pastey, probably should be
    // factored out.

    printf("Enter catalog entry: ");
    fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.catalog, tmp_str, CAT_LEN - 1);

    printf("Enter title: ");
    fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.title, tmp_str, TITLE_LEN - 1);
    
    printf("Enter type: ");
    fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.type, tmp_str, TYPE_LEN - 1);

    printf("Enter artist: ");
    fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.artist, tmp_str, ARTIST_LEN - 1);
    printf("\nNew catalog entry entry is :-\n");

    // get a user confirmation before continuint
    display_cdc(&new_entry);
    if (get_confirm("Add this entry ?")) {
        memcpy(entry_to_update, &new_entry, sizeof(new_entry));
        return(1);
    }
    return(0);
} 
예제 #11
0
/* Get information for a new cdc entry.
 *    (just the metadata, not track information)
 * We use strip_return to get rid of the '\n' that fgets returns.
 * We avoid using gets, because it is unsafe (no buffer overflow size checks!)
 * Return 1 on success, 0 if user doesn't confirm.
 */
static int enter_new_cat_entry(cdc_entry *entry_to_update)
{
    // impl notes: we copy all the data into a new entry first so that we
    // can display it and ask for confirmation. Only then do we copy into
    // `entry_to_update`.
    cdc_entry new_entry;
    char tmp_str[TMP_STRING_LEN + 1];

    memset(&new_entry, '\0', sizeof(new_entry));

    printf("Enter catalog entry: ");
    fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.catalog, tmp_str, CAT_CAT_LEN - 1);

    printf("Enter title: ");
    fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.title, tmp_str, CAT_TITLE_LEN - 1);

    printf("Enter type: ");
    fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.type, tmp_str, CAT_TYPE_LEN - 1);

    printf("Enter artist: ");
    fgets(tmp_str, TMP_STRING_LEN, stdin);
    strip_return(tmp_str);
    strncpy(new_entry.artist, tmp_str, CAT_ARTIST_LEN - 1);

    printf("\nNew catalog entry entry is :-\n");
    display_cdc(&new_entry);
    if (get_confirm("Add this entry ?")) {
        memcpy(entry_to_update, &new_entry, sizeof(new_entry));
        return(1);
    }
    return(0);
}
예제 #12
0
/* Delete a catalog entry. Delete all of its tracks. */
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? ")) {

        // loop over all the track entries and delete (we could have called
        // del_track_entries; the reason we don't is b/c that function has
        // it's own confirmation inside of it. Better factoring would have
        // been able to follow DRY better.
        do {
            delete_ok = del_cdt_entry(entry_to_delete->catalog, 
                                      track_no);
            track_no++;
        } while(delete_ok);

        // now delete the catalog entry itself
        if (!del_cdc_entry(entry_to_delete->catalog)) {
            fprintf(stderr, "Failed to delete entry\n");
        }
    }
}