source: tags/ms_r16q2/GDE/CLUSTAL/util.c

Last change on this file was 13845, checked in by westram, 9 years ago
  • remove executable flag
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 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 "clustalv.h"
8
9/*
10*       Prototypes
11*/
12
13void *  ckalloc(size_t);
14void            fatal(char *,...);
15void            error(char *,...);
16void            warning(char *,...);
17char *  rtrim(char *);
18char *  upstr(char *);
19char *  lowstr(char *);
20void            getstr(char *,char *);
21double  getreal(char *,double,double);
22int             getint(char *,int,int,int);
23void            do_system(void);
24Boolean linetype(char *,char *);
25void            get_path(char *,char *);
26
27/*
28*       ckalloc()
29*
30*       Tries to allocate "bytes" bytes of memory. Exits program if failed.
31*       Return value:
32*               Generic pointer to the newly allocated memory.
33*/
34
35void *ckalloc(size_t bytes)
36{
37        register void *ret;
38       
39        if( (ret = malloc(bytes)) == NULL)
40                fatal("Out of memory\n");
41        else
42                return ret;     
43}
44
45/*
46*       fatal()
47*
48*       Prints error msg to stderr and exits.
49*       Variadic parameter list can be passed.
50*
51*       Return values:
52*               none
53*/
54
55void fatal( char *msg,...)
56{
57        va_list ap;
58       
59        va_start(ap,msg);
60        fprintf(stderr,"\n\nFATAL ERROR: ");
61        vfprintf(stderr,msg,ap);
62        fprintf(stderr,"\n\n");
63        va_end(ap);
64        exit(1);
65}
66
67/*
68*       error()
69*
70*       Prints error msg to stderr.
71*       Variadic parameter list can be passed.
72*
73*       Return values:
74*               none
75*/
76
77void error( char *msg,...)
78{
79        va_list ap;
80       
81        va_start(ap,msg);
82        fprintf(stderr,"\n\nERROR: ");
83        vfprintf(stderr,msg,ap);
84        fprintf(stderr,"\n\n");
85        va_end(ap);
86}
87
88/*
89*       warning()
90*
91*       Prints warning msg to stderr.
92*       Variadic parameter list can be passed.
93*
94*       Return values:
95*               none
96*/
97
98void warning( char *msg,...)
99{
100        va_list ap;
101       
102        va_start(ap,msg);
103        fprintf(stderr,"\n\nWARNING: ");
104        vfprintf(stderr,msg,ap);
105        fprintf(stderr,"\n\n");
106        va_end(ap);
107}
108
109
110/*
111*       rtrim()
112*
113*       Removes trailing blanks from a string
114*
115*       Return values:
116*               Pointer to the processed string
117*/
118
119char * rtrim(char *str)
120{
121        register int p;
122
123        p = strlen(str) - 1;
124       
125        while ( isspace(str[p]) )
126                p--;
127               
128        str[p + 1] = EOS;
129       
130        return str;
131}
132
133
134/*
135*       upstr()
136*
137*       Converts string str to uppercase.
138*       Return values:
139*               Pointer to the converted string.
140*/
141
142char * upstr(char *str)
143{
144        register char *s = str;
145       
146        while( *s = toupper(*s) )
147                s++;
148               
149        return str;
150}
151
152/*
153*       lowstr()
154*
155*       Converts string str to lower case.
156*       Return values:
157*               Pointer to the converted string.
158*/
159
160char * lowstr(char *str)
161{
162        register char *s = str;
163       
164        while( *s = tolower(*s) )
165                s++;
166               
167        return str;
168}
169
170void getstr(char *instr,char *outstr)
171{       
172        fprintf(stdout,"%s: ",instr);
173        gets(outstr);
174}
175
176double getreal(char *instr,double minx,double maxx)
177{
178        double ret;
179       
180        while(TRUE) {
181                fprintf(stdout,"%s (%.1lf-%.1lf): ",instr,minx,maxx);
182                ret=0.0;
183                scanf("%lf",&ret);
184                getchar();
185                if(ret>maxx) {
186                        fprintf(stderr,"ERROR: Max. value=%.1lf\n\n",maxx);
187                        continue;
188                }
189                if(ret<minx) {
190                        fprintf(stderr,"ERROR: Min. value=%.1lf\n\n",minx);
191                        continue;
192                }
193                break;
194        }
195        return ret;
196}
197
198
199int getint(char *instr,int minx,int maxx, int def)
200{
201        int ret;
202        char line[MAXLINE];     
203
204        while(TRUE) {
205                fprintf(stdout,"%s (%d..%d)    [%d]: ",instr,minx,maxx,def);
206                ret=0;
207                gets(line);
208                sscanf(line,"%d",&ret);
209                if(ret == 0) return def;
210                if(ret>maxx) {
211                        fprintf(stderr,"ERROR: Max. value=%d\n\n",maxx);
212                        continue;
213                }
214                if(ret<minx) {
215                        fprintf(stderr,"ERROR: Min. value=%d\n\n",minx);
216                        continue;
217                }
218                break;
219        }
220        return ret;
221}
222
223void do_system()
224{
225        char line[MAXLINE];
226       
227        getstr("\n\nEnter system command",line);
228        if(*line != EOS)
229                system(line);
230        fprintf(stdout,"\n\n");
231}
232
233
234Boolean linetype(char *line,char *code)
235{
236        return( strncmp(line,code,strlen(code)) == 0 );
237}
238
239
240void get_path(char *str,char *path)
241{
242        register int i;
243       
244        strcpy(path,str);
245        for(i=strlen(path)-1;i>-1;--i) {
246                if(str[i]==DIRDELIM) {
247                        i = -1;
248                        break;
249                }
250                if(str[i]=='.') break;
251        }
252        if(i<0)
253                strcat(path,".");
254        else
255                path[i+1]=EOS;
256}
Note: See TracBrowser for help on using the repository browser.