-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2freq.c
125 lines (114 loc) · 2.33 KB
/
csv2freq.c
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
// csv2freq.c
// Converts a CSV list of frequencies (in Hertz) into an array of
// frequencies (as integers).
// rev 1.0 Shabaz December 2013
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SYSFREQ 400E6
#define TWLEN 4294967296
void freq2ftw(double freq, unsigned char* data);
int
main(int argc, char *argv[])
{
FILE* infile;
FILE* outfile;
unsigned char c;
unsigned char freq_s[128];
double freq;
unsigned char data[4];
int freq_i;
int ret;
int notfirst=0;
int ctr=0;
int i;
if (argc<2)
{
fprintf(stderr, "%s: Error: Invalid args\n", argv[0]);
fprintf(stderr, "Syntax is: %s <infile> [outfile]\n", argv[0]);
exit(1);
}
infile=fopen(argv[1], "rb");
if (infile==NULL)
{
fprintf(stderr, "%s: Error: Unable to open input file '%s'\n", argv[0], argv[1]);
exit(1);
}
if (argc>2)
{
outfile=fopen(argv[2], "w");
}
else
{
outfile=fopen("out.txt", "w");
}
if (outfile==NULL)
{
fprintf(stderr, "%s: Error: Unable to create output file\n", argv[0]);
exit(1);
}
// read until we see a comma or EOF
// Once we see comma or EOF, we can convert to FTW format
i=0;
while(1)
{
ret=fread(&c, 1, 1, infile);
if ((ret!=1) || (c==',') || (c=='\n') || c=='\r' || (c==' '))
{
// We have seen a separator or whitespace or EOF
if (i==0) // no characters read?
{
if (ret!=1) // EOF
{
// we're done
break;
}
continue;
}
else
{
// some characters were read. Convert to FTW
freq_s[i]='\0'; // terminate the string
i=0;
sscanf(freq_s, "%lf", &freq);
freq_i=(int)freq;
//freq2ftw(freq, data); // data array now contains the 4-byte FTW
if (notfirst)
{
fprintf(outfile, ", ");
}
else
{
notfirst=1;
}
if (ctr%10==0) // do a newline every so often
{
fprintf(outfile, "\n");
}
ctr++;
fprintf(outfile, "%d", freq_i);
}
}
else
{
freq_s[i]=c;
i++;
}
} // end while(1)
fclose(infile);
fclose(outfile);
return(0);
}
// converts a frequency into the 4 data bytes that represent it
void
freq2ftw(double freq, unsigned char* data)
{
unsigned char* tptr;
unsigned int tword; // 32-bit tuning word
tptr=(unsigned char*)&tword;
tword=(unsigned int)((freq)*TWLEN/(SYSFREQ));
data[3]=*tptr; // adjust these lines for endian'ness
data[2]=*(tptr+1);
data[1]=*(tptr+2);
data[0]=*(tptr+3);
}