511251d3261f45bb45c1a7793498e988fb8b1870
[ctsim.git] / libctsupport / filefuncs.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: filefuncs.cpp,v 1.2 2000/06/19 19:04:05 kevin Exp $
6 **
7 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
8 **  Initial CVS import for first public release
9 **
10 **  This program is free software; you can redistribute it and/or modify
11 **  it under the terms of the GNU General Public License (version 2) as
12 **  published by the Free Software Foundation.
13 **
14 **  This program is distributed in the hope that it will be useful,
15 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 **  GNU General Public License for more details.
18 **
19 **  You should have received a copy of the GNU General Public License
20 **  along with this program; if not, write to the Free Software
21 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
23 ******************************************************************************/
24
25 #include <string.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <iostream>
29 #include "ctsupport.h"
30
31
32 const char* 
33 fileBasename (const char* const filename)
34 {
35   const char* p = strrchr (filename, '/');
36   return (p ? p + 1 : filename);
37 }
38
39
40 /* NAME
41  *   file_exists                Checks if a specified disk fie exists
42  *
43  * SYNOPSIS
44  *   exist = file_exists (fname)
45  *   bool exist                 TRUE if specified file exists
46  */
47
48 bool
49 file_exists (const char *fname)
50 {
51   FILE *fp;
52   bool exist;
53
54   if (strlen(fname) == 0)
55     exist = false;
56   else if ((fp = fopen(fname, "r")) == NULL)
57     exist = false;
58   else {
59     fclose (fp);
60     exist = true;
61   }
62   
63   return (exist);
64 }
65
66 /*-----------------------------------------------------------------------------
67  *
68  * FUNCTION IDENTIFICATION
69  *
70  *      Name:           sys_fopen               Open a file for user
71  *      Date:           12-17-84
72  *      Programmer:     Kevin Rosenberg
73  *
74  * SYNOPSIS
75  *      fp = sys_fopen (filename, mode, progname)
76  *      FILE *fp                        Standard pointer to 'C' file
77  *      char *filename                  Name of file to open
78  *                                      If user enters a new name, it goes here
79  *      char *mode                      Mode to open file (std. 'C')
80  *      char *progname                  Name of program calling this routine
81  *
82  * DESCRIPTION
83  *        This routine opens a file using the standard C fopen() routine.  If
84  *    the file is not found, the user is given to option to:
85  *
86  *              1 - Retry opening file with same name
87  *              2 - Enter new file name
88  *              3 - Abort and return to DOS
89  *
90  * CAUTIONS
91  *        If the the requested file is not found, the name of the file given
92  *    entered at keyboard will be returned in filename.  So, make sure there
93  *   is room for a maximum length filename (MAXFULLNAME)
94  *
95  *---------------------------------------------------------------------------*/
96
97 FILE *
98 sys_fopen (const char *filename, const char *mode, const char *progname)
99 {
100   FILE *fp;
101   char  fname[256];     /* name used for call to fopen() */
102   char  c;                      /* keyboard response */
103
104   strncpy (fname, filename, sizeof(fname));
105
106   do {
107     if ((fp = fopen (fname, mode)) == NULL) {
108       cerr << endl;
109       cerr << "Can't open file " << fname << " [" << progname << "]" << endl;
110       cerr << "Enter: <R> - Retry | <N> - New name | <A> - Abort program --> ";
111       c = cio_kb_waitc ("RrNnAa", TRUE);
112       c = tolower(c);
113       cerr << c << endl;
114
115       if (c == 'r')             // Retry -- Nothing to do here 
116         ;                       
117       else if (c == 'a')        // Abort -- Exit to OS
118         exit (1);
119       else if (c == 'n') {      // New name - get from console
120         cerr << "Enter new file name -- ";
121         fgets (fname, sizeof(fname), stdin);
122         str_wrm_tail (fname);
123         cerr << endl;
124       } 
125     }
126   } while (fp == NULL);
127
128   return (fp);
129 }