/* ========================================================================== ** * util.c * * Copyright: * Copyright (C) 2007 by Christopher R. Hertel * * Email: crh@ubiqx.mn.org * * $Id: util.c 53 2011-04-25 20:39:01Z crh $ * * -------------------------------------------------------------------------- ** * * Description: * Utility functions used with the STiB tool. * * -------------------------------------------------------------------------- ** * * License: * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * -------------------------------------------------------------------------- ** * * Notes: * * ========================================================================== ** */ #include /* For toupper(3). */ #include /* For strcpy(3). */ #include /* For strcasecmp(3). */ #include "util.h" /* Module header. */ /* -------------------------------------------------------------------------- ** * Exported functions: */ void Fail( char *fmt, ... ) /* ------------------------------------------------------------------------ ** * Format and print a failure message on , then exit the process. * * Input: fmt - Format string, as used in printf(), etc. * ... - Variable parameter list. * * Output: none * * Notes: Exits the process returning EXIT_FAILURE. * * ------------------------------------------------------------------------ ** */ { va_list ap; va_start( ap, fmt ); (void)fprintf( stderr, "Failure: " ); (void)vfprintf( stderr, fmt, ap ); va_end( ap ); exit( EXIT_FAILURE ); } /* Fail */ void Usage( const char *msg[], const char *prognam ) /* ------------------------------------------------------------------------ ** * Print a usage message to . * * Input: msg[] - An array of strings; the usage message. * prognam - The program name. * * Output: * * Notes: The error message is printed to so that it can be * easily piped through a program like "more" or "less". * * ------------------------------------------------------------------------ ** */ { int i; for( i = 0; NULL != msg[i]; i++ ) { Say( msg[i], prognam ); Say( "\n" ); } } /* Usage */ int matchStr( const char *s, const char *m[] ) /* ------------------------------------------------------------------------ ** * Find a string that matches in the set of strings . * * Input: s - A string to be matched. * m[] - A set of strings that may match, presented as a * NULL-terminated variable-length array of strings. * * Output: The number (starting from 0) of the string in that * matched , or -1 if there was no match. * * Notes: The string comparison is case insensitive. * * See Also: * * ------------------------------------------------------------------------ ** */ { int i; for( i = 0; (NULL != m[i]); i++ ) { if( 0 == strcasecmp( s, m[i] ) ) return( i ); } return( -1 ); } /* matchStr */ bool prefixMatch( const char *src, const char *match, const unsigned int min ) /* ------------------------------------------------------------------------ ** * Perform a case-insensitive string match, limited to strlen( src ) bytes. * * Input: src - A pointer to a string that may be a prefix of . * match - A pointer to the string against which to test. * min - Minimum length of needed in order to declare * a match. * * Output: Boolean if the string in is a prefix of * and is at least bytes in length, otherwise . * * Notes: This function tests to see whether strlen( src ) bytes of * are the same as . So, for example: * prefixMatch( "foo", "fooberry", 1 ) == true * prefixMatch( "food", "fooberry", 1 ) == false * * The parameter is used to determine the minimum number * of characters in which must be matched. So: * prefixMatch( "foo", "fooberry", 3 ) == true * prefixMatch( "foo", "fooberry", 4 ) == false * since the string "foo" is not long enough to match 4 bytes of * "fooberry". * * The function is case insensitive, so: * prefixMatch( "foo", "Fooberry", 3 ) == true * * ------------------------------------------------------------------------ ** */ { unsigned int i; for( i = 0; '\0' != src[i]; i++ ) { if( toupper( src[i] ) != toupper( match[i] ) ) return( false ); } if( i < min ) return( false ); return( true ); } /* prefixMatch */ char *vString( char *Rev ) /* ------------------------------------------------------------------------ ** * Extract just the version number from a cvs or svn Revision string. * * Input: Rev - Full Revision string (including the dollar signs). * * Output: Pointer to static space that contains the extracted version * number string. * * ------------------------------------------------------------------------ ** */ { static char versionString[ 16 ]; int i, j; if( prefixMatch( "$Revision:", Rev, 10 ) ) { i = 10; while( ' ' == Rev[i] ) i++; j = 0; while( (j < 15) && ('\0' != Rev[i]) && ('$' != Rev[i]) && (!isspace( Rev[i] )) ) { versionString[j++] = Rev[i++]; } versionString[j] = '\0'; } /* If unparsable... default to "0.0". */ if( '\0' == *versionString ) (void)strcpy( versionString, "0.0" ); return( versionString ); } /* vString */ /* ========================================================================== */