strings.c

Go to the documentation of this file.
00001 /*
00002  ** Copyright (C) 2008 INL
00003  ** Written by Sebastien Tricaud <s.tricaud@inl.fr>
00004  ** INL http://www.inl.fr/
00005  **
00006  ** $Id: strings.c 4847 2008-06-13 14:48:31Z regit $
00007  **
00008  ** This program is free software; you can redistribute it and/or modify
00009  ** it under the terms of the GNU General Public License as published by
00010  ** the Free Software Foundation, version 3 of the License.
00011  **
00012  ** This program is distributed in the hope that it will be useful,
00013  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  ** GNU General Public License for more details.
00016  **
00017  ** You should have received a copy of the GNU General Public License
00018  ** along with this program; if not, write to the Free Software
00019  ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020  */
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <stdarg.h>
00024 #include <stdlib.h>
00025 #include <ctype.h>         /* isspace() */
00026 
00027 #include <nubase.h>
00028 
00050 int secure_snprintf(char *buffer, size_t buffer_size,
00051                          char *format, ...)
00052 {
00053         va_list args;
00054         int ret;
00055         va_start(args, format);
00056 #ifdef DEBUG_ENABLE
00057         memset(buffer, 0, buffer_size);
00058 #else
00059         buffer[0] = 0;
00060 #endif
00061         ret = vsnprintf(buffer, buffer_size, format, args);
00062         va_end(args);
00063         buffer[buffer_size - 1] = '\0';
00064         if (0 <= ret && ret <= ((int) buffer_size - 1))
00065                 return TRUE;
00066         else
00067                 return FALSE;
00068 }
00069 
00078 char *str_extract_until(char *str, int c)
00079 {
00080         unsigned int i;
00081 
00082         char *newstr;
00083         char *last_str;
00084 
00085         size_t last_size;
00086         size_t str_size;
00087         size_t newstr_size;
00088 
00089         last_str = strrchr(str, c);
00090         if ( ! last_str ) return NULL;
00091         last_size = strlen(last_str);
00092         str_size = strlen(str);
00093         newstr_size = str_size - last_size;
00094         newstr = malloc(newstr_size + 2);
00095         if ( ! newstr ) return NULL;
00096 
00097         for (i=0;i<newstr_size;i++) {
00098                 newstr[i] = *str++;
00099         }
00100 
00101         newstr[i] = c;
00102         newstr[i+1] = '\0';
00103 
00104         return newstr;
00105 
00106 }
00107 
00113 int str_to_long(const char *text, long *value)
00114 {
00115         char *err = NULL;
00116         long longvalue;
00117 
00118         /* skip spaces */
00119         while (isspace(*text))
00120                 text++;
00121 
00122         /* call strtol */
00123         longvalue = strtol(text, &err, 10);
00124         if (err == NULL || *err != 0)
00125                 return 0;
00126         *value = longvalue;
00127         return 1;
00128 }
00129 
00135 int str_to_ulong(const char *text, unsigned long *value)
00136 {
00137         char *err = NULL;
00138         unsigned long ulongvalue;
00139 
00140         /* skip spaces */
00141         while (isspace(*text))
00142                 text++;
00143 
00144         /* call strtol */
00145         ulongvalue = strtoul(text, &err, 10);
00146         if (err == NULL || *err != 0)
00147                 return 0;
00148         *value = ulongvalue;
00149         return 1;
00150 }
00151 
00157 int str_to_int(const char *text, int *value)
00158 {
00159         long longvalue;
00160         if (!str_to_long(text, &longvalue))
00161                 return 0;
00162         if (longvalue < INT_MIN || INT_MAX < longvalue)
00163                 return 0;
00164         *value = (int)longvalue;
00165         return 1;
00166 }
00167 
00173 int str_to_uint32(const char *text, uint32_t * value)
00174 {
00175         unsigned long ulongvalue;
00176         if (!str_to_ulong(text, &ulongvalue))
00177                 return 0;
00178         if (4294967295UL < ulongvalue)
00179                 return 0;
00180         *value = (uint32_t)ulongvalue;
00181         return 1;
00182 }
00183 
00184 char *str_itoa(int i)
00185 {
00186 
00187         char *str;
00188         int strsize;
00189         int ret;
00190 
00191         strsize = snprintf(NULL, 0, "%d", i);
00192         if ( strsize <= 0 ) return strdup("");
00193         str = malloc(strsize + 1);
00194         if ( ! str ) return strdup("");
00195         ret = snprintf(str, strsize + 1, "%d", i);
00196         if ( ret <= 0 ) return strdup("");
00197 
00198         str[ret] = '\0';
00199 
00200         return str;
00201 }
00202 

Generated on Sat Nov 22 04:00:37 2008 for NuFW by  doxygen 1.4.7