Advertisement
cperryoh

Untitled

Apr 29th, 2022
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.24 KB | None | 0 0
  1. /*
  2.  * CSSE 132
  3.  * Rose-Hulman Institute of Technology
  4.  * Computer Science and Software Engineering
  5.  *
  6.  * local.c - Source file with your solutions to the lab.  The
  7.  *           main functionality for this lab should be implemented
  8.  *           in data.c and in here.  This file is used for running the lab's
  9.  *           program.
  10.  *
  11.  *           This is a file you need to hand in!
  12.  *
  13.  * This file contains code used by labs in the CSSE 132 class.
  14.  * When you edit this file for class, be sure to put your name(s) here!
  15.  *
  16.  * Edited by
  17.  * NAMES: Cole Perry
  18.  *
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25. #include "data.h"
  26.  
  27.  
  28. /**
  29.  * Runloop that accepts commands from the user and prints out results.
  30.  *
  31.  * Commands to implement:
  32.  *
  33.  * q - quits the program.
  34.  *
  35.  * l - lists the database entries
  36.  *       (do_list_database)
  37.  *
  38.  * a - adds an entry, prompting the user for name and values
  39.  *       (do_add_entry)
  40.  *
  41.  * f - finds an entry, prompting user for a target and
  42.  *       printing the first entries with a matching name.
  43.  *       (do_find_all_matches)
  44.  *
  45.  * r - prompts user for a target and removes the first matching
  46.  *       entry from the database.  No effect if nothing matches.
  47.  *       (do_remove_first_match)
  48.  *
  49.  * e - prompts user for a filename, then saves all the database
  50.  *       entries into that file (one per line, same format as
  51.  *       how they're printed).
  52.  *
  53.  * i - prompts user for a filename, then loads database entries
  54.  *       from that file (expecting one per line, same format as
  55.  *       how they're printed).
  56.  *
  57.  * If given a command that doesn't match one in this list, print "instructions"
  58.  * to show what commands are available.  Edit the "instructions" string to
  59.  * include any instructions you implement.
  60.  *
  61.  * @param db: a pointer to the database structure
  62.  */
  63. void
  64. handleLocalInput(struct db_entry** db)
  65. {
  66.   // This is a string you can print to give the user instructions.
  67.   // Be sure to update it as you implement new commands!
  68.   const char* instructions = " (a)dd, (l)ist, or (q)uit";
  69.   char buf[512];
  70.   char buf2[512];
  71.  
  72.   // loop forever
  73.   for( ;; ) {
  74.     printf("(Local)> ");
  75.  
  76.     // read input
  77.     // TODO: Uncomment the next line once you've implemented getALine()
  78.     getALine(buf, 512, stdin);
  79.  
  80.     // select instruction to run based on the input
  81.     switch(buf[0]) {
  82.     case 'q':
  83.     case 'x':
  84.       // TODO: somehow get out of this run loop
  85.       return;
  86.     case 'l': // LIST
  87.       // TODO: implement this
  88.       do_list_database(db);
  89.       break;
  90.  
  91.     case 'a': // ADD
  92.       // TODO:
  93.       // 1. prompt for the name and value (use printf) and
  94.       //    read the name and value into buffers (use getALine).
  95.       //    HINT: use the arrays declared at the top of this function.
  96.       // 2. call do_add_entry to add it to the database
  97.       printf("Enter name: ");
  98.       getALine(buf,sizeof(buf),stdin);
  99.       printf("Enter value: ");
  100.       getALine(buf2,sizeof(buf2),stdin);
  101.       do_add_entry(db,buf,buf2);
  102.       break;
  103.  
  104.     case 'r': // FIND and REMOVE ONE
  105.       // TODO:
  106.       // prompt for the name
  107.       // HINT: use the arrays declared at the top of this function.
  108.       // remove the first entry that matches the name.
  109.       printf("Enter name of entry to remove: ");
  110.       getALine(buf,sizeof(buf),stdin);
  111.       int b = db_find_one(db,buf,0);
  112.       db_remove(db,b);
  113.       break;
  114.  
  115.     case 'f': // FIND
  116.       // TODO:  This is part 6 of the lab (and is optional for this lab).  You'll need
  117.       //        to complete this before the next lab but you don't have to do it now.
  118.       // prompt for the name
  119.       // find and print all the matches.
  120.       printf("Enter an name of an entry: ");
  121.       getALine(buf,sizeof(buf),stdin);
  122.       do_find_all_matches(db,buf);
  123.       break;
  124.  
  125.     case 'e': // EXPORT
  126.       // TODO
  127.       // 1. prompt for the file name
  128.       // 2. read the file name into a buffer
  129.       // 3. run the export function you implemented.
  130.       printf("Enter file name: ");
  131.       getALine(buf,sizeof(buf),stdin);
  132.       do_export_db(db,buf);
  133.       break;
  134.  
  135.     case 'i': // IMPORT
  136.       // TODO: implement this (similar to export)
  137.       printf("Enter file name: ");
  138.       getALine(buf,sizeof(buf),stdin);
  139.       do_import_db(db,buf);
  140.       break;
  141.  
  142.     default:
  143.       // TODO:
  144.       // The instruction was not one of the above instructions.
  145.       // Print out a help screen that explains what instructions
  146.       // are available.
  147.       printf("None of the avaible options matched what you entered. The following commands are available:\n'q' or 'x': quit\n'l': list database\n'a': add entry to database\n'r': remove entry from data base\n'f': find and print all matches in data base\n'e': export database to text file\n'i': import database from text file\n");
  148.       break;
  149.     }
  150.   }
  151. }
  152.  
  153. int
  154. main(int argc, char** argv)
  155. {
  156.   // make a big, empty database in the heap
  157.   // this is a _pointer_ to an _array_ of db_entry structs.
  158.   struct db_entry** db = malloc(DB_MAX_SIZE * sizeof(struct db_entry*));
  159.  
  160.   // zero out the memory, just in case.
  161.   memset(db, 0, DB_MAX_SIZE * sizeof(struct db_entry*));
  162.  
  163.   // go execute the run loop
  164.   handleLocalInput(db);
  165.  
  166.   // return the used memory
  167.   free(db);
  168.  
  169.   return 0;
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement