summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-01-07 11:12:48 +0100
committerGravatar Lubomir I. Ivanov <neolit123@gmail.com>2018-01-11 06:07:13 +0100
commite85ecdd9254a8bd222ccb2dfed7d3b3bb96c8294 (patch)
tree0483cdb3c5770ca75eeeb0cc2de787b0e7bdbd3c /core
parent86ef9fce7517313570838ca8e853132876035611 (diff)
downloadsubsurface-e85ecdd9254a8bd222ccb2dfed7d3b3bb96c8294.tar.gz
Introduce helper function empty_string()
There are ca. 50 constructs of the kind same_string(s, "") to test for empty or null strings. Replace them by the new helper function empty_string(). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core')
-rw-r--r--core/dive.c10
-rw-r--r--core/dive.h5
-rw-r--r--core/divelist.c4
-rw-r--r--core/divesite.c6
-rw-r--r--core/file.c4
-rw-r--r--core/libdivecomputer.c2
-rw-r--r--core/linux.c2
-rw-r--r--core/load-git.c2
-rw-r--r--core/macos.c2
-rw-r--r--core/parse.c2
-rw-r--r--core/qthelper.cpp14
-rw-r--r--core/subsurfacestartup.c2
12 files changed, 30 insertions, 25 deletions
diff --git a/core/dive.c b/core/dive.c
index 1d2168206..84acecb38 100644
--- a/core/dive.c
+++ b/core/dive.c
@@ -1190,7 +1190,7 @@ static struct event *find_previous_event(struct divecomputer *dc, struct event *
struct event *ev = dc->events;
struct event *previous = NULL;
- if (same_string(event->name, ""))
+ if (empty_string(event->name))
return NULL;
while (ev && ev != event) {
if (same_string(ev->name, event->name))
@@ -2954,7 +2954,7 @@ void taglist_cleanup(struct tag_entry **tag_list)
struct tag_entry **tl = tag_list;
while (*tl) {
/* skip tags that are empty or that we have seen before */
- if (same_string((*tl)->tag->name, "") || tag_seen_before(*tag_list, *tl)) {
+ if (empty_string((*tl)->tag->name) || tag_seen_before(*tag_list, *tl)) {
*tl = (*tl)->next;
continue;
}
@@ -3128,7 +3128,7 @@ int count_dives_with_tag(const char *tag)
struct dive *d;
for_each_dive (i, d) {
- if (same_string(tag, "")) {
+ if (empty_string(tag)) {
// count dives with no tags
if (d->tag_list == NULL)
counter++;
@@ -3148,9 +3148,9 @@ int count_dives_with_person(const char *person)
struct dive *d;
for_each_dive (i, d) {
- if (same_string(person, "")) {
+ if (empty_string(person)) {
// solo dive
- if (same_string(d->buddy, "") && same_string(d->divemaster, ""))
+ if (empty_string(d->buddy) && empty_string(d->divemaster))
counter++;
} else if (string_sequence_contains(d->buddy, person) || string_sequence_contains(d->divemaster, person)) {
counter++;
diff --git a/core/dive.h b/core/dive.h
index cd48aee30..294620df7 100644
--- a/core/dive.h
+++ b/core/dive.h
@@ -38,6 +38,11 @@ static inline bool same_string_caseinsensitive(const char *a, const char *b)
return !strcasecmp(a ?: "", b ?: "");
}
+static inline bool empty_string(const char *s)
+{
+ return !s || !*s;
+}
+
static inline bool includes_string_caseinsensitive(const char *haystack, const char *needle)
{
if (!needle)
diff --git a/core/divelist.c b/core/divelist.c
index 5549825dc..7c6259a6b 100644
--- a/core/divelist.c
+++ b/core/divelist.c
@@ -1177,11 +1177,11 @@ void filter_dive(struct dive *d, bool shown)
* (or the second one if the first one is empty */
void combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b)
{
- if (same_string(trip_a->location, "") && trip_b->location) {
+ if (empty_string(trip_a->location) && trip_b->location) {
free(trip_a->location);
trip_a->location = strdup(trip_b->location);
}
- if (same_string(trip_a->notes, "") && trip_b->notes) {
+ if (empty_string(trip_a->notes) && trip_b->notes) {
free(trip_a->notes);
trip_a->notes = strdup(trip_b->notes);
}
diff --git a/core/divesite.c b/core/divesite.c
index 56521bd71..889c94572 100644
--- a/core/divesite.c
+++ b/core/divesite.c
@@ -239,9 +239,9 @@ uint32_t create_dive_site_with_gps(const char *name, degrees_t latitude, degrees
/* a uuid is always present - but if all the other fields are empty, the dive site is pointless */
bool dive_site_is_empty(struct dive_site *ds)
{
- return same_string(ds->name, "") &&
- same_string(ds->description, "") &&
- same_string(ds->notes, "") &&
+ return empty_string(ds->name) &&
+ empty_string(ds->description) &&
+ empty_string(ds->notes) &&
ds->latitude.udeg == 0 &&
ds->longitude.udeg == 0;
}
diff --git a/core/file.c b/core/file.c
index 55a7b73a3..237056bed 100644
--- a/core/file.c
+++ b/core/file.c
@@ -268,7 +268,7 @@ int check_git_sha(const char *filename, struct git_repository **git_p, const cha
* get the SHA and compare with what we currently have */
if (git && git != dummy_git_repository) {
const char *sha = get_sha(git, branch);
- if (!same_string(sha, "") &&
+ if (!empty_string(sha) &&
same_string(sha, current_sha)) {
fprintf(stderr, "already have loaded SHA %s - don't load again\n", sha);
free(current_sha);
@@ -301,7 +301,7 @@ int parse_file(const char *filename)
* get the SHA and compare with what we currently have */
if (git && git != dummy_git_repository) {
const char *sha = get_sha(git, branch);
- if (!same_string(sha, "") &&
+ if (!empty_string(sha) &&
same_string(sha, current_sha) &&
!unsaved_changes()) {
fprintf(stderr, "already have loaded SHA %s - don't load again\n", sha);
diff --git a/core/libdivecomputer.c b/core/libdivecomputer.c
index 75cb0ce4c..c8afa47bf 100644
--- a/core/libdivecomputer.c
+++ b/core/libdivecomputer.c
@@ -232,7 +232,7 @@ static int parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_parser_t
fill_default_cylinder(&dive->cylinder[i]);
}
/* whatever happens, make sure there is a name for the cylinder */
- if (same_string(dive->cylinder[i].type.description, ""))
+ if (empty_string(dive->cylinder[i].type.description))
dive->cylinder[i].type.description = strdup(translate("gettextFromC", "unknown"));
}
return DC_STATUS_SUCCESS;
diff --git a/core/linux.c b/core/linux.c
index d60094a0e..5403990b7 100644
--- a/core/linux.c
+++ b/core/linux.c
@@ -81,7 +81,7 @@ const char *system_default_filename(void)
static const char *path = NULL;
if (!path) {
const char *user = getenv("LOGNAME");
- if (same_string(user, ""))
+ if (empty_string(user))
user = "username";
char *filename = calloc(strlen(user) + 5, 1);
strcat(filename, user);
diff --git a/core/load-git.c b/core/load-git.c
index 7214f864e..dd8cb3eac 100644
--- a/core/load-git.c
+++ b/core/load-git.c
@@ -217,7 +217,7 @@ static void parse_dive_location(char *line, struct membuffer *str, void *_dive)
dive->dive_site_uuid = uuid;
} else {
// we already had a dive site linked to the dive
- if (same_string(ds->name, "")) {
+ if (empty_string(ds->name)) {
ds->name = strdup(name);
} else {
// and that dive site had a name. that's weird - if our name is different, add it to the notes
diff --git a/core/macos.c b/core/macos.c
index 37bbd5d25..711b96a06 100644
--- a/core/macos.c
+++ b/core/macos.c
@@ -84,7 +84,7 @@ const char *system_default_filename(void)
static const char *path = NULL;
if (!path) {
const char *user = getenv("LOGNAME");
- if (same_string(user, ""))
+ if (empty_string(user))
user = "username";
char *filename = calloc(strlen(user) + 5, 1);
strcat(filename, user);
diff --git a/core/parse.c b/core/parse.c
index 48e62c2e0..69c064051 100644
--- a/core/parse.c
+++ b/core/parse.c
@@ -469,7 +469,7 @@ void add_dive_site(char *ds_name, struct dive *dive)
}
if (ds) {
// we have a uuid, let's hope there isn't a different name
- if (same_string(ds->name, "")) {
+ if (empty_string(ds->name)) {
ds->name = copy_string(buffer);
} else if (!same_string(ds->name, buffer)) {
// if it's not the same name, it's not the same dive site
diff --git a/core/qthelper.cpp b/core/qthelper.cpp
index 058c2f253..6916b1a68 100644
--- a/core/qthelper.cpp
+++ b/core/qthelper.cpp
@@ -408,7 +408,7 @@ extern "C" void copy_image_and_overwrite(const char *cfileName, const char *path
extern "C" bool string_sequence_contains(const char *string_sequence, const char *text)
{
- if (same_string(text, "") || same_string(string_sequence, ""))
+ if (empty_string(text) || empty_string(string_sequence))
return false;
QString stringSequence(string_sequence);
@@ -525,7 +525,7 @@ QString uiLanguage(QLocale *callerLoc)
if (callerLoc)
*callerLoc = loc;
- if (!prefs.date_format_override || same_string(prefs.date_format_short, "") || same_string(prefs.date_format, "")) {
+ if (!prefs.date_format_override || empty_string(prefs.date_format_short) || empty_string(prefs.date_format)) {
// derive our standard date format from what the locale gives us
// the short format is fine
// the long format uses long weekday and month names, so replace those with the short ones
@@ -536,16 +536,16 @@ QString uiLanguage(QLocale *callerLoc)
// special hack for Swedish as our switching from long weekday names to short weekday names
// messes things up there
dateFormat.replace("'en' 'den' d:'e'", " d");
- if (!prefs.date_format_override || same_string(prefs.date_format, "")) {
+ if (!prefs.date_format_override || empty_string(prefs.date_format)) {
free((void *)prefs.date_format);
prefs.date_format = strdup(qPrintable(dateFormat));
}
- if (!prefs.date_format_override || same_string(prefs.date_format_short, "")) {
+ if (!prefs.date_format_override || empty_string(prefs.date_format_short)) {
free((void *)prefs.date_format_short);
prefs.date_format_short = strdup(qPrintable(shortDateFormat));
}
}
- if (!prefs.time_format_override || same_string(prefs.time_format, "")) {
+ if (!prefs.time_format_override || empty_string(prefs.time_format)) {
timeFormat = loc.timeFormat();
timeFormat.replace("(t)", "").replace(" t", "").replace("t", "").replace("hh", "h").replace("HH", "H").replace("'kl'.", "");
timeFormat.replace(".ss", "").replace(":ss", "").replace("ss", "");
@@ -1186,7 +1186,7 @@ void hashPicture(struct picture *picture)
return;
char *oldHash = copy_string(picture->hash);
learnHash(picture, hashFile(localFilePath(picture->filename)));
- if (!same_string(picture->hash, "") && !same_string(picture->hash, oldHash))
+ if (!empty_string(picture->hash) && !same_string(picture->hash, oldHash))
mark_divelist_changed((true));
free(oldHash);
picture_free(picture);
@@ -1430,7 +1430,7 @@ int getCloudURL(QString &filename)
{
QString email = QString(prefs.cloud_storage_email);
email.replace(QRegularExpression("[^a-zA-Z0-9@._+-]"), "");
- if (email.isEmpty() || same_string(prefs.cloud_storage_password, ""))
+ if (email.isEmpty() || empty_string(prefs.cloud_storage_password))
return report_error("Please configure Cloud storage email and password in the preferences");
if (email != prefs.cloud_storage_email_encoded) {
free((void *)prefs.cloud_storage_email_encoded);
diff --git a/core/subsurfacestartup.c b/core/subsurfacestartup.c
index 13189cc26..90611a8dd 100644
--- a/core/subsurfacestartup.c
+++ b/core/subsurfacestartup.c
@@ -159,7 +159,7 @@ void print_files()
const char *filename, *local_git;
printf("\nFile locations:\n\n");
- if (!same_string(prefs.cloud_storage_email, "") && !same_string(prefs.cloud_storage_password, "")) {
+ if (!empty_string(prefs.cloud_storage_email) && !empty_string(prefs.cloud_storage_password)) {
filename = cloud_url();
is_git_repository(filename, &branch, &remote, true);