diff options
author | Lukas Slebodnik <lslebodn@redhat.com> | 2014-07-25 10:22:59 +0200 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2014-07-28 10:20:48 +0200 |
commit | 50d24652b5321f4cc5709bc0877dde5da4e67f08 (patch) | |
tree | 31193ac66e4e184d646d5241789951004c1f901d /src/util | |
parent | 31cd021079212a78ebeb6ec2f10d66acc160f89b (diff) | |
download | sssd-50d24652b5321f4cc5709bc0877dde5da4e67f08.tar.gz sssd-50d24652b5321f4cc5709bc0877dde5da4e67f08.tar.xz sssd-50d24652b5321f4cc5709bc0877dde5da4e67f08.zip |
UTIL: Add functions for replacing whitespaces.
Reviewed-by: Pavel Reichl <preichl@redhat.com>
Reviewed-by: Michal Židek <mzidek@redhat.com>
(cherry picked from commit 022c6b90bb37851c0e8704c0e5388ebc113c6470)
Conflicts:
Makefile.am
src/util/util.h
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/string_utils.c | 150 | ||||
-rw-r--r-- | src/util/util.h | 8 |
2 files changed, 158 insertions, 0 deletions
diff --git a/src/util/string_utils.c b/src/util/string_utils.c new file mode 100644 index 000000000..8d878923f --- /dev/null +++ b/src/util/string_utils.c @@ -0,0 +1,150 @@ +/* + SSSD + + Authors: + Lukas Slebodnik <slebodnikl@redhat.com> + + Copyright (C) 2014 Red Hat + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "util/util.h" + +const char * sss_replace_whitespaces(TALLOC_CTX *mem_ctx, + const char *orig_name, + const char *replace_string) +{ + char *new_name; + const char *ptr; + size_t replace_string_len; + TALLOC_CTX *tmp_ctx; + + if (replace_string == NULL || replace_string[0] == '\0') { + return orig_name; + } + + replace_string_len = strlen(replace_string); + /* faster implementations without multiple allocations */ + if (replace_string_len == 1) { + char *p; + new_name = talloc_strdup(mem_ctx, orig_name); + if (new_name == NULL) { + return NULL; + } + + for (p = new_name; *p != '\0'; ++p) { + if (isspace(*p)) { + *p = replace_string[0]; + } + } + + return new_name; + } + + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + return NULL; + } + + new_name = talloc_strdup(tmp_ctx, ""); + if (new_name == NULL) { + goto done; + } + + ptr = orig_name; + while (*ptr != '\0') { + if (isspace(*ptr)) { + new_name = talloc_asprintf_append(new_name, "%s", replace_string); + } else { + new_name = talloc_asprintf_append(new_name, "%c", *ptr); + } + if (new_name == NULL) { + goto done;; + } + + ++ptr; + } + + new_name = talloc_steal(mem_ctx, new_name); +done: + talloc_free(tmp_ctx); + return new_name; +} + +char * sss_reverse_replace_whitespaces(TALLOC_CTX *mem_ctx, + char *orig_name, + const char *replace_string) +{ + TALLOC_CTX *tmp_ctx; + char *substr; + char *new_name; + const char *ptr = orig_name; + size_t replace_string_len; + + if (replace_string == NULL || replace_string[0] == '\0') { + return orig_name; + } + + replace_string_len = strlen(replace_string); + /* faster implementations without multiple allocations */ + if (replace_string_len == 1) { + char *p; + new_name = talloc_strdup(mem_ctx, orig_name); + if (new_name == NULL) { + return NULL; + } + + for (p = new_name; *p != '\0'; ++p) { + if (*p == replace_string[0] ) { + *p = ' '; + } + } + + return new_name; + } + + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + return NULL; + } + + new_name = talloc_strdup(tmp_ctx, ""); + if (new_name == NULL) { + goto done; + } + + do { + substr = strstr(ptr, replace_string); + if (substr != NULL) { + new_name = talloc_asprintf_append(new_name, "%.*s ", + (int)(substr - ptr), ptr); + if (new_name == NULL) { + goto done; + } + ptr += substr - ptr; + ptr += replace_string_len; + } else { + new_name = talloc_asprintf_append(new_name, "%s", ptr); + if (new_name == NULL) { + goto done; + } + } + } while (substr != NULL); + + new_name = talloc_steal(mem_ctx, new_name); +done: + talloc_free(tmp_ctx); + return new_name; +} diff --git a/src/util/util.h b/src/util/util.h index df754608e..7b53972de 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -535,4 +535,12 @@ errno_t sss_br_lock_file(int fd, size_t start, size_t len, #define BUILD_WITH_PAC_RESPONDER false #endif +/* from string_utils.c */ +const char * sss_replace_whitespaces(TALLOC_CTX *mem_ctx, + const char *orig_name, + const char *replace_string); +char * sss_reverse_replace_whitespaces(TALLOC_CTX *mem_ctx, + char *orig_name, + const char *replace_string); + #endif /* __SSSD_UTIL_H__ */ |