aboutsummaryrefslogtreecommitdiff
path: root/src/pmt.c
blob: 13f3eeac7cca522f69c63d5f57d67fa86106b9f6 (plain)
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
#include "pmt.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "vector.h"
#include "util.h"

static int initialized = 0;

pmt pmts[MAX_PMTS];

int load_pmt_info()
{
    int i, j;
    char line[256];
    char *str;
    double value;
    int n;

    FILE *f = open_file("pmt.txt", "r");

    if (!f) {
        fprintf(stderr, "failed to open pmt.txt: %s\n", strerror(errno));
        return -1;
    }

    i = 0;
    n = 0;
    /* For the first pass, we just count how many values there are. */
    while (fgets(line, sizeof(line), f)) {
        size_t len = strlen(line);
        if (len && (line[len-1] != '\n')) {
            fprintf(stderr, "got incomplete line on line %i: '%s'\n", i, line);
            goto err;
        }

        i += 1;

        if (!len) continue;
        else if (line[0] == '#') continue;

        str = strtok(line," \n");

        j = 0;
        while (str) {
            value = strtod(str, NULL);
            switch (j) {
            case 0:
            case 1:
            case 2:
                pmts[n].pos[j] = value/10.0;
                break;
            case 3:
            case 4:
            case 5:
                pmts[n].normal[j-3] = value;
                break;
            case 6:
                pmts[n].pmt_type = value;
                break;
            }
            j += 1;
            str = strtok(NULL," \n");
        }

        normalize(pmts[n].normal);

        n += 1;
    }

    fclose(f);

    initialized = 1;

    return 0;

err:
    fclose(f);

    return -1;
}