100960ac8d81b4fcf2d409e4bac054401cdbad81
[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.1 2000/06/19 02:58:08 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 "kstddef.h"
30 #include "cio.h"
31
32
33
34 const char* 
35 fileBasename (const char* const filename)
36 {
37   const char* p = strrchr (filename, '/');
38   return (p ? p + 1 : filename);
39 }
40
41
42 /* NAME
43  *   file_exists                Checks if a specified disk fie exists
44  *
45  * SYNOPSIS
46  *   exist = file_exists (fname)
47  *   bool exist                 TRUE if specified file exists
48  */
49
50 bool
51 file_exists (const char *fname)
52 {
53   FILE *fp;
54   bool exist;
55
56   if (strlen(fname) == 0)
57     exist = false;
58   else if ((fp = fopen(fname, "r")) == NULL)
59     exist = false;
60   else {
61     fclose (fp);
62     exist = true;
63   }
64   
65   return (exist);
66 }
67
68 /*-----------------------------------------------------------------------------
69  *
70  * FUNCTION IDENTIFICATION
71  *
72  *      Name:           sys_fopen               Open a file for user
73  *      Date:           12-17-84
74  *      Programmer:     Kevin Rosenberg
75  *
76  * SYNOPSIS
77  *      fp = sys_fopen (filename, mode, progname)
78  *      FILE *fp                        Standard pointer to 'C' file
79  *      char *filename                  Name of file to open
80  *                                      If user enters a new name, it goes here
81  *      char *mode                      Mode to open file (std. 'C')
82  *      char *progname                  Name of program calling this routine
83  *
84  * DESCRIPTION
85  *        This routine opens a file using the standard C fopen() routine.  If
86  *    the file is not found, the user is given to option to:
87  *
88  *              1 - Retry opening file with same name
89  *              2 - Enter new file name
90  *              3 - Abort and return to DOS
91  *
92  * CAUTIONS
93  *        If the the requested file is not found, the name of the file given
94  *    entered at keyboard will be returned in filename.  So, make sure there
95  *   is room for a maximum length filename (MAXFULLNAME)
96  *
97  *---------------------------------------------------------------------------*/
98
99 FILE *
100 sys_fopen (const char *filename, const char *mode, const char *progname)
101 {
102   FILE *fp;
103   char  fname[256];     /* name used for call to fopen() */
104   char  c;                      /* keyboard response */
105
106   strncpy (fname, filename, sizeof(fname));
107
108   do {
109     if ((fp = fopen (fname, mode)) == NULL) {
110       cerr << endl;
111       cerr << "Can't open file " << fname << " [" << progname << "]" << endl;
112       cerr << "Enter: <R> - Retry | <N> - New name | <A> - Abort program --> ";
113       c = cio_kb_waitc ("RrNnAa", TRUE);
114       c = tolower(c);
115       cerr << c << endl;
116
117       if (c == 'r')             // Retry -- Nothing to do here 
118         ;                       
119       else if (c == 'a')        // Abort -- Exit to OS
120         exit (1);
121       else if (c == 'n') {      // New name - get from console
122         cerr << "Enter new file name -- ";
123         fgets (fname, sizeof(fname), stdin);
124         str_wrm_tail (fname);
125         cerr << endl;
126       } 
127     }
128   } while (fp == NULL);
129
130   return (fp);
131 }