int main(int argc, char *argv[]) {
    menu_options current_option;
    cdc_entry current_cdc_entry;
    int command_result;

    // this block provides a command line api via `command_mode`
    if (argc > 1) {
        command_result = command_mode(argc, argv);
        exit(command_result);
    }

    // if we get here, then we are in interactive mode....
    //    start with an announcement message
    announce();

    // initialize the db. Using 0 here means open an exiting db (actually it
    // will create one, but it won't delete old data for you). To clear the
    // dbs and initialize a new one, use cmd line mode with -i.
    if (!database_initialize(0)) {
        fprintf(stderr, "Sorry, unable to initialize database\n");
        fprintf(stderr, "To create a new database use %s -i\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    // loop over an interactive console menu
    //    the semantics are kind of similar to the ch6 menu, where there's
    //    an "active" cd, and the operations you can do affect that cd

    while(current_option != mo_exit) {
        current_option = get_menu_choice(&current_cdc_entry);
        switch(current_option) {
            case mo_add_cat:
                // add a new catalog item. Lots of error checking here.
                if (enter_new_cat_entry(&current_cdc_entry)) {
                    if (!add_cdc_entry(current_cdc_entry)) {
                        // print message, but also make sure to zero out data
                        fprintf(stderr, "Failed to add new entry\n");
                        memset(&current_cdc_entry, '\0',
                               sizeof(current_cdc_entry));
                    }
                }
                break;
            case mo_add_tracks:
                enter_new_track_entries(&current_cdc_entry);
                break;
            case mo_del_cat:
                del_cat_entry(&current_cdc_entry);
                break;
            case mo_find_cat:
                current_cdc_entry = find_cat();
                break;
            case mo_list_cat_tracks:
                list_tracks(&current_cdc_entry);
                break;
            case mo_del_tracks:
                del_track_entries(&current_cdc_entry);
                break;
            case mo_count_entries:
                count_all_entries();
                break;
            case mo_exit:
            case mo_invalid:
            default:
                // in all three cases, do nothing
                break;
        }
    }

    // close db and exit
    database_close();
    exit(EXIT_SUCCESS);
} // end of main
int main(int argc, char *argv[]) {
    menu_options current_option;
    cdc_entry current_cdc_entry;
    int command_result;

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

    // if there's an arg, switch to command mode and exit
    if (argc > 1) {
        command_result = command_mode(argc, argv);
        exit(command_result);
    }
       
    // print a greeting, and initialize the database (it must already exist)
    announce();
    if (!database_initialize(0)) {
        fprintf(stderr, "Sorry, unable to initialize database\n");
        fprintf(stderr, "To create a new database use %s -i\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* loop over menu choices, calling show_menu every time.
     * the behavior of show_menu depends on whether a track is currently
     * selected (which is determined by the var `current_cdc_entry`) */
    while(current_option != mo_exit) {
        current_option = show_menu(&current_cdc_entry);

        switch(current_option) {
            case mo_add_cat:
                if (enter_new_cat_entry(&current_cdc_entry)) {
                    if (!add_cdc_entry(current_cdc_entry)) {
                        fprintf(stderr, "Failed to add new entry\n");
                        memset(&current_cdc_entry, '\0', 
                               sizeof(current_cdc_entry));
                    }
                }
                break;
            case mo_add_tracks:
                enter_new_track_entries(&current_cdc_entry);
                break;
            case mo_del_cat:
                del_cat_entry(&current_cdc_entry);
                break;
            case mo_find_cat:
                current_cdc_entry = find_cat();
                break;
            case mo_list_cat_tracks:
                list_tracks(&current_cdc_entry);
                break;
            case mo_del_tracks:
                del_track_entries(&current_cdc_entry);
                break;
            case mo_count_entries:
                count_all_entries();
                break;
            case mo_exit:
                break;
            case mo_invalid:
                break;
            default:
                break;
        } /* switch */
    } /* while */


    // when the loop exits, clean up and quit
    database_close();
    exit(EXIT_SUCCESS);
}
Beispiel #3
0
void main(int argc, char *argv[])
{
    menu_options current_option;
    cdc_entry current_cdc_entry;
    int command_result;

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

    if (argc > 1) {
        command_result = command_mode(argc, argv);
        exit(command_result);
    }
       
    announce();

    if (!database_initialize(0)) {
        fprintf(stderr, "Sorry, unable to initialize database\n");
        fprintf(stderr, "To create a new database use %s -i\n", argv[0]);
        exit(EXIT_FAILURE);
    }

/*  We're now ready to process user input. We sit in a loop, asking for a menu choice
 and processing it, until the user selects the exit option.
 We pass the current_cdc_entry structure to the show_menu function.
 We do this to allow the menu choices to change if a catalog entry is currently selected.  */

while(current_option != mo_exit) {
        current_option = show_menu(&current_cdc_entry);

        switch(current_option) {
            case mo_add_cat:
                if (enter_new_cat_entry(&current_cdc_entry)) {
                    if (!add_cdc_entry(current_cdc_entry)) {
                        fprintf(stderr, "Failed to add new entry\n");
                        memset(&current_cdc_entry, '\0', 
                               sizeof(current_cdc_entry));
                    }
                }
                break;
            case mo_add_tracks:
                enter_new_track_entries(&current_cdc_entry);
                break;
            case mo_del_cat:
                del_cat_entry(&current_cdc_entry);
                break;
            case mo_find_cat:
                current_cdc_entry = find_cat();
                break;
            case mo_list_cat_tracks:
                list_tracks(&current_cdc_entry);
                break;
            case mo_del_tracks:
                del_track_entries(&current_cdc_entry);
                break;
            case mo_count_entries:
                count_all_entries();
                break;
            case mo_exit:
                break;
            case mo_invalid:
                break;
            default:
                break;
        } /* switch */
    } /* while */

/* When the main loop exits, we close the database and exit back to the environment.
 The welcoming sentence is printed by the announce function. */

    database_close();
    exit(EXIT_SUCCESS);
} /* main */
Beispiel #4
0
int main(int argc, char *argv[])
{
  menu_options current_option;
  cdc_entry current_cdc_entry;
  int command_result;

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

  if (argc > 1) {
    command_result = command_mode(argc, argv);
    exit(command_result);
  }

  announce();

  if (!database_initialize(0)) {
    fprintf(stderr, "Sorry, unable to initialize database\n");
    fprintf(stderr, "To create a new database use %s -i\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  while (current_option != mo_exit) {
    current_option = show_menu(&current_cdc_entry);
    switch (current_option) {
      case mo_add_cat:
        if (enter_new_cat_entry(&current_cdc_entry)) {
          if (!add_cdc_entry(current_cdc_entry)) {
            fprintf(stderr, "Failed to add new entry\n");
            memset(&current_cdc_entry, '\0', sizeof(current_cdc_entry));
          }
        }
        break;
      case mo_add_tracks:
        enter_new_track_entries(&current_cdc_entry);
        break;
      case mo_del_cat:
        del_cat_entry(&current_cdc_entry);
        break;
      case mo_find_cat:
        current_cdc_entry = find_cat();
        break;
      case mo_list_cat_tracks:
        list_tracks(&current_cdc_entry);
        break;
      case mo_del_tracks:
        del_track_entries(&current_cdc_entry);
        break;
      case mo_count_entries:
        count_all_entries();
        break;
      case mo_exit:
        break;
      case mo_invalid:
        break;
      default:
        break;
    }
  }

  database_close();
  return 0;
}