[go: up one dir, main page]

File: test-baro

package info (click to toggle)
altos 1.9.21-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 83,636 kB
  • sloc: ansic: 119,717; java: 42,907; makefile: 8,459; sh: 4,995; xml: 2,154; pascal: 2,008
file content (99 lines) | stat: -rwxr-xr-x 1,767 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/nickle

import File;

string timed_read(file f, int timeout) {
	thread reader = fork func() {
		try {
			return fgets(f);
		} catch Thread::signal(int i) {
			return "";
		}
	}();

	thread killer = fork func() {
		try {
			sleep (timeout);
			Thread::send_signal(reader, 1);
		} catch Thread::signal(int i) {
			return;
		}
	}();

	poly v = Thread::join(reader);
	Thread::send_signal(killer, 1);
	Thread::join(killer);
	if (is_string(v))
		return v;
	return "";
}

void flush_input(file f) {
	for (;;) {
		string s = timed_read(f, 200);
		if (s == "")
			break;
	}
}

string[*] baro(file f) {
	string[...] x = {};

	flush_input(f);
	fprintf (f, "B\n");
	flush(f);
	for (;;) {
		string l = timed_read(f, 1000);
		if (l == "") {
			File::fprintf(stderr, "read timedout\n");
			exit(1);
		}
		x[dim(x)] = l;
		if (String::index(l, "Altitude:") == 0)
			break;
	}
	return x;
}

string[*] find_baro(string[*] s, string match) {
	for (int i = 0; i < dim(s); i++)
		if (String::index(s[i], match) >= 0)
			return String::wordsplit(s[i], " ");
	return (string[*]) {};
}

bool
do_baro(file f) {
	string[*] i = baro(f);
	string[*] temp = find_baro(i, "Temperature");
	string[*] alt = find_baro(i, "Altitude");

	real temperature = string_to_integer(temp[2]) / 100.0;
	real altitude = string_to_integer(alt[1]);

	if (altitude < -50 || 3000 < altitude) {
		printf ("weird altitude %f\n", altitude);
		return false;
	}

	if (temperature < 20 || 30 < temperature) {
		printf ("weird temperature %f\n", temperature);
		return false;
	}

	printf ("altitude %f temperature %f\n", altitude, temperature);

	return true;
}

void main () {
	string	name = argv[1];
	file	f = open(name, "r+");
	bool ret = true;

	if (!do_baro(f))
		ret = false;
	exit (ret? 0 : 1);
}

main();