r99: *** empty log message ***
[ctsim.git] / libctsupport / filefuncs.cpp
diff --git a/libctsupport/filefuncs.cpp b/libctsupport/filefuncs.cpp
new file mode 100644 (file)
index 0000000..100960a
--- /dev/null
@@ -0,0 +1,131 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: filefuncs.cpp,v 1.1 2000/06/19 02:58:08 kevin Exp $
+**
+**  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
+**  Initial CVS import for first public release
+**
+**  This program is free software; you can redistribute it and/or modify
+**  it under the terms of the GNU General Public License (version 2) as
+**  published by the Free Software Foundation.
+**
+**  This program is distributed in the hope that it will be useful,
+**  but WITHOUT ANY WARRANTY; without even the implied warranty of
+**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+**  GNU General Public License for more details.
+**
+**  You should have received a copy of the GNU General Public License
+**  along with this program; if not, write to the Free Software
+**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+******************************************************************************/
+
+#include <string.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <iostream>
+#include "kstddef.h"
+#include "cio.h"
+
+
+
+const char* 
+fileBasename (const char* const filename)
+{
+  const char* p = strrchr (filename, '/');
+  return (p ? p + 1 : filename);
+}
+
+
+/* NAME
+ *   file_exists               Checks if a specified disk fie exists
+ *
+ * SYNOPSIS
+ *   exist = file_exists (fname)
+ *   bool exist                        TRUE if specified file exists
+ */
+
+bool
+file_exists (const char *fname)
+{
+  FILE *fp;
+  bool exist;
+
+  if (strlen(fname) == 0)
+    exist = false;
+  else if ((fp = fopen(fname, "r")) == NULL)
+    exist = false;
+  else {
+    fclose (fp);
+    exist = true;
+  }
+  
+  return (exist);
+}
+
+/*-----------------------------------------------------------------------------
+ *
+ * FUNCTION IDENTIFICATION
+ *
+ *     Name:           sys_fopen               Open a file for user
+ *     Date:           12-17-84
+ *     Programmer:     Kevin Rosenberg
+ *
+ * SYNOPSIS
+ *     fp = sys_fopen (filename, mode, progname)
+ *     FILE *fp                        Standard pointer to 'C' file
+ *     char *filename                  Name of file to open
+ *                                     If user enters a new name, it goes here
+ *     char *mode                      Mode to open file (std. 'C')
+ *     char *progname                  Name of program calling this routine
+ *
+ * DESCRIPTION
+ *       This routine opens a file using the standard C fopen() routine.  If
+ *    the file is not found, the user is given to option to:
+ *
+ *             1 - Retry opening file with same name
+ *             2 - Enter new file name
+ *             3 - Abort and return to DOS
+ *
+ * CAUTIONS
+ *       If the the requested file is not found, the name of the file given
+ *    entered at keyboard will be returned in filename.  So, make sure there
+ *   is room for a maximum length filename (MAXFULLNAME)
+ *
+ *---------------------------------------------------------------------------*/
+
+FILE *
+sys_fopen (const char *filename, const char *mode, const char *progname)
+{
+  FILE *fp;
+  char  fname[256];    /* name used for call to fopen() */
+  char  c;                     /* keyboard response */
+
+  strncpy (fname, filename, sizeof(fname));
+
+  do {
+    if ((fp = fopen (fname, mode)) == NULL) {
+      cerr << endl;
+      cerr << "Can't open file " << fname << " [" << progname << "]" << endl;
+      cerr << "Enter: <R> - Retry | <N> - New name | <A> - Abort program --> ";
+      c = cio_kb_waitc ("RrNnAa", TRUE);
+      c = tolower(c);
+      cerr << c << endl;
+
+      if (c == 'r')            // Retry -- Nothing to do here 
+       ;                       
+      else if (c == 'a')       // Abort -- Exit to OS
+       exit (1);
+      else if (c == 'n') {     // New name - get from console
+       cerr << "Enter new file name -- ";
+       fgets (fname, sizeof(fname), stdin);
+       str_wrm_tail (fname);
+       cerr << endl;
+      } 
+    }
+  } while (fp == NULL);
+
+  return (fp);
+}