chiark / gitweb /
Some trivial string hacks.
authormdw <mdw>
Mon, 17 May 1999 20:37:01 +0000 (20:37 +0000)
committermdw <mdw>
Mon, 17 May 1999 20:37:01 +0000 (20:37 +0000)
str.c [new file with mode: 0644]
str.h [new file with mode: 0644]

diff --git a/str.c b/str.c
new file mode 100644 (file)
index 0000000..51a9805
--- /dev/null
+++ b/str.c
@@ -0,0 +1,144 @@
+/* -*-c-*-
+ *
+ * $Id: str.c,v 1.1 1999/05/17 20:37:01 mdw Exp $
+ *
+ * Functions for hacking with strings
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * mLib 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 Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with mLib; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: str.c,v $
+ * Revision 1.1  1999/05/17 20:37:01  mdw
+ * Some trivial string hacks.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "str.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @str_getword@ --- *
+ *
+ * Arguments:  @char **pp@ = address of pointer into string
+ *
+ * Returns:    Pointer to the next space-separated word from the string,
+ *             or null.
+ *
+ * Use:                Parses off space-separated words from a string.
+ */
+
+char *str_getword(char **pp)
+{
+  char *p = *pp, *q;
+
+  if (!p)
+    return (0);
+
+  while (isspace((unsigned char)*p))
+    p++;
+
+  for (q = p; *q; q++) {
+    if (isspace((unsigned char)*q)) {
+      *q = 0;
+      *pp = q + 1;
+      return (p);
+    }
+  }
+
+  *pp = 0;
+  return (p);
+}
+
+/* --- @str_split@ --- *
+ *
+ * Arguments:  @char *p@ = pointer to string
+ *             @char *v[]@ = pointer to array to fill in
+ *             @size_t c@ = count of strings to fill in
+ *
+ * Returns:    Number of strings filled in.
+ *
+ * Use:                Fills an array with pointers to the individual words of a
+ *             string.  The string is modified in place to contain zero
+ *             bytes at the word boundaries, and the words have leading
+ *             and trailing space stripped off.  No more than @c@ words
+ *             are read; the actual number is returned as the value of the
+ *             function.  Unused slots in the array are populated with
+ *             null bytes.
+ */
+
+size_t str_split(char *p, char *v[], size_t c)
+{
+  size_t n = 0;
+  char *q;
+
+  while (c && (q = str_getword(&p)) != 0) {
+    *v++ = q;
+    c--;
+    n++;
+  }
+
+  while (c) {
+    *v++ = 0;
+    c--;
+  }
+  return (n);
+}
+
+/* --- @str_sanitize@ --- *
+ *
+ * Arguments:  @char *d@ = destination buffer
+ *             @const char *p@ = pointer to source string
+ *             @size_t sz@ = size of destination buffer
+ *
+ * Returns:    ---
+ *
+ * Use:                Writes a string into a buffer, being careful not to overflow
+ *             the buffer, to null terminate the result, and to prevent
+ *             nasty nonprintable characters ending up in the buffer.
+ */
+
+void str_sanitize(char *d, const char *p, size_t sz)
+{
+  if (!sz)
+    return;
+  sz--;
+  while (*p && sz) {
+    int ch = *p++;
+    if (!isgraph((unsigned char)ch))
+      ch = '_';
+    *d++ = ch;
+    sz--;
+  }
+  *d++ = 0;
+}
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/str.h b/str.h
new file mode 100644 (file)
index 0000000..ee33d11
--- /dev/null
+++ b/str.h
@@ -0,0 +1,103 @@
+/* -*-c-*-
+ *
+ * $Id: str.h,v 1.1 1999/05/17 20:37:01 mdw Exp $
+ *
+ * Functions for hacking with strings
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * mLib 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 Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with mLib; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: str.h,v $
+ * Revision 1.1  1999/05/17 20:37:01  mdw
+ * Some trivial string hacks.
+ *
+ */
+
+#ifndef STR_H
+#define STR_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stddef.h>
+
+/*----- Functions provided ------------------------------------------------*/
+
+/* --- @str_getword@ --- *
+ *
+ * Arguments:  @char **pp@ = address of pointer into string
+ *
+ * Returns:    Pointer to the next space-separated word from the string,
+ *             or null.
+ *
+ * Use:                Parses off space-separated words from a string.
+ */
+
+extern char *str_getword(char **/*pp*/);
+
+/* --- @str_split@ --- *
+ *
+ * Arguments:  @char *p@ = pointer to string
+ *             @char *v[]@ = pointer to array to fill in
+ *             @size_t c@ = count of strings to fill in
+ *
+ * Returns:    Number of strings filled in.
+ *
+ * Use:                Fills an array with pointers to the individual words of a
+ *             string.  The string is modified in place to contain zero
+ *             bytes at the word boundaries, and the words have leading
+ *             and trailing space stripped off.  No more than @c@ words
+ *             are read; the actual number is returned as the value of the
+ *             function.  Unused slots in the array are populated with
+ *             null bytes.
+ */
+
+extern size_t str_split(char */*p*/, char */*v*/[], size_t /*c*/);
+
+/* --- @str_sanitize@ --- *
+ *
+ * Arguments:  @char *d@ = destination buffer
+ *             @const char *p@ = pointer to source string
+ *             @size_t sz@ = size of destination buffer
+ *
+ * Returns:    ---
+ *
+ * Use:                Writes a string into a buffer, being careful not to overflow
+ *             the buffer, to null terminate the result, and to prevent
+ *             nasty nonprintable characters ending up in the buffer.
+ */
+
+extern void str_sanitize(char */*d*/, const char */*p*/, size_t /*sz*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif