| 1 |
|
| /* ------------------------------------------------------------------------ |
| 2 |
|
| @NAME : names.c |
| 3 |
|
| @DESCRIPTION: Splitting lists of names (or anything else) on a delimiter |
| 4 |
|
| like "and", and splitting single names into their first, |
| 5 |
|
| von, last and jr parts according to the BibTeX rules: |
| 6 |
|
| bt_split_list |
| 7 |
|
| bt_split_name |
| 8 |
|
| Name tokens are classified as lowercase ("von" particles) |
| 9 |
|
| by the Unicode General_Category of their first character; |
| 10 |
|
| TeX markup is not interpreted. |
| 11 |
|
| @COPYRIGHT : Copyright (c) 1996-99 by Gregory P. Ward. All rights reserved. |
| 12 |
|
| |
| 13 |
|
| This file is part of the btparse library. This library is |
| 14 |
|
| free software; you can redistribute it and/or modify it under |
| 15 |
|
| the terms of the GNU Library General Public License as |
| 16 |
|
| published by the Free Software Foundation; either version 2 |
| 17 |
|
| of the License, or (at your option) any later version. |
| 18 |
|
| -------------------------------------------------------------------------- */ |
| 19 |
|
| |
| 20 |
|
| #include <stdlib.h> |
| 21 |
|
| #include <string.h> |
| 22 |
|
| #include "bt_internal.h" |
| 23 |
|
| |
| 24 |
|
| #define MAX_COMMAS 2 |
| 25 |
|
| |
| 26 |
|
| #define BT_IS_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || \ |
| 27 |
|
| (c) == '\r' || (c) == '\f' || (c) == '\v') |
| 28 |
|
| |
| 29 |
|
| typedef struct |
| 30 |
|
| { |
| 31 |
|
| bt_errors * errs; |
| 32 |
|
| const char * filename; |
| 33 |
|
| int line; |
| 34 |
|
| int name_num; |
| 35 |
|
| } name_loc; |
| 36 |
|
| |
| 37 |
|
| static void |
| 38 |
18 |
| name_warning (name_loc *loc, const char *msg) |
| 39 |
|
| { |
| 40 |
18 |
| bt_report (loc->errs, BTERR_CONTENT, loc->filename, loc->line, |
| 41 |
|
| "name", loc->name_num, "%s", msg); |
| 42 |
18 |
| } |
| 43 |
|
| |
| 44 |
|
| static void |
| 45 |
5069 |
| update_depth (char c, int *depth, name_loc *loc) |
| 46 |
|
| { |
| 47 |
5069 |
▶ | if (c == '{') |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '{') |
69 |
5000 |
5069 |
|
| 48 |
69 |
| (*depth)++; |
| 49 |
5000 |
▶ | else if (c == '}') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (c == '}') |
63 |
4937 |
5000 |
|
| 50 |
|
| { |
| 51 |
63 |
▶ | if (*depth) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*depth) |
61 |
2 |
63 |
|
| 52 |
61 |
| (*depth)--; |
| 53 |
|
| else |
| 54 |
2 |
| name_warning (loc, "unmatched '}' (ignoring)"); |
| 55 |
|
| } |
| 56 |
5069 |
| } |
| 57 |
|
| |
| 58 |
|
| |
| 59 |
|
| /* ------------------------------------------------------------------------ |
| 60 |
|
| @NAME : bt_split_list() |
| 61 |
|
| @INPUT : string - string to split up; whitespace must be collapsed |
| 62 |
|
| eg. by bt_postprocess_string() |
| 63 |
|
| delim - delimiter to use; must be lowercase and should be |
| 64 |
|
| free of whitespace (code requires that delimiters |
| 65 |
|
| in string be surrounded by whitespace) |
| 66 |
|
| filename, line - source of string (for warning messages) |
| 67 |
|
| description - what substrings are (eg. "name") (for warning |
| 68 |
|
| messages); if NULL will use "substring" |
| 69 |
|
| @RETURNS : a bt_stringlist (free with bt_free_list()), or NULL if |
| 70 |
|
| the string is empty |
| 71 |
|
| @DESCRIPTION: Splits a string into substrings at each occurrence of |
| 72 |
|
| `delim' that is at brace-depth zero and surrounded by |
| 73 |
|
| whitespace. The comparison is case-insensitive. |
| 74 |
|
| -------------------------------------------------------------------------- */ |
| 75 |
|
| bt_stringlist * |
| 76 |
38 |
| bt_split_list (bt_errors *errs, const char *string, const char *delim, |
| 77 |
|
| const char *filename, int line, const char *description) |
| 78 |
|
| { |
| 79 |
|
| int depth; /* brace depth */ |
| 80 |
|
| int i, j; /* offset into string and delim */ |
| 81 |
|
| int inword; /* flag telling if prev. char != ws */ |
| 82 |
|
| int string_len; |
| 83 |
|
| int delim_len; |
| 84 |
|
| int maxdiv; /* upper limit on no. of divisions */ |
| 85 |
|
| int numdiv; /* number of divisions */ |
| 86 |
|
| int * start; /* start of each division */ |
| 87 |
|
| int * stop; /* stop of each division */ |
| 88 |
|
| bt_stringlist * list; |
| 89 |
|
| name_loc loc; |
| 90 |
|
| |
| 91 |
38 |
| loc.errs = errs; |
| 92 |
38 |
| loc.filename = filename; |
| 93 |
38 |
| loc.line = line; |
| 94 |
38 |
| loc.name_num = 0; |
| 95 |
|
| |
| 96 |
38 |
▶ | if (string == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (string == NULL) |
0 |
38 |
38 |
|
| 97 |
0 |
| return NULL; |
| 98 |
38 |
▶ | if (description == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (description == NULL) |
13 |
25 |
38 |
|
| 99 |
13 |
| description = "substring"; |
| 100 |
38 |
▶ | if (delim == NULL || *delim == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (delim == NULL || *delim == 0) |
38 |
0 |
38 |
| if (delim == NULL || *delim == 0) |
0 |
38 |
38 |
|
| 101 |
|
| { |
| 102 |
0 |
| bt_report (errs, BTERR_USAGEERR, NULL, -1, NULL, -1, |
| 103 |
|
| "bt_split_list: delimiter must not be empty"); |
| 104 |
0 |
| return NULL; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
38 |
| string_len = (int) strlen (string); |
| 108 |
38 |
| delim_len = (int) strlen (delim); |
| 109 |
38 |
▶ | if (string_len == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (string_len == 0) |
0 |
38 |
38 |
|
| 110 |
0 |
| return NULL; |
| 111 |
|
| |
| 112 |
38 |
| maxdiv = (string_len / delim_len) + 1; |
| 113 |
38 |
| start = bt_xmalloc (maxdiv * sizeof (int)); |
| 114 |
38 |
| stop = bt_xmalloc (maxdiv * sizeof (int)); |
| 115 |
|
| |
| 116 |
38 |
| depth = 0; |
| 117 |
38 |
| i = j = 0; |
| 118 |
38 |
| inword = 1; /* so leading delim ignored */ |
| 119 |
38 |
| numdiv = 0; |
| 120 |
38 |
| start[0] = 0; /* first substring @ start of string */ |
| 121 |
|
| |
| 122 |
1826 |
▶ | while (i < string_len) |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < string_len) |
1788 |
38 |
1826 |
|
| 123 |
|
| { |
| 124 |
|
| /* does current char. in string match current char. in delim? */ |
| 125 |
1788 |
▶ | if (depth == 0 && !inword |
Branch: 2 branches
| Branch | True | False | Total |
| if (depth == 0 && !inword |
1472 |
316 |
1788 |
| if (depth == 0 && !inword |
463 |
1009 |
1472 |
|
| 126 |
463 |
▶ | && BT_TOLOWER ((unsigned char) string[i]) == delim[j]) |
Branch: 3 branches
| Branch | True | False | Total |
| && BT_TOLOWER ((unsigned char) string[i]) == delim[j]) |
450 |
13 |
463 |
| && BT_TOLOWER ((unsigned char) string[i]) == delim[j]) |
164 |
286 |
450 |
| && BT_TOLOWER ((unsigned char) string[i]) == delim[j]) |
256 |
207 |
463 |
|
| 127 |
|
| { |
| 128 |
256 |
| j++; i++; |
| 129 |
|
| |
| 130 |
|
| /* have we found an entire delim, followed by a space? */ |
| 131 |
256 |
▶ | if (j == delim_len && string[i] == ' ') |
Branch: 2 branches
| Branch | True | False | Total |
| if (j == delim_len && string[i] == ' ') |
82 |
174 |
256 |
| if (j == delim_len && string[i] == ' ') |
71 |
11 |
82 |
|
| 132 |
|
| { |
| 133 |
71 |
| stop[numdiv] = i - delim_len - 1; |
| 134 |
71 |
| start[++numdiv] = ++i; |
| 135 |
71 |
| j = 0; |
| 136 |
|
| } |
| 137 |
|
| } |
| 138 |
|
| /* no match between string and delim, at non-zero depth, or in a word */ |
| 139 |
|
| else |
| 140 |
|
| { |
| 141 |
1532 |
| update_depth (string[i], &depth, &loc); |
| 142 |
1532 |
| inword = string[i] != ' '; |
| 143 |
1532 |
| i++; |
| 144 |
1532 |
| j = 0; |
| 145 |
|
| } |
| 146 |
|
| } |
| 147 |
38 |
▶ | if (depth) |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth) |
0 |
38 |
38 |
|
| 148 |
0 |
| name_warning (&loc, "unmatched '{' (ignoring)"); |
| 149 |
|
| |
| 150 |
38 |
| stop[numdiv] = string_len; /* last substring ends just past eos */ |
| 151 |
|
| |
| 152 |
38 |
| list = bt_xmalloc (sizeof (bt_stringlist)); |
| 153 |
38 |
| list->num_items = numdiv + 1; |
| 154 |
38 |
| list->items = bt_xmalloc (list->num_items * sizeof (char *)); |
| 155 |
38 |
| list->string = bt_xstrdup (string); |
| 156 |
|
| |
| 157 |
147 |
▶ | for (i = 0; i < list->num_items; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < list->num_items; i++) |
109 |
38 |
147 |
|
| 158 |
|
| { |
| 159 |
109 |
▶ | if (stop[i] > start[i]) /* the usual case */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (stop[i] > start[i]) /* the usual case */ |
104 |
5 |
109 |
|
| 160 |
|
| { |
| 161 |
104 |
| list->string[stop[i]] = 0; |
| 162 |
104 |
| list->items[i] = list->string + start[i]; |
| 163 |
|
| } |
| 164 |
|
| else /* empty element */ |
| 165 |
|
| { |
| 166 |
5 |
| list->items[i] = NULL; |
| 167 |
5 |
| bt_report (errs, BTERR_CONTENT, filename, line, description, i + 1, |
| 168 |
|
| "empty %s", description); |
| 169 |
|
| } |
| 170 |
|
| } |
| 171 |
|
| |
| 172 |
38 |
| free (start); |
| 173 |
38 |
| free (stop); |
| 174 |
38 |
| return list; |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
|
| void |
| 178 |
137 |
| bt_free_list (bt_stringlist *list) |
| 179 |
|
| { |
| 180 |
137 |
▶ | if (list) |
Branch: 1 branch
| Branch | True | False | Total |
| if (list) |
136 |
1 |
137 |
|
| 181 |
|
| { |
| 182 |
136 |
| free (list->string); |
| 183 |
136 |
| free (list->items); |
| 184 |
136 |
| free (list); |
| 185 |
|
| } |
| 186 |
137 |
| } |
| 187 |
|
| |
| 188 |
|
| |
| 189 |
|
| /* ---------------------------------------------------------------------- |
| 190 |
|
| * Splitting a single name |
| 191 |
|
| */ |
| 192 |
|
| |
| 193 |
|
| /* ------------------------------------------------------------------------ |
| 194 |
|
| @NAME : find_commas |
| 195 |
|
| @INPUT : name - string to search for commas |
| 196 |
|
| max_commas - maximum number of commas to allow (if more than |
| 197 |
|
| this number are seen, a warning is given and |
| 198 |
|
| the excess commas are removed) |
| 199 |
|
| @RETURNS : number of commas found |
| 200 |
|
| @DESCRIPTION: Counts commas at brace-depth 0. Modifies the string in |
| 201 |
|
| place to remove whitespace around commas, excess commas, |
| 202 |
|
| and any trailing commas; warns on excess or trailing commas. |
| 203 |
|
| -------------------------------------------------------------------------- */ |
| 204 |
|
| static int |
| 205 |
98 |
| find_commas (name_loc *loc, char *name, int max_commas) |
| 206 |
|
| { |
| 207 |
98 |
| char * i = name, *j = name; |
| 208 |
98 |
| int depth = 0; |
| 209 |
98 |
| int num_commas = 0; |
| 210 |
98 |
| boolean last_whitespace = FALSE; |
| 211 |
98 |
| boolean warned = FALSE; |
| 212 |
|
| |
| 213 |
|
| /* First pass to check for and blank out excess commas */ |
| 214 |
1886 |
▶ | while (*i) |
Branch: 1 branch
| Branch | True | False | Total |
| while (*i) |
1788 |
98 |
1886 |
|
| 215 |
|
| { |
| 216 |
1788 |
| char c = *i++; |
| 217 |
|
| |
| 218 |
1788 |
| update_depth (c, &depth, loc); |
| 219 |
1788 |
▶ | if (depth == 0 && c == ',') |
Branch: 2 branches
| Branch | True | False | Total |
| if (depth == 0 && c == ',') |
1408 |
380 |
1788 |
| if (depth == 0 && c == ',') |
42 |
1366 |
1408 |
|
| 220 |
|
| { |
| 221 |
42 |
| num_commas++; |
| 222 |
42 |
▶ | if (num_commas > max_commas) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_commas > max_commas) |
6 |
36 |
42 |
|
| 223 |
|
| { |
| 224 |
6 |
▶ | if (!warned) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!warned) |
4 |
2 |
6 |
|
| 225 |
|
| { |
| 226 |
4 |
| name_warning (loc, "too many commas in name (removing extras)"); |
| 227 |
4 |
| warned = TRUE; |
| 228 |
|
| } |
| 229 |
6 |
| c = ' '; |
| 230 |
|
| } |
| 231 |
|
| /* replace any preceding (collapsed) whitespace with the comma, |
| 232 |
|
| * and drop any following whitespace */ |
| 233 |
42 |
▶ | if (last_whitespace && j > name) |
Branch: 2 branches
| Branch | True | False | Total |
| if (last_whitespace && j > name) |
6 |
36 |
42 |
| if (last_whitespace && j > name) |
6 |
0 |
6 |
|
| 234 |
6 |
| --j; |
| 235 |
|
| else |
| 236 |
36 |
| last_whitespace = TRUE; |
| 237 |
|
| } |
| 238 |
1746 |
▶ | else if (BT_IS_SPACE (c)) |
Branch: 6 branches
| Branch | True | False | Total |
| else if (BT_IS_SPACE (c)) |
1549 |
197 |
1746 |
| else if (BT_IS_SPACE (c)) |
1549 |
0 |
1549 |
| else if (BT_IS_SPACE (c)) |
1549 |
0 |
1549 |
| else if (BT_IS_SPACE (c)) |
1549 |
0 |
1549 |
| else if (BT_IS_SPACE (c)) |
1549 |
0 |
1549 |
| else if (BT_IS_SPACE (c)) |
0 |
1549 |
1549 |
|
| 239 |
|
| { |
| 240 |
197 |
▶ | if (last_whitespace) |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_whitespace) |
31 |
166 |
197 |
|
| 241 |
31 |
| continue; |
| 242 |
166 |
| last_whitespace = TRUE; |
| 243 |
|
| } |
| 244 |
|
| else |
| 245 |
1549 |
| last_whitespace = FALSE; |
| 246 |
1757 |
| *j++ = c; |
| 247 |
|
| } |
| 248 |
98 |
| *j = 0; |
| 249 |
|
| |
| 250 |
98 |
▶ | if (depth) |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth) |
2 |
96 |
98 |
|
| 251 |
2 |
| name_warning (loc, "unmatched '{' (ignoring)"); |
| 252 |
|
| |
| 253 |
98 |
▶ | if (num_commas == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_commas == 0) |
69 |
29 |
98 |
|
| 254 |
69 |
| return 0; |
| 255 |
|
| |
| 256 |
29 |
| j = name + strlen (name); |
| 257 |
29 |
▶ | if (j > name && j[-1] == ',') |
Branch: 2 branches
| Branch | True | False | Total |
| if (j > name && j[-1] == ',') |
29 |
0 |
29 |
| if (j > name && j[-1] == ',') |
2 |
27 |
29 |
|
| 258 |
|
| { |
| 259 |
2 |
| name_warning (loc, "comma(s) at end of name (removing)"); |
| 260 |
4 |
▶ | while (j > name && j[-1] == ',') |
Branch: 2 branches
| Branch | True | False | Total |
| while (j > name && j[-1] == ',') |
4 |
0 |
4 |
| while (j > name && j[-1] == ',') |
2 |
2 |
4 |
|
| 261 |
|
| { |
| 262 |
2 |
| *--j = 0; |
| 263 |
2 |
| num_commas--; |
| 264 |
|
| } |
| 265 |
|
| } |
| 266 |
|
| |
| 267 |
29 |
| return num_commas > max_commas ? max_commas : num_commas; |
| 268 |
|
| } |
| 269 |
|
| |
| 270 |
|
| |
| 271 |
|
| /* ------------------------------------------------------------------------ |
| 272 |
|
| @NAME : find_tokens |
| 273 |
|
| @INPUT : name - string to tokenize (a private copy that we're |
| 274 |
|
| free to clobber) |
| 275 |
|
| @OUTPUT : comma_token- number of token immediately preceding each comma |
| 276 |
|
| num_commas - number of commas recorded in comma_token |
| 277 |
|
| @RETURNS : newly-allocated bt_stringlist structure |
| 278 |
|
| @DESCRIPTION: Finds tokens in a string; the delimiter is space or comma at |
| 279 |
|
| brace-depth zero. Empty tokens (caused by consecutive |
| 280 |
|
| commas) are recorded as NULL. |
| 281 |
|
| -------------------------------------------------------------------------- */ |
| 282 |
|
| static bt_stringlist * |
| 283 |
98 |
| find_tokens (char *name, int *comma_token, int *num_commas, name_loc *loc) |
| 284 |
|
| { |
| 285 |
98 |
| int i = 0; |
| 286 |
98 |
| int num_tok = 0; |
| 287 |
98 |
| int in_boundary = 1; /* previous char was ' ' or ',' */ |
| 288 |
98 |
| int len = (int) strlen (name); |
| 289 |
98 |
| int depth = 0; |
| 290 |
|
| bt_stringlist * tokens; |
| 291 |
|
| |
| 292 |
98 |
| *num_commas = 0; |
| 293 |
98 |
| tokens = bt_xmalloc (sizeof (bt_stringlist)); |
| 294 |
98 |
| tokens->string = name; |
| 295 |
98 |
| tokens->num_items = 0; |
| 296 |
98 |
| tokens->items = NULL; |
| 297 |
|
| |
| 298 |
98 |
▶ | if (len == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) |
0 |
98 |
98 |
|
| 299 |
0 |
| return tokens; |
| 300 |
|
| |
| 301 |
98 |
| tokens->items = bt_xmalloc (sizeof (char *) * (len + 1)); |
| 302 |
|
| |
| 303 |
1847 |
▶ | while (i < len) |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < len) |
1749 |
98 |
1847 |
|
| 304 |
|
| { |
| 305 |
1749 |
▶ | if (depth == 0 && in_boundary) /* at start of a new token */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (depth == 0 && in_boundary) /* at start of a new token */ |
1371 |
378 |
1749 |
| if (depth == 0 && in_boundary) /* at start of a new token */ |
257 |
1114 |
1371 |
|
| 306 |
257 |
| tokens->items[num_tok++] = name + i; |
| 307 |
|
| |
| 308 |
1749 |
▶ | if (depth == 0 && (name[i] == ' ' || name[i] == ',')) |
Branch: 3 branches
| Branch | True | False | Total |
| if (depth == 0 && (name[i] == ' ' || name[i] == ',')) |
1371 |
378 |
1749 |
| if (depth == 0 && (name[i] == ' ' || name[i] == ',')) |
1237 |
134 |
1371 |
| if (depth == 0 && (name[i] == ' ' || name[i] == ',')) |
29 |
1208 |
1237 |
|
| 309 |
|
| { |
| 310 |
|
| /* if we're at a comma, record the token preceding the comma */ |
| 311 |
163 |
▶ | if (name[i] == ',' && *num_commas < MAX_COMMAS) |
Branch: 2 branches
| Branch | True | False | Total |
| if (name[i] == ',' && *num_commas < MAX_COMMAS) |
29 |
134 |
163 |
| if (name[i] == ',' && *num_commas < MAX_COMMAS) |
29 |
0 |
29 |
|
| 312 |
29 |
| comma_token[(*num_commas)++] = num_tok - 1; |
| 313 |
|
| |
| 314 |
|
| /* if already in a boundary zone, we have an empty token |
| 315 |
|
| * (caused by multiple consecutive commas) */ |
| 316 |
163 |
▶ | if (in_boundary) |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_boundary) |
6 |
157 |
163 |
|
| 317 |
6 |
| tokens->items[num_tok - 1] = NULL; |
| 318 |
|
| |
| 319 |
163 |
| name[i] = 0; |
| 320 |
163 |
| in_boundary = 1; |
| 321 |
|
| } |
| 322 |
|
| else |
| 323 |
1586 |
| in_boundary = 0; |
| 324 |
|
| |
| 325 |
1749 |
| update_depth (name[i], &depth, loc); |
| 326 |
1749 |
| i++; |
| 327 |
|
| } |
| 328 |
|
| |
| 329 |
98 |
▶ | if (depth) |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth) |
2 |
96 |
98 |
|
| 330 |
2 |
| name_warning (loc, "unmatched '{' (ignoring)"); |
| 331 |
|
| |
| 332 |
98 |
| tokens->num_items = num_tok; |
| 333 |
98 |
| return tokens; |
| 334 |
|
| } |
| 335 |
|
| |
| 336 |
|
| |
| 337 |
|
| /* ------------------------------------------------------------------------ |
| 338 |
|
| @NAME : find_lc_tokens() |
| 339 |
|
| @OUTPUT : first_lc, last_lc - first and last token of the first run of |
| 340 |
|
| lowercase tokens (-1 if none) |
| 341 |
|
| @DESCRIPTION: A token is "lowercase" if its first character is a |
| 342 |
|
| lowercase letter. |
| 343 |
|
| -------------------------------------------------------------------------- */ |
| 344 |
|
| static void |
| 345 |
98 |
| find_lc_tokens (bt_stringlist *tokens, int *first_lc, int *last_lc) |
| 346 |
|
| { |
| 347 |
98 |
| int i = 0; |
| 348 |
|
| |
| 349 |
98 |
| *first_lc = *last_lc = -1; |
| 350 |
334 |
▶ | while (i < tokens->num_items) |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < tokens->num_items) |
236 |
98 |
334 |
|
| 351 |
|
| { |
| 352 |
236 |
| char * token = tokens->items[i]; |
| 353 |
|
| |
| 354 |
236 |
▶ | if (*first_lc == -1 && token != NULL && bt_uni_islower (token)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (*first_lc == -1 && token != NULL && bt_uni_islower (token)) |
199 |
37 |
236 |
| if (*first_lc == -1 && token != NULL && bt_uni_islower (token)) |
193 |
6 |
199 |
| if (*first_lc == -1 && token != NULL && bt_uni_islower (token)) |
35 |
158 |
193 |
|
| 355 |
|
| { |
| 356 |
35 |
| *first_lc = i; |
| 357 |
35 |
| i++; |
| 358 |
82 |
▶ | while (i < tokens->num_items && tokens->items[i] != NULL |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < tokens->num_items && tokens->items[i] != NULL |
47 |
0 |
47 |
|
| 359 |
103 |
▶ | && bt_uni_islower (tokens->items[i])) |
Branch: 2 branches
| Branch | True | False | Total |
| && bt_uni_islower (tokens->items[i])) |
47 |
9 |
56 |
| && bt_uni_islower (tokens->items[i])) |
21 |
26 |
47 |
|
| 360 |
21 |
| i++; |
| 361 |
35 |
| *last_lc = i - 1; |
| 362 |
|
| } |
| 363 |
|
| else |
| 364 |
201 |
| i++; |
| 365 |
|
| } |
| 366 |
98 |
| } |
| 367 |
|
| |
| 368 |
|
| |
| 369 |
|
| static void |
| 370 |
320 |
| resolve_token_range (bt_stringlist *tokens, int *tok_range, |
| 371 |
|
| char ***part, int *num_tok) |
| 372 |
|
| { |
| 373 |
320 |
| *num_tok = (tok_range[1] - tok_range[0]) + 1; |
| 374 |
320 |
▶ | if (*num_tok <= 0 || tok_range[0] < 0 |
Branch: 2 branches
| Branch | True | False | Total |
| if (*num_tok <= 0 || tok_range[0] < 0 |
208 |
112 |
320 |
| if (*num_tok <= 0 || tok_range[0] < 0 |
208 |
0 |
208 |
|
| 375 |
208 |
▶ | || tok_range[1] >= tokens->num_items) |
Branch: 1 branch
| Branch | True | False | Total |
| || tok_range[1] >= tokens->num_items) |
0 |
208 |
208 |
|
| 376 |
|
| { |
| 377 |
112 |
| *num_tok = 0; |
| 378 |
112 |
| *part = NULL; |
| 379 |
|
| } |
| 380 |
|
| else |
| 381 |
208 |
| *part = tokens->items + tok_range[0]; |
| 382 |
320 |
| } |
| 383 |
|
| |
| 384 |
|
| |
| 385 |
|
| /* ------------------------------------------------------------------------ |
| 386 |
|
| @NAME : split_simple_name() |
| 387 |
|
| @DESCRIPTION: Splits a name with no commas according to the BibTeX rules: |
| 388 |
|
| - tokens up to the first lowercase token (or all but the |
| 389 |
|
| last token, if there are no lowercase tokens) become |
| 390 |
|
| the `first' part |
| 391 |
|
| - the run of lowercase tokens, up to (but not including) |
| 392 |
|
| the last token, becomes the `von' part |
| 393 |
|
| - the remaining tokens (at least the last) become the |
| 394 |
|
| `last' part |
| 395 |
|
| -------------------------------------------------------------------------- */ |
| 396 |
|
| static void |
| 397 |
72 |
| split_simple_name (bt_name *name, int first_lc, int last_lc) |
| 398 |
|
| { |
| 399 |
|
| int first_t[2], von_t[2], last_t[2]; |
| 400 |
72 |
| int end = name->tokens->num_items - 1; |
| 401 |
|
| |
| 402 |
72 |
▶ | if (first_lc > -1) /* any lowercase tokens at all? */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (first_lc > -1) /* any lowercase tokens at all? */ |
28 |
44 |
72 |
|
| 403 |
|
| { |
| 404 |
28 |
| first_t[0] = 0; |
| 405 |
28 |
| first_t[1] = first_lc - 1; |
| 406 |
28 |
▶ | if (last_lc == end) /* lowercase run goes to the end */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_lc == end) /* lowercase run goes to the end */ |
7 |
21 |
28 |
|
| 407 |
7 |
| last_lc--; /* -- keep the last as lastname */ |
| 408 |
28 |
| von_t[0] = first_lc; |
| 409 |
28 |
| von_t[1] = last_lc; |
| 410 |
28 |
| last_t[0] = last_lc + 1; |
| 411 |
28 |
| last_t[1] = end; |
| 412 |
|
| } |
| 413 |
|
| else /* no lowercase tokens */ |
| 414 |
|
| { |
| 415 |
44 |
| von_t[0] = 0; |
| 416 |
44 |
| von_t[1] = -1; |
| 417 |
44 |
| first_t[0] = 0; |
| 418 |
44 |
| first_t[1] = end - 1; |
| 419 |
44 |
| last_t[0] = last_t[1] = end; |
| 420 |
|
| } |
| 421 |
|
| |
| 422 |
72 |
| resolve_token_range (name->tokens, first_t, |
| 423 |
72 |
| name->parts + BTN_FIRST, name->part_len + BTN_FIRST); |
| 424 |
72 |
| resolve_token_range (name->tokens, von_t, |
| 425 |
72 |
| name->parts + BTN_VON, name->part_len + BTN_VON); |
| 426 |
72 |
| resolve_token_range (name->tokens, last_t, |
| 427 |
72 |
| name->parts + BTN_LAST, name->part_len + BTN_LAST); |
| 428 |
72 |
| name->parts[BTN_JR] = NULL; |
| 429 |
72 |
| name->part_len[BTN_JR] = 0; |
| 430 |
72 |
| } |
| 431 |
|
| |
| 432 |
|
| |
| 433 |
|
| /* ------------------------------------------------------------------------ |
| 434 |
|
| @NAME : split_general_name() |
| 435 |
|
| @DESCRIPTION: Splits a name with 1 or 2 commas: |
| 436 |
|
| - a leading run of lowercase tokens before the first |
| 437 |
|
| comma becomes the `von' part |
| 438 |
|
| - the tokens after that up to the first comma become |
| 439 |
|
| the `last' part |
| 440 |
|
| - with one comma, the rest is the `first' part |
| 441 |
|
| - with two commas, the tokens between them are the `jr' |
| 442 |
|
| part and the tokens after the second are `first' |
| 443 |
|
| -------------------------------------------------------------------------- */ |
| 444 |
|
| static void |
| 445 |
26 |
| split_general_name (bt_name *name, int num_commas, int *comma_token, |
| 446 |
|
| int first_lc, int last_lc) |
| 447 |
|
| { |
| 448 |
|
| int first_t[2], von_t[2], last_t[2], jr_t[2]; |
| 449 |
26 |
| int end = name->tokens->num_items - 1; |
| 450 |
|
| |
| 451 |
26 |
▶ | if (first_lc == 0) /* initial run of lowercase tokens */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (first_lc == 0) /* initial run of lowercase tokens */ |
6 |
20 |
26 |
|
| 452 |
|
| { |
| 453 |
6 |
▶ | if (last_lc == comma_token[0]) /* lc string ends at first comma */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_lc == comma_token[0]) /* lc string ends at first comma */ |
0 |
6 |
6 |
|
| 454 |
0 |
| last_lc--; |
| 455 |
6 |
| von_t[0] = first_lc; |
| 456 |
6 |
| von_t[1] = last_lc; |
| 457 |
|
| } |
| 458 |
|
| else |
| 459 |
|
| { |
| 460 |
20 |
| von_t[0] = 0; |
| 461 |
20 |
| von_t[1] = -1; |
| 462 |
|
| } |
| 463 |
|
| |
| 464 |
26 |
| last_t[0] = von_t[1] + 1; |
| 465 |
26 |
| last_t[1] = comma_token[0]; |
| 466 |
|
| |
| 467 |
26 |
▶ | if (num_commas == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_commas == 1) |
23 |
3 |
26 |
|
| 468 |
|
| { |
| 469 |
23 |
| first_t[0] = comma_token[0] + 1; |
| 470 |
23 |
| first_t[1] = end; |
| 471 |
23 |
| jr_t[0] = 0; |
| 472 |
23 |
| jr_t[1] = -1; |
| 473 |
|
| } |
| 474 |
|
| else |
| 475 |
|
| { |
| 476 |
3 |
| jr_t[0] = comma_token[0] + 1; |
| 477 |
3 |
| jr_t[1] = comma_token[1]; |
| 478 |
3 |
| first_t[0] = comma_token[1] + 1; |
| 479 |
3 |
| first_t[1] = end; |
| 480 |
|
| } |
| 481 |
|
| |
| 482 |
26 |
| resolve_token_range (name->tokens, first_t, |
| 483 |
26 |
| name->parts + BTN_FIRST, name->part_len + BTN_FIRST); |
| 484 |
26 |
| resolve_token_range (name->tokens, von_t, |
| 485 |
26 |
| name->parts + BTN_VON, name->part_len + BTN_VON); |
| 486 |
26 |
| resolve_token_range (name->tokens, last_t, |
| 487 |
26 |
| name->parts + BTN_LAST, name->part_len + BTN_LAST); |
| 488 |
26 |
| resolve_token_range (name->tokens, jr_t, |
| 489 |
26 |
| name->parts + BTN_JR, name->part_len + BTN_JR); |
| 490 |
26 |
| } |
| 491 |
|
| |
| 492 |
|
| |
| 493 |
|
| /* A part made up only of empty tokens (from stray commas, as in |
| 494 |
|
| * ", John") is no part at all */ |
| 495 |
|
| static void |
| 496 |
98 |
| drop_empty_parts (bt_name *name) |
| 497 |
|
| { |
| 498 |
|
| int i, j; |
| 499 |
|
| |
| 500 |
490 |
▶ | for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
392 |
98 |
490 |
|
| 501 |
|
| { |
| 502 |
398 |
▶ | for (j = 0; j < name->part_len[i]; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < name->part_len[i]; j++) |
208 |
190 |
398 |
|
| 503 |
208 |
▶ | if (name->parts[i][j] != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (name->parts[i][j] != NULL) |
202 |
6 |
208 |
|
| 504 |
202 |
| break; |
| 505 |
392 |
▶ | if (j == name->part_len[i]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == name->part_len[i]) |
190 |
202 |
392 |
|
| 506 |
|
| { |
| 507 |
190 |
| name->parts[i] = NULL; |
| 508 |
190 |
| name->part_len[i] = 0; |
| 509 |
|
| } |
| 510 |
|
| } |
| 511 |
98 |
| } |
| 512 |
|
| |
| 513 |
|
| static void |
| 514 |
99 |
| empty_name (bt_name *name) |
| 515 |
|
| { |
| 516 |
|
| int i; |
| 517 |
|
| |
| 518 |
495 |
▶ | for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
396 |
99 |
495 |
|
| 519 |
|
| { |
| 520 |
396 |
| name->parts[i] = NULL; |
| 521 |
396 |
| name->part_len[i] = 0; |
| 522 |
|
| } |
| 523 |
99 |
| name->tokens = NULL; |
| 524 |
99 |
| } |
| 525 |
|
| |
| 526 |
|
| /* ------------------------------------------------------------------------ |
| 527 |
|
| @NAME : bt_split_name() |
| 528 |
|
| @INPUT : name - string to split (whitespace should be collapsed) |
| 529 |
|
| filename, line, name_num - for warning messages |
| 530 |
|
| @RETURNS : a bt_name structure (free with bt_free_name()) |
| 531 |
|
| @DESCRIPTION: Splits a single name into its first, von, last and jr |
| 532 |
|
| parts. See split_simple_name() for the rules for names |
| 533 |
|
| without commas, and split_general_name() for names with 1 or |
| 534 |
|
| 2 commas (any extra commas are removed with a warning). |
| 535 |
|
| -------------------------------------------------------------------------- */ |
| 536 |
|
| bt_name * |
| 537 |
99 |
| bt_split_name (bt_errors *errs, const char *string, |
| 538 |
|
| const char *filename, int line, int name_num) |
| 539 |
|
| { |
| 540 |
|
| name_loc loc; |
| 541 |
|
| bt_stringlist * tokens; |
| 542 |
|
| int comma_token[MAX_COMMAS]; |
| 543 |
|
| int num_commas, num_comma_tokens; |
| 544 |
|
| int first_lc, last_lc; |
| 545 |
|
| bt_name * split_name; |
| 546 |
|
| char * name; |
| 547 |
|
| |
| 548 |
99 |
| split_name = bt_xmalloc (sizeof (bt_name)); |
| 549 |
99 |
| empty_name (split_name); |
| 550 |
99 |
▶ | if (string == NULL || *string == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (string == NULL || *string == 0) |
99 |
0 |
99 |
| if (string == NULL || *string == 0) |
1 |
98 |
99 |
|
| 551 |
1 |
| return split_name; |
| 552 |
|
| |
| 553 |
98 |
| name = bt_xstrdup (string); /* private copy that we may clobber */ |
| 554 |
98 |
| loc.errs = errs; |
| 555 |
98 |
| loc.filename = filename; |
| 556 |
98 |
| loc.line = line; |
| 557 |
98 |
| loc.name_num = name_num; |
| 558 |
|
| |
| 559 |
98 |
| num_commas = find_commas (&loc, name, MAX_COMMAS); |
| 560 |
98 |
▶ | if (*name == 0) /* (check before find_tokens() puts */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (*name == 0) /* (check before find_tokens() puts */ |
0 |
98 |
98 |
|
| 561 |
|
| { /* NULs at the commas) */ |
| 562 |
0 |
| free (name); |
| 563 |
0 |
| return split_name; |
| 564 |
|
| } |
| 565 |
98 |
| tokens = find_tokens (name, comma_token, &num_comma_tokens, &loc); |
| 566 |
98 |
▶ | if (num_comma_tokens < num_commas) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_comma_tokens < num_commas) |
3 |
95 |
98 |
|
| 567 |
3 |
| num_commas = num_comma_tokens; |
| 568 |
98 |
| find_lc_tokens (tokens, &first_lc, &last_lc); |
| 569 |
|
| |
| 570 |
98 |
▶ | if (tokens->num_items == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (tokens->num_items == 0) |
0 |
98 |
98 |
|
| 571 |
|
| { |
| 572 |
0 |
| bt_free_list (tokens); /* frees `name' too */ |
| 573 |
0 |
| return split_name; |
| 574 |
|
| } |
| 575 |
|
| |
| 576 |
98 |
| split_name->tokens = tokens; |
| 577 |
98 |
▶ | if (num_commas == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_commas == 0) |
72 |
26 |
98 |
|
| 578 |
72 |
| split_simple_name (split_name, first_lc, last_lc); |
| 579 |
|
| else |
| 580 |
26 |
| split_general_name (split_name, num_commas, comma_token, |
| 581 |
|
| first_lc, last_lc); |
| 582 |
98 |
| drop_empty_parts (split_name); |
| 583 |
|
| |
| 584 |
98 |
▶ | if (split_name->parts[BTN_LAST] == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (split_name->parts[BTN_LAST] == NULL) |
6 |
92 |
98 |
|
| 585 |
6 |
| name_warning (&loc, "name has no last name"); |
| 586 |
98 |
| return split_name; |
| 587 |
|
| } |
| 588 |
|
| |
| 589 |
|
| void |
| 590 |
99 |
| bt_free_name (bt_name *name) |
| 591 |
|
| { |
| 592 |
99 |
▶ | if (name) |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) |
99 |
0 |
99 |
|
| 593 |
|
| { |
| 594 |
99 |
| bt_free_list (name->tokens); |
| 595 |
99 |
| free (name); |
| 596 |
|
| } |
| 597 |
99 |
| } |