]> code.delx.au - pulseaudio/commitdiff
core-util: introduce generic function pa_strip()
authorLennart Poettering <lennart@poettering.net>
Sun, 21 Feb 2010 20:59:53 +0000 (21:59 +0100)
committerLennart Poettering <lennart@poettering.net>
Sun, 21 Feb 2010 20:59:53 +0000 (21:59 +0100)
src/pulsecore/core-util.c
src/pulsecore/core-util.h

index b64c51e18fb71d2a111b0a0236bc81bc5d849950..323c98d2fb1838a3a00c32ae8ff8c0223f75b9a1 100644 (file)
 #define MSG_NOSIGNAL 0
 #endif
 
+#define NEWLINE "\r\n"
+#define WHITESPACE "\n\r \t"
+
 static pa_strlist *recorded_env = NULL;
 
 #ifdef OS_IS_WIN32
@@ -830,9 +833,6 @@ char *pa_split(const char *c, const char *delimiter, const char**state) {
     return pa_xstrndup(current, l);
 }
 
-/* What is interpreted as whitespace? */
-#define WHITESPACE " \t\n"
-
 /* Split a string into words. Otherwise similar to pa_split(). */
 char *pa_split_spaces(const char *c, const char **state) {
     const char *current = *state ? *state : c;
@@ -1189,7 +1189,27 @@ int pa_lock_fd(int fd, int b) {
 char* pa_strip_nl(char *s) {
     pa_assert(s);
 
-    s[strcspn(s, "\r\n")] = 0;
+    s[strcspn(s, NEWLINE)] = 0;
+    return s;
+}
+
+char *pa_strip(char *s) {
+    char *e, *l = NULL;
+
+    /* Drops trailing whitespace. Modifies the string in
+     * place. Returns pointer to first non-space character */
+
+    s += strspn(s, WHITESPACE);
+
+    for (e = s; *e; e++)
+        if (!strchr(WHITESPACE, *e))
+            l = e;
+
+    if (l)
+        *(l+1) = 0;
+    else
+        *s = 0;
+
     return s;
 }
 
index 31a83bcc2336e0cb304395899cd901edeeeee4cb..0d63cfcba1172f1c5e9fea15b5537959a6fc66e4 100644 (file)
@@ -103,6 +103,7 @@ char *pa_split(const char *c, const char*delimiters, const char **state);
 char *pa_split_spaces(const char *c, const char **state);
 
 char *pa_strip_nl(char *s);
+char *pa_strip(char *s);
 
 const char *pa_sig2str(int sig) PA_GCC_PURE;