summaryrefslogtreecommitdiffabout
path: root/test/sleeptracker-decode.cc
authorMichael Krelin <hacker@klever.net>2008-04-05 11:17:33 (UTC)
committer Michael Krelin <hacker@klever.net>2008-04-05 11:17:33 (UTC)
commit04fb190243442e83349f129b523ab747e58100bf (patch) (unidiff)
treeddc28357fbe78b07fd3a5e0aa8088130bf305829 /test/sleeptracker-decode.cc
downloadnapkin-04fb190243442e83349f129b523ab747e58100bf.zip
napkin-04fb190243442e83349f129b523ab747e58100bf.tar.gz
napkin-04fb190243442e83349f129b523ab747e58100bf.tar.bz2
Initial commit into public repository0.0
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'test/sleeptracker-decode.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--test/sleeptracker-decode.cc49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/sleeptracker-decode.cc b/test/sleeptracker-decode.cc
new file mode 100644
index 0000000..77d4fbd
--- a/dev/null
+++ b/test/sleeptracker-decode.cc
@@ -0,0 +1,49 @@
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <fcntl.h>
4#include <unistd.h>
5#include <termios.h>
6#include <iostream>
7#include <stdexcept>
8#include <algorithm>
9#include <iterator>
10using namespace std;
11#include <napkin/st/decode.h>
12
13string str_f_time(const char *fmt,time_t t) {
14 struct tm tt;
15 localtime_r(&t,&tt);
16 char rv[1024];
17 strftime(rv,sizeof(rv),fmt,&tt);
18 return rv;
19}
20
21ostream& operator<<(ostream& o,const napkin::hypnodata_t& hd) {
22 o
23 << "Window is " << hd.window << endl
24 << "'To bed' time is " << str_f_time("%Y-%m-%d %H:%M",hd.to_bed) << endl
25 << "Alarm time is " << str_f_time("%Y-%m-%d %H:%M",hd.alarm) << endl
26 << "Data A is " << hd.data_a/60 << ":" << hd.data_a%60 << endl;
27 for(vector<time_t>::const_iterator i=hd.almost_awakes.begin();i!=hd.almost_awakes.end();++i)
28 o << " almost awake at " << str_f_time("%Y-%m-%d %H:%M:%S",*i) << endl;
29 return o;
30}
31
32int main(int/*argc*/,char **argv) {
33 try {
34 int fd = open(argv[1],O_RDONLY);
35 if(fd<0)
36 throw runtime_error("failed to open() data");
37 unsigned char buffer[1024];
38 int rb = read(fd,buffer,sizeof(buffer));
39 if(!(rb>0))
40 throw runtime_error("failed to read() data");
41 close(fd);
42
43 napkin::hypnodata_t hd;
44 cout << napkin::sleeptracker::decode(hd,buffer,rb);
45 }catch(exception& e) {
46 cerr << "oops: " << e.what() << endl;
47 }
48}
49