SHARE
TWEET

file_util.c

TwisterMike Feb 16th, 2020 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Group 2 Program 2
  2. Paul Maclean- mac7537@calu.edu
  3. Mike Gorse- gor9632@calu.edu
  4. Robert Breckenridge- bre6896@calu.edu
  5. Chase Smith- smi8808@calu.edu
  6.  
  7. CSC 460
  8. Language Translations
  9. */
  10.  
  11. #include "file_util.h"
  12.  
  13.  
  14. typedef enum { none, overwrite, tryagain, userquit } overwriteoption;
  15.  
  16.  
  17.  
  18.  
  19. //***Extension Manipulation Functions***//
  20.  
  21. void appendExtension(char* FileName, char* DesiredExtension) {
  22.     //If no '.' (extension) was given:
  23.     if (strrchr(FileName, '.') == NULL) {
  24.         //Append extension
  25.         strcat_s(FileName, FILENAME_MAXSZ, DesiredExtension);
  26.     }
  27. }
  28.  
  29. void stripExtension(char* FileName) {
  30.     //find extension position
  31.     char* extpos = strrchr(FileName, '.');
  32.     if (extpos != NULL) {
  33.         extpos[0] = '\0';
  34.     }
  35. }
  36.  
  37. void setExtension(char* FileName, char* DesiredExtension) {
  38.     stripExtension(FileName);
  39.     appendExtension(FileName, DesiredExtension);
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. //***Support Functions***//
  48.  
  49. logical getSameNameSelection()
  50. {
  51.     char choice[100] = { 0 };
  52.     overwriteoption selection = none;
  53.     printf("The file you selected already exists. \n");
  54.  
  55.  
  56.     do {
  57.         printf("Enter one of the following characters \n\t'o': Overwrite\n\t'n': New Filename (Try again)\n\t'q': Quit\n Your selection: ");
  58.         gets(choice);
  59.         if (strlen(choice) == 1)
  60.         {
  61.             switch (choice[0]) {
  62.             case 'o':
  63.                 selection = overwrite;
  64.                 break;
  65.             case 'n':
  66.                 selection = tryagain;
  67.                 break;
  68.             case 'q':
  69.                 selection = userquit;
  70.                 break;
  71.             default:
  72.                 printf("**Please enter a valid input.** \n");
  73.             }
  74.         }
  75.         else {
  76.             printf("**Please enter a valid input.** \n");
  77.         }
  78.     } while (selection == none);
  79.  
  80.     return selection;
  81. }
  82.  
  83.  
  84.  
  85.  
  86. //***File Management Functions***//
  87.  
  88. void copyToFile(FILE* DstFile, FILE* SrcFile)
  89. {
  90.     char currentChar;
  91.     rewind(SrcFile);
  92.     while ((currentChar = fgetc(SrcFile)) != EOF)
  93.         fputc(currentChar, DstFile);
  94.  
  95.     rewind(SrcFile);
  96. }
  97.  
  98. logical createBackup(char* OutFileName)
  99. {
  100.     logical success = lfalse;
  101.     char bckfilename[FILENAME_MAXSZ] = { 0 };
  102.  
  103.     strcpy_s(bckfilename, FILENAME_MAXSZ, OutFileName);
  104.     setExtension(bckfilename, ".bak");
  105.  
  106.     fopen_s(&BckFile, bckfilename, "w");
  107.     copyToFile(BckFile, OutFile);
  108.     if (BckFile != NULL) {
  109.         fclose(BckFile); //close backfile
  110.         success = ltrue;
  111.         printf("Successfully created backup %s\n", bckfilename);
  112.     }
  113.     else {
  114.         printf("Failure creating backup %s. ", bckfilename);
  115.         success = lfalse;
  116.     }
  117.  
  118.     return success;
  119. }
  120.  
  121. logical getinpfile(char* InpFileName, logical PromptFile) {
  122.     logical askagain = lfalse;
  123.     logical aborted = lfalse;
  124.  
  125.     do {
  126.         if (PromptFile) {
  127.             printf("Please enter an input file name, or 'Q'/'' to quit: ");
  128.             gets(InpFileName);
  129.         }
  130.  
  131.         char firstchar = tolower(InpFileName[0]);
  132.         int len = strlen(InpFileName);
  133.  
  134.         //if input is 'q'
  135.         if (PromptFile && ((firstchar == 'q' && len == 1) || len == 0)) {
  136.             printf("You have elected to exit. ");
  137.             aborted = ltrue;
  138.         }
  139.         else {
  140.             appendExtension(InpFileName, INPFILE_EXTENSION);
  141.  
  142.             //Open the file
  143.             fopen_s(&InpFile, InpFileName, "r");
  144.  
  145.             if (InpFile == NULL) {
  146.                 printf("Could not find %s! Please try again. \n", InpFileName);
  147.                 askagain = lfalse;
  148.             }
  149.             else {
  150.                 askagain = ltrue;
  151.             }
  152.         }
  153.  
  154.         //Default the flag that prompts the user rather than uses cmdline args
  155.         PromptFile = ltrue;
  156.  
  157.     } while (askagain == lfalse && aborted == lfalse);
  158.  
  159.     //Close all file handles.
  160.     if (InpFile != NULL) {
  161.         fclose(InpFile);
  162.     }
  163.  
  164.     return aborted; //indicate whether the user quit or not
  165. }
  166.  
  167.  
  168.  
  169. logical getOutfile(char* OutFileName, char* InpFileName, logical PromptFile) {
  170.     logical askagain = lfalse;
  171.     logical aborted = lfalse;
  172.     char Bad_Out_File[] = "<>\":/\\|?*";
  173.  
  174.     do {
  175.         if (PromptFile)
  176.         {
  177.             printf("Please enter an output file name, enter to use your input file, or q to quit: ");
  178.             gets(OutFileName);
  179.         }
  180.  
  181.         char firstchar = tolower(OutFileName[0]);
  182.         int len = strlen(OutFileName);
  183.  
  184.         //if input is 'q'
  185.         if (firstchar == 'q' && len == 1) {
  186.             aborted = ltrue;
  187.         }
  188.         else {
  189.  
  190.             if (len == 0)
  191.             {
  192.                 //if provided name is '', use input filename w/ output extension
  193.  
  194.                 strcpy_s(OutFileName, FILENAME_MAXSZ, InpFileName);
  195.                 setExtension(OutFileName, OUTFILE_EXTENSION);
  196.  
  197.                 printf("Using input file name for output\n");
  198.             }
  199.             else {
  200.                 //if anything else was provided, slap an extension on it.
  201.                 appendExtension(OutFileName, OUTFILE_EXTENSION);
  202.             }
  203.  
  204.             printf("Prospective output file name: %s\n", OutFileName);
  205.  
  206.  
  207.             //File name is fully composed by now. Check if invalid, then process
  208.             if (strcmp(InpFileName, OutFileName) == 0) {
  209.                 printf("I don't think you understand what Read/Write access means. Try again, using different names.\n");
  210.                 askagain = ltrue;
  211.             }
  212.             else {
  213.                 if (strpbrk(Bad_Out_File, OutFileName) == NULL)
  214.                 {
  215.                     fopen_s(&OutFile, OutFileName, "r");
  216.  
  217.                     //---------------------Check to see if file exists or not-----------
  218.  
  219.                     if (OutFile != NULL)
  220.                     {
  221.                         //Trying to overwrite existing file? Check options
  222.                         overwriteoption selection = getSameNameSelection();
  223.                         logical backupsuccessful;
  224.  
  225.                         switch (selection) {
  226.                         case overwrite:
  227.                             backupsuccessful = createBackup(OutFileName);
  228.                             if (backupsuccessful)
  229.                                 askagain = lfalse;
  230.                             else
  231.                                 aborted = ltrue;
  232.  
  233.                             break;
  234.                         case tryagain:
  235.                             askagain = ltrue;
  236.                             break;
  237.                         case userquit:
  238.                             aborted = ltrue;
  239.                             break;
  240.                         default:
  241.                             printf("Something has gone horribly wrong\n");
  242.                             aborted = ltrue;
  243.                             break;
  244.                         }
  245.  
  246.                     }
  247.                     else
  248.                     {
  249.                         askagain = lfalse;
  250.                     }
  251.                 }
  252.                 else {
  253.                     PromptFile = ltrue;
  254.                     askagain = ltrue;
  255.                     printf("File name invalid\n");
  256.                 }
  257.             }
  258.         }
  259.  
  260.         //Default the flag that prompts the user rather than uses cmdline args
  261.         PromptFile = ltrue;
  262.     } while (askagain == ltrue && aborted == lfalse);
  263.  
  264.     if (OutFile != NULL)
  265.     {
  266.         fclose(OutFile);
  267.     }
  268.  
  269.     return aborted; //indicate whether the user quit or not
  270. }
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. //*** Exported functions ***//
  280.  
  281. file_outcome openFiles(int argc, char* argv[]) {
  282.     //data declarations for main
  283.     logical promptinpfile = ltrue;
  284.     logical promptoutfile = ltrue;
  285.     logical abort;
  286.     file_outcome fileCheck = quit;
  287.  
  288.     char inpfilename[FILENAME_MAXSZ] = { 0 };
  289.     char outfilename[FILENAME_MAXSZ] = { 0 };
  290.     char lisfilename[FILENAME_MAXSZ] = { 0 };
  291.     char tmpfilename[FILENAME_MAXSZ] = "unlimited.power";
  292.  
  293.     //Checks for arg 1
  294.     if (argc > 1) {
  295.         promptinpfile = lfalse;
  296.         strcpy_s(inpfilename, FILENAME_MAXSZ, argv[1]);
  297.     }
  298.  
  299.     //Checkes for arg 2
  300.     if (argc > 2) {
  301.         promptoutfile = lfalse;
  302.         strcpy_s(outfilename, FILENAME_MAXSZ, argv[2]);
  303.     }
  304.  
  305.  
  306.     abort = getinpfile(inpfilename, promptinpfile);
  307.  
  308.     if (abort == lfalse) {
  309.         printf("Using input file %s\n", inpfilename);
  310.         abort = getOutfile(outfilename, inpfilename, promptoutfile);
  311.  
  312.         if (abort == lfalse) {
  313.  
  314.             //Fix up the listing file name to back the outfile name
  315.             strcpy_s(lisfilename, FILENAME_MAXSZ, outfilename);
  316.             setExtension(lisfilename, LISFILE_EXTENSION);
  317.  
  318.             if (strcmp(outfilename, lisfilename) == 0) {
  319.                 abort = ltrue;
  320.             }
  321.             else {
  322.                 //Prints for file names
  323.                 printf("\n\nCongratulations on getting this far. Your files:\n");
  324.                 printf("Input file     %s\n", inpfilename);
  325.                 printf("Output file    %s\n", outfilename);
  326.                 printf("Listing file   %s\n", lisfilename);
  327.                 printf("Temp file      %s\n", tmpfilename);
  328.  
  329.                 //Opens Files
  330.                 fopen_s(&InpFile, inpfilename, "r");
  331.                 fopen_s(&OutFile, outfilename, "w");
  332.                 fopen_s(&LisFile, lisfilename, "w");
  333.                 fopen_s(&TmpFile, tmpfilename, "w");
  334.  
  335.                 fileCheck = cont;
  336.             }
  337.         }
  338.     }
  339.  
  340.     if (abort) {
  341.         printf("Aborting program.\n");
  342.     }
  343.  
  344.  
  345.     return fileCheck;
  346. }
  347.  
  348. void closeFiles() {
  349.     if (InpFile != NULL) {
  350.         fclose(InpFile);
  351.     }
  352.     if (TmpFile != NULL) {
  353.         fclose(TmpFile);
  354.     }
  355.     if (OutFile != NULL) {
  356.         fclose(OutFile);
  357.     }
  358.     if (LisFile != NULL) {
  359.         fclose(LisFile);
  360.     }
  361. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top