-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
75 lines (66 loc) · 1.59 KB
/
main.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
/*
* This source code is free and open source. You may use it as you
* see fit so long as this header comment stays put!
*
* Author MatCat
*/
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
long map(float x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void printHelp(char *argv[])
{
printf("Usage: %s <input> <output>\n",argv[0]);
}
int main(int argc, char *argv[])
{
printf("CSV to ARB Converter\n\nThis program is free and open source.\n\nWritten by MatCat\n");
if (argc < 3)
{
printf("\nPlease specify an input and output file.\n");
printHelp(argv);
} else {
printf("\nInput File: %s\n",argv[1]);
FILE *ifp, *ofp;
char *mode = "r";
char *oMode = "wb";
ifp = fopen(argv[1], mode);
ofp = fopen(argv[2], oMode);
if (ifp == NULL) {
fprintf(stderr, "Error opening %s\n",argv[1]);
exit(1);
}
if (ofp == NULL) {
fprintf(stderr, "Error opening %s\n", argv[2]);
exit(1);
}
char line[1000];
int lineCount = 0;
int charCount = 0;
while (fgets(line, sizeof line, ifp) != NULL) /* read a line from a file */ {
if (line[0] == '#')
{
; // Ignore Comments
}
else {
lineCount++;
char * pEnd;
float iValue;
long oValue;
iValue = strtof(line, &pEnd, 10);
oValue = map(iValue, -1, 1, 0, 16383) % 16384 + 32768;
charCount++;
putc(oValue & 0xFF, ofp);
charCount++;
putc(oValue >> 8, ofp);
}
}
printf("%d points, wrote %d bytes\n",lineCount,charCount);
fclose(ifp);
fclose(ofp);
}
return 1;
}