Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / get_file_pointer_with_right_filename.c
1 /* atlc - arbitrary transmission line calculator, for the analysis of
2 transmission lines are directional couplers. 
3
4 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either package_version 2
9 of the License, or (at your option) any later package_version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
19 USA.
20
21 Dr. David Kirkby, e-mail drkirkby at ntlworld.com 
22
23 */
24
25 /* This function take a filename with the extension .bmp (eg coax.bmp) 
26 and will produce files such as coax.V.bmp, coax.E.bmp, coax.E.bin etc */
27
28 #include "config.h"
29
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33
34 #ifdef HAVE_STRINGS_H
35 #include <strings.h>
36 #endif
37
38 #ifdef HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41
42 #include "definitions.h"
43 #include "exit_codes.h"
44
45 FILE *get_file_pointer_with_right_filename(char *filename, const char *extension)
46 {
47   char *basename, *temp;
48   FILE *fp;
49   basename=string(0,1024);
50   temp=string(0,1024);
51   (void) memset((void *)temp,0,(size_t) 1024);
52   (void) memset((void *)basename,0,(size_t) 1024);
53
54   basename=strncpy(basename,filename,strlen(filename)-4);
55   (void) strcpy(temp,basename);
56   (void) strcat(temp,extension);
57   if((fp=fopen(temp,"w+b"))==NULL)
58   {
59     fprintf(stderr,"Sorry, cant open %s for writing\n", temp);
60     exit_with_msg_and_exit_code("",CANT_OPEN_FOR_WRITING);
61   }
62   free_string(basename,0,1024);
63   free_string(temp,0,1024);
64   return(fp);
65 }