1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
|
/*
library_utils.c: CCP4 Library Utilities
Copyright (C) 2001 CCLRC, Charles Ballard
This library is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
version 3, modified in accordance with the provisions of the
license to address the requirements of UK law.
You should have received a copy of the modified GNU Lesser General
Public License along with this library. If not, copies may be
downloaded from http://www.ccp4.ac.uk/ccp4license.php
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
*/
/** @page utilities_page CCP4 Library Utilities
*
* @section utilities_list File list
<ul>
<li>library_utils.c
<li>ccp4_general.c
<li>ccp4_parser.c
<li>ccp4_program.c
</ul>
* @section utilities_overview Overview
The CCP4 C-library provides many utility functions which either give
specific CCP4 functionality (e.g. traditional keyword parsing) or
are just generally useful (platform independent date).
*/
/** @file library_utils.c
* @brief Utility functions.
* @author Charles Ballard
*/
#include "ccp4_sysdep.h"
#include <time.h>
#include <math.h>
#include "ccp4_utils.h"
#include "ccp4_errno.h"
#if defined (_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#define CCP4_ERRNO(y) (CCP4_ERR_UTILS | (y))
/* rcsid[] = "$Id$" */
/* static uint16 nativeIT = NATIVEIT; */ /* machine integer type - currently unused here */
static uint16 nativeFT = NATIVEFT; /* machine float type */
/** .
*
* @return
*/
int ccp4_utils_translate_mode_float(float *out, const void *buffer, int dim, int mode)
{
unsigned char *ucp;
unsigned short *usp;
float *fp = out, *ufp, tmp1, tmp2;
register int ctr=0;
switch(mode) {
case 0:
ucp = (unsigned char *)buffer;
for(ctr = 0; ctr < dim ; ++ctr)
*fp++ = (float) *ucp++;
break;
case 1:
usp = (unsigned short *)buffer;
for(ctr = 0; ctr < dim ; ++ctr)
*fp++ = (float) *usp++;
break;
case 3:
/* complex short (define type ?) */
usp = (unsigned short *)buffer;
for(ctr = 0; ctr < dim ; ++ctr) {
tmp1 = (float) *usp++;
tmp2 = (float) *usp++;
*fp++ = (float) sqrt(tmp1*tmp1 + tmp2*tmp2);
}
break;
case 4:
/* complex real (define type ?) */
ufp = (float *)buffer;
for(ctr = 0; ctr < dim ; ++ctr) {
tmp1 = *ufp++;
tmp2 = *ufp++;
*fp++ = (float) sqrt(tmp1*tmp1 + tmp2*tmp2);
}
break;
case 2:
default:
break;
}
return (ctr);
}
/** Gets the length of a Fortran string with trailing blanks removed.
*
* @return length of string
*/
size_t ccp4_utils_flength (char *s, int len)
{
if (len <= 0 ) return 0;
while (s[--len] == ' ')
if (len == 0) return 0;
return (++len);
}
/** .
*
* @return
*/
void ccp4_utils_print (const char *message)
{
printf ("%s\n",message);
}
#if ! defined (VMS)
/** .
*
* @return
*/
int ccp4_utils_setenv (char *str)
{
#if defined (sgi) || defined (sun) || defined (__hpux) || \
defined(_AIX) || defined (__OSF1__) || \
defined (__osf__) || defined (__FreeBSD__) || defined (linux) || \
defined (_WIN32) || defined __linux__
/* putenv is the POSIX.1, draft 3 proposed mechanism */
#if !(defined(__hpux) && defined(__HP_cc))
int putenv (char *);
#endif
char *param;
if ( (param = (char *) ccp4_utils_malloc( (strlen(str)+1)*sizeof(char) )) == NULL) {
ccp4_errno = CCP4_ERRNO(errno);
return -1; }
strcpy(param,str);
return (putenv (param));
/* note the necessary lack of free() */
#else
/* setenv is not POSIX, BSD might have to use `index' */
int setenv ();
char *param1,*param2;
if ( (param1 = (char *) ccp4_utils_malloc( (strlen(str)+1)*sizeof(char) )) == NULL) {
ccp4_errno = CCP4_ERRNO(errno);
return -1; }
strcpy(param1,str);
if ((param2 = (char *) strchr(param1, '=')) == NULL) {
ccp4_errno = CCP4_ERRNO(errno);
return -1; }
*param2++ = '\0';
return (setenv (param1, param2, 1));
#endif
}
#endif
#if ! defined (VMS)
/** .
*
* @return
*/
int ccp4_utils_outbuf(void)
{
#if defined (sgi) || defined (sun) || \
defined (__OSF1__) || \
defined (__FreeBSD__)
return setlinebuf(stdout);
#else
#if defined(_MSC_VER)
return setvbuf(stdout, NULL, _IONBF, 80);
#else
# if defined (_AIX)
return -1;
# else
/* Windows requires size argument, though 0 works on unix */
return setvbuf(stdout, NULL, _IOLBF, 80);
# endif
#endif
#endif
}
/** .
*
* @return
*/
int ccp4_utils_noinpbuf(void)
{
return setvbuf(stdin, NULL, _IONBF, 0);
}
#endif
union float_uint_uchar ccp4_nan ()
#if NATIVEFT == DFNTF_BEIEEE || NATIVEFT == DFNTF_LEIEEE
# define CCP4_NAN 0xfffa5a5a
#endif
/* For \idx{Convex} native mode and \idx{VAX} use a \idx{Rop} value: */
/* */
/* <magic numbers>= */
#if NATIVEFT == DFNTF_CONVEXNATIVE
# define CCP4_NAN 0x80000000
#endif
#if NATIVEFT == DFNTF_VAX
# define CCP4_NAN 0x00008000
#endif
#ifndef CCP4_NAN
# error "CCP4_NAN isn't defined (needs NATIVEFT)"
#endif
{
union float_uint_uchar realnum;
realnum.i = CCP4_NAN;
return (realnum);
}
/** .
*
* @return
*/
int ccp4_utils_isnan (const union float_uint_uchar *realnum)
{
switch (nativeFT) {
case DFNTF_BEIEEE :
case DFNTF_LEIEEE :
return ((realnum->i & 0x7f800000) == 0x7f800000); /* exponent all 1s */
case DFNTF_CONVEXNATIVE :
return ((realnum->i & 0xff800000) == 0x80000000);
case DFNTF_VAX :
return ((realnum->i & 0x0000ff80) == 0x00008000);
default :
ccp4_fatal("CCP4_UTILS_ISNAN: bad nativeFT");
return 0; /* avoid compiler warning */
}
}
#define MDFBIG -1.0E10 /* BIOMOL absence flag value */
/** .
*
* @return
*/
void ccp4_utils_bml (int ncols, union float_uint_uchar cols[])
{
int i;
for (i=0; i<ncols; i++)
if (cols[i].i != CCP4_NAN)
if (cols[i].f <= MDFBIG) cols[i].f = 0.0;
}
/** .
*
* @return
*/
void ccp4_utils_wrg (int ncols, union float_uint_uchar cols[], float wminmax[])
{
int i;
for (i=0; i<ncols; i++)
if (cols[i].i != CCP4_NAN)
if (cols[i].f > MDFBIG) {
if (cols[i].f < wminmax[2*i]) wminmax[2*i] = cols[i].f;
if (cols[i].f > wminmax[1+2*i]) wminmax[1+2*i] = cols[i].f; }
}
/** .
*
* @return
*/
void ccp4_utils_hgetlimits (int *IValueNotDet, float *ValueNotDet)
{
*IValueNotDet = INT_MAX;
*ValueNotDet = FLT_MAX;
}
#ifndef _WIN32
static unsigned parse_mode(const char *cmode)
{
unsigned mode = 0;
#if defined (__APPLE__)
static const unsigned TBM = 0x07;
switch (strlen(cmode)) {
case 4:
mode |= (*cmode & TBM) << 9 ;
mode |= (*(cmode+1) & TBM) << 6 ;
mode |= (*(cmode+2) & TBM) << 3 ;
mode |= (*(cmode+3) & TBM) ;
break;
case 3:
mode |= (*cmode & TBM) << 6 ;
mode |= (*(cmode+1) & TBM) << 3 ;
mode |= (*(cmode+2) & TBM) ;
break;
case 2:
mode |= (*cmode & TBM) << 3 ;
mode |= (*(cmode+1) & TBM) ;
break;
case 1:
mode |= (*cmode & TBM) ;
break;
default:
mode = 0x0fff ;
}
#else
/* Possible modes (see stat.h)
Currently pass 3-character string and interpret as octal.
Try also S_IRWXU, S_IRWXG, etc. */
sscanf(cmode,"%o",&mode);
#endif
return mode;
}
#endif
/** .
*
* @return
*/
int ccp4_utils_mkdir (const char *path, const char *cmode)
{
int result;
#if defined(_WIN32)
result = mkdir(path);
#else
unsigned mode = parse_mode(cmode);
result = mkdir(path, (mode_t) mode);
#endif
if (result == -1) {
if (errno == EEXIST) {
result = 1;
}
}
return (result);
}
/** .
*
* @return
*/
int ccp4_utils_chmod (const char *path, const char *cmode)
{
#if defined(_WIN32)
return (chmod(path,0x0fff));
#else
unsigned mode = parse_mode(cmode);
return (chmod(path, (mode_t) mode));
#endif
}
/** This is a wrapper for the malloc function, which adds some
* error trapping.
*
* @return void
*/
void *ccp4_utils_malloc(size_t size)
{ void *val;
val = malloc (size);
if (!val && size)
{
perror ("Failure in ccp4_utils_malloc");
abort ();
}
return val;}
/** This is a wrapper for the realloc function, which adds some
* error trapping.
*
* @return
*/
void *ccp4_utils_realloc(void *ptr, size_t size)
{ void *val;
val = realloc (ptr, size);
if (!val && size)
{
perror ("Failure in ccp4_utils_realloc");
abort ();
}
return val;}
/** This is a wrapper for the calloc function, which adds some
* error trapping.
*
* @return
*/
void *ccp4_utils_calloc(size_t nelem , size_t elsize)
{ void *val;
val = calloc (nelem, elsize);
if (!val && elsize)
{
perror ("Failure in ccp4_utils_calloc");
abort ();
}
return val;}
/** Return the user's login name.
* Note that getlogin only works for processes attached to
* a terminal (and hence won't work from the GUI).
* @return pointer to character string containing login name.
*/
char *ccp4_utils_username(void)
{
static char userid_unknown[] = "unknown";
char *userid = NULL;
#if defined(_WIN32)
static char windows_username[512];
DWORD bufsize = sizeof(windows_username);
if (GetUserName(windows_username, &bufsize))
userid = windows_username;
#else
userid = getlogin();
#endif
return userid ? userid : userid_unknown;
}
static int is_sep(char c)
{
#ifdef _WIN32
/* allow alternative separator for Windows (for MSYS, Cygwin, Wine) */
return c == PATH_SEPARATOR || c == '/';
#else
return c == PATH_SEPARATOR;
#endif
}
/** Extracts the basename from a full file name.
* Separators for directories and extensions are OS-specific.
* @param filename full file name string.
* @return pointer to basename
*/
char *ccp4_utils_basename(const char *filename)
{
int i, indx1=-1, length;
char *basename;
for ( i = strlen(filename)-1; i >= 0; i-- ) {
if (is_sep(filename[i])) {
indx1 = i;
break;
}
}
length = strlen(filename) - indx1;
/* Search for extension separators must be performed backwards
in case filename has multiple extension separators */
for ( i = strlen(filename)-1; i >= (indx1 < 0 ? 0 : indx1) ; i-- ) {
if (filename[i] == EXT_SEPARATOR) {
length = i - indx1;
break;
}
}
basename = ccp4_utils_malloc(length*sizeof(char));
strncpy(basename,filename+indx1+1,length-1);
basename[length-1]='\0';
return basename;
}
/** Extracts the pathname from a full file name.
* Separators for directories and extensions are OS-specific.
* @param filename full file name string.
* @return pointer to pathname with trailing separator.
*/
char *ccp4_utils_pathname(const char *filename)
{
int i, indx1=-1, length;
char *pathname;
for ( i = strlen(filename)-1; i >= 0; i-- ) {
if (is_sep(filename[i])) {
indx1 = i;
break;
}
}
length = indx1+2;
pathname = ccp4_utils_malloc(length*sizeof(char));
strncpy(pathname,filename,length-1);
pathname[length-1]='\0';
return pathname;
}
/** Extracts the extension from a full file name.
* Separators for directories and extensions are OS-specific.
* @param filename full file name string.
* @return pointer to extension
*/
char *ccp4_utils_extension(const char *filename)
{
int i, indx1=-1, length=1;
char *extension;
for ( i = strlen(filename)-1; i >= 0; i-- ) {
if (filename[i] == EXT_SEPARATOR) {
indx1 = i;
length = strlen(filename) - indx1;
break;
} else if (is_sep(filename[i])) {
indx1 = i;
length = 1;
break;
}
}
extension = ccp4_utils_malloc(length*sizeof(char));
strncpy(extension,filename+indx1+1,length-1);
extension[length-1]='\0';
return extension;
}
/** Joins a leading directory with a filename.
* Separators for directories and extensions are OS-specific.
* @param dir directory path.
* @param file file name string.
* @return pointer to joined directory-filename path.
*/
char *ccp4_utils_joinfilenames(const char *dir, const char *file)
{
char *join=NULL;
int lendir,lenfile,lenjoin;
lendir = strlen(dir);
lenfile = strlen(file);
lenjoin = lendir + lenfile + 2;
join = (char *) ccp4_utils_malloc(sizeof(char)*lenjoin);
if (!join) {
return NULL;
}
strncpy(join,dir,lendir);
join[lendir] = PATH_SEPARATOR;
join[lendir+1] = '\0';
strncat(join,file,lenfile);
join[lenjoin-1] = '\0';
return join;
}
/** .
*
* @return
*/
void ccp4_utils_idate (int iarray[3])
{
struct tm *lt=NULL;
time_t tim;
tim = time(NULL);
lt = localtime(&tim);
iarray[0] = lt->tm_mday;
iarray[1] = lt->tm_mon+1; /* need range 1-12 */
iarray[2] = lt->tm_year + 1900;
}
/** .
*
* @return
*/
char *ccp4_utils_date(char *date)
{
int iarray[3];
ccp4_utils_idate(iarray);
sprintf(date,"%2d/%2d/%4d",iarray[0],iarray[1],iarray[2]);
date[10] = '\0';
return (date);
}
/** Function to obtain current time.
* @param iarray Array containing hours, minutes and seconds.
* @return void.
*/
void ccp4_utils_itime (int iarray[3])
{
struct tm *lt;
time_t tim;
tim = time(NULL);
lt = localtime(&tim);
iarray[0] = lt->tm_hour;
iarray[1] = lt->tm_min;
iarray[2] = lt->tm_sec;
}
/** Alternative to ccp4_utils_itime with time as character string.
* @param time Character string of form HH:MM:SS
* @return pointer to character string.
*/
char *ccp4_utils_time(char *time)
{
int iarray[3];
ccp4_utils_itime(iarray);
sprintf(time,"%2.2d:%2.2d:%2.2d",iarray[0],iarray[1],iarray[2]);
time[8] = '\0';
return (time);
}
/** Function to obtain User and System times.
* @param tarray Array containing User and System times.
* @return Sum of User and System times.
*/
float ccp4_utils_etime (float tarray[2])
{
#ifdef _WIN32
tarray[0] = tarray[1] = 0.;
#else
static long clk_tck = 0;
struct tms buffer;
if (! clk_tck) clk_tck = sysconf(_SC_CLK_TCK);
(void) times(&buffer);
tarray[0] = (float) buffer.tms_utime / (float)clk_tck;
tarray[1] = (float) buffer.tms_stime / (float)clk_tck;
#endif
return (tarray[0]+tarray[1]);
}
#if defined(_MSC_VER)
double ccp4_erfc( double x )
{
double t,z,ans;
z=fabs(x);
t=1.0/(1.0+0.5*z);
ans=t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+
t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587+
t*(-0.82215223+t*0.17087277)))))))));
return x >= 0.0 ? ans : 2.0-ans;
}
#endif
#if defined (__APPLE__) && defined (__GNUC__) && ( __GNUC__ < 3 )
void _carbon_init(int argc, char **argv) {}
void _objcInit(void) {}
#endif
#if defined (__APPLE__) && defined (__GNUC__) && ( __GNUC__ == 3 ) && (__GNUC_MINOR__ == 1)
float acosf(float x) {
return (float) acos( (double) x);
}
float atanf(float x) {
return (float) atan( (double) x);
}
float asinf(float x) {
return (float) asin( (double) x);
}
#endif
#if defined(_MSC_VER) && _MSC_VER < 1800
double rint(double x) {
if (x >= 0.) {
return (double)(int)(x+.5);
}
return (double)(int)(x-.5);
}
#endif
|