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