/* librock/text/strfn.c librock_CHISEL _summary Variants on string searching and comparison Table of Contents. [License and Disclaimer of Warranty] [License Attributes] [How to Reuse this software] [Definitions and header file inclusions] [Function Implementation and inline documentation] [Revision summary and Changelog] */ /*************************************************************/ /***[License and Disclaimer of Warranty]**********************/ /* By copyright law, you must agree to these terms before you modify or copy this software or any part of it. Copyright (c) 2004, Forrest J. Cavalier III Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************/ /***[License Attributes]**************************************/ /* The librock LIDESC tool reports some license attributes. It reports the following for the MIT license used for this software: A notice must be included in documentation or other materials when a binary is distributed. (librock_DOCNOTE_1C_WITHBIN) Notices must remain in source code. (librock_SRCNOTE_1) The Free Software Foundation says this license is a Free software license. (librock_LIDESC_COMBINE_FSF_OK) The Free Software Foundation says this license is GPL compatible. (librock_LIDESC_COMBINE_GPL_OK) The Open Source Initiative approved this license as OSD compliant. (librock_LIDESC_COMBINE_OSD_OK) Note: You may be able to use LIDESC tools to check compatibility with other licenses. */ /*************************************************************/ /***[How to Reuse this software]******************************/ /* 1. Agree to the license and disclaimer of warranty. 2. Read this file for function documentation and examples. Verify the implementation is suitable for you. 3. Define preprocessor symbols for your machine and compilation environment. This file is originally from libROCK: the Reusable Opensource Code and Knowledge library. For an automated update and build system, license certificates, and more, see http://www.mibsoftware.com/librock/ This file is written for ANSI C: librock_CHISEL _portreport_permit_ansi Some comments used for CHISEL portreport suppression (!empty) (!=0). */ /*************************************************************/ /***[Definitions and header file inclusions]******************/ #ifdef librock_ISOLATED #else #define librock_IMPLEMENT_strfn #include #include #include #endif /* ifdef librock_ISOLATED else */ /*************************************************************/ /***[Function Implementation and inline documentation]********/ #ifdef librock_IMPL_LIDESC /**************************************************************/ #include /* librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ void *librock_LIDESC_strfn[] = { "\n" __FILE__ librock_LIDESC_mit "\n", 0 }; /**************************************************************/ #endif /****************************************************************/ int librock_wordcmp(librock_CONST char librock_PTR *w1,librock_CONST char librock_PTR *w2) { /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_wordcmp /* librock_wordcmp - compare two strings up until the first blank character librock_wordcasecmp - compare two strings up until the first blank character, ignore case. */ /**/ #include int librock_wordcmp(const char *w1,char *w2); /* Compare two strings up until the first blank character. Returns > 0 if (w1 > w2) 0 if (w1 == w2) < 0 if (w1 < w2) This is a straight character by character compare, and is not affected by LOCALE. It is suitable for comparing words with characters in the range 0x21-0x7f. Typical use is similar to strcmp(), with the difference that any character <= ' ' is taken as a string terminator. For example: */ #ifdef librock_TYPICAL_USE_wordcmp const char *ptr = "keyword test"; if (!librock_wordcmp(ptr,"keyword")) { /* Process here */ printf("%s\n",ptr); } #endif /* // [Makes no calls] */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ #endif /* MANUAL */ /*-------------------------------------------*/ while((*w1 == *w2) && (*w1 > ' ') && (*w2 > ' ')) { w1++;w2++; } if ((*w1 > ' ') || (*w2 > ' ')) { return *w1 - *w2; } else { return 0; } } /* wordcmp */ /****************************************************************/ /****************************************************************/ int librock_wordcasecmp(librock_CONST char librock_PTR *word,librock_CONST char librock_PTR *lcword) { /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ librock_CONST char librock_PTR *w1; librock_CONST char librock_PTR *w2; char ch; char ch2; w1 = word; w2 = lcword; while(*w1 > ' ') { if (!*w2) { return 256; /* w1 is longer, therefore greater. */ } ch = *w1; if ((ch >= 'A')&&(ch <= 'Z')) { ch += 'a' - 'A'; } ch2 = *w2; if ((ch2 >= 'A')&&(ch2 <= 'Z')) { ch2 += 'a' - 'A'; } if (ch != ch2) { return ch - ch2; } w2++; w1++; } if (*w2 > ' ') { /* w2 is longer, therefore w1 is smaller */ return -256; } return 0; } /* wordcasecmp */ /****************************************************************/ /****************************************************************/ int librock_BTSinlist(librock_CONST char librock_PTR *bts,librock_CONST char librock_PTR *list) { /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_BTSinlist /* librock_BTSinlist - Find a blank terminated word in a word list. */ /**/ #include int librock_BTSinlist(const char *bts,const char *list); /* Finds a word in a space- or tab-separated word list. Returns an integer corresponding to the position in the list. Returns 0 if not found. Typical use is to recognize keywords from a list. */ #ifdef librock_TYPICAL_USE_BTSinlist char *keywords ="one two three four"; int i; i = librock_BTSinlist("one",keywords); if (i) { printf("Found at word %d\n",i); } else { printf("Did not recognize\n"); } #endif /* librock_wordcmp() librock_CHISEL _usesrc "./text/count.c" librock_countbl() librock_counttowh() */ /* Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #endif /* MANUAL */ /*-------------------------------------------*/ int ret = 1; if (!list) { return 0; } while(1) { list += librock_countbl(list); if (!librock_counttowh(list)) { break; } if (!librock_wordcmp(bts,list)) { return ret; } list += librock_counttowh(list); ret++; } return 0; } /* BTSinlist */ /****************************************************************/ /****************************************************************/ char librock_PTR *librock_strn0cpy(char librock_PTR *dest,librock_CONST char librock_PTR *src, int n) { /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_strn0cpy /* librock_strn0cpy - copy at most n characters, always appending a \0. */ /**/ #include char * librock_strn0cpy( char *dest, const char *src, int n ); /* Copy the smaller of (strlen(src), n) characters from src to dest, always appending a '\0' onto dest. This is a well-behaved work-alike for strncpy. Remember that n+1 bytes at dest will be affected if strlen(src) >= n. Returns the first argument. This does not '\0' fill dest[> strlen(src)] as some strncpy implementations do. Typical use is to replace clumsy two-step calls to strncpy such as: */ strncpy(dest,src,sizeof(dest)); dest[sizeof(dest)-1] = '\0'; /* with: */ librock_strn0cpy(dest,src,sizeof(dest-1)); /* If you don't want the '\0', then use memmove() */ /* // [Makes no calls] */ /* Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #endif /* MANUAL */ /*-------------------------------------------*/ char *ret = dest; while((n > 0) && *src) { *dest++ = *src++; n--; } *dest = 0; return ret; } /* strn0cpy */ /****************************************************************/ /***[Revision summary and Changelog]**************************/ /* $Log: strfn.c,v $ Revision 1.8 2004/06/16 19:53:29 fcavalier@mibsoftware.com rights=#1 #include librock/target/local.h Revision 1.7 2004/06/16 19:24:53 fcavalier@mibsoftware.com rights=#1 Convert to MIT license. Update for Table of Contents Revision 1.6 2002/08/01 16:11:55 fcavalier@mibsoftware.com rights=#1 Update TYPICAL_USE sections. Man page clarification. Revision 1.5 2002/08/01 14:17:27 fcavalier@mibsoftware.com rights=#1 Moved CVS log to end. Added NOTLIBROCK section. Changed LIDESC MD5 to HC. Revision 1.4 2002/04/09 03:28:49 fcavalier@mibsoftware.com rights=#1 FNTYPEs added. Updated LICENSE in manual. Revision 1.3 2002/02/05 21:42:49 fcavalier@mibsoftware.com rights=#1 Standardized chg log Revision 1.2 2002/01/30 16:08:25 fcavalier@mibsoftware.com rights=#1 Renamed some .h files Revision 1.1 2002/01/29 14:25:17 fcavalier@mibsoftware.com rights=#1 moved rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */