Skip to content

Commit

Permalink
retire strsub, gstrsub. (GPSBabel#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteven4 authored Mar 11, 2022
1 parent c39cf96 commit a4769c8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 81 deletions.
3 changes: 0 additions & 3 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1061,9 +1061,6 @@ inline int case_ignore_strncmp(const QString& s1, const QString& s2, int n)

int str_match(const char* str, const char* match);

char* strsub(const char* s, const char* search, const char* replace);
char* gstrsub(const char* s, const char* search, const char* replace);

void rtrim(char* s);
char* lrtrim(char* buff);
int xasprintf(char** strp, const char* fmt, ...) PRINTFLIKE(2, 3);
Expand Down
24 changes: 12 additions & 12 deletions explorist_ini.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ explorist_ini_try(const char* path)
info->track_path = nullptr;
info->waypoint_path = nullptr;

char* s = xstrdup(inifile_readstr(inifile, "UGDS", "WpFolder"));
if (s) {
s = gstrsub(s, "\\", "/");
xasprintf(&info->waypoint_path, "%s/%s", path, s);
QString s = inifile_readstr(inifile, "UGDS", "WpFolder");
if (!s.isNull()) {
s.replace('\\', '/');
xasprintf(&info->waypoint_path, "%s/%s", path, CSTR(s));
}
s = xstrdup(inifile_readstr(inifile, "UGDS", "GcFolder"));
if (s) {
s = gstrsub(s, "\\", "/");
xasprintf(&info->geo_path, "%s/%s", path, s);
s = inifile_readstr(inifile, "UGDS", "GcFolder");
if (!s.isNull()) {
s.replace('\\', '/');
xasprintf(&info->geo_path, "%s/%s", path, CSTR(s));
}
s = xstrdup(inifile_readstr(inifile, "UGDS", "TrkFolder"));
if (s) {
s = gstrsub(s, "\\", "/");
xasprintf(&info->track_path, "%s/%s", path, s);
s = inifile_readstr(inifile, "UGDS", "TrkFolder");
if (!s.isNull()) {
s.replace('\\', '/');
xasprintf(&info->track_path, "%s/%s", path, CSTR(s));
}

inifile_done(inifile);
Expand Down
66 changes: 0 additions & 66 deletions util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -935,72 +935,6 @@ double degrees2ddmm(double deg_val)
return (deg * 100.0) + ((deg_val - deg) * 60.0);
}

/*
* replace a single occurrence of "search" in "s" with "replace".
* Returns an allocated copy if substitution was made, otherwise returns NULL.
* Doesn't try to make an optimally sized dest buffer.
*/
char*
strsub(const char* s, const char* search, const char* replace)
{
int len = strlen(s);
int slen = strlen(search);
int rlen = strlen(replace);

const char* p = strstr(s, search);
if (!slen || !p) {
return nullptr;
}

char* d = (char*) xmalloc(len + rlen + 1);

/* Copy first part */
len = p - s;
memcpy(d, s, len);
d[len] = 0;

/* Copy replacement */
strcat(d, replace);

/* Copy last part */
strcat(d, p + slen);
return d;
}

/*
* As strsub, but do it globally.
*/
char*
gstrsub(const char* s, const char* search, const char* replace)
{
int ooffs = 0;
const char* c;
const char* src = s;
int olen = strlen(src);
int slen = strlen(search);
int rlen = strlen(replace);

char* o = (char*) xmalloc(olen + 1);

while ((c = strstr(src, search))) {
olen += (rlen - slen);
o = (char*) xrealloc(o, olen + 1);
memcpy(o + ooffs, src, c - src);
ooffs += (c - src);
src = c + slen;
if (rlen) {
memcpy(o + ooffs, replace, rlen);
ooffs += rlen;
}
}

if (ooffs < olen) {
memcpy(o + ooffs, src, olen - ooffs);
}
o[olen] = '\0';
return o;
}

/*
*
*/
Expand Down

0 comments on commit a4769c8

Please sign in to comment.