File: open_fil.c

package info (click to toggle)
codonw 1.4.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,060 kB
  • sloc: ansic: 11,227; makefile: 209; sh: 170; perl: 36
file content (236 lines) | stat: -rwxr-xr-x 11,638 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**************************************************************************/
/* CodonW codon usage analysis package                                    */
/* Copyright (C) 2005            John F. Peden                            */
/* This program is free software; you can redistribute                    */
/* it and/or modify it under the terms of the GNU General Public License  */
/* as published by the Free Software Foundation; version 2 of the         */
/* License,                                                               */
/*                                                                        */
/* 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.,*/
/* 675 Mass Ave, Cambridge, MA 02139, USA.                                */
/*                                                                        */
/*                                                                        */
/* The author can be contacted by email (jfp#hanson-codonw@yahoo.com Anti-*/
/* Spam please change the # in my email to an _)                          */
/*                                                                        */
/* For the latest version and information see                             */
/* http://codonw.sourceforge.net 					  */
/**************************************************************************/

/* This is a general subroutine, so we might as well redefine TRUE & FALSE*/
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

/* What to do if we can't locate the file we where asked to open          */
/* On most systems we will try and be nice and show a choice of filenames */

#ifdef _DOS
#define no_file_found() system("dir/w");
#elif BSD || SYSV            
#define no_file_found() system("ls -F");
#elif defined (WIN32) || defined (_WIN) 
#define no_file_found() system("dir/w");
#else
#define no_file_found() printf("This would have presented a list of files\n\tbut I do not know howto your operating system\n");
#endif

/* Include header files                                                   */
#include <stdio.h>             
#include <limits.h> 
#include <stdlib.h>  
#include <string.h>
#include <ctype.h>         
#include "codonW.h"

/************** open_file **************************************************/
/* This subroutine is a front end to fopen open. It takes four parameters  */
/* the parameters are used to generate a user prompt for the               */
/* filename, and to give a suggested filename, to give the write perms     */
/* for the file, and whether or not to overwrite existing files.           */
/* File_needed is just a description of the file being opened. It is       */
/* assumed that if this descriptor is missing the file is to be opened     */
/* without further user input. If default_filename is blank then there is  */
/* no default_filename                                                     */
/* write_perm sets up the type of file being opened                        */
/* verbose tells this function whether to check if there is a         */
/* previous version of any file being opened for writing                   */
/***************************************************************************/

