4 * UEMIS SDA file importer
5 * AUTHOR: Dirk Hohndel - Copyright 2011
7 * Licensed under the MIT license.
26 * following code is based on code found in at base64.sourceforge.net/b64.c
27 * AUTHOR: Bob Trower 08/04/01
28 * COPYRIGHT: Copyright (c) Trantor Standard Systems Inc., 2001
29 * NOTE: This source code may be used as you wish, subject to
33 * Translation Table to decode (created by Bob Trower)
35 static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
38 * decodeblock -- decode 4 '6-bit' characters into 3 8-bit binary bytes
40 static void decodeblock( unsigned char in[4], unsigned char out[3] ) {
41 out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
42 out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
43 out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
47 * decode a base64 encoded stream discarding padding, line breaks and noise
49 static void decode( uint8_t *inbuf, uint8_t *outbuf, int inbuf_len ) {
50 uint8_t in[4], out[3], v;
51 int i,len,indx_in=0,indx_out=0;
53 while (indx_in < inbuf_len) {
54 for (len = 0, i = 0; i < 4 && (indx_in < inbuf_len); i++ ) {
56 while ((indx_in < inbuf_len) && v == 0) {
58 v = ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
60 v = ((v == '$') ? 0 : v - 61);
62 if (indx_in < inbuf_len) {
71 decodeblock( in, out );
72 for( i = 0; i < len - 1; i++ )
73 outbuf[indx_out++] = out[i];
77 /* end code from Bob Trower */
79 /* small helper functions */
80 /* simpleregex allocates (and reallocates) the found buffer
81 * don't forget to free it when you are done
83 static int simpleregex(char *buffer, char *regex, char **found) {
88 if (regcomp(&re, regex, 0) !=0) {
89 fprintf(stderr,"internal error, regex failed!\n");
92 status = regexec(&re,buffer,5,match,0);
94 *found = realloc(*found,match[1].rm_eo-match[1].rm_so + 1);
95 strncpy(*found,buffer+match[1].rm_so,match[1].rm_eo-match[1].rm_so);
96 (*found)[match[1].rm_eo-match[1].rm_so] = '\0';
101 /* read in line of arbitrary length (important for SDA files that can
102 * have lines that are tens of kB long
103 * don't forget to free it when you are done
105 #define MYGETL_INCR 1024
106 static char * mygetline(FILE * f) {
113 if ((buf = realloc(buf,size)) == NULL)
115 fgets(buf+len,MYGETL_INCR,f);
117 } while (!feof(f) && buf[len-1]!='\n');
121 /* text matching, used to build very poor man's XML parser */
122 int matchit(FILE *infd, char *regex, char *typeregex, char **found) {
125 while (!feof(infd)) {
126 buffer = mygetline(infd);
127 if (buffer && simpleregex(buffer,regex,found)) {
128 buffer = mygetline(infd);
129 if (buffer && simpleregex(buffer,typeregex,found)) {
139 * convert the base64 data blog
141 int uemis_convert_base64(char *base64, uint8_t **data) {
144 len = strlen(base64);
145 datalen = (len/4 + 1)*3;
146 if (datalen < 0x123+0x25) {
147 /* less than header + 1 sample??? */
148 fprintf(stderr,"suspiciously short data block\n");
150 *data = malloc(datalen);
153 fprintf(stderr,"Out of memory\n");
156 decode(base64, *data, len);
158 if (memcmp(*data,"Dive\01\00\00",7))
159 fprintf(stderr,"Missing Dive100 header\n");
166 * parse uemis base64 data blob into struct dive
168 static void parse_divelog_binary(char *base64, struct dive **divep) {
172 struct sample *sample;
174 datalen = uemis_convert_base64(base64, &data);
176 /* first byte of divelog data is at offset 0x123 */
178 while ((i < datalen) && (*(uint16_t *)(data+i))) {
179 /* it seems that a dive_time of 0 indicates the end of the valid readings */
180 sample = prepare_sample(divep);
181 sample->time.seconds = *(uint16_t *)(data+i);
182 sample->depth.mm = (*(uint16_t *)(data+i+2) - 100) / 0.101428571 + 0.5;
183 sample->temperature.mkelvin = (*(uint16_t *)(data+i+4) * 100) + 273150;
184 sample->cylinderpressure.mbar= *(uint16_t *)(data+i+23) * 10;
185 sample->cylinderindex = *(uint8_t *)(data+i+22);
186 finish_sample(*divep, sample);
193 parse_uemis_file(char *divelogfilename,GError **error) {
198 FILE *divelogfile = fopen(divelogfilename,"r");
202 if (! matchit(divelogfile,"val key=\"date\"","<ts>\\([^<]*\\)</ts>",&found)) {
203 /* some error handling */
206 strptime(found, "%Y-%m-%dT%H:%M:%S", &tm);
207 dive->when = utc_mktime(&tm);
208 if (! matchit(divelogfile,"<val key=\"duration\">",
209 "<float>\\([0-9.]*\\)</float>", &found)) {
210 /* some error handling */
213 dive->duration.seconds = 60.0 * atof(found);
215 if (! matchit(divelogfile,"<val key=\"depth\">",
216 "<int>\\([0-9.]*\\)</int>", &found)) {
217 /* some error handling */
220 dive->maxdepth.mm = atof(found) / 0.10143 + 0.5;
222 if (! matchit(divelogfile,"<val key=\"file_content\">",
223 ">\\([a-zA-Z0-9+/]*\\)<", &found)) {
224 /* some error handling */
227 parse_divelog_binary(found,&dive);
235 * parse the two files extracted from the SDA
240 GtkFileFilter *filter = gtk_file_filter_new ();
241 gtk_file_filter_add_pattern (filter, "*.SDA");
242 gtk_file_filter_set_name(filter, "uemis Zurich SDA files");
243 dialog = gtk_file_chooser_dialog_new("Open File",
244 GTK_WINDOW(main_window),
245 GTK_FILE_CHOOSER_ACTION_OPEN,
246 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
247 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
249 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
250 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
252 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
255 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
257 GError *error = NULL;
258 while(filenames != NULL) {
259 filename = (char *)filenames->data;
260 parse_uemis_file(filename, &error);
269 filenames = g_slist_next(filenames);
271 g_slist_free(filenames);
274 gtk_widget_destroy(dialog);