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>
This commit is contained in:
Berthold Stoeger 2018-01-07 11:12:48 +01:00 committed by Lubomir I. Ivanov
parent 86ef9fce75
commit e85ecdd925
21 changed files with 53 additions and 48 deletions

View file

@ -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++;

View file

@ -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)

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -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

View file

@ -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);

View file

@ -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

View file

@ -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);

View file

@ -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);