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

View file

@ -112,13 +112,13 @@ void DownloadFromDCWidget::updateProgressBar()
{
static char *last_text = NULL;
if (same_string(last_text, "")) {
if (empty_string(last_text)) {
// if we get the first actual text after the download is finished
// (which happens for example on the OSTC), then don't bother
if (!same_string(progress_bar_text, "") && IS_FP_SAME(progress_bar_fraction, 1.0))
if (!empty_string(progress_bar_text) && IS_FP_SAME(progress_bar_fraction, 1.0))
progress_bar_text = "";
}
if (!same_string(progress_bar_text , "")) {
if (!empty_string(progress_bar_text)) {
ui.progressBar->setFormat(progress_bar_text);
#if defined(Q_OS_MAC)
// on mac the progress bar doesn't show its text

View file

@ -186,7 +186,7 @@ void LocationInformationWidget::acceptChanges()
// if the user entered a different contriy, first update the taxonomy
// for the displayed dive site; this below will get copied into the currentDs
if (!same_string(uiString, taxonomy_get_country(&displayed_dive_site.taxonomy)) &&
!same_string(uiString, ""))
!empty_string(uiString))
taxonomy_set_country(&displayed_dive_site.taxonomy, uiString, taxonomy_origin::GEOMANUAL);
else
free(uiString);

View file

@ -1669,7 +1669,7 @@ int MainWindow::file_save_as(void)
selection_dialog.setAcceptMode(QFileDialog::AcceptSave);
selection_dialog.setFileMode(QFileDialog::AnyFile);
selection_dialog.setDefaultSuffix("");
if (same_string(default_filename, "")) {
if (empty_string(default_filename)) {
QFileInfo defaultFile(system_default_filename());
selection_dialog.setDirectory(qPrintable(defaultFile.absolutePath()));
}

View file

@ -272,8 +272,8 @@ SubsurfaceWebServices::SubsurfaceWebServices(QWidget *parent, Qt::WindowFlags f)
QString userid(prefs.userid);
if (userid.isEmpty() &&
!same_string(prefs.cloud_storage_email, "") &&
!same_string(prefs.cloud_storage_password, "") &&
!empty_string(prefs.cloud_storage_email) &&
!empty_string(prefs.cloud_storage_password) &&
GpsLocation::hasInstance())
userid = GpsLocation::instance()->getUserid(prefs.cloud_storage_email, prefs.cloud_storage_password);

View file

@ -309,7 +309,7 @@ void QMLManager::finishSetup()
// but we need to make sure we stay the only ones accessing git storage
alreadySaving = true;
openLocalThenRemote(url);
} else if (!same_string(existing_filename, "") && credentialStatus() != CS_UNKNOWN) {
} else if (!empty_string(existing_filename) && credentialStatus() != CS_UNKNOWN) {
setCredentialStatus(CS_NOCLOUD);
saveCloudCredentials();
appendTextToLog(tr("working in no-cloud mode"));
@ -433,8 +433,8 @@ void QMLManager::checkCredentialsAndExecute(execute_function_type execute)
{
// if the cloud credentials are present, we should try to get the GPS Webservice ID
// and (if we haven't done so) load the dive list
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)) {
setStartPageText(tr("Testing cloud credentials"));
appendTextToLog("Have credentials, let's see if they are valid");
CloudStorageAuthenticate *csa = new CloudStorageAuthenticate(this);
@ -540,7 +540,7 @@ void QMLManager::retrieveUserid()
setCredentialStatus(CS_VERIFIED);
QString userid(prefs.userid);
if (userid.isEmpty()) {
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)) {
appendTextToLog("cloud user name or password are empty, can't retrieve web user id");
revertToNoCloudIfNeeded();
return;
@ -1117,7 +1117,7 @@ void QMLManager::saveChangesLocal()
{
if (unsaved_changes()) {
if (credentialStatus() == CS_NOCLOUD) {
if (same_string(existing_filename, "")) {
if (empty_string(existing_filename)) {
char *filename = NOCLOUD_LOCALSTORAGE;
if (git_create_local_repo(filename))
appendTextToLog(get_error_string());
@ -1682,7 +1682,7 @@ QStringList QMLManager::cylinderInit() const
int i = 0;
for_each_dive (i, d) {
for (int j = 0; j < MAX_CYLINDERS; j++) {
if (! same_string(d->cylinder[j].type.description, ""))
if (!empty_string(d->cylinder[j].type.description))
cylinders << d->cylinder[j].type.description;
}
}

View file

@ -80,7 +80,7 @@ void DiveEventItem::setupPixmap()
#define EVENT_PIXMAP(PIX) QPixmap(QString(PIX)).scaled(sz_pix, sz_pix, Qt::KeepAspectRatio, Qt::SmoothTransformation)
#define EVENT_PIXMAP_BIGGER(PIX) QPixmap(QString(PIX)).scaled(sz_bigger, sz_bigger, Qt::KeepAspectRatio, Qt::SmoothTransformation)
if (same_string(internalEvent->name, "")) {
if (empty_string(internalEvent->name)) {
setPixmap(EVENT_PIXMAP(":status-warning-icon"));
} else if (internalEvent->type == SAMPLE_EVENT_BOOKMARK) {
setPixmap(EVENT_PIXMAP(":dive-bookmark-icon"));

View file

@ -718,7 +718,7 @@ void ProfileWidget2::plotDive(struct dive *d, bool force)
while (event) {
// if print mode is selected only draw headings, SP change, gas events or bookmark event
if (printMode) {
if (same_string(event->name, "") ||
if (empty_string(event->name) ||
!(strcmp(event->name, "heading") == 0 ||
(same_string(event->name, "SP change") && event->time.seconds == 0) ||
event_is_gaschange(event) ||
@ -1488,7 +1488,7 @@ void ProfileWidget2::hideEvents()
if (QMessageBox::question(this,
TITLE_OR_TEXT(tr("Hide events"), tr("Hide all %1 events?").arg(event->name)),
QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok) {
if (!same_string(event->name, "")) {
if (!empty_string(event->name)) {
for (int i = 0; i < evn_used; i++) {
if (same_string(event->name, ev_namelist[i].ev_name)) {
ev_namelist[i].plot_ev = false;

View file

@ -151,7 +151,7 @@ void DivePlannerPointsModel::setupCylinders()
if (!cylinder_none(&(displayed_dive.cylinder[i])))
return; // We have at least one cylinder
}
if (!same_string(prefs.default_cylinder, "")) {
if (!empty_string(prefs.default_cylinder)) {
fill_default_cylinder(&displayed_dive.cylinder[0]);
}
if (cylinder_none(&displayed_dive.cylinder[0])) {

View file

@ -95,7 +95,7 @@ static void smtk_date_to_tm(char *d_buffer, struct tm *tm_date)
{
int n, d, m, y;
if ((d_buffer) && (!same_string(d_buffer, ""))) {
if ((d_buffer) && (!empty_string(d_buffer))) {
n = sscanf(d_buffer, "%d/%d/%d ", &m, &d, &y);
y = (y < 70) ? y + 100 : y;
if (n == 3) {
@ -118,7 +118,7 @@ static void smtk_time_to_tm(char *t_buffer, struct tm *tm_date)
{
int n, hr, min, sec;
if ((t_buffer) && (!same_string(t_buffer, ""))) {
if ((t_buffer) && (!empty_string(t_buffer))) {
n = sscanf(t_buffer, "%*[0-9/] %d:%d:%d ", &hr, &min, &sec);
if (n == 3) {
tm_date->tm_hour = hr;
@ -143,7 +143,7 @@ static unsigned int smtk_time_to_secs(char *t_buffer)
{
int n, hr, min, sec;
if (!same_string(t_buffer, "")) {
if (!empty_string(t_buffer)) {
n = sscanf(t_buffer, "%*[0-9/] %d:%d:%d ", &hr, &min, &sec);
return((n == 3) ? (((hr*60)+min)*60)+sec : 0);
} else {
@ -607,11 +607,11 @@ static char *smtk_locate_buddy(MdbHandle *mdb, char *dive_idx)
* it's not a good idea to use a complex data structure and algorithm.
*/
while (mdb_fetch_row(table)) {
if (!same_string(col[3]->bind_ptr, ""))
if (!empty_string(col[3]->bind_ptr))
fullname = smtk_concat_str(fullname, " ", "%s", col[3]->bind_ptr);
if (!same_string(col[4]->bind_ptr, ""))
if (!empty_string(col[4]->bind_ptr))
fullname = smtk_concat_str(fullname, " ", "%s", col[4]->bind_ptr);
if (!same_string(col[2]->bind_ptr, ""))
if (!empty_string(col[2]->bind_ptr))
fullname = smtk_concat_str(fullname, " ", "%s", col[2]->bind_ptr);
if (fullname && !same_string(col[1]->bind_ptr, fullname))
buddies[atoi(col[0]->bind_ptr)] = smtk_concat_str(buddies[atoi(col[0]->bind_ptr)], "", "%s (%s)", col[1]->bind_ptr, fullname);