Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

util_str.c

Go to the documentation of this file.
00001 /**
00002  * @file   util_str.c
00003  * @author Chris Green <cmg@sourcefire.com>
00004  * @date   Fri Jun 27 10:41:59 2003
00005  * 
00006  * @brief  utility string functions
00007  * 
00008  * 
00009  */
00010 #ifdef HAVE_CONFIG_H
00011 #include "config.h"
00012 #endif
00013 
00014 
00015 #include "util_str.h"
00016 #include <stdlib.h>
00017 #ifdef HAVE_STRINGS_H
00018 #include <strings.h>
00019 #endif
00020 
00021 /** 
00022  * Convert a string to an int and check for problems
00023  * 
00024  * @param str string to parse as an int
00025  * @param ret return value for the int
00026  * @param allow_negative allow negative values 
00027  * 
00028  * @return 0 on sucess, else failure
00029  */
00030 int str2int(char *str, int *ret, int allow_negative)
00031 {
00032     char *endptr;
00033     long int value;
00034 
00035     if(ret && str && *str != '\0')
00036     {
00037         value = strtol(str, &endptr, 10);
00038 
00039         if(endptr == str)
00040         {
00041             /* parsing has failed */
00042             return -1;
00043         }
00044 
00045         if(!allow_negative)
00046         {
00047             if(value < 0)
00048             {
00049                 return -1;
00050             }
00051         }
00052 
00053         *ret = value;
00054            
00055         return 0;
00056     }
00057 
00058     return -1;
00059 }
00060 
00061 /** 
00062  * Set opt_value to 1 if the value is on, 0 if it's off
00063  * 
00064  * @param name option name to configure (not used but useful for debugging)
00065  * @param value value to configure (should be either on or off )
00066  * @param opt_value ptr to integer to configure
00067  *
00068  * @returns 0 on success , else failure
00069  */
00070 int toggle_option(char *name, char *value, int *opt_value)
00071 {
00072     int opt_on, opt_off;
00073     
00074     if(!name || !value || !opt_value || (*value == '\0') || (*name == '\0') )
00075         return -1;
00076 
00077     opt_on  = strcasecmp(value,"on");
00078     opt_off = strcasecmp(value,"off");
00079 
00080     if(opt_off && opt_on)
00081     {
00082         /*
00083          * the string is neither "on" or "off"
00084          *
00085          * we don't know what the hell we're looking at. return error.
00086          */
00087         return -2;
00088     }
00089 
00090     if(opt_on == 0)
00091         *opt_value = 1;
00092     else
00093         *opt_value = 0;
00094 
00095     return 0;
00096 }
00097 
00098 
00099 #ifdef TEST_UTIL_STR
00100 int main(void)
00101 {
00102     int value;
00103 
00104     printf("you should see 4 pass messages\n");
00105 
00106     if(str2int("-1",&value,0) != 0)
00107         printf("test 1 passed and failed to parse\n");
00108 
00109     if(str2int("-1",&value,1) == 0 && value == -1)
00110         printf("test 2 passed: %d\n", value);
00111 
00112     if(str2int("0",&value,1) == 0 && value == 0 )
00113         printf("test 3 passed: %d\n", value);
00114     
00115     if(str2int("124",&value,1) == 0 && value == 124 )
00116         printf("test 4 passed: %d\n", value);
00117 
00118 
00119     
00120     
00121     
00122     
00123 }
00124 #endif /* TEST_UTIL_STR */

Generated on Sun May 14 14:51:18 2006 by  doxygen 1.4.2