home *** CD-ROM | disk | FTP | other *** search
/ Sound Sensations! / sound_sensations.iso / midifile / cmtcmu / userio.c < prev    next >
C/C++ Source or Header  |  1990-06-28  |  5KB  |  149 lines

  1. /* userio.c -- handy user interface functions */
  2.  
  3. /*****************************************************************************
  4. *        Change Log
  5. *  Date        | Change
  6. *-----------+-----------------------------------------------------------------
  7. * 21-May-86 | Created
  8. *****************************************************************************/
  9.  
  10. #include "cext.h"
  11. #include "stdio.h"
  12. #include "userio.h"
  13.  
  14.  
  15. /****************************************************************************
  16. *                askbool
  17. * Inputs:
  18. *    char *prompt: string to prompt for user input
  19. *    int deflt: true or false default
  20. * Returns:
  21. *    boolean: true or false as entered by user
  22. * Effect:
  23. *    prompts user for yes or no input, returns result
  24. ****************************************************************************/
  25.  
  26. int askbool(prompt, deflt)
  27.     char *prompt;
  28.     int deflt;
  29. {
  30. #define undefined -1
  31.     char defchar;    /* the default answer */
  32.     char c;        /* user input */
  33.     int result = -1;    /* the result: -1 = undefined, 0 = false, 1 = true */
  34.     if (deflt) defchar = 'y';
  35.     else defchar = 'n';
  36.     while (result == undefined) {
  37.     fprintf(stderr, "%s? [%c]: ", prompt, defchar);
  38.     c = getchar();
  39.     if (toupper(c) == 'Y') result = true;
  40.     else if (toupper(c) == 'N') result = false;
  41.     else if (c == '\n') result = deflt;
  42.     else fprintf(stderr, "Please type Y or N.\n");
  43.     }
  44.     while (c != '\n') c = getchar();    /* flush the input line */
  45.     return result;
  46. }
  47.  
  48.  
  49. /****************************************************************************
  50. *                fileopen
  51. * Inputs:
  52. *    char *deflt: the default file name (e.g. from command line)
  53. *    char *extension: default extension
  54. *    char *mode: read ("r") or write ("w")
  55. *    char *prompt: prompt for user
  56. * Returns:
  57. *    opened file pointer
  58. * Effect: 
  59. *    opens file, prompts for user input if necessary and warns about
  60. *    possible confusion.  If deflt is a null string, the user will
  61. *    be prompted for a name.     The routine loops until a file is opened.
  62. *    If the mode is "r", a check is made to see if the file exists
  63. *    with and without the extension.     If both exist a warning is given.
  64. *    For mode "w", a check is made to see if the file will be overwritten.
  65. *    The extension is automatically added if the default or user-typed
  66. *    file has no "."     At the bottom of the loop body, if no file has
  67. *    been opened, the user is prompted for another file name.
  68. ****************************************************************************/
  69.  
  70. FILE *fileopen(deflt, extension, mode, prompt)
  71.     char *deflt;
  72.     char *extension;    /* default extension */
  73.     char *mode;        /* read "r" or write "w" */
  74.     char *prompt;    /* prompt for user */
  75. {
  76.     char filename[100]; /* trial name */
  77.     char extname[100];    /* trial name with extension added */
  78.     FILE *fp = NULL;    /* file corresponding to filename */
  79.     FILE *fpext;    /* file corresponding to extname */
  80.     char *problem;    /* tells user why he has to try again */
  81.  
  82.     strcpy(filename, deflt);
  83.     while (fp == NULL) {    /* keep trying until a good file is found */
  84.     while (strlen(filename) == 0) { /* avoid null file names */
  85.         fprintf(stderr, "%s: ", prompt);
  86.         gets(filename);
  87.     }
  88.     if (mode[0] == 'r') {
  89.         strcpy(extname, filename);
  90.         strcat(extname, ".");
  91.         strcat(extname, extension);
  92.         fp = fopen(filename, mode);
  93.         fpext = fopen(extname, mode);
  94.         if (fp != NULL && fpext != NULL) {
  95.         fprintf(stderr,
  96.             "warning: both %s and %s exist.     %s will be used.\n",
  97.             filename, extname, filename);
  98.         fclose(fpext);
  99.         } else if (fpext != NULL) {
  100.         fp = fpext;
  101.         }
  102.         if (fp == NULL) problem = "Couldn't find %s.";
  103.     } else if (mode[0] == 'w') {
  104.         /* add the extension if there is no '.' in the file name */
  105.         if (!strchr(filename, '.')) {
  106.         strcat(filename, ".");
  107.         strcat(filename, extension);
  108.         }
  109.         fp = fopen(filename, "r");
  110.         if (fp != NULL) {
  111.         char question[100];
  112.         fclose(fp);
  113.         strcpy(question, "OK to overwrite ");
  114.         strcat(question, filename);
  115.         if (askbool(question, false)) {
  116.             fp = fopen(filename, mode);
  117.         } else {
  118.             fp = NULL;
  119.             problem = "";
  120.         }
  121.         } else {
  122.         fp = fopen(filename, mode);
  123.         if (fp == NULL) problem = "Couldn't create %s.";
  124.         }
  125.     }
  126.     if (fp == NULL) {
  127.         fprintf(stderr, problem, filename);
  128.         fprintf(stderr, "  Try again.\n%s: ", prompt);
  129.         gets(filename);
  130.     }
  131.     }
  132.     return fp;
  133. }
  134.  
  135. /****************************************************************************
  136. *                    readln
  137. * Inputs:
  138. *    FILE * fp: File to read from
  139. * Effect: 
  140. *    Reads and discards characters until a newline is seen
  141. ****************************************************************************/
  142.  
  143. void readln(fp)
  144. FILE *fp;
  145. {
  146.     while (getc(fp) != '\n')
  147.         ;
  148. }
  149.