-rw-r--r-- | test/.gitignore | 4 | ||||
-rw-r--r-- | test/Makefile.am | 7 | ||||
-rw-r--r-- | test/sleeptracker-decode.cc | 49 |
3 files changed, 60 insertions, 0 deletions
diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..579d0cc --- a/dev/null +++ b/test/.gitignore | |||
@@ -0,0 +1,4 @@ | |||
1 | /sleeptracker-decode | ||
2 | /sleeptracker-decode.o | ||
3 | /.libs | ||
4 | /.deps | ||
diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 0000000..09c3ed7 --- a/dev/null +++ b/test/Makefile.am | |||
@@ -0,0 +1,7 @@ | |||
1 | noinst_PROGRAMS = sleeptracker-decode | ||
2 | |||
3 | INCLUDES = -I${top_srcdir}/include/ ${MODULES_CFLAGS} | ||
4 | LIBS = ${top_builddir}/lib/libnapkin.la | ||
5 | |||
6 | sleeptracker_decode_SOURCES = sleeptracker-decode.cc | ||
7 | sleeptracker_decode_DEPENDENCIES = ${LIBS} | ||
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> | ||
10 | using namespace std; | ||
11 | #include <napkin/st/decode.h> | ||
12 | |||
13 | string 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 | |||
21 | ostream& 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 | |||
32 | int 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 | |||