📄 utilities.c
字号:
#include <stdlib.h>#include <unistd.h>#include <stdio.h>#include <string.h>#include <time.h>#include <sys/time.h>#include <sys/times.h>#include <sys/types.h>#include "fastDNAml_types.h"#include "fastDNAml_funcs.h"#include "fastDNAml_globals.h"/* Utility functions for fastDNAml *//******************************************************************************* * Parse command line. */extern char *optarg; /* for use by getopt(3) */extern int optind,opterr,optopt; /* for use by getopt(3) */int get_args(int argc, char *argv[], boolean print_usage){ /* get_args */ int c; char *s; boolean bad_opt=FALSE; int i; char buf[2048]; infol = 0; run_id[0] = 0; workdir[0] = 0; seq_file[0] = 0; ship_seq_data = FALSE; while(!bad_opt && ((c=getopt(argc,argv,"m:g:p:d:tn:w:s"))!=-1) ) { switch(c) { case 'm': /* rank or tid of monitor process */ sscanf(optarg,"%x",&monitor_id); break; case 'g': /* tid of group to join (currently, foreman's tid) */ sscanf(optarg,"%d",foreman_id); break; case 'p': /* number of workers to spawn */ sscanf(optarg,"%d",&nworkers); break; case 'd': /* Info level */ sscanf(optarg,"%d",&infol); break; case 'n': /* Name, or ID of run */ strcpy(run_id,optarg); break; case 'w': /* Working directory */ strcpy(workdir,optarg); break; case 's': /* Master is to ship sequence data to all workers */ ship_seq_data = TRUE; break; case '?': default: bad_opt = TRUE; break; } }/* Set the full path to the working directory. It's up to using functions * of workdir to resolve this path, which may contain instances of ./ ../ * and //, and handle resulting errors. (N.B.: We take care not to introduce * double slashes because some implementations of realpath() (notably in AIX) * choke on them). */ if(workdir[0]==0 || workdir[0] != '/') { getcwd(buf,sizeof(buf)); if( buf[strlen(buf)-1] != '/') strcat(buf,"/"); strcat(buf,workdir); if( buf[strlen(buf)-1] != '/') strcat(buf,"/"); strcpy(workdir,buf); }/* There should be one argument left, the sequence data file name. */ if(optind==argc-1) { strcpy(seq_file,argv[argc-1]); }/* Check for errors */ if(bad_opt || (myprogtype==DNAML_MONITOR && seq_file[0]==0 ) ) { if(print_usage) { s = strrchr(argv[0],'/'); s = (s==NULL) ? argv[0] : s+1; fprintf(stderr,"Usage: %s [-d<infolevel>] [-p<#_workers>] [-n<run_name>]\ [-w<work_dir>] [-s] sequence_file\n",s); fflush(stderr); } return 0; } else { if(run_id[0]==0) { s = strrchr(seq_file,'/'); s = (s==NULL) ? seq_file : s+1; strcpy(run_id,s); } return 1; }} /* get_args *//******************************************************************************* */boolean digitchar (int ch) { return (ch >= '0' && ch <= '9'); }boolean whitechar (int ch) { return (ch == ' ' || ch == '\n' || ch == '\t'); }void uppercase (int *chptr) /* convert character to upper case -- either ASCII or EBCDIC */ { /* uppercase */ int ch; ch = *chptr; if ((ch >= 'a' && ch <= 'i') || (ch >= 'j' && ch <= 'r') || (ch >= 's' && ch <= 'z')) *chptr = ch + 'A' - 'a'; } /* uppercase */int base36 (int ch) { /* base36 */ if (ch >= '0' && ch <= '9') return (ch - '0'); else if (ch >= 'A' && ch <= 'I') return (ch - 'A' + 10); else if (ch >= 'J' && ch <= 'R') return (ch - 'J' + 19); else if (ch >= 'S' && ch <= 'Z') return (ch - 'S' + 28); else if (ch >= 'a' && ch <= 'i') return (ch - 'a' + 10); else if (ch >= 'j' && ch <= 'r') return (ch - 'j' + 19); else if (ch >= 's' && ch <= 'z') return (ch - 's' + 28); else return -1; } /* base36 */int itobase36 (int i) { /* itobase36 */ if (i < 0) return '?'; else if (i < 10) return (i + '0'); else if (i < 19) return (i - 10 + 'A'); else if (i < 28) return (i - 19 + 'J'); else if (i < 36) return (i - 28 + 'S'); else return '?'; } /* itobase36 */int findch (FILE *fp, int c) { /* findch */ int ch; while ((ch = getc(fp)) != EOF && ch != c) ; return ch; } /* findch */double randum (long *seed) /* random number generator, modified to use 12 bit chunks */ { /* randum */ long sum, mult0, mult1, seed0, seed1, seed2, newseed0, newseed1, newseed2; mult0 = 1549; seed0 = *seed & 4095; sum = mult0 * seed0; newseed0 = sum & 4095; sum >>= 12; seed1 = (*seed >> 12) & 4095; mult1 = 406; sum += mult0 * seed1 + mult1 * seed0; newseed1 = sum & 4095; sum >>= 12; seed2 = (*seed >> 24) & 255; sum += mult0 * seed2 + mult1 * seed1; newseed2 = sum & 255; *seed = newseed2 << 24 | newseed1 << 12 | newseed0; return 0.00390625 * (newseed2 + 0.000244140625 * (newseed1 + 0.000244140625 * newseed0)); } /* randum *//* Makes a fastDNAml standard file name: * if id == 0 then return "dir/run_id.ext" * if id != 0 then return "dir/run_id.ext.id" * (A slash is appended to the directory path if it does not already have one.) */void make_filename(char *fn, char *dir, char *run_id, char *ext, int id) { /* make_filename */ char *s; int k; s = strcpy(fn,dir); k=strlen(s); if(s[k-1]!='/') {s[k]='/'; s[k+1]='\0';} /* Append slash if necessary */ s = strcat(s,run_id); s = strcat(s,ext); if(id>0) sprintf(s+strlen(s),".t%x",id); return; } /* make_filename */double sec;struct timeval tv;struct tms cpu;double dwalltime00(){ gettimeofday(&tv,0); sec = tv.tv_sec + tv.tv_usec/1000000.0; return sec;}void record_times(stat_data *sd){ times(&cpu); sd->utime = (double)(cpu.tms_utime)/(double)(CLK_TCK); sd->stime = (double)(cpu.tms_stime)/(double)(CLK_TCK); sd->t1 = dwalltime00();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -