| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Functions for hacking with strings |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the mLib utilities library. |
| 11 | * |
| 12 | * mLib is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Library General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of the |
| 15 | * License, or (at your option) any later version. |
| 16 | * |
| 17 | * mLib is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with mLib; if not, write to the Free |
| 24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #ifndef MLIB_STR_H |
| 29 | #define MLIB_STR_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Header files ------------------------------------------------------*/ |
| 36 | |
| 37 | #include <stddef.h> |
| 38 | |
| 39 | /*----- Functions provided ------------------------------------------------*/ |
| 40 | |
| 41 | /* --- @str_qword@ --- * |
| 42 | * |
| 43 | * Arguments: @char **pp@ = address of pointer into string |
| 44 | * @unsigned f@ = various flags |
| 45 | * |
| 46 | * Returns: Pointer to the next space-separated possibly-quoted word from |
| 47 | * the string, or null. |
| 48 | * |
| 49 | * Use: Fetches the next word from a string. If the flag |
| 50 | * @STRF_QUOTE@ is set, the `\' character acts as an escape, and |
| 51 | * single and double quotes protect whitespace. |
| 52 | */ |
| 53 | |
| 54 | #define STRF_QUOTE 1u |
| 55 | |
| 56 | extern char *str_qword(char **/*pp*/, unsigned /*f*/); |
| 57 | |
| 58 | /* --- @str_qsplit@ --- * |
| 59 | * |
| 60 | * Arguments: @char *p@ = pointer to string |
| 61 | * @char *v[]@ = pointer to array to fill in |
| 62 | * @size_t c@ = count of strings to fill in |
| 63 | * @char **rest@ = where to store the remainder of the string |
| 64 | * @unsigned f@ = flags for @str_qword@ |
| 65 | * |
| 66 | * Returns: Number of strings filled in. |
| 67 | * |
| 68 | * Use: Fills an array with pointers to the individual words of a |
| 69 | * string. The string is modified in place to contain zero |
| 70 | * bytes at the word boundaries, and the words have leading |
| 71 | * and trailing space stripped off. No more than @c@ words |
| 72 | * are read; the actual number is returned as the value of the |
| 73 | * function. Unused slots in the array are populated with |
| 74 | * null bytes. If there's any string left, the address of the |
| 75 | * remainder is stored in @rest@ (if it's non-null); otherwise |
| 76 | * @rest@ is set to a null pointer. |
| 77 | */ |
| 78 | |
| 79 | extern size_t str_qsplit(char */*p*/, char */*v*/[], size_t /*c*/, |
| 80 | char **/*rest*/, unsigned /*f*/); |
| 81 | |
| 82 | /* --- @str_getword@ --- * |
| 83 | * |
| 84 | * Arguments: @char **pp@ = address of pointer into string |
| 85 | * |
| 86 | * Returns: Pointer to the next space-separated word from the string, |
| 87 | * or null. |
| 88 | * |
| 89 | * Use: Parses off space-separated words from a string. This is a |
| 90 | * compatibility veneer over @str_qword@. |
| 91 | */ |
| 92 | |
| 93 | extern char *str_getword(char **/*pp*/); |
| 94 | |
| 95 | /* --- @str_split@ --- * |
| 96 | * |
| 97 | * Arguments: @char *p@ = pointer to string |
| 98 | * @char *v[]@ = pointer to array to fill in |
| 99 | * @size_t c@ = count of strings to fill in |
| 100 | * @char **rest@ = where to store the remainder of the string |
| 101 | * |
| 102 | * Returns: Number of strings filled in. |
| 103 | * |
| 104 | * Use: Fills an array with pointers to the individual words of a |
| 105 | * string. This is a compatibility veneer over @str_qsplit@. |
| 106 | */ |
| 107 | |
| 108 | extern size_t str_split(char */*p*/, char */*v*/[], |
| 109 | size_t /*c*/, char **/*rest*/); |
| 110 | |
| 111 | /* --- @str_matchx@ --- * |
| 112 | * |
| 113 | * Arguments: @const char *p@ = pointer to pattern string |
| 114 | * @const char *s@ = string to compare with |
| 115 | * @unsigned f@ = various flags |
| 116 | * |
| 117 | * Returns: Nonzero if the pattern matches the string. |
| 118 | * |
| 119 | * Use: Does simple wildcard matching. This is quite nasty and more |
| 120 | * than a little slow. Supports metacharacters `*', `?' and |
| 121 | * '['. |
| 122 | */ |
| 123 | |
| 124 | #define STRF_PREFIX 1u /* Accept if @s@ is exhausted during |
| 125 | * the attempted match */ |
| 126 | |
| 127 | extern int str_matchx(const char */*p*/, const char */*s*/, unsigned /*f*/); |
| 128 | |
| 129 | /* --- @str_match@ --- * |
| 130 | * |
| 131 | * Arguments: @const char *p@ = pointer to pattern string |
| 132 | * @const char *s@ = string to compare with |
| 133 | * |
| 134 | * Returns: Nonzero if the pattern matches the string. |
| 135 | * |
| 136 | * Use: Does simple wildcard matching. Equivalent to @str_matchx@ |
| 137 | * with zero flags word. |
| 138 | */ |
| 139 | |
| 140 | extern int str_match(const char */*p*/, const char */*s*/); |
| 141 | |
| 142 | /* --- @str_sanitize@ --- * |
| 143 | * |
| 144 | * Arguments: @char *d@ = destination buffer |
| 145 | * @const char *p@ = pointer to source string |
| 146 | * @size_t sz@ = size of destination buffer |
| 147 | * |
| 148 | * Returns: --- |
| 149 | * |
| 150 | * Use: Writes a string into a buffer, being careful not to overflow |
| 151 | * the buffer, to null terminate the result, and to prevent |
| 152 | * nasty nonprintable characters ending up in the buffer. |
| 153 | */ |
| 154 | |
| 155 | extern void str_sanitize(char */*d*/, const char */*p*/, size_t /*sz*/); |
| 156 | |
| 157 | /*----- That's all, folks -------------------------------------------------*/ |
| 158 | |
| 159 | #ifdef __cplusplus |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | #endif |