"Fossies" - the Fresh Open Source Software Archive

Member "sitecopy-0.16.6/lib/neon/ne_string.h" (16 Jul 2007, 6954 Bytes) of archive /linux/www/sitecopy-0.16.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ne_string.h" see the Fossies "Dox" file reference documentation.

    1 /* 
    2    String utility functions
    3    Copyright (C) 1999-2007, Joe Orton <joe@manyfish.co.uk>
    4 
    5    This library is free software; you can redistribute it and/or
    6    modify it under the terms of the GNU Library General Public
    7    License as published by the Free Software Foundation; either
    8    version 2 of the License, or (at your option) any later version.
    9    
   10    This library is distributed in the hope that it will be useful,
   11    but WITHOUT ANY WARRANTY; without even the implied warranty of
   12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13    Library General Public License for more details.
   14 
   15    You should have received a copy of the GNU Library General Public
   16    License along with this library; if not, write to the Free
   17    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   18    MA 02111-1307, USA
   19 
   20 */
   21 
   22 #ifndef NE_STRING_H
   23 #define NE_STRING_H
   24 
   25 #include "ne_defs.h"
   26 #include "ne_alloc.h"
   27 
   28 #include <stdarg.h>
   29 
   30 NE_BEGIN_DECLS
   31 
   32 /* ne_token and ne_qtoken return the next token in *str before either
   33  * the next separator character 'sep' or the NUL terminator.
   34  * ne_qtoken skips over any parts quoted using a pair of any one of
   35  * the characters given in 'quotes'.  After returning, *str will point
   36  * to the next character after the separator, or NULL if no separator
   37  * character was found.
   38  * 
   39  * ne_qtoken will return NULL if unterminated quotes are found. */
   40 char *ne_token(char **str, char sep);
   41 char *ne_qtoken(char **str, char sep, const char *quotes);
   42 
   43 /* Return portion of 'str' with any characters in 'whitespace' shaved
   44  * off the beginning and end.  Modifies str in-place. */
   45 char *ne_shave(char *str, const char *whitespace);
   46 
   47 /* Cleanse 'str' of non-printable (e.g. control) characters.  'str' is
   48  * modified in-place, and returned. */
   49 char *ne_strclean(char *str);
   50 
   51 /* Encode 'len' bytes of 'text' to base64.  Returns malloc-allocated
   52  * NUL-terminated buffer which the caller must free(). */
   53 char *ne_base64(const unsigned char *text, size_t len);
   54 
   55 /* Decode NUL-terminated base64-encoded string 'data', placing
   56  * malloc-allocated raw decoder output in '*out'.  Returns length, or
   57  * zero on decode error (in which case the content of *out is
   58  * undefined). */
   59 size_t ne_unbase64(const char *data, unsigned char **out);
   60 
   61 /* Dynamically-allocated string buffer.  A string buffer which grows
   62  * dynamically . (Strings are zero-terminated still).  A
   63  * string buffer ne_buffer which grows dynamically with the string. */
   64 typedef struct {
   65     char *data; /* contents: NUL-terminated string */
   66     size_t used; /* strlen(data) + 1 */
   67     size_t length; /* number of bytes allocated */
   68 } ne_buffer;
   69 
   70 /* Create a new string buffer object. */
   71 ne_buffer *ne_buffer_create(void);
   72 
   73 /* Create a new string buffer object with at least 'size' bytes of
   74  * allocated space. */
   75 ne_buffer *ne_buffer_ncreate(size_t size);
   76 
   77 /* Returns size of data in buffer, equiv to strlen(ne_buffer_data(buf)) */
   78 #define ne_buffer_size(buf) ((buf)->used - 1)
   79 
   80 /* Concatenate all given strings onto the end of the buffer.  The
   81  * strings must all be NUL-terminated, and MUST be followed by a NULL
   82  * argument marking the end of the list.  */
   83 void ne_buffer_concat(ne_buffer *buf, ...);
   84 
   85 /* Append a NUL-terminated string 'str' to buf. */
   86 void ne_buffer_zappend(ne_buffer *buf, const char *str);
   87 
   88 /* Append 'len' bytes of 'data' to buf, where 'data' does not contain
   89  * a NUL terminator.  (A NUL terminator is appended to buf) */
   90 void ne_buffer_append(ne_buffer *buf, const char *data, size_t len);
   91 
   92 /* Print a string to the end of the buffer using printf-style format
   93  * string 'format' and subsqeuent arguments.  At most 'max' characters
   94  * are appended; the number of characters appended (excluding the NUL
   95  * terminator) is returned.  Behaviour is undefined if 'max' is passed
   96  * as zero. */
   97 size_t ne_buffer_snprintf(ne_buffer *buf, size_t max, 
   98                           const char *format, ...)
   99     ne_attribute((format(printf, 3, 4)));
  100 
  101 /* Append a literal, NUL-terminated constant string 'str' to buffer
  102  * 'buf'. */
  103 #define ne_buffer_czappend(buf, str) \
  104 ne_buffer_append((buf), (str), sizeof((str)) - 1)
  105 
  106 /* Clear the string buffer 'buf', making it equivalent to the empty
  107  * string. */
  108 void ne_buffer_clear(ne_buffer *buf);
  109 
  110 /* Grow the allocated size of string buffer 'buf' to at least 'size'
  111  * bytes. */
  112 void ne_buffer_grow(ne_buffer *buf, size_t size);
  113 
  114 /* Re-establish the 'used' invariant if the string buffer data field is
  115  * altered directly. */
  116 void ne_buffer_altered(ne_buffer *buf);
  117 
  118 /* Destroy the string buffer object 'buf' without deallocating the
  119  * data string.  The data string must subsequently be freed using
  120  * ne_free(). */
  121 char *ne_buffer_finish(ne_buffer *buf);
  122 
  123 /* Destroy a string buffer object. */
  124 void ne_buffer_destroy(ne_buffer *buf);
  125 
  126 /* Thread-safe strerror() wrapper; place system error for errno value
  127  * 'errnum' in 'buffer', which is of length 'buflen'.  Returns
  128  * 'buffer'. */
  129 char *ne_strerror(int errnum, char *buffer, size_t buflen);
  130 
  131 /* ne_strnzcpy copies at most 'n'-1 bytes of 'src' to 'dest', and
  132  * ensures that 'dest' is subsequently NUL-terminated. */
  133 #define ne_strnzcpy(dest, src, n) do { size_t ne__nm1 = (n) - 1;      \
  134 strncpy(dest, src, ne__nm1); dest[ne__nm1] = '\0'; } while (0)
  135 
  136 /* Return malloc-allocated concatenation of all NUL-terminated string
  137  * arguments, up to a terminating NULL pointer. */
  138 char *ne_concat(const char *str, ...);
  139 
  140 /* Wrapper for snprintf: always NUL-terminates returned buffer, and
  141  * returns strlen(str). */
  142 size_t ne_snprintf(char *str, size_t size, const char *fmt, ...)
  143     ne_attribute((format(printf, 3, 4)));
  144 
  145 /* Wrapper for vsnprintf. */
  146 size_t ne_vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
  147     ne_attribute((format(printf, 3, 0)));
  148 
  149 /* Implementations of strcasecmp and strncasecmp which behave as
  150  * defined by the ANSI C strcasecmp() and strncasecmp() when in the
  151  * POSIX locale; i.e. ignoring the process locale. */
  152 
  153 /* Compares 's1' and 's2', ignoring differences in case. */
  154 int ne_strcasecmp(const char *s1, const char *s2);
  155 /* Compares up to 'n' characters of 's1' and 's2', ignoring
  156  * differences in case. */
  157 int ne_strncasecmp(const char *s1, const char *s2, size_t n);
  158 
  159 /* Return lowercase 'c' as in POSIX locale; note difference from ANSI
  160  * C semantics as both the argument and return value are unsigned
  161  * char. */
  162 #define ne_tolower(c) (ne_tolower_array()[(unsigned char)c])
  163 
  164 const unsigned char *ne_tolower_array(void);
  165 
  166 /* Convert an ASCII hexadecimal character in the ranges '0'..'9'
  167  * 'a'..'f' 'A'..'F' to its numeric equivalent. */
  168 #define NE_ASC2HEX(x) (((x) <= '9') ? ((x) - '0') : \
  169                        (ne_tolower((x)) + 10 - 'a'))
  170 
  171 /* Convert an integer in the range 0..15 to the equivalent (lowercase)
  172  * ASCII hexadecimal equivalent character, in the range '0..9,'a..f' */
  173 #define NE_HEX2ASC(x) ((char) ((x) > 9 ? ((x) - 10 + 'a') : ((x) + '0')))
  174 
  175 NE_END_DECLS
  176 
  177 #endif /* NE_STRING_H */