return result;
}
-
/*
- * Strip leading and trailing spaces from a string
+ * removes double slashes from pathnames
+ * allows / disallows trailing slashes
*/
-long striplt(char *buf)
+void StripSlashes(char *Dir, int TrailingSlash)
{
- int CountTrail = 0;
- int FromStart = 1;
- char *aptr, *bptr;
+ char *a, *b;
- if ((buf==NULL) || (IsEmptyStr(buf)))
- return 0;
+ a = b = Dir;
- bptr = aptr = buf;
-
- while (!IsEmptyStr(aptr)) {
- if (isspace(*aptr)) {
- if (FromStart)
- aptr ++;
- else {
- CountTrail ++;
- *bptr = *aptr;
- aptr++; bptr++;
- }
+ while (!IsEmptyStr(a)) {
+ if (*a == '/') {
+ while (*a == '/')
+ a++;
+ *b = '/';
+ b++;
}
else {
- CountTrail = 0;
- *bptr = *aptr;
- aptr++; bptr++;
+ *b = *a;
+ b++; a++;
}
}
-
- if (CountTrail > 0) {
- bptr -= CountTrail;
+ if ((TrailingSlash) && (*(b - 1) != '/')){
+ *b = '/';
+ b++;
}
+ *b = '\0';
- *bptr = '\0';
- return bptr - buf;
}
+/*
+ * Strip leading and trailing spaces from a string
+ */
+size_t striplt(char *buf) {
+ char *first_nonspace = NULL;
+ char *last_nonspace = NULL;
+ char *ptr;
+ size_t new_len = 0;
+
+ if (!buf) {
+ return 0;
+ }
+
+ for (ptr=buf; *ptr!=0; ++ptr) {
+ if (!isspace(*ptr)) {
+ if (!first_nonspace) {
+ first_nonspace = ptr;
+ }
+ last_nonspace = ptr;
+ }
+ }
+ if ((!first_nonspace) || (!last_nonspace)) {
+ buf[0] = 0;
+ return 0;
+ }
+ new_len = last_nonspace - first_nonspace + 1;
+ memmove(buf, first_nonspace, new_len);
+ buf[new_len] = 0;
+ return new_len;
+}
/**