| 1 | #include <stdio.h> |
|---|
| 2 | #include <string.h> |
|---|
| 3 | #include <ctype.h> |
|---|
| 4 | #include <stdlib.h> |
|---|
| 5 | #include "convert.h" |
|---|
| 6 | #include "global.h" |
|---|
| 7 | |
|---|
| 8 | int warning_out = 1; |
|---|
| 9 | |
|---|
| 10 | /* -------------------------------------------------------------- |
|---|
| 11 | * Function Cmpstr(). |
|---|
| 12 | * Compare string s1 and string s2, if eq. return 1 |
|---|
| 13 | * else return 0. |
|---|
| 14 | */ |
|---|
| 15 | int Cmpcasestr(s1, s2) |
|---|
| 16 | const char *s1, *s2; |
|---|
| 17 | { |
|---|
| 18 | int indi; |
|---|
| 19 | |
|---|
| 20 | for(indi=0; s1[indi]!='\0'&&s2[indi]!='\0'; indi++) |
|---|
| 21 | if(!same_char(s1[indi], s2[indi])) return(0); |
|---|
| 22 | |
|---|
| 23 | return(same_char(s1[indi], s2[indi])); |
|---|
| 24 | } |
|---|
| 25 | /* -------------------------------------------------------------- |
|---|
| 26 | * Function Cmpstr(). |
|---|
| 27 | * Compare string s1 and string s2, if eq. return 1 |
|---|
| 28 | * else return 0. |
|---|
| 29 | */ |
|---|
| 30 | int Cmpstr(s1, s2) |
|---|
| 31 | const char *s1, *s2; |
|---|
| 32 | { |
|---|
| 33 | if(strcmp(s1,s2)==0) return(1); |
|---|
| 34 | else return(0); |
|---|
| 35 | } |
|---|
| 36 | /* --------------------------------------------------------------- * |
|---|
| 37 | * Function Freespace(). |
|---|
| 38 | * Free the pointer if not NULL. |
|---|
| 39 | */ |
|---|
| 40 | void Freespace(pointer) |
|---|
| 41 | void *pointer; |
|---|
| 42 | { |
|---|
| 43 | char **the_pointer = (char**)pointer; |
|---|
| 44 | |
|---|
| 45 | if((*the_pointer)!=NULL) { |
|---|
| 46 | free((*the_pointer)); |
|---|
| 47 | } |
|---|
| 48 | (*the_pointer) = NULL; |
|---|
| 49 | } |
|---|
| 50 | /* --------------------------------------------------------------- |
|---|
| 51 | * Function error() |
|---|
| 52 | * Syntax error in prolog input file, print out error and exit. |
|---|
| 53 | */ |
|---|
| 54 | void error(error_num, error_message) |
|---|
| 55 | int error_num; |
|---|
| 56 | const char *error_message; |
|---|
| 57 | { |
|---|
| 58 | fprintf(stderr, "ERROR(%d): %s\n", error_num, error_message); |
|---|
| 59 | exit(error_num); |
|---|
| 60 | } |
|---|
| 61 | /* -------------------------------------------------------------- |
|---|
| 62 | * Function warning() |
|---|
| 63 | * print out warning_message and continue execution. |
|---|
| 64 | */ |
|---|
| 65 | /* ------------------------------------------------------------- */ |
|---|
| 66 | void warning(warning_num, warning_message) |
|---|
| 67 | int warning_num; |
|---|
| 68 | const char *warning_message; |
|---|
| 69 | { |
|---|
| 70 | if(warning_out) |
|---|
| 71 | fprintf(stderr, "WARNING(%d): %s\n", warning_num, warning_message); |
|---|
| 72 | } |
|---|
| 73 | /* ------------------------------------------------------------ |
|---|
| 74 | * Function Reallspace(). |
|---|
| 75 | * Realloc a continue space, expand or shrink the |
|---|
| 76 | * original space. |
|---|
| 77 | */ |
|---|
| 78 | char *Reallocspace(block, size) |
|---|
| 79 | void *block; |
|---|
| 80 | unsigned size; |
|---|
| 81 | { |
|---|
| 82 | char *temp, answer; |
|---|
| 83 | /* void warning(); */ |
|---|
| 84 | |
|---|
| 85 | if((block==NULL&&size<=0)||size<=0) return(NULL); |
|---|
| 86 | if(block==NULL) { |
|---|
| 87 | temp=(char*)calloc(1,size); |
|---|
| 88 | } else { |
|---|
| 89 | temp=(char*)realloc(block, size); |
|---|
| 90 | } |
|---|
| 91 | if(temp==NULL) { |
|---|
| 92 | warning(999, "Run out of memory"); |
|---|
| 93 | fprintf(stderr, "Are you converting to Paup or Phylip?(y/n) "); |
|---|
| 94 | scanf("%c", &answer); |
|---|
| 95 | if(answer=='y') return(NULL); |
|---|
| 96 | else exit(999); |
|---|
| 97 | } |
|---|
| 98 | return(temp); |
|---|
| 99 | } |
|---|
| 100 | /* ------------------------------------------------------------------- |
|---|
| 101 | * Function Dupstr(). |
|---|
| 102 | * Duplicate string and return the address of string. |
|---|
| 103 | */ |
|---|
| 104 | char *Dupstr(string) |
|---|
| 105 | const char *string; |
|---|
| 106 | { |
|---|
| 107 | |
|---|
| 108 | char *temp, answer; |
|---|
| 109 | /* void warning(); */ |
|---|
| 110 | unsigned size; |
|---|
| 111 | |
|---|
| 112 | if(string==NULL) return(NULL); |
|---|
| 113 | else { |
|---|
| 114 | size = strlen(string); |
|---|
| 115 | if((temp=(char*)calloc(1,size+1))==NULL) { |
|---|
| 116 | warning(888, "Run out of memory"); |
|---|
| 117 | fprintf(stderr, "Are you converting to Paup or Phylip?(y/n) "); |
|---|
| 118 | scanf("%c", &answer); |
|---|
| 119 | if(answer=='y') return(NULL); |
|---|
| 120 | else exit(888); |
|---|
| 121 | } |
|---|
| 122 | strcpy(temp, string); |
|---|
| 123 | return(temp); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | /* ------------------------------------------------------------ |
|---|
| 127 | * Function Catstr(). |
|---|
| 128 | * Append string s2 to string s1. |
|---|
| 129 | */ |
|---|
| 130 | char *Catstr(s1, s2) |
|---|
| 131 | char *s1; |
|---|
| 132 | const char *s2; |
|---|
| 133 | { |
|---|
| 134 | return((char*)strcat(s1, s2)); |
|---|
| 135 | } |
|---|
| 136 | /* ------------------------------------------------------------ |
|---|
| 137 | * Function Lenstr(). |
|---|
| 138 | * Length of the string s1. |
|---|
| 139 | */ |
|---|
| 140 | int Lenstr(s1) |
|---|
| 141 | const char *s1; |
|---|
| 142 | { |
|---|
| 143 | if(s1==NULL) return(0); |
|---|
| 144 | else return(strlen(s1)); |
|---|
| 145 | } |
|---|
| 146 | /* -------------------------------------------------------- |
|---|
| 147 | * Function Cpystr(). |
|---|
| 148 | * Copy string s2 to string s1. |
|---|
| 149 | */ |
|---|
| 150 | void Cpystr(s1, s2) |
|---|
| 151 | char *s1; |
|---|
| 152 | const char *s2; |
|---|
| 153 | { |
|---|
| 154 | strcpy(s1, s2); |
|---|
| 155 | } |
|---|
| 156 | /* --------------------------------------------------------- |
|---|
| 157 | * Function Skip_white_space(). |
|---|
| 158 | * Skip white space from (index)th char of string line. |
|---|
| 159 | */ |
|---|
| 160 | int Skip_white_space(line, index) |
|---|
| 161 | char *line; |
|---|
| 162 | int index; |
|---|
| 163 | { |
|---|
| 164 | /* skip white space */ |
|---|
| 165 | |
|---|
| 166 | while (line[index] == ' ' || line[index] == '\t') ++index; |
|---|
| 167 | /* for(; */ |
|---|
| 168 | /* line[index] == ' ' || line[index]=='\t' && line[index]!='\0'; */ |
|---|
| 169 | /* index++) ; */ |
|---|
| 170 | return(index); |
|---|
| 171 | } |
|---|
| 172 | /* ---------------------------------------------------------------- |
|---|
| 173 | * Function Reach_white_space(). |
|---|
| 174 | * Reach the next available white space of string line. |
|---|
| 175 | */ |
|---|
| 176 | int Reach_white_space(line, index) |
|---|
| 177 | char *line; |
|---|
| 178 | int index; |
|---|
| 179 | { |
|---|
| 180 | int length; |
|---|
| 181 | |
|---|
| 182 | length = Lenstr(line); |
|---|
| 183 | |
|---|
| 184 | /* skip to white space */ |
|---|
| 185 | for(; line[index]!=' '&&line[index]!='\t' |
|---|
| 186 | &&index<length; index++) ; |
|---|
| 187 | |
|---|
| 188 | return(index); |
|---|
| 189 | } |
|---|
| 190 | /* --------------------------------------------------------------- |
|---|
| 191 | * Function Fgetline(). |
|---|
| 192 | * Get a line from assigned file, also checking for buffer |
|---|
| 193 | * overflow. |
|---|
| 194 | */ |
|---|
| 195 | |
|---|
| 196 | char *Fgetline(char *line, size_t maxread, FILE_BUFFER fb) { |
|---|
| 197 | size_t len; |
|---|
| 198 | const char *fullLine = FILE_BUFFER_read(fb, &len); |
|---|
| 199 | if (!fullLine) return 0; |
|---|
| 200 | |
|---|
| 201 | if (len <= (maxread-2)) { |
|---|
| 202 | memcpy(line, fullLine, len); |
|---|
| 203 | line[len] = '\n'; |
|---|
| 204 | line[len+1] = 0; |
|---|
| 205 | return line; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | fprintf(stderr, "Error(148): OVERFLOW LINE: %s\n", fullLine); |
|---|
| 209 | return 0; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | /* ------------------------------------------------------------ |
|---|
| 213 | * Function Getstr(). |
|---|
| 214 | * Get input string from terminal. |
|---|
| 215 | */ |
|---|
| 216 | void Getstr(line, linenum) |
|---|
| 217 | char *line; |
|---|
| 218 | int linenum; |
|---|
| 219 | { |
|---|
| 220 | |
|---|
| 221 | char c; |
|---|
| 222 | int indi=0; |
|---|
| 223 | |
|---|
| 224 | for(; (c=getchar())!='\n'&&indi<(linenum-1); line[indi++]=c) ; |
|---|
| 225 | |
|---|
| 226 | line[indi]='\0'; |
|---|
| 227 | } |
|---|
| 228 | /* ---------------------------------------------------------------- |
|---|
| 229 | * Function Append_char(). |
|---|
| 230 | * Append a char before '\n' if there is no such char. |
|---|
| 231 | * (Assume there is '\n' at the end of string |
|---|
| 232 | * already and length of string except '\n' must |
|---|
| 233 | * greater than 1.) |
|---|
| 234 | */ |
|---|
| 235 | void Append_char(string, ch) |
|---|
| 236 | char **string, ch; |
|---|
| 237 | { |
|---|
| 238 | /* int Lenstr(); */ |
|---|
| 239 | /* void Append_rm_eoln(); */ |
|---|
| 240 | char temp[10]; |
|---|
| 241 | |
|---|
| 242 | if(Lenstr((*string))<=0||(*string)[0]=='\n') return; |
|---|
| 243 | |
|---|
| 244 | if((*string)[Lenstr((*string))-2]!=ch) { |
|---|
| 245 | sprintf(temp, "%c\n", ch); |
|---|
| 246 | Append_rm_eoln(string, temp); |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | /* ---------------------------------------------------------------- |
|---|
| 250 | * Function Append_rm_eoln(). |
|---|
| 251 | * Remove the eoln of string1 and then append string2 |
|---|
| 252 | * to string1. |
|---|
| 253 | */ |
|---|
| 254 | void Append_rm_eoln(string1, string2) |
|---|
| 255 | char **string1; |
|---|
| 256 | const char *string2; |
|---|
| 257 | { |
|---|
| 258 | int length; |
|---|
| 259 | /* char *Reallocspace(); */ |
|---|
| 260 | /* char *Catstr(); */ |
|---|
| 261 | |
|---|
| 262 | length = Lenstr(*string1); |
|---|
| 263 | |
|---|
| 264 | if(((length>1)&&(*string1)[length-1]=='\n') ||(*string1)[0]=='\n') |
|---|
| 265 | length--; |
|---|
| 266 | |
|---|
| 267 | (*string1)=(char*)Reallocspace((*string1), |
|---|
| 268 | (unsigned int)(sizeof(char)*(Lenstr(string2)+length+1))); |
|---|
| 269 | |
|---|
| 270 | (*string1)[length]='\0'; |
|---|
| 271 | |
|---|
| 272 | Catstr((*string1), string2); |
|---|
| 273 | } |
|---|
| 274 | /* ---------------------------------------------------------------- |
|---|
| 275 | * Function Append_rp_eoln(). |
|---|
| 276 | * Replace the eoln by a blank in string1 and then append |
|---|
| 277 | * string2 to string1. |
|---|
| 278 | */ |
|---|
| 279 | void Append_rp_eoln(string1, string2) |
|---|
| 280 | char **string1, *string2; |
|---|
| 281 | { |
|---|
| 282 | int length; |
|---|
| 283 | /* char *Reallocspace(); */ |
|---|
| 284 | /* char *Catstr(); */ |
|---|
| 285 | |
|---|
| 286 | length = Lenstr(*string1); |
|---|
| 287 | if((length>1)&&(*string1)[length-1]=='\n') |
|---|
| 288 | (*string1)[length-1]=' '; |
|---|
| 289 | (*string1)=(char*)Reallocspace((*string1), |
|---|
| 290 | (unsigned int)(sizeof(char) *(Lenstr(string2)+Lenstr(*string1)+1))); |
|---|
| 291 | (*string1)[length]='\0'; |
|---|
| 292 | Catstr((*string1), string2); |
|---|
| 293 | } |
|---|
| 294 | /* ---------------------------------------------------------------- |
|---|
| 295 | * Function Append(). |
|---|
| 296 | * Append string2 to string1. |
|---|
| 297 | */ |
|---|
| 298 | void Append(string1, string2) |
|---|
| 299 | char **string1; |
|---|
| 300 | const char *string2; |
|---|
| 301 | { |
|---|
| 302 | int length; |
|---|
| 303 | /* char *Reallocspace(); */ |
|---|
| 304 | /* char *Catstr(); */ |
|---|
| 305 | |
|---|
| 306 | length = Lenstr(*string1); |
|---|
| 307 | if(length==0&&Lenstr(string2)==0) return; |
|---|
| 308 | |
|---|
| 309 | (*string1)=(char*)Reallocspace((*string1), |
|---|
| 310 | (unsigned int)(sizeof(char)* (Lenstr(string2)+Lenstr(*string1)+1))); |
|---|
| 311 | (*string1)[length]='\0'; |
|---|
| 312 | |
|---|
| 313 | Catstr((*string1), string2); |
|---|
| 314 | } |
|---|
| 315 | /* ---------------------------------------------------------- |
|---|
| 316 | * Function find_pattern() |
|---|
| 317 | * Return TRUE if the desired pattern in the input text |
|---|
| 318 | */ |
|---|
| 319 | int find_pattern(text, pattern) |
|---|
| 320 | const char *text, *pattern; |
|---|
| 321 | { |
|---|
| 322 | int indi, index, indj; |
|---|
| 323 | int stop; |
|---|
| 324 | |
|---|
| 325 | stop = Lenstr(text)-Lenstr(pattern)+1; |
|---|
| 326 | for(indi=0; indi<stop; indi++) |
|---|
| 327 | { |
|---|
| 328 | index = -2; |
|---|
| 329 | for(indj=0; index==-2&&pattern[indj]!='\0'; indj++) |
|---|
| 330 | if(same_char(text[indi+indj], |
|---|
| 331 | pattern[indj])==0) |
|---|
| 332 | index = -1; |
|---|
| 333 | if(index == -2) return(indi); |
|---|
| 334 | /* find PARTIALLY matched pattern */ |
|---|
| 335 | } |
|---|
| 336 | return(-1); |
|---|
| 337 | } |
|---|
| 338 | /* -------------------------------------------------------------- |
|---|
| 339 | * Function not_ending_mark(). |
|---|
| 340 | * Return true if the char is not a '.' nor ';'. |
|---|
| 341 | */ |
|---|
| 342 | int not_ending_mark(ch) |
|---|
| 343 | char ch; |
|---|
| 344 | { |
|---|
| 345 | return((ch!='.'&&ch!=';')); |
|---|
| 346 | } |
|---|
| 347 | /* ---------------------------------------------------------------- |
|---|
| 348 | * Function last_word(). |
|---|
| 349 | * return true if the char is not white space. |
|---|
| 350 | */ |
|---|
| 351 | int last_word(ch) |
|---|
| 352 | char ch; |
|---|
| 353 | { |
|---|
| 354 | int true; |
|---|
| 355 | |
|---|
| 356 | switch(ch) { |
|---|
| 357 | case ',': |
|---|
| 358 | |
|---|
| 359 | case '.': |
|---|
| 360 | |
|---|
| 361 | case ';': |
|---|
| 362 | |
|---|
| 363 | case ' ': |
|---|
| 364 | |
|---|
| 365 | case '?': |
|---|
| 366 | |
|---|
| 367 | case ':': |
|---|
| 368 | |
|---|
| 369 | case '!': |
|---|
| 370 | |
|---|
| 371 | case ')': |
|---|
| 372 | |
|---|
| 373 | case ']': |
|---|
| 374 | |
|---|
| 375 | case '}': |
|---|
| 376 | true=1; |
|---|
| 377 | break; |
|---|
| 378 | default: true=0; |
|---|
| 379 | } |
|---|
| 380 | return(true); |
|---|
| 381 | } |
|---|
| 382 | /* ---------------------------------------------------------------- |
|---|
| 383 | * Function is_separator(). |
|---|
| 384 | * Return 1 if ch is a separator in string separators. |
|---|
| 385 | */ |
|---|
| 386 | int is_separator(ch, separators) |
|---|
| 387 | char ch; |
|---|
| 388 | const char *separators; |
|---|
| 389 | { |
|---|
| 390 | int indi, len, index; |
|---|
| 391 | |
|---|
| 392 | for(indi=index=0, len=Lenstr(separators); indi<len&&index==0; |
|---|
| 393 | indi++) |
|---|
| 394 | if(ch==separators[indi]) index=1; |
|---|
| 395 | |
|---|
| 396 | return(index); |
|---|
| 397 | } |
|---|
| 398 | /* ------------------------------------------------------------ |
|---|
| 399 | * Function same_char() |
|---|
| 400 | * Return TRUE if two characters are the same. |
|---|
| 401 | * case insensitive or not is decided by buffer.charcase. |
|---|
| 402 | */ |
|---|
| 403 | int same_char(ch1, ch2) |
|---|
| 404 | char ch1, ch2; |
|---|
| 405 | { |
|---|
| 406 | if(ch1 == ch2) return(1); |
|---|
| 407 | if((ch1-'a')>=0) ch1 = ch1-'a'+'A'; |
|---|
| 408 | if((ch2-'a')>=0) ch2 = ch2-'a'+'A'; |
|---|
| 409 | if(ch1 == ch2) return(1); |
|---|
| 410 | else return(0); |
|---|
| 411 | } |
|---|
| 412 | /* -------------------------------------------------------------- |
|---|
| 413 | * Function Upper_case(). |
|---|
| 414 | * Capitalize all char in the string. |
|---|
| 415 | */ |
|---|
| 416 | void Upper_case(string) |
|---|
| 417 | char *string; |
|---|
| 418 | { |
|---|
| 419 | int len, indi; |
|---|
| 420 | |
|---|
| 421 | len=Lenstr(string); |
|---|
| 422 | for(indi=0; indi<len; indi++) { |
|---|
| 423 | if(string[indi]>='a'&&string[indi]<='z') |
|---|
| 424 | string[indi] = string[indi]-'a'+'A'; |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | /* --------------------------------------------------------------- |
|---|
| 428 | * Function Blank_num(). |
|---|
| 429 | * Count the number of blanks at the beginning of |
|---|
| 430 | * the string. |
|---|
| 431 | */ |
|---|
| 432 | int Blank_num(string) |
|---|
| 433 | char *string; |
|---|
| 434 | { |
|---|
| 435 | int index; |
|---|
| 436 | |
|---|
| 437 | for(index=0;string[index]==' ';index++); |
|---|
| 438 | |
|---|
| 439 | return(index); |
|---|
| 440 | } |
|---|