Next version of JABA
[jabaws.git] / binaries / src / tcoffee / t_coffee_source / pb_util_read_seq_util.c
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
8 #include "io_lib_header.h"
9 #include "util_lib_header.h"
10 #include "define_header.h"
11
12
13 /*
14 *       Prototypes
15 */
16
17
18 void            fatal(char *,...);
19 void            error(char *,...);
20 void            warning(char *,...);
21 char            *rtrim(char *);
22 char            *blank_to_(char *);     /* DES change blanks to _ */
23 char            *upstr(char *);
24 char            *lowstr(char *);
25 void            getstr(char *,char *);
26 double          getreal(char *,double,double,double);
27 int             getint(char *,int,int,int);
28 void            do_system(void);
29 Boolean         linetype(char *,char *);
30 Boolean         blankline(char *);
31 void            get_path(char *,char *);
32
33 /*
34 *       ckalloc()
35 *
36 *       Tries to allocate "bytes" bytes of memory. Exits program if failed.
37 *       Return value:
38 *               Generic pointer to the newly allocated memory.
39 */
40
41 void *ckalloc(size_t bytes)
42 {
43         register void *ret;
44         extern void *vcalloc (size_t nelem, size_t elsize);
45         
46         if( (ret = vcalloc(bytes, sizeof(char))) == NULL)
47 /*
48         if( (ret = vmalloc(bytes)) == NULL)
49 */
50                 fatal("Out of memory\n");
51         else
52                 return ret;     
53         return ret;
54 }
55
56 /*
57 *       ckvrealloc()
58 *
59 *       Tries to vreallocate "bytes" bytes of memory. Exits program if failed.
60 *       Return value:
61 *               Generic pointer to the re-allocated memory.
62 */
63
64 void *ckvrealloc(void *ptr, size_t bytes)
65 {
66         register void *ret;
67         extern void *vrealloc (void *ptr, size_t size);
68         
69         if( (ret = vrealloc(ptr, bytes)) == NULL)
70                 fatal("Out of memory\n");
71         else
72                 return ret;     
73         return ret;
74 }
75
76 /*
77 *       ckfree()
78 *
79 *       Tries to free memory allocated by ckalloc.
80 *       Return value:
81 *               None.
82 */
83
84 void ckfree(void *ptr)
85 {
86         vfree(ptr);
87 }
88
89
90 /*
91 *       fatal()
92 *
93 *       Prints error msg to stderr and exits.
94 *       Variadic parameter list can be passed.
95 *
96 *       Return values:
97 *               none
98 */
99
100 void fatal( char *msg,...)
101 {
102         va_list ap;
103         
104         va_start(ap,msg);
105         fprintf(stderr,"\n\nFATAL ERROR: ");
106         vfprintf(stderr,msg,ap);
107         fprintf(stderr,"\n\n");
108         va_end(ap);
109         myexit(EXIT_FAILURE);
110 }
111
112 /*
113 *       error()
114 *
115 *       Prints error msg to stderr.
116 *       Variadic parameter list can be passed.
117 *
118 *       Return values:
119 *               none
120 */
121
122 void error( char *msg,...)
123 {
124         va_list ap;
125         
126         va_start(ap,msg);
127         fprintf(stderr,"\n\nERROR: ");
128         vfprintf(stderr,msg,ap);
129         fprintf(stderr,"\n\n");
130         va_end(ap);
131 }
132
133 /*
134 *       warning()
135 *
136 *       Prints warning msg to stderr.
137 *       Variadic parameter list can be passed.
138 *
139 *       Return values:
140 *               none
141 */
142
143 void warning( char *msg,...)
144 {
145         va_list ap;
146         
147         va_start(ap,msg);
148         fprintf(stderr,"\n\nWARNING: ");
149         vfprintf(stderr,msg,ap);
150         fprintf(stderr,"\n\n");
151         va_end(ap);
152 }
153
154
155 /*
156 *       rtrim()
157 *
158 *       Removes trailing blanks from a string
159 *
160 *       Return values:
161 *               Pointer to the processed string
162 */
163
164 char * rtrim(char *str)
165 {
166         register int p;
167
168         p = strlen(str) - 1;
169         
170         while ( isspace(str[p]) )
171                 p--;
172                 
173         str[p + 1] = EOS;
174         
175         return str;
176 }
177
178
179 /*
180 *       blank_to_()
181 *
182 *       Replace blanks in a string with underscores
183 *
184 *       Also replaces , ; : ( or ) with _
185 *
186 *       Return value:
187 *               Pointer to the processed string
188 */
189
190 char * blank_to_(char *str)
191 {
192         int i,p;
193
194         
195         p = strlen(str) - 1;
196         for(i=0;i<=p;i++) 
197           {
198             if( strrchr(";,():",str[i]))str[i]='_';
199             else if (isspace(str[i]));
200           }
201         return str;
202 }
203
204
205 /*
206 *       upstr()
207 *
208 *       Converts string str to uppercase.
209 *       Return values:
210 *               Pointer to the converted string.
211 */
212
213 char * upstr(char *str)
214 {
215         register char *s = str;
216         
217         while( (*s = toupper(*s)) )
218                 s++;
219                 
220         return str;
221 }
222
223 /*
224 *       lowstr()
225 *
226 *       Converts string str to lower case.
227 *       Return values:
228 *               Pointer to the converted string.
229 */
230
231 char * lowstr(char *str)
232 {
233         register char *s = str;
234         
235         while( (*s = tolower(*s)) )
236                 s++;
237                 
238         return str;
239 }
240
241 void getstr(char *instr,char *outstr)
242 {       
243         fprintf(stdout,"%s: ",instr);
244         fgets(outstr, 100, stdin);
245 }
246
247 double getreal(char *instr,double minx,double maxx,double def)
248 {
249         int status;
250         double ret;
251         char line[MAXLINE];     
252         
253         while(TRUE) {
254                 fprintf(stdout,"%s (%.1lf-%.1lf)   [%.1lf]: ",instr,minx,maxx,def);
255                 fgets(line, MAXLINE, stdin);
256                 status=sscanf(line,"%lf",&ret);
257                 if(status == EOF) return def;
258                 if(ret>maxx) {
259                         fprintf(stderr,"ERROR: Max. value=%.1lf\n\n",maxx);
260                         continue;
261                 }
262                 if(ret<minx) {
263                         fprintf(stderr,"ERROR: Min. value=%.1lf\n\n",minx);
264                         continue;
265                 }
266                 break;
267         }
268         return ret;
269 }
270
271
272 int getint(char *instr,int minx,int maxx, int def)
273 {
274         int ret,status;
275         char line[MAXLINE];     
276
277         while(TRUE) {
278                 fprintf(stdout,"%s (%d..%d)    [%d]: ",
279                 instr,(pint)minx,(pint)maxx,(pint)def);
280                 fgets(line, MAXLINE, stdin);
281                 status=sscanf(line,"%d",&ret);
282                 if(status == EOF) return def;
283                 if(ret>maxx) {
284                         fprintf(stderr,"ERROR: Max. value=%d\n\n",(pint)maxx);
285                         continue;
286                 }
287                 if(ret<minx) {
288                         fprintf(stderr,"ERROR: Min. value=%d\n\n",(pint)minx);
289                         continue;
290                 }
291                 break;
292         }
293         return ret;
294 }
295
296 void do_system()
297 {
298         char line[MAXLINE];
299         
300         getstr("\n\nEnter system command",line);
301         if(*line != EOS)
302                 system(line);
303         fprintf(stdout,"\n\n");
304 }
305
306
307 Boolean linetype(char *line,char *code)
308 {
309         return( strncmp(line,code,strlen(code)) == 0 );
310 }
311
312 Boolean blankline(char *line)
313 {
314         int i;
315
316         for(i=0;line[i]!='\n' && line[i]!=EOS;i++) {
317                 if( isdigit(line[i]) ||
318                     isspace(line[i]) ||
319                     (line[i] == '*') ||
320                     (line[i] == '.')) 
321                         ;
322                 else
323                         return FALSE;
324         }
325         return TRUE;
326 }
327
328
329 void get_path(char *str,char *path)
330 {
331         register int i;
332         
333         strcpy(path,str);
334         for(i=strlen(path)-1;i>-1;--i) {
335                 if(str[i]==DIRDELIM) {
336                         i = -1;
337                         break;
338                 }
339                 if(str[i]=='.') break;
340         }
341         if(i<0)
342                 strcat(path,".");
343         else
344                 path[i+1]=EOS;
345 }
346 /*********************************COPYRIGHT NOTICE**********************************/
347 /*© Centro de Regulacio Genomica */
348 /*and */
349 /*Cedric Notredame */
350 /*Tue Oct 27 10:12:26 WEST 2009. */
351 /*All rights reserved.*/
352 /*This file is part of T-COFFEE.*/
353 /**/
354 /*    T-COFFEE is free software; you can redistribute it and/or modify*/
355 /*    it under the terms of the GNU General Public License as published by*/
356 /*    the Free Software Foundation; either version 2 of the License, or*/
357 /*    (at your option) any later version.*/
358 /**/
359 /*    T-COFFEE is distributed in the hope that it will be useful,*/
360 /*    but WITHOUT ANY WARRANTY; without even the implied warranty of*/
361 /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
362 /*    GNU General Public License for more details.*/
363 /**/
364 /*    You should have received a copy of the GNU General Public License*/
365 /*    along with Foobar; if not, write to the Free Software*/
366 /*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/
367 /*...............................................                                                                                      |*/
368 /*  If you need some more information*/
369 /*  cedric.notredame@europe.com*/
370 /*...............................................                                                                                                                                     |*/
371 /**/
372 /**/
373 /*      */
374 /*********************************COPYRIGHT NOTICE**********************************/