datatrak: some codings style fixes and fopen() usage

- subsurface_fopen() is needed
- translated one puts("") message
- sizeof(unsigned char | char) is always 1
- pointer symbol (*) location
- padding fixes
- braces fixes

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Lubomir I. Ivanov 2015-03-11 00:23:14 +02:00 committed by Dirk Hohndel
parent 76ce2b095b
commit 2a1a868db2
3 changed files with 99 additions and 106 deletions

View file

@ -16,7 +16,6 @@ unsigned int tmp_2bytes;
char is_nitrox, is_O2, is_SCR; char is_nitrox, is_O2, is_SCR;
unsigned long tmp_4bytes; unsigned long tmp_4bytes;
static unsigned int two_bytes_to_int(unsigned char x, unsigned char y) static unsigned int two_bytes_to_int(unsigned char x, unsigned char y)
{ {
return (x << 8) + y; return (x << 8) + y;
@ -24,16 +23,15 @@ static unsigned int two_bytes_to_int(unsigned char x, unsigned char y)
static unsigned long four_bytes_to_long(unsigned char x, unsigned char y, unsigned char z, unsigned char t) static unsigned long four_bytes_to_long(unsigned char x, unsigned char y, unsigned char z, unsigned char t)
{ {
return (((long)x << 24) + ((long)y << 16) + ((long)z << 8) + ((long)t)); return ((long)x << 24) + ((long)y << 16) + ((long)z << 8) + (long)t;
} }
static unsigned char *byte_to_bits(unsigned char byte) static unsigned char *byte_to_bits(unsigned char byte)
{ {
unsigned char i, *bits = (unsigned char*) malloc(8 * sizeof(unsigned char)); unsigned char i, *bits = (unsigned char *)malloc(8);
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++)
bits[i] = (byte & (1 << i)); bits[i] = byte & (1 << i);
}
return bits; return bits;
} }
@ -46,30 +44,28 @@ static unsigned char* byte_to_bits(unsigned char byte)
static time_t date_time_to_ssrfc(unsigned long date, int time) static time_t date_time_to_ssrfc(unsigned long date, int time)
{ {
time_t tmp; time_t tmp;
tmp = ((date - 135140) * 86400) + (time * 60); tmp = (date - 135140) * 86400 + time * 60;
return tmp; return tmp;
} }
static unsigned char to_8859(unsigned char char_cp850) static unsigned char to_8859(unsigned char char_cp850)
{ {
unsigned char outchar; static const unsigned char char_8859[46] = { 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
unsigned char char_8859[46] = {0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x66, 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x66,
0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1 }; 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1 };
outchar = char_8859[char_cp850 - 0x80]; return char_8859[char_cp850 - 0x80];
return (outchar);
} }
static char *to_utf8(unsigned char *in_string) static char *to_utf8(unsigned char *in_string)
{ {
int outlen, inlen, i = 0, j = 0; int outlen, inlen, i = 0, j = 0;
inlen = strlen(in_string); inlen = strlen(in_string);
outlen = (inlen * 2) + 1; outlen = inlen * 2 + 1;
char *out_string = calloc(outlen, sizeof(char)); char *out_string = calloc(outlen, 1);
for (i = 0; i < inlen; i++) { for (i = 0; i < inlen; i++) {
if (in_string[i] < 127) if (in_string[i] < 127)
out_string[j] = in_string[i]; out_string[j] = in_string[i];
@ -83,7 +79,7 @@ static char *to_utf8(unsigned char *in_string)
j++; j++;
} }
out_string[j + 1] = '\0'; out_string[j + 1] = '\0';
return(out_string); return out_string;
} }
/* /*
@ -97,10 +93,10 @@ static struct sample *dtrak_profile(struct dive *dt_dive, FILE* archivo)
struct divecomputer *dc = &dt_dive->dc; struct divecomputer *dc = &dt_dive->dc;
for (i = 1; i <= dt_dive->dc.alloc_samples; i++) { for (i = 1; i <= dt_dive->dc.alloc_samples; i++) {
fread(&lector_bytes, sizeof(unsigned char), 2, archivo); fread(&lector_bytes, 1, 2, archivo);
interval= 20 * (i + 1); interval= 20 * (i + 1);
sample = add_sample(sample, interval, dc); sample = add_sample(sample, interval, dc);
sample->depth.mm = ((two_bytes_to_int(lector_bytes[0], lector_bytes[1]) & 0xFFC0) * 1000 / 410); sample->depth.mm = (two_bytes_to_int(lector_bytes[0], lector_bytes[1]) & 0xFFC0) * 1000 / 410;
byte = byte_to_bits(two_bytes_to_int(lector_bytes[0], lector_bytes[1]) & 0x003F); byte = byte_to_bits(two_bytes_to_int(lector_bytes[0], lector_bytes[1]) & 0x003F);
if (byte[0] != 0) if (byte[0] != 0)
sample->in_deco = true; sample->in_deco = true;
@ -124,12 +120,12 @@ static struct sample *dtrak_profile(struct dive *dt_dive, FILE* archivo)
} }
j = 0; j = 0;
} }
if (is_O2)
// In commit 5f44fdd setpoint replaced po2, so although this is not necesarily CCR dive ... // In commit 5f44fdd setpoint replaced po2, so although this is not necesarily CCR dive ...
if (is_O2)
sample->setpoint.mbar = calculate_depth_to_mbar(sample->depth.mm, dt_dive->surface_pressure, 0) * o2percent / 100; sample->setpoint.mbar = calculate_depth_to_mbar(sample->depth.mm, dt_dive->surface_pressure, 0) * o2percent / 100;
j++; j++;
} }
return(sample); return sample;
} }
/* /*
@ -138,21 +134,21 @@ static struct sample *dtrak_profile(struct dive *dt_dive, FILE* archivo)
*/ */
static dtrakheader read_file_header(FILE *archivo) static dtrakheader read_file_header(FILE *archivo)
{ {
dtrakheader fileheader = {0,0,0,0}; dtrakheader fileheader = { 0 };
const short headerbytes = 12; const short headerbytes = 12;
unsigned char *lector = (unsigned char *) malloc(headerbytes * sizeof(unsigned char)); unsigned char *lector = (unsigned char *)malloc(headerbytes);
fread(lector, sizeof(unsigned char), headerbytes, archivo); fread(lector, 1, headerbytes, archivo);
if (two_bytes_to_int(lector[0], lector[1]) != 0xA100) { if (two_bytes_to_int(lector[0], lector[1]) != 0xA100) {
puts("Error, el archivo no parece un divelog DATATRAK"); puts("Error: the file does not appear to be a DATATRAK divelog");
return(fileheader); return fileheader;
} }
fileheader.header = (lector[0] << 8) + lector[1]; fileheader.header = (lector[0] << 8) + lector[1];
fileheader.dc_serial_1 = two_bytes_to_int(lector[2], lector[3]); fileheader.dc_serial_1 = two_bytes_to_int(lector[2], lector[3]);
fileheader.dc_serial_2 = two_bytes_to_int(lector[4], lector[5]); fileheader.dc_serial_2 = two_bytes_to_int(lector[4], lector[5]);
fileheader.divesNum = two_bytes_to_int(lector[7], lector[6]); fileheader.divesNum = two_bytes_to_int(lector[7], lector[6]);
free(lector); free(lector);
return(fileheader); return fileheader;
} }
@ -176,19 +172,18 @@ static struct dive dt_dive_parser(FILE* archivo, struct dive *dt_dive)
* Parse byte to byte till next dive entry * Parse byte to byte till next dive entry
*/ */
n = 0; n = 0;
fread(&lector_bytes[n], sizeof(char), 1, archivo); fread(&lector_bytes[n], 1, 1, archivo);
while (lector_bytes[n] != 0xA0) { while (lector_bytes[n] != 0xA0)
fread(&lector_bytes[n], sizeof(char), 1, archivo); fread(&lector_bytes[n], 1, 1, archivo);
}
/* /*
* Found dive header 0xA000, verify second byte * Found dive header 0xA000, verify second byte
*/ */
fread(&lector_bytes[n+1], sizeof(char), 1, archivo); fread(&lector_bytes[n+1], 1, 1, archivo);
if (two_bytes_to_int(lector_bytes[0], lector_bytes[1]) != 0xA000) { if (two_bytes_to_int(lector_bytes[0], lector_bytes[1]) != 0xA000) {
printf("ERROR, Byte = %4x\n", two_bytes_to_int(lector_bytes[0], lector_bytes[1])); printf("Error: byte = %4x\n", two_bytes_to_int(lector_bytes[0], lector_bytes[1]));
dt_dive = NULL; dt_dive = NULL;
return(*dt_dive); return *dt_dive;
} }
/* /*
@ -375,7 +370,7 @@ static struct dive dt_dive_parser(FILE* archivo, struct dive *dt_dive)
* Air used in bar*100. * Air used in bar*100.
*/ */
read_bytes(2); read_bytes(2);
if ((tmp_2bytes != 0x7FFF) && (dt_dive->cylinder[0].type.size.mliter)) if (tmp_2bytes != 0x7FFF && dt_dive->cylinder[0].type.size.mliter)
dt_dive->cylinder[0].gas_used.mliter = dt_dive->cylinder[0].type.size.mliter * (tmp_2bytes / 100.0); dt_dive->cylinder[0].gas_used.mliter = dt_dive->cylinder[0].type.size.mliter * (tmp_2bytes / 100.0);
/* /*
@ -484,13 +479,12 @@ static struct dive dt_dive_parser(FILE* archivo, struct dive *dt_dive)
tmp_notes_str ? tmp_notes_str : "", tmp_notes_str ? tmp_notes_str : "",
QT_TRANSLATE_NOOP("gettextFromC", "Datatrak/Wlog notes"), QT_TRANSLATE_NOOP("gettextFromC", "Datatrak/Wlog notes"),
tmp_string1); tmp_string1);
dt_dive->notes = calloc((len +1), sizeof(char)); dt_dive->notes = calloc((len +1), 1);
dt_dive->notes = memcpy(dt_dive->notes, buffer, len); dt_dive->notes = memcpy(dt_dive->notes, buffer, len);
free(tmp_string1); free(tmp_string1);
if (tmp_notes_str != NULL) { if (tmp_notes_str != NULL)
free(tmp_notes_str); free(tmp_notes_str);
} }
}
/* /*
* Alarms 1 - Bit table - Not in Subsurface, we use the profile * Alarms 1 - Bit table - Not in Subsurface, we use the profile
@ -521,55 +515,55 @@ static struct dive dt_dive_parser(FILE* archivo, struct dive *dt_dive)
*/ */
read_bytes(1); read_bytes(1);
switch (tmp_1byte) { switch (tmp_1byte) {
case (0x00): case 0x00:
dt_dive->dc.model = strdup(QT_TRANSLATE_NOOP("gettextFromC", "Manually entered dive")); dt_dive->dc.model = strdup(QT_TRANSLATE_NOOP("gettextFromC", "Manually entered dive"));
break; break;
case (0x1C): case 0x1C:
dt_dive->dc.model = strdup("Aladin Air"); dt_dive->dc.model = strdup("Aladin Air");
break; break;
case (0x1D): case 0x1D:
dt_dive->dc.model = strdup("Spiro Monitor 2 plus"); dt_dive->dc.model = strdup("Spiro Monitor 2 plus");
break; break;
case (0x1E): case 0x1E:
dt_dive->dc.model = strdup("Aladin Sport"); dt_dive->dc.model = strdup("Aladin Sport");
break; break;
case (0x1F): case 0x1F:
dt_dive->dc.model = strdup("Aladin Pro"); dt_dive->dc.model = strdup("Aladin Pro");
break; break;
case (0x34): case 0x34:
dt_dive->dc.model = strdup("Aladin Air X"); dt_dive->dc.model = strdup("Aladin Air X");
break; break;
case (0x3D): case 0x3D:
dt_dive->dc.model = strdup("Spiro Monitor 2 plus"); dt_dive->dc.model = strdup("Spiro Monitor 2 plus");
break; break;
case (0x3F): case 0x3F:
dt_dive->dc.model = strdup("Mares Genius"); dt_dive->dc.model = strdup("Mares Genius");
break; break;
case (0x44): case 0x44:
dt_dive->dc.model = strdup("Aladin Air X"); dt_dive->dc.model = strdup("Aladin Air X");
break; break;
case (0x48): case 0x48:
dt_dive->dc.model = strdup("Spiro Monitor 3 Air"); dt_dive->dc.model = strdup("Spiro Monitor 3 Air");
break; break;
case (0xA4): case 0xA4:
dt_dive->dc.model = strdup("Aladin Air X O2"); dt_dive->dc.model = strdup("Aladin Air X O2");
break; break;
case (0xB1): case 0xB1:
dt_dive->dc.model = strdup("Citizen Hyper Aqualand"); dt_dive->dc.model = strdup("Citizen Hyper Aqualand");
break; break;
case (0xB2): case 0xB2:
dt_dive->dc.model = strdup("Citizen ProMaster"); dt_dive->dc.model = strdup("Citizen ProMaster");
break; break;
case (0xB3): case 0xB3:
dt_dive->dc.model = strdup("Mares Guardian"); dt_dive->dc.model = strdup("Mares Guardian");
break; break;
case (0xBC): case 0xBC:
dt_dive->dc.model = strdup("Aladin Air X Nitrox"); dt_dive->dc.model = strdup("Aladin Air X Nitrox");
break; break;
case (0xF4): case 0xF4:
dt_dive->dc.model = strdup("Aladin Air X Nitrox"); dt_dive->dc.model = strdup("Aladin Air X Nitrox");
break; break;
case (0xFF): case 0xFF:
dt_dive->dc.model = strdup("Aladin Pro Nitrox"); dt_dive->dc.model = strdup("Aladin Pro Nitrox");
break; break;
default: default:
@ -594,7 +588,6 @@ static struct dive dt_dive_parser(FILE* archivo, struct dive *dt_dive)
read_bytes(2); read_bytes(2);
profile_length = tmp_2bytes; profile_length = tmp_2bytes;
if (profile_length != 0) { if (profile_length != 0) {
/* /*
* 8 x 2 bytes for the tissues saturation useless for subsurface * 8 x 2 bytes for the tissues saturation useless for subsurface
* and other 6 bytes without known use * and other 6 bytes without known use
@ -624,7 +617,7 @@ static struct dive dt_dive_parser(FILE* archivo, struct dive *dt_dive)
* 2bytes per sample plus another one each three samples. Also includes the * 2bytes per sample plus another one each three samples. Also includes the
* bytes jumped over (22) and the nitrox (2) or O2 (3). * bytes jumped over (22) and the nitrox (2) or O2 (3).
*/ */
int samplenum = is_O2 ? ((profile_length - 25) * 3 / 8) : ((profile_length - 24) * 3 / 7); int samplenum = is_O2 ? (profile_length - 25) * 3 / 8 : (profile_length - 24) * 3 / 7;
dc->events = calloc(samplenum, sizeof(struct event)); dc->events = calloc(samplenum, sizeof(struct event));
dc->alloc_samples = samplenum; dc->alloc_samples = samplenum;
@ -646,7 +639,7 @@ static struct dive dt_dive_parser(FILE* archivo, struct dive *dt_dive)
dt_dive->cylinder[0].end.mbar = dt_dive->cylinder[0].start.mbar - dt_dive->cylinder[0].end.mbar = dt_dive->cylinder[0].start.mbar -
((dt_dive->cylinder[0].gas_used.mliter / dt_dive->cylinder[0].type.size.mliter) * 1000); ((dt_dive->cylinder[0].gas_used.mliter / dt_dive->cylinder[0].type.size.mliter) * 1000);
} }
return(*dt_dive); return *dt_dive;
} }
void datatrak_import(const char *file, struct dive_table *table) void datatrak_import(const char *file, struct dive_table *table)
@ -655,8 +648,8 @@ void datatrak_import(const char *file, struct dive_table *table)
dtrakheader *fileheader = (dtrakheader *)malloc(sizeof(dtrakheader)); dtrakheader *fileheader = (dtrakheader *)malloc(sizeof(dtrakheader));
int i = 0; int i = 0;
if ((archivo = fopen(file, "rb")) == NULL) { if ((archivo = subsurface_fopen(file, "rb")) == NULL) {
puts("Error, couldn't open the file"); puts("Error: couldn't open the file");
return; return;
} }
@ -666,12 +659,12 @@ void datatrak_import(const char *file, struct dive_table *table)
*fileheader = read_file_header(archivo); *fileheader = read_file_header(archivo);
if (fileheader->header == 0) if (fileheader->header == 0)
puts("Error. Not a DATATRAK/WLOG file\n"); puts("Error: not a DATATRAK/WLOG file\n");
while (i < fileheader->divesNum) { while (i < fileheader->divesNum) {
struct dive *ptdive = alloc_dive(); struct dive *ptdive = alloc_dive();
*ptdive = dt_dive_parser(archivo, ptdive); *ptdive = dt_dive_parser(archivo, ptdive);
if (!ptdive) if (!ptdive)
puts("Error, no dive\n"); puts("Error: no dive\n");
i++; i++;
record_dive(ptdive); record_dive(ptdive);
} }

View file

@ -27,8 +27,8 @@ typedef struct dtrakheader_ {
} }
#define read_string(_property) \ #define read_string(_property) \
_property = (unsigned char *) calloc((tmp_1byte + 1), sizeof(unsigned char));\ _property = (unsigned char *)calloc(tmp_1byte + 1, 1); \
fread (_property, sizeof(unsigned char), tmp_1byte, archivo);\ fread(_property, 1, tmp_1byte, archivo); \
_property = strcat(to_utf8(_property), "");\ _property = strcat(to_utf8(_property), "");
#endif // DATATRAK_HEADER_H #endif // DATATRAK_HEADER_H

2
file.c
View file

@ -457,7 +457,7 @@ int parse_file(const char *filename)
} }
/* DataTrak/Wlog */ /* DataTrak/Wlog */
if (fmt && (!strcasecmp(fmt + 1, "LOG"))) { if (fmt && !strcasecmp(fmt + 1, "LOG")) {
datatrak_import(filename, &dive_table); datatrak_import(filename, &dive_table);
return 0; return 0;
} }