]> diplodocus.org Git - nmh/blobdiff - sbr/trimcpy.c
Use mh_xstrdup() instead of getcpy() for a string constant.
[nmh] / sbr / trimcpy.c
index 3a2fbd763fb991b633b9b29136d4f251bd305cf6..7a2ac03abecf05f4553a7511cb39393b7287b300 100644 (file)
@@ -10,6 +10,7 @@
  */
 
 #include <h/mh.h>
  */
 
 #include <h/mh.h>
+#include <h/utils.h>
 
 
 char *
 
 
 char *
@@ -38,3 +39,58 @@ trimcpy (char *cp)
     /* now return a copy */
     return getcpy(cp);
 }
     /* now return a copy */
     return getcpy(cp);
 }
+
+
+/*
+ * cpytrim() -- return a copy of the argument with:
+ *           -- stripped leading and trailing whitespace, and
+ *           -- internal whitespace replaced with spaces.
+ *
+ * This code is Copyright (c) 2013, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
+ */
+char *
+cpytrim (const char *sp) {
+    char *dp;
+    char *cp;
+
+    /* skip over leading whitespace */
+    while (isspace ((unsigned char) *sp)) ++sp;
+
+    dp = add (sp, NULL);
+
+    /* start at the end and zap trailing whitespace */
+    for (cp = dp + strlen (dp) - 1;
+         cp >= dp  &&  isspace ((unsigned char) *cp);
+         *cp-- = '\0') continue;
+
+    /* replace remaining whitespace with spaces */
+    for (cp = dp; *cp; ++cp) {
+        if (isspace ((unsigned char) *cp)) *cp = ' ';
+    }
+
+    return dp;
+}
+
+
+/*
+ * rtrim() -- modify the argument to:
+ *         -- strip trailing whitespace
+ *
+ * This code is Copyright (c) 2014, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
+ */
+char *
+rtrim (char *sp) {
+    char *cp;
+
+    /* start at the end and zap trailing whitespace */
+    for (cp = sp + strlen (sp) - 1;
+         cp >= sp  &&  isspace ((unsigned char) *cp);
+         --cp) { continue; }
+    *++cp = '\0';
+
+    return sp;
+}