root/libutil/strmake.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. strmake
  2. strtrim
  3. strcmp_withterm
  4. strcpy_withterm

   1 /*
   2  * Copyright (c) 1998, 1999, 2000, 2004, 2006, 2010
   3  *      Tama Communications Corporation
   4  *
   5  * This file is part of GNU GLOBAL.
   6  *
   7  * This program is free software: you can redistribute it and/or modify
   8  * it under the terms of the GNU General Public License as published by
   9  * the Free Software Foundation, either version 3 of the License, or
  10  * (at your option) any later version.
  11  * 
  12  * This program is distributed in the hope that it will be useful,
  13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  * GNU General Public License for more details.
  16  * 
  17  * You should have received a copy of the GNU General Public License
  18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19  */
  20 
  21 #ifdef HAVE_CONFIG_H
  22 #include <config.h>
  23 #endif
  24 #ifdef STDC_HEADERS
  25 #include <stdlib.h>
  26 #endif
  27 
  28 #include "strbuf.h"
  29 #include "strmake.h"
  30 
  31 /**
  32  * strmake: make string from original string with limit character.
  33  *
  34  *      @param[in]      p       original string.
  35  *      @param[in]      lim     limitter
  36  *      @return         result string
  37  *
  38  * @par Usage:
  39  * @code
  40  *      strmake("aaa:bbb", ":/=")       => "aaa"
  41  * @endcode
  42  *
  43  * @note The result string area is function local. So, following call
  44  *       to this function may destroy the area.
  45  */
  46 const char *
  47 strmake(const char *p, const char *lim)
  48 {
  49         STATIC_STRBUF(sb);
  50         const char *c;
  51 
  52         strbuf_clear(sb);
  53         for (; *p; p++) {
  54                 for (c = lim; *c; c++)
  55                         if (*p == *c)
  56                                 goto end;
  57                 strbuf_putc(sb,*p);
  58         }
  59 end:
  60         return strbuf_value(sb);
  61 }
  62 
  63 /**
  64  * strtrim: make string from original string with deleting blanks.
  65  *
  66  *      @param[in]      p       original string.
  67  *      @param[in]      flag    #TRIM_HEAD:     from only head <br>
  68  *                      #TRIM_TAIL:     from only tail <br>
  69  *                      #TRIM_BOTH:     from head and tail <br>
  70  *                      #TRIM_ALL:      from all
  71  *      @param[out]     len     length of result string <br>
  72  *                      if @CODE{len == NULL} then nothing returned.
  73  *      @return         result string
  74  *
  75  * @par Usage:
  76  * @code
  77  *      strtrim(" # define ", TRIM_HEAD, NULL)  => "# define "
  78  *      strtrim(" # define ", TRIM_TAIL, NULL)  => " # define"
  79  *      strtrim(" # define ", TRIM_BOTH, NULL)  => "# define"
  80  *      strtrim(" # define ", TRIM_ALL, NULL)   => "#define"
  81  * @endcode
  82  *
  83  * @note The result string area is function local. So, following call
  84  *       to this function may destroy the area.
  85  */
  86 const char *
  87 strtrim(const char *p, int flag, int *len)
  88 {
  89         STATIC_STRBUF(sb);
  90         int cut_off = -1;
  91 
  92         strbuf_clear(sb);
  93         /*
  94          * Delete blanks of the head.
  95          */
  96         if (flag != TRIM_TAIL)
  97                 SKIP_BLANKS(p);
  98         /*
  99          * Copy string.
 100          */
 101         for (; *p; p++) {
 102                 if (isspace(*p)) {
 103                         if (flag != TRIM_ALL) {
 104                                 if (cut_off == -1 && flag != TRIM_HEAD)
 105                                         cut_off = strbuf_getlen(sb);
 106                                 strbuf_putc(sb,*p);
 107                         }
 108                 } else {
 109                         strbuf_putc(sb,*p);
 110                         cut_off = -1;
 111                 }
 112         }
 113         /*
 114          * Delete blanks of the tail.
 115          */
 116         if (cut_off != -1)
 117                 strbuf_setlen(sb, cut_off);
 118         if (len)
 119                 *len = strbuf_getlen(sb);
 120         return strbuf_value(sb);
 121 }
 122 /**
 123  * strcmp with terminate character.
 124  *
 125  *      @param[in]      s1      string1
 126  *      @param[in]      s2      string2
 127  *      @param[in]      term    terminate character
 128  *      @return         ==0: equal, !=0: not equal
 129  *
 130  * @par Usage:
 131  * @code
 132  *      strcmp_withterm("aaa", "aaa", ':')              => 0
 133  *      strcmp_withterm("aaa:bbb", "aaa", ':')          => 0
 134  *      strcmp_withterm("aaa:bbb", "aaa:ccc", ':')      => 0
 135  *      strcmp_withterm("aaa/bbb", "aaa/ccc", ':')      => -1
 136  * @endcode
 137  */
 138 int
 139 strcmp_withterm(const char *s1, const char *s2, int term)
 140 {
 141         unsigned int c1, c2;
 142 
 143         do {
 144                 c1 = *s1++;
 145                 c2 = *s2++;
 146                 /* replace terminate character with NULL */
 147                 if (c1 == term)
 148                         c1 = '\0';
 149                 if (c2 == term)
 150                         c2 = '\0';
 151         } while (c1 == c2 && c1 != '\0');
 152 
 153         return c1 - c2;
 154 }
 155 /**
 156  * strcpy with terminate character.
 157  *
 158  *      @param[in]      b       buffer
 159  *      @param[in]      s       string
 160  *      @param[in]      size    buffer size
 161  *      @param[in]      term    terminate character
 162  *      @return         terminator's position
 163  */
 164 const char *
 165 strcpy_withterm(char *b, const char *s, int size, int term)
 166 {
 167         char *endp = b + size - 1;
 168 
 169         while (*s && *s != term)
 170                 if (b < endp)
 171                         *b++ = *s++;
 172         *b = '\0';
 173 
 174         return s;
 175 }

/* [previous][next][first][last][top][bottom][index][help] */