core: convert file.c to C++

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-02-28 07:50:11 +01:00
parent b060df91e7
commit 9cd5c0e0e6
3 changed files with 31 additions and 37 deletions

View file

@ -41,7 +41,7 @@ SOURCES += subsurface-mobile-main.cpp \
core/qt-init.cpp \ core/qt-init.cpp \
core/subsurfacesysinfo.cpp \ core/subsurfacesysinfo.cpp \
core/windowtitleupdate.cpp \ core/windowtitleupdate.cpp \
core/file.c \ core/file.cpp \
core/fulltext.cpp \ core/fulltext.cpp \
core/subsurfacestartup.cpp \ core/subsurfacestartup.cpp \
core/pref.c \ core/pref.c \

View file

@ -89,7 +89,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
exif.cpp exif.cpp
exif.h exif.h
extradata.h extradata.h
file.c file.cpp
file.h file.h
filterconstraint.cpp filterconstraint.cpp
filterconstraint.h filterconstraint.h

View file

@ -31,7 +31,7 @@
#define O_BINARY 0 #define O_BINARY 0
#endif #endif
int readfile(const char *filename, struct memblock *mem) extern "C" int readfile(const char *filename, struct memblock *mem)
{ {
int ret, fd; int ret, fd;
struct stat st; struct stat st;
@ -52,7 +52,7 @@ int readfile(const char *filename, struct memblock *mem)
ret = 0; ret = 0;
if (!st.st_size) if (!st.st_size)
goto out; goto out;
buf = malloc(st.st_size + 1); buf = (char *)malloc(st.st_size + 1);
ret = -1; ret = -1;
errno = ENOMEM; errno = ENOMEM;
if (!buf) if (!buf)
@ -80,19 +80,18 @@ out:
static void zip_read(struct zip_file *file, const char *filename, struct divelog *log) static void zip_read(struct zip_file *file, const char *filename, struct divelog *log)
{ {
int size = 1024, n, read = 0; int size = 1024, n, read = 0;
char *mem = malloc(size); std::vector<char> mem(size);
while ((n = zip_fread(file, mem + read, size - read)) > 0) { while ((n = zip_fread(file, mem.data() + read, size - read)) > 0) {
read += n; read += n;
size = read * 3 / 2; size = read * 3 / 2;
mem = realloc(mem, size); mem.resize(size);
} }
mem[read] = 0; mem[read] = 0;
(void) parse_xml_buffer(filename, mem, read, log, NULL); (void) parse_xml_buffer(filename, mem.data(), read, log, NULL);
free(mem);
} }
int try_to_open_zip(const char *filename, struct divelog *log) extern "C" int try_to_open_zip(const char *filename, struct divelog *log)
{ {
int success = 0; int success = 0;
/* Grr. libzip needs to re-open the file, it can't take a buffer */ /* Grr. libzip needs to re-open the file, it can't take a buffer */
@ -119,11 +118,8 @@ int try_to_open_zip(const char *filename, struct divelog *log)
return success; return success;
} }
static int db_test_func(void *param, int columns, char **data, char **column) static int db_test_func(void *, int, char **data, char **)
{ {
UNUSED(param);
UNUSED(columns);
UNUSED(column);
return *data[0] == '0'; return *data[0] == '0';
} }
@ -149,7 +145,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Suunto DM5 database format */ /* Testing if DB schema resembles Suunto DM5 database format */
retval = sqlite3_exec(handle, dm5_test, &db_test_func, 0, NULL); retval = sqlite3_exec(handle, dm5_test, &db_test_func, 0, NULL);
if (!retval) { if (!retval) {
retval = parse_dm5_buffer(handle, filename, mem->buffer, mem->size, log); retval = parse_dm5_buffer(handle, filename, (char *)mem->buffer, mem->size, log);
sqlite3_close(handle); sqlite3_close(handle);
return retval; return retval;
} }
@ -157,7 +153,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Suunto DM4 database format */ /* Testing if DB schema resembles Suunto DM4 database format */
retval = sqlite3_exec(handle, dm4_test, &db_test_func, 0, NULL); retval = sqlite3_exec(handle, dm4_test, &db_test_func, 0, NULL);
if (!retval) { if (!retval) {
retval = parse_dm4_buffer(handle, filename, mem->buffer, mem->size, log); retval = parse_dm4_buffer(handle, filename, (char *)mem->buffer, mem->size, log);
sqlite3_close(handle); sqlite3_close(handle);
return retval; return retval;
} }
@ -165,7 +161,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Shearwater database format */ /* Testing if DB schema resembles Shearwater database format */
retval = sqlite3_exec(handle, shearwater_test, &db_test_func, 0, NULL); retval = sqlite3_exec(handle, shearwater_test, &db_test_func, 0, NULL);
if (!retval) { if (!retval) {
retval = parse_shearwater_buffer(handle, filename, mem->buffer, mem->size, log); retval = parse_shearwater_buffer(handle, filename, (char *)mem->buffer, mem->size, log);
sqlite3_close(handle); sqlite3_close(handle);
return retval; return retval;
} }
@ -173,7 +169,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Shearwater cloud database format */ /* Testing if DB schema resembles Shearwater cloud database format */
retval = sqlite3_exec(handle, shearwater_cloud_test, &db_test_func, 0, NULL); retval = sqlite3_exec(handle, shearwater_cloud_test, &db_test_func, 0, NULL);
if (!retval) { if (!retval) {
retval = parse_shearwater_cloud_buffer(handle, filename, mem->buffer, mem->size, log); retval = parse_shearwater_cloud_buffer(handle, filename, (char *)mem->buffer, mem->size, log);
sqlite3_close(handle); sqlite3_close(handle);
return retval; return retval;
} }
@ -181,7 +177,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Atomic Cobalt database format */ /* Testing if DB schema resembles Atomic Cobalt database format */
retval = sqlite3_exec(handle, cobalt_test, &db_test_func, 0, NULL); retval = sqlite3_exec(handle, cobalt_test, &db_test_func, 0, NULL);
if (!retval) { if (!retval) {
retval = parse_cobalt_buffer(handle, filename, mem->buffer, mem->size, log); retval = parse_cobalt_buffer(handle, filename, (char *)mem->buffer, mem->size, log);
sqlite3_close(handle); sqlite3_close(handle);
return retval; return retval;
} }
@ -189,7 +185,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Divinglog database format */ /* Testing if DB schema resembles Divinglog database format */
retval = sqlite3_exec(handle, divinglog_test, &db_test_func, 0, NULL); retval = sqlite3_exec(handle, divinglog_test, &db_test_func, 0, NULL);
if (!retval) { if (!retval) {
retval = parse_divinglog_buffer(handle, filename, mem->buffer, mem->size, log); retval = parse_divinglog_buffer(handle, filename, (char *)mem->buffer, mem->size, log);
sqlite3_close(handle); sqlite3_close(handle);
return retval; return retval;
} }
@ -197,7 +193,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Seac database format */ /* Testing if DB schema resembles Seac database format */
retval = sqlite3_exec(handle, seacsync_test, &db_test_func, 0, NULL); retval = sqlite3_exec(handle, seacsync_test, &db_test_func, 0, NULL);
if (!retval) { if (!retval) {
retval = parse_seac_buffer(handle, filename, mem->buffer, mem->size, log); retval = parse_seac_buffer(handle, filename, (char *)mem->buffer, mem->size, log);
sqlite3_close(handle); sqlite3_close(handle);
return retval; return retval;
} }
@ -227,9 +223,10 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
static int open_by_filename(const char *filename, const char *fmt, struct memblock *mem, struct divelog *log) static int open_by_filename(const char *filename, const char *fmt, struct memblock *mem, struct divelog *log)
{ {
// hack to be able to provide a comment for the translated string // hack to be able to provide a comment for the translated string
static char *csv_warning = QT_TRANSLATE_NOOP3("gettextFromC", static struct { const char *s; const char *comment; } csv_warning =
"Cannot open CSV file %s; please use Import log file dialog", QT_TRANSLATE_NOOP3("gettextFromC",
"'Import log file' should be the same text as corresponding label in Import menu"); "Cannot open CSV file %s; please use Import log file dialog",
"'Import log file' should be the same text as corresponding label in Import menu");
/* Suunto Dive Manager files: SDE, ZIP; divelogs.de files: DLD */ /* Suunto Dive Manager files: SDE, ZIP; divelogs.de files: DLD */
if (!strcasecmp(fmt, "SDE") || !strcasecmp(fmt, "ZIP") || !strcasecmp(fmt, "DLD")) if (!strcasecmp(fmt, "SDE") || !strcasecmp(fmt, "ZIP") || !strcasecmp(fmt, "DLD"))
@ -237,7 +234,7 @@ static int open_by_filename(const char *filename, const char *fmt, struct memblo
/* CSV files */ /* CSV files */
if (!strcasecmp(fmt, "CSV")) if (!strcasecmp(fmt, "CSV"))
return report_error(translate("gettextFromC", csv_warning), filename); return report_error(translate("gettextFromC", csv_warning.s), filename);
/* Truly nasty intentionally obfuscated Cochran Anal software */ /* Truly nasty intentionally obfuscated Cochran Anal software */
if (!strcasecmp(fmt, "CAN")) if (!strcasecmp(fmt, "CAN"))
return try_to_open_cochran(filename, mem, log); return try_to_open_cochran(filename, mem, log);
@ -257,17 +254,17 @@ static int open_by_filename(const char *filename, const char *fmt, struct memblo
static int parse_file_buffer(const char *filename, struct memblock *mem, struct divelog *log) static int parse_file_buffer(const char *filename, struct memblock *mem, struct divelog *log)
{ {
int ret; int ret;
char *fmt = strrchr(filename, '.'); const char *fmt = strrchr(filename, '.');
if (fmt && (ret = open_by_filename(filename, fmt + 1, mem, log)) != 0) if (fmt && (ret = open_by_filename(filename, fmt + 1, mem, log)) != 0)
return ret; return ret;
if (!mem->size || !mem->buffer) if (!mem->size || !mem->buffer)
return report_error("Out of memory parsing file %s\n", filename); return report_error("Out of memory parsing file %s\n", filename);
return parse_xml_buffer(filename, mem->buffer, mem->size, log, NULL); return parse_xml_buffer(filename, (char *)mem->buffer, mem->size, log, NULL);
} }
bool remote_repo_uptodate(const char *filename, struct git_info *info) extern "C" bool remote_repo_uptodate(const char *filename, struct git_info *info)
{ {
char *current_sha = copy_string(saved_git_id); char *current_sha = copy_string(saved_git_id);
@ -286,11 +283,11 @@ bool remote_repo_uptodate(const char *filename, struct git_info *info)
return false; return false;
} }
int parse_file(const char *filename, struct divelog *log) extern "C" int parse_file(const char *filename, struct divelog *log)
{ {
struct git_info info; struct git_info info;
struct memblock mem; struct memblock mem;
char *fmt; const char *fmt;
int ret; int ret;
if (is_git_repository(filename, &info)) { if (is_git_repository(filename, &info)) {
@ -330,7 +327,7 @@ int parse_file(const char *filename, struct divelog *log)
/* Divesoft Freedom */ /* Divesoft Freedom */
if (fmt && (!strcasecmp(fmt + 1, "DLF"))) { if (fmt && (!strcasecmp(fmt + 1, "DLF"))) {
ret = parse_dlf_buffer(mem.buffer, mem.size, log); ret = parse_dlf_buffer((unsigned char *)mem.buffer, mem.size, log);
free(mem.buffer); free(mem.buffer);
return ret; return ret;
} }
@ -339,18 +336,15 @@ int parse_file(const char *filename, struct divelog *log)
if (fmt && !strcasecmp(fmt + 1, "LOG")) { if (fmt && !strcasecmp(fmt + 1, "LOG")) {
struct memblock wl_mem; struct memblock wl_mem;
const char *t = strrchr(filename, '.'); const char *t = strrchr(filename, '.');
char *wl_name = memcpy(calloc(t - filename + 1, 1), filename, t - filename); std::string wl_name = std::string(filename, t - filename) + ".add";
wl_name = realloc(wl_name, strlen(wl_name) + 5); if((ret = readfile(wl_name.c_str(), &wl_mem)) < 0) {
wl_name = strcat(wl_name, ".add"); fprintf(stderr, "No file %s found. No WLog extensions.\n", wl_name.c_str());
if((ret = readfile(wl_name, &wl_mem)) < 0) {
fprintf(stderr, "No file %s found. No WLog extensions.\n", wl_name);
ret = datatrak_import(&mem, NULL, log); ret = datatrak_import(&mem, NULL, log);
} else { } else {
ret = datatrak_import(&mem, &wl_mem, log); ret = datatrak_import(&mem, &wl_mem, log);
free(wl_mem.buffer); free(wl_mem.buffer);
} }
free(mem.buffer); free(mem.buffer);
free(wl_name);
return ret; return ret;
} }