Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / readbin.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 #include "config.h"
25
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29
30 #include "definitions.h"
31 #include "exit_codes.h"
32
33 #ifdef WINDOWS
34 #pragma hrdstop
35 #include <condefs.h>
36 #endif
37
38 int main(int argc, char **argv)
39 {
40   FILE *fp;
41   double *data, x;
42   int q;
43   int one=0, zero=0, minusone=0, other=0, length=0, i, reverse=0;
44   int metal_er=0;
45   while((q=get_options(argc,argv,"Cr")) != -1)
46   switch (q)
47   {
48     case 'C':
49       print_copyright("2002");
50       exit_with_msg_and_exit_code("",OKAY);
51     break;
52     case 'r':
53       reverse=1;
54     break;
55     case '?':
56       usage_readbin();
57   }
58   if(argc-my_optind == 1)
59   {
60     fp=fopen(argv[my_optind],"rb");
61     if (fp==NULL)
62     {
63       fprintf(stderr,"Can't open %s for reading\n",argv[my_optind]);
64       exit_with_msg_and_exit_code("Can't open file for reading",CANT_OPEN_FILE_FOR_READING);
65     }
66     if (fseek(fp,0,SEEK_END) != 0)
67       exit_with_msg_and_exit_code("failed to fseek in readbin.c #1", FSEEK_FAILURE);
68     length=ftell(fp);
69     printf("file length=%d bytes. There are %ld pixels\n", length, (long)
70     length/sizeof(double));
71     data=dvector(0,length);
72     if( fseek(fp,0,SEEK_SET) != 0)
73       exit_with_msg_and_exit_code("failed to fseek in readbin.c #2", FSEEK_FAILURE);
74
75     if (fread(&(data[0]), (size_t) length, (size_t) 1,fp) != 1)
76       exit_with_msg_and_exit_code("can't read all fo the file in readbin.c", CANT_READ_ALL_OF_FILE);
77     for(i=0;i<length/sizeof(double);++i)
78     {
79        x=data[i];
80        if(reverse==1)
81          byteswap_doubles(&x);
82        if (fabs(x) <= 1e-15)
83          zero++;
84        else if ( x > 0.9999999999 && x < 1.000000000000001)
85          one++;
86        else if (x < -0.9999999999 && x > -1.000000000000001)
87          minusone++;
88        else if (x == METAL_ER)
89          metal_er++;
90        else 
91          other++;
92     }
93     free_dvector(data,0L,(long) length);
94   }
95   else
96     usage_readbin();
97   return(OKAY);
98 }