src/str.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. vf_strdup
  2. vf_strcmp_ci
  3. vf_strncmp_ci
  4. vf_index
  5. vf_index_i
  6. vf_rindex
  7. vf_rindex_i
  8. vf_index_str_i
  9. vf_index_str
  10. vf_rindex_str_i
  11. vf_rindex_str
  12. vf_parse_bool

   1 /*
   2  * str.c - string functions
   3  *
   4  */
   5 /*
   6  * Copyright (C) 1996-1998  Hirotsugu Kakugawa. 
   7  * All rights reserved.
   8  *
   9  * This file is part of the VFlib Library.  This library is free
  10  * software; you can redistribute it and/or modify it under the terms of
  11  * the GNU Library General Public License as published by the Free
  12  * Software Foundation; either version 2 of the License, or (at your
  13  * option) any later version.  This library is distributed in the hope
  14  * that it will be useful, but WITHOUT ANY WARRANTY; without even the
  15  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  16  * PURPOSE.  See the GNU Library General Public License for more details.
  17  * You should have received a copy of the GNU Library General Public
  18  * License along with this library; if not, write to the Free Software
  19  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20  */
  21 
  22 #include "config.h"
  23 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <ctype.h>
  26 #ifdef HAVE_UNISTD_H
  27 #  include <unistd.h>
  28 #endif
  29 #if HAVE_STRING_H
  30 # include <string.h>
  31 #endif
  32 #if HAVE_STRINGS_H
  33 # include <strings.h>
  34 #endif
  35 #include <sys/types.h>
  36 #include <sys/param.h>
  37 
  38 #include "VFlib-3_6.h"
  39 #include "VFsys.h"
  40 #include "str.h"
  41 
  42 
  43 Glocal char*
  44 vf_strdup(char *s)
     /* [<][>][^][v][top][bottom][index][help] */
  45 {
  46   char *p;
  47 
  48   if (s == NULL)
  49     return NULL;
  50 
  51   if ((p = (char*)malloc(strlen(s)+1)) == NULL){
  52     vf_error = VF_ERR_NO_MEMORY;
  53     return NULL;
  54   }
  55   strcpy(p, s);
  56   return p;
  57 }
  58 
  59 
  60 Glocal int
  61 vf_strcmp_ci(char *s1, char *s2)
     /* [<][>][^][v][top][bottom][index][help] */
  62 {
  63   for (;;){
  64     if ((*s1 == '\0') && (*s2 == '\0'))
  65       return 0;
  66     if ((*s1 == '\0') || (*s2 == '\0'))
  67       return 1;
  68     if (tolower(*s1) != tolower(*s2))
  69       return 1;
  70     s1++;
  71     s2++;
  72   }
  73 }
  74 
  75 
  76 Glocal int
  77 vf_strncmp_ci(char *s1, char *s2, int n)
     /* [<][>][^][v][top][bottom][index][help] */
  78 {
  79 
  80   while (n > 0){
  81     if ((*s1 == '\0') && (*s2 == '\0'))
  82       return 0;
  83     if ((*s1 == '\0') || (*s2 == '\0'))
  84       return 1;
  85     if (tolower(*s1) != tolower(*s2))
  86       return 1;
  87     s1++;
  88     s2++;
  89     n--;
  90   }
  91   return 0;
  92 }
  93 
  94 
  95 Glocal char*
  96 vf_index(char *s, char ch)
     /* [<][>][^][v][top][bottom][index][help] */
  97 {
  98   int  i;
  99 
 100   if ((i = vf_index_i(s, ch)) < 0)
 101     return NULL;
 102   return &s[i];
 103 }
 104 
 105 
 106 Glocal int
 107 vf_index_i(char *s, char ch)
     /* [<][>][^][v][top][bottom][index][help] */
 108 {
 109   int  i;
 110 
 111   i = 0;
 112   while (s[i] != '\0'){
 113     if (s[i] == ch)
 114       return i;
 115     i++;
 116   }
 117   return -1;
 118 }
 119 
 120 
 121 Glocal char*
 122 vf_rindex(char *s, char ch)
     /* [<][>][^][v][top][bottom][index][help] */
 123 {
 124   int  i;
 125 
 126   if ((i = vf_rindex_i(s, ch)) < 0)
 127     return NULL;
 128   return &s[i];
 129 }
 130 
 131 
 132 Glocal int
 133 vf_rindex_i(char *s, char ch)
     /* [<][>][^][v][top][bottom][index][help] */
 134 {
 135   int  i;
 136 
 137   if (s == NULL)
 138     return -1;
 139 
 140   i = strlen(s)-1;
 141   while (i >= 0){
 142     if (s[i] == ch)
 143       return i;
 144     --i;
 145   }
 146   return -1;
 147 }
 148 
 149 
 150 Glocal int
 151 vf_index_str_i(char *s, char *t)
     /* [<][>][^][v][top][bottom][index][help] */
 152 {
 153   int  i, tlen;
 154 
 155   if (s == NULL)
 156     return -1;
 157 
 158   tlen = strlen(t);
 159   i = 0;
 160   while (s[i] != '\0'){
 161     if (strncmp(&s[i], t, tlen) == 0)
 162       return i;
 163     i++;
 164   }
 165   return -1;
 166 }
 167 
 168 
 169 Glocal char*
 170 vf_index_str(char *s, char *t)
     /* [<][>][^][v][top][bottom][index][help] */
 171 {
 172   int  i;
 173 
 174   if ((i = vf_index_str_i(s, t)) < 0)
 175     return NULL;
 176   return &s[i];
 177 }
 178 
 179 
 180 Glocal int
 181 vf_rindex_str_i(char *s, char *t)
     /* [<][>][^][v][top][bottom][index][help] */
 182 {
 183   int  i, tlen;
 184 
 185   if ((s == NULL) | (t == NULL))
 186     return -1;
 187 
 188   tlen = strlen(t);
 189   i = strlen(s) - tlen;
 190   while (i >= 0){
 191     if (strncmp(&s[i], t, tlen) == 0)
 192       return i;
 193     --i;
 194   }
 195   return -1;
 196 }
 197 
 198 Glocal char*
 199 vf_rindex_str(char *s, char *t)
     /* [<][>][^][v][top][bottom][index][help] */
 200 {
 201   int  i;
 202 
 203   if ((i = vf_rindex_str_i(s, t)) < 0)
 204     return NULL;
 205   return &s[i];
 206 }
 207 
 208 
 209 Glocal int
 210 vf_parse_bool(char *s)
     /* [<][>][^][v][top][bottom][index][help] */
 211 
 212 {
 213   if (   (vf_strncmp_ci(s, "TRUE", 4) == 0)
 214       || (vf_strncmp_ci(s, "YES", 3) == 0) 
 215       || (vf_strncmp_ci(s, "OK", 2) == 0) 
 216       || (vf_strncmp_ci(s, "ON", 2) == 0) 
 217       || (vf_strncmp_ci(s, "T", 1) == 0)   /* lisp   */
 218       || (vf_strncmp_ci(s, "#T", 2) == 0)  /* scheme */
 219       || (vf_strncmp_ci(s, "1", 1) == 0)  )
 220     return TRUE;
 221 
 222   if (   (vf_strncmp_ci(s, "FALSE", 5) == 0)
 223       || (vf_strncmp_ci(s, "NO", 2) == 0) 
 224       || (vf_strncmp_ci(s, "OFF", 2) == 0) 
 225       || (vf_strncmp_ci(s, "NIL", 3) == 0)  /* lisp   */
 226       || (vf_strncmp_ci(s, "#F", 2) == 0)   /* scheme */
 227       || (vf_strncmp_ci(s, "0", 1) == 0)  )
 228     return FALSE;
 229 
 230     /*XXX*/
 231     /* Need a warning message? */   
 232     /* Need to specify default value? */
 233 
 234   return FALSE;
 235 }
 236 
 237 
 238 /*EOF*/

/* [<][>][^][v][top][bottom][index][help] */