| 1 | /*** type.c ******************************************************************* |
| 2 | ** |
| 3 | ** This file is part of BibTool. |
| 4 | ** It is distributed under the GNU General Public License. |
| 5 | ** See the file COPYING for details. |
| 6 | ** |
| 7 | ** (c) 1996-2020 Gerd Neugebauer |
| 8 | ** |
| 9 | ** Net: [email protected] |
| 10 | ** |
| 11 | ** This program is free software; you can redistribute it and/or modify |
| 12 | ** it under the terms of the GNU General Public License as published by |
| 13 | ** the Free Software Foundation; either version 2, or (at your option) |
| 14 | ** any later version. |
| 15 | ** |
| 16 | ** This program is distributed in the hope that it will be useful, |
| 17 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | ** GNU General Public License for more details. |
| 20 | ** |
| 21 | ** You should have received a copy of the GNU General Public License |
| 22 | ** along with this program; if not, write to the Free Software |
| 23 | ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | ** |
| 25 | **----------------------------------------------------------------------------- |
| 26 | ** |
| 27 | ** Description: |
| 28 | ** This file contains functions to support a separate treatment of |
| 29 | ** character types. The normal functions and macros in |ctype.h| are |
| 30 | ** replaced by those in |type.h|. This file contains an initialization |
| 31 | ** function which is required for the macros in |type.h| to work |
| 32 | ** properly. |
| 33 | ** |
| 34 | ** See also the documentation of the header file |type.h| for |
| 35 | ** further information. |
| 36 | ** |
| 37 | ******************************************************************************/ |
| 38 | |
| 39 | /*****************************************************************************/ |
| 40 | /* Internal Programs */ |
| 41 | /*===========================================================================*/ |
| 42 | |
| 43 | #define INIT_TYPE |
| 44 | #include <bibtool/general.h> |
| 45 | #include <bibtool/type.h> |
| 46 | |
| 47 | /*****************************************************************************/ |
| 48 | /* External Programs */ |
| 49 | /*===========================================================================*/ |
| 50 | |
| 51 | /*---------------------------------------------------------------------------*/ |
| 52 | |
| 53 | #define to_lower(C) ((C) + 'a'-'A') |
| 54 | #define to_upper(C) ((C) + 'A'-'a') |
| 55 | |
| 56 | /*----------------------------------------------------------------------------- |
| 57 | ** Function: init_type() |
| 58 | ** Purpose: This is the initialization routine for this file. This |
| 59 | ** has to be called before some of the macros in |type.h| |
| 60 | ** will work as described. It does no harm to call this |
| 61 | ** initialization more than once. It just takes some time. |
| 62 | ** |
| 63 | ** Note that this function is for internal purposes |
| 64 | ** only. The normal user should call |init_bibtool()| |
| 65 | ** instead. |
| 66 | ** Arguments: none |
| 67 | ** Returns: nothing |
| 68 | **___________________________________________________ */ |
| 69 | void init_type() /* */ |
| 70 | { register unsigned short int i; /* */ |
| 71 | /* */ |
| 72 | for ( i = 0; i < 256; ++i ) /* */ |
| 73 | { trans_lower[i] = is_upper(i) ? to_lower(i) : i;/* */ |
| 74 | trans_upper[i] = is_lower(i) ? to_upper(i) : i;/* */ |
| 75 | trans_id[i] = i; /* */ |
| 76 | } /* */ |
| 77 | } /*------------------------*/ |
| 78 | |
| 79 | /*----------------------------------------------------------------------------- |
| 80 | ** Function: add_word_sep() |
| 81 | ** Type: void |
| 82 | ** Purpose: |
| 83 | ** Mark some characters as word separator. |
| 84 | ** Arguments: |
| 85 | ** s the allowed word separator characters |
| 86 | ** Returns: nothing |
| 87 | **___________________________________________________ */ |
| 88 | void add_word_sep(s) /* */ |
| 89 | register String s; /* */ |
| 90 | { /* */ |
| 91 | if ( s == StringNULL ) return; /* */ |
| 92 | for ( ; *s ; s++ ) /* */ |
| 93 | { type__allowed[*s] |= T__WordSep; } /* */ |
| 94 | } /*------------------------*/ |
| 95 | |
| 96 | #ifdef DEBUG |
| 97 | #include <assert.h> |
| 98 | #endif |
| 99 | |
| 100 | /*----------------------------------------------------------------------------- |
| 101 | ** Function: case_eq() |
| 102 | ** Purpose: Compare two strings ignoring cases. If the strings are |
| 103 | ** identical up to differences in case then this function |
| 104 | ** returns |true|. |
| 105 | ** Arguments: |
| 106 | ** s First string to consider. |
| 107 | ** t Second string to consider. |
| 108 | ** Returns: |true| iff the strings are equal. |
| 109 | **___________________________________________________ */ |
| 110 | bool case_eq(s, t) /* */ |
| 111 | register String s; /* */ |
| 112 | register String t; /* */ |
| 113 | { /* */ |
| 114 | #ifdef DEBUG |
| 115 | assert(s != NULL); /* */ |
| 116 | assert(t != NULL); /* */ |
| 117 | #endif |
| 118 | while ( *s ) /* */ |
| 119 | { if (ToLower(*(s++)) != ToLower(*(t++))) /* */ |
| 120 | return false; /* */ |
| 121 | } /* */ |
| 122 | #ifdef DEBUG |
| 123 | assert(t != NULL); /* */ |
| 124 | #endif |
| 125 | return (*t == '\0' ? true : false); /* */ |
| 126 | } /*------------------------*/ |
| 127 | |
| 128 | /*----------------------------------------------------------------------------- |
| 129 | ** Function: cmp() |
| 130 | ** Type: int |
| 131 | ** Purpose: Compare two strings. |
| 132 | ** Arguments: |
| 133 | ** s the first string |
| 134 | ** t the second string |
| 135 | ** Returns: |
| 136 | **___________________________________________________ */ |
| 137 | int cmp(s, t) /* */ |
| 138 | register String s; /* */ |
| 139 | register String t; /* */ |
| 140 | { register int d; /* */ |
| 141 | while ( *s ) /* */ |
| 142 | { d = *(s++) - *(t++); /* */ |
| 143 | if ( d ) return d; /* */ |
| 144 | } /* */ |
| 145 | return *s - *t; /* */ |
| 146 | } /*------------------------*/ |
| 147 | |
| 148 | /*----------------------------------------------------------------------------- |
| 149 | ** Function: lower() |
| 150 | ** Purpose: Function to translate all letters in a string to lower case. |
| 151 | ** Arguments: |
| 152 | ** s string to convert |
| 153 | ** Returns: The converted string. |
| 154 | **___________________________________________________ */ |
| 155 | String lower(s) /* */ |
| 156 | register String s; /* */ |
| 157 | { String t = s; /* */ |
| 158 | while ( *s ) { *s = ToLower(*s); ++s; } /* */ |
| 159 | return t; /* */ |
| 160 | } /*------------------------*/ |