source: tags/initial/CONVERTALN/util.c

Last change on this file was 2, checked in by oldcode, 24 years ago

Initial revision

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