Back to index

tetex-bin  3.0
strings.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 2000,2003 Free Software Foundation, Inc.                   *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Thomas E. Dickey                                                *
00031  ****************************************************************************/
00032 
00033 /*
00034 **     lib_mvcur.c
00035 **/
00036 
00037 #include <curses.priv.h>
00038 
00039 MODULE_ID("$Id: strings.c,v 1.5 2003/08/16 23:46:00 tom Exp $")
00040 
00041 /****************************************************************************
00042  * Useful string functions (especially for mvcur)
00043  ****************************************************************************/
00044 
00045 #if !HAVE_STRSTR
00046 NCURSES_EXPORT(char *)
00047 _nc_strstr(const char *haystack, const char *needle)
00048 {
00049     size_t len1 = strlen(haystack);
00050     size_t len2 = strlen(needle);
00051     char *result = 0;
00052 
00053     while ((len1 != 0) && (len1-- >= len2)) {
00054        if (!strncmp(haystack, needle, len2)) {
00055            result = haystack;
00056            break;
00057        }
00058        haystack++;
00059     }
00060     return result;
00061 }
00062 #endif
00063 
00064 /*
00065  * Initialize the descriptor so we can append to it.  Note that 'src' may
00066  * be a null pointer (see _nc_str_null), so the corresponding strcat and
00067  * strcpy calls have to allow for this.
00068  */
00069 NCURSES_EXPORT(string_desc *)
00070 _nc_str_init(string_desc * dst, char *src, size_t len)
00071 {
00072     if (dst != 0) {
00073        dst->s_head = src;
00074        dst->s_tail = src;
00075        dst->s_size = len - 1;
00076        dst->s_init = dst->s_size;
00077        if (src != 0)
00078            *src = 0;
00079     }
00080     return dst;
00081 }
00082 
00083 /*
00084  * Initialize the descriptor for only tracking the amount of memory used.
00085  */
00086 NCURSES_EXPORT(string_desc *)
00087 _nc_str_null(string_desc * dst, size_t len)
00088 {
00089     return _nc_str_init(dst, 0, len);
00090 }
00091 
00092 /*
00093  * Copy a descriptor
00094  */
00095 NCURSES_EXPORT(string_desc *)
00096 _nc_str_copy(string_desc * dst, string_desc * src)
00097 {
00098     *dst = *src;
00099     return dst;
00100 }
00101 
00102 /*
00103  * Replaces strcat into a fixed buffer, returning false on failure.
00104  */
00105 NCURSES_EXPORT(bool)
00106 _nc_safe_strcat(string_desc * dst, const char *src)
00107 {
00108     if (src != 0) {
00109        size_t len = strlen(src);
00110 
00111        if (len < dst->s_size) {
00112            if (dst->s_tail != 0) {
00113               strcpy(dst->s_tail, src);
00114               dst->s_tail += len;
00115            }
00116            dst->s_size -= len;
00117            return TRUE;
00118        }
00119     }
00120     return FALSE;
00121 }
00122 
00123 /*
00124  * Replaces strcpy into a fixed buffer, returning false on failure.
00125  */
00126 NCURSES_EXPORT(bool)
00127 _nc_safe_strcpy(string_desc * dst, const char *src)
00128 {
00129     if (src != 0) {
00130        size_t len = strlen(src);
00131 
00132        if (len < dst->s_size) {
00133            if (dst->s_head != 0) {
00134               strcpy(dst->s_head, src);
00135               dst->s_tail = dst->s_head + len;
00136            }
00137            dst->s_size = dst->s_init - len;
00138            return TRUE;
00139        }
00140     }
00141     return FALSE;
00142 }