FILE *open_file(char *file_needed, char *default_filename, 
char *write_perm, int  verbose )
{
    char   infile_name[MAX_FILENAME_LEN]="";
    FILE  *input=NULL;
    char   temp[4];
    char  *answer = pm->junk;

    /**********************************************************************/
    /* If a string has been given for file_needed it is assumed           */
    /* that the user will have a choice of file_names to choose           */
    /* therefore (s)he will be prompted for a name                        */
    /* if a default filename was supplied by the calling function this    */
    /* will be suggested as well, otherwise there is no default           */
    /**********************************************************************/

    if ( strlen(file_needed)) {
        while (!strlen(infile_name) )  {
            printf("\nName of %s (h for help) [%s] ", 
                      file_needed,default_filename);
            gets(infile_name);                          /* get filename   */
            
            if ( WasHelpCalled ( infile_name ) ) {
                     chelp("open_file_query");          /* Help ....      */
                     infile_name[0]='\0';
                     continue;
                }

            if ( !strlen(infile_name) && default_filename )
                strcpy(infile_name, default_filename);
        }                                         /* end of get filename  */
    } else if ( strlen(default_filename) )        /* use default filename */
        strcpy(infile_name, default_filename);
    else {                                        /* not enough info      */
        fprintf(stderr, "Programming error: no filename supplied\n");
        my_exit (0,"open file");
    }


    /**********************************************************************/
    /* At this point infile_name contains a possible filename             */
    /* Depending on the mode (write_perm) string this is tested 3 ways    */
    /*                                                                    */
    /* (r or r+) Test if the file exists if not, all the files in the     */
    /* current directory are listed and the the user is prompted for      */
    /* an alternative name or they may quit the programme                 */
    /*                                                                    */
    /* (a, a+) Not tested, just open the file                             */
    /*                                                                    */
    /* (w, w+) If the variable verbose = FALSE then no test          */
    /* If verbose == TRUE then the file is checked to see if         */
    /* it already exsists, if it does then the user is prompted for       */
    /* either for permission to overwrite this file or to                 */
    /* suggest an alternative file_name which is then tested as well      */
    /* the user can type q to quit at any stage of this prompting process */
    /**********************************************************************/

    if ( !strcmp(write_perm, "r") || !strcmp(write_perm, "r+") 
       ||!strcmp(write_perm, "rb") ){
        while ( !(input = fopen (infile_name , write_perm )))  {
            fprintf(stderr,"\nThese are the files in the current directory "
                "I cannot find %.*s \n\n",strlen(infile_name),infile_name);
            no_file_found();
            fprintf(stderr, "\n\nPlease enter another filename, "
                " (Q)uit, (H)elp [%s] ",infile_name);
            gets(answer);
            
            if (strlen (answer)==1 && 
                   ((char)toupper((int)answer[0])=='Q'))
                   my_exit(2,"open_file");
            else if (WasHelpCalled ( infile_name )){
                   chelp ("File_not_found");
                }
            else if (strlen (answer))
                strcpy (infile_name, answer);  
		}                                     /* end of while loop */       
        strcpy ( answer,infile_name);           /* allow transfer    */
        return input;
    }                                               

    /************************* Append  ***********************************/
    else if ( !strcmp(write_perm, "a") || !strcmp(write_perm, "a+")
           || !strcmp(write_perm, "ab") ) {
        input = fopen (infile_name, write_perm);
        strcpy ( answer,infile_name);      
        return input;
    }                                              
    /************************* Write    **********************************/
    else if ( !strcmp( write_perm, "w") || !strcmp(write_perm, "w+") 
            ||!strcmp( write_perm, "wb") ) {

         while ( verbose == TRUE ) {
            if ( (input = fopen (infile_name , "r")) ) {
                fclose(input);                  /* close the filehandle  */
                fprintf(stderr, "\nWarning :File %.*s "
                    "exists already \n\tDo you wish to"
                    " overwrite ? (y/n/h/q)\t [y] ",
                    strlen(infile_name), infile_name);
                fgets(temp, 3, stdin);

                switch (toupper( (int) temp[0])) {
                case 'Y': 
                case '\0': 
                case '\n':
                    verbose = FALSE;
                    continue;
                case 'Q':
                    my_exit(2,"open_file2");
                    break;
                case 'H':
                    chelp("file_exists");
                    continue;
                    break;
                default:
                    fprintf(stderr, 
                        "\nYou decided not to overwrite, please enter\n"
                        " another filename, (q)uit, (a)ppend, (h)elp \n"
                        " (a/q/h/filename)\t[a] ");
                    gets(answer);
                }

                /* if the answer is 'a' then the default file is opened  */
                /* as appendable else if 'q' then the programme exits    */
                /* anything else is taken as a file name                 */

                if ( strlen(answer) <= 1 ) {
                    switch (toupper( (int) answer[0])) {
                    case 'Q':
                        return (NULL);
                    case 'A':
                    case '\0':
                    case'\n':
                        verbose = FALSE;  /* leave the while loop   */
                        strcpy(write_perm, "a+");
                        break;
                    case 'H':
                     chelp("file_append");        
                     continue;
                     break;
                    default:
                        continue;
                    };                                /* end of switch   */
                }               
            } else                              /* filename is unique    */
              verbose = FALSE;             /* exit the while loop   */
              }                                 /* match while preserve  */
        input = fopen (infile_name,write_perm); /* opens filehandle      */
        strcpy ( answer,infile_name);         
        return input;
    }                                           /* matchs if w or w+     */
    return (NULL);
}

/************** Main just for testing purposes ***************************/
/* uncomment to test function as a standalone subroutine                 */
/* will also need to replace my_exit with exit calls                     */
/*************************************************************************/
/*  main ()
 {
 FILE *test=NULL;
 if( test = open_file( "test file","","r",NULL))
    printf( "Success\n");
 else 
    printf( "Failed\n");
 } */
/*************************************************************************/