source: tags/initial/GDE/CLUSTALW/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: 7.0 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <errno.h>
5#include <stdarg.h>
6#include <ctype.h>
7#include "clustalw.h"
8
9extern char **seq_array;
10extern sint  *seqlen_array;
11extern char **names,**titles;
12extern sint *output_index;
13extern double **tmat;
14
15
16/*
17*       ckalloc()
18*
19*       Tries to allocate "bytes" bytes of memory. Exits program if failed.
20*       Return value:
21*               Generic pointer to the newly allocated memory.
22*/
23
24void *ckalloc(size_t bytes)
25{
26        register void *ret;
27       
28        if( (ret = calloc(bytes, sizeof(char))) == NULL)
29/*
30        if( (ret = malloc(bytes)) == NULL)
31*/
32                fatal("Out of memory\n");
33        else
34                return ret;     
35
36        return ret;     
37}
38
39/*
40*       ckrealloc()
41*
42*       Tries to reallocate "bytes" bytes of memory. Exits program if failed.
43*       Return value:
44*               Generic pointer to the re-allocated memory.
45*/
46
47void *ckrealloc(void *ptr, size_t bytes)
48{
49        register void *ret=NULL;
50
51        if (ptr == NULL)       
52                fatal("Bad call to ckrealloc\n");
53        else if( (ret = realloc(ptr, bytes)) == NULL)
54                fatal("Out of memory\n");
55        else
56                return ret;     
57
58        return ret;     
59}
60
61/*
62*       ckfree()
63*
64*       Tries to free memory allocated by ckalloc.
65*       Return value:
66*               None.
67*/
68
69void *ckfree(void *ptr)
70{
71        if (ptr == NULL)
72                warning("Bad call to ckfree\n");
73        else {
74                free(ptr);
75                ptr = NULL;
76        }
77        return ptr;
78}
79
80
81/*
82*       rtrim()
83*
84*       Removes trailing blanks from a string
85*
86*       Return values:
87*               Pointer to the processed string
88*/
89
90char * rtrim(char *str)
91{
92        register int p;
93
94        p = strlen(str) - 1;
95       
96        while ( isspace(str[p]) )
97                p--;
98               
99        str[p + 1] = EOS;
100       
101        return str;
102}
103
104
105/*
106*       blank_to_()
107*
108*       Replace blanks in a string with underscores
109*
110*       Also replaces , ; : ( or ) with _
111*
112*       Return value:
113*               Pointer to the processed string
114*/
115
116char * blank_to_(char *str)
117{
118        int i,p;
119
120        p = strlen(str) - 1;
121       
122        for(i=0;i<=p;i++) 
123                if(
124                     (str[i]==' ') ||
125                     (str[i]==';') ||
126                     (str[i]==',') ||
127                     (str[i]=='(') ||
128                     (str[i]==')') ||
129                     (str[i]==':') 
130                  )
131                      str[i] = '_';
132       
133        return str;
134}
135
136
137/*
138*       upstr()
139*
140*       Converts string str to uppercase.
141*       Return values:
142*               Pointer to the converted string.
143*/
144
145char * upstr(char *str)
146{
147        register char *s = str;
148       
149        while( (*s = toupper(*s)) )
150                s++;
151               
152        return str;
153}
154
155/*
156*       lowstr()
157*
158*       Converts string str to lower case.
159*       Return values:
160*               Pointer to the converted string.
161*/
162
163char * lowstr(char *str)
164{
165        register char *s = str;
166       
167        while( (*s = tolower(*s)) )
168                s++;
169               
170        return str;
171}
172
173void getstr(char *instr,char *outstr)
174{       
175        fprintf(stdout,"%s: ",instr);
176        gets(outstr);
177}
178
179double getreal(char *instr,double minx,double maxx,double def)
180{
181        int status;
182        float ret;
183        char line[MAXLINE];     
184       
185        while(TRUE) {
186                fprintf(stdout,"%s (%.1f-%.1f)   [%.1f]: ",instr,minx,maxx,def);
187                gets(line);
188                status=sscanf(line,"%f",&ret);
189                if(status == EOF) return def;
190                if(ret>maxx) {
191                        fprintf(stdout,"ERROR: Max. value=%.1f\n\n",maxx);
192                        continue;
193                }
194                if(ret<minx) {
195                        fprintf(stdout,"ERROR: Min. value=%.1f\n\n",minx);
196                        continue;
197                }
198                break;
199        }
200        return (double)ret;
201}
202
203
204int getint(char *instr,int minx,int maxx, int def)
205{
206        int ret,status;
207        char line[MAXLINE];     
208
209        while(TRUE) {
210                fprintf(stdout,"%s (%d..%d)    [%d]: ",
211                instr,(pint)minx,(pint)maxx,(pint)def);
212                gets(line);
213                status=sscanf(line,"%d",&ret);
214                if(status == EOF) return def;
215                if(ret>maxx) {
216                        fprintf(stdout,"ERROR: Max. value=%d\n\n",(pint)maxx);
217                        continue;
218                }
219                if(ret<minx) {
220                        fprintf(stdout,"ERROR: Min. value=%d\n\n",(pint)minx);
221                        continue;
222                }
223                break;
224        }
225        return ret;
226}
227
228void do_system(void)
229{
230        char line[MAXLINE];
231       
232        getstr("\n\nEnter system command",line);
233        if(*line != EOS)
234                system(line);
235        fprintf(stdout,"\n\n");
236}
237
238
239Boolean linetype(char *line,char *code)
240{
241        return( strncmp(line,code,strlen(code)) == 0 );
242}
243
244Boolean keyword(char *line,char *code)
245{
246        int i;
247        char key[MAXLINE];
248
249        for(i=0;!isspace(line[i]) && line[i]!=EOS;i++)
250                key[i]=line[i];
251        key[i]=EOS;
252        return( strcmp(key,code) == 0 );
253}
254
255Boolean blankline(char *line)
256{
257        int i;
258
259        for(i=0;line[i]!='\n' && line[i]!=EOS;i++) {
260                if( isdigit(line[i]) ||
261                    isspace(line[i]) ||
262                    (line[i] == '*') ||
263                    (line[i] == ':') ||
264                    (line[i] == '.')) 
265                        ;
266                else
267                        return FALSE;
268        }
269        return TRUE;
270}
271
272
273void get_path(char *str,char *path)
274{
275        register int i;
276       
277        strcpy(path,str);
278        for(i=strlen(path)-1;i>-1;--i) {
279                if(str[i]==DIRDELIM) {
280                        i = -1;
281                        break;
282                }
283                if(str[i]=='.') break;
284        }
285        if(i<0)
286                strcat(path,".");
287        else
288                path[i+1]=EOS;
289}
290
291void alloc_aln(sint nseqs)
292{
293        sint i;
294
295        seqlen_array = (sint *)ckalloc( (nseqs+1) * sizeof (sint));
296
297        seq_array = (char **)ckalloc( (nseqs + 1) * sizeof (char *) );
298
299        names = (char **)ckalloc( (nseqs+1) * sizeof (char *) );
300        for(i=1;i<=nseqs;i++)
301                names[i] = (char *)ckalloc((MAXNAMES+1) * sizeof (char));
302
303        titles = (char **)ckalloc( (nseqs+1) * sizeof (char *) );
304        for(i=1;i<=nseqs;i++)
305                titles[i] = (char *)ckalloc((MAXTITLES+1) * sizeof (char));
306
307        output_index = (sint *)ckalloc( (nseqs+1) * sizeof (sint));
308
309        tmat = (double **) ckalloc( (nseqs+1) * sizeof (double *) );
310        for(i=1;i<=nseqs;i++)
311                tmat[i] = (double *)ckalloc( (nseqs+1) * sizeof (double) );
312
313}
314
315void realloc_aln(sint first_seq,sint nseqs)
316{
317        sint i;
318
319        seqlen_array = (sint *)ckrealloc(seqlen_array, (first_seq+nseqs+1) * sizeof (sint));
320
321        seq_array = (char **)ckrealloc(seq_array, (first_seq+nseqs+1) * sizeof (char *) );
322
323        names = (char **)ckrealloc(names, (first_seq+nseqs+1) * sizeof (char *) );
324        for(i=first_seq;i<first_seq+nseqs;i++)
325                names[i] = (char *)ckalloc((MAXNAMES+1) * sizeof (char));
326
327        titles = (char **)ckrealloc(titles, (first_seq+nseqs+1) * sizeof (char *) );
328        for(i=first_seq;i<first_seq+nseqs;i++)
329                titles[i] = (char *)ckalloc((MAXTITLES+1) * sizeof (char));
330
331        output_index = (sint *)ckrealloc(output_index, (first_seq+nseqs+1) * sizeof (sint));
332
333        tmat = (double **) ckrealloc(tmat, (first_seq+nseqs+1) * sizeof (double *) );
334        for(i=1;i<first_seq;i++)
335                tmat[i] = (double *)ckrealloc(tmat[i], (first_seq+nseqs+1) * sizeof (double) );
336        for(i=first_seq;i<first_seq+nseqs;i++)
337                tmat[i] = (double *)ckalloc( (first_seq+nseqs+1) * sizeof (double) );
338}
339
340void free_aln(sint nseqs)
341{
342        sint i;
343
344        if(nseqs<=0) return;
345
346        seqlen_array = ckfree(seqlen_array);
347
348        for(i=1;i<=nseqs;i++)
349                seq_array[i] = ckfree(seq_array[i]);
350        seq_array = ckfree(seq_array);
351
352        for(i=1;i<=nseqs;i++)
353                names[i] = ckfree(names[i]);
354        names = ckfree(names);
355
356        for(i=1;i<=nseqs;i++)
357                titles[i] = ckfree(titles[i]);
358        titles = ckfree(titles);
359
360        output_index = ckfree(output_index);
361
362        for(i=1;i<=nseqs;i++)
363                tmat[i] = ckfree(tmat[i]);
364        tmat = ckfree(tmat);
365}
366
367void alloc_seq(sint seq_no,sint length)
368{
369        seq_array[seq_no] = (char *)ckalloc((length+2) * sizeof (char));
370}
371
372void realloc_seq(sint seq_no,sint length)
373{
374        seq_array[seq_no] = (char *)realloc(seq_array[seq_no], (length+2) * sizeof (char));
375
376}
377
378void free_seq(sint seq_no)
379{
380        seq_array[seq_no]=ckfree(seq_array[seq_no]);
381}
382
Note: See TracBrowser for help on using the repository browser.