char* LytCtgData::readName(char* s, GHash<int>& names) { char* p=strchrs(s, " \t"); if (p!=NULL) { char* tmp; char* tmp2; GMALLOC(tmp, (p-s+30)*sizeof(char)); strncpy(tmp, s,p-s); tmp[p-s]='\0'; GMALLOC(tmp2, (p-s+30)*sizeof(char)); strcpy(tmp2, tmp); //make it unique (by simple versioning) int v=0; while (names.hasKey(tmp2)) { v++; sprintf(tmp2, "%s.%d", tmp, v); } name=Gstrdup(tmp2); GFREE(tmp); GFREE(tmp2); names.shkAdd(name, new int(1)); p++; } else { GMessage("LytCtgData::readName: Cannot find the token delimiter in:\n%s\n", s); } return p; }
char* replaceStr(char* &str, char* newvalue) { if (str!=NULL) GFREE(str); if (newvalue==NULL) { return NULL; } GMALLOC(str, strlen(newvalue)+1); strcpy(str,newvalue); return str; }
char* Gstrdup(const char* str) { if (str==NULL) return NULL; char *copy=NULL; GMALLOC(copy, strlen(str)+1); strcpy(copy,str); return copy; }
char* loCase(const char* str) { if (str==NULL) return NULL; int len=strlen(str); char* lostr; GMALLOC(lostr, len+1); lostr[len]='\0'; for (int i=0;i<len;i++) lostr[i]=tolower(str[i]); return lostr; }
char* upCase(const char* str) { if (str==NULL) return NULL; int len=strlen(str); char* upstr; GMALLOC(upstr, len+1); upstr[len]='\0'; for (int i=0;i<len;i++) upstr[i]=toupper(str[i]); return upstr; }
char* Gstrdup(const char* sfrom, const char* sto) { if (sfrom==NULL || sto==NULL) return NULL; char *copy=NULL; if (sfrom[0]==0) return newEmptyStr(); GMALLOC(copy, sto-sfrom+2); strncpy(copy, sfrom, sto-sfrom+1); copy[sto-sfrom+1]=0; return copy; }
GPrivate RBTreeP_t urbt_insert(RBTreeP_t pTree, Int32_t data) { Int32P_t pData = NULL; do { GMALLOC(pData, Int32_t, sizeof(Int32_t)); } while (NULL == pData); *pData = data; return rbtree_insert(pTree, pData, NULL); }
char* Gsubstr(const char* str, char* from, char* to) { //extract (and allocate) a substring, including boundaries (from/to) int len=strlen(str); if (to==NULL) to=(char *)(str+len-1); //extract tail from 'from' char, including it if (!(from>=str && to>from && to<str+len)) return NULL; int newlen=to-from+1; char* subs; GMALLOC(subs, newlen); memcpy(subs, str, newlen-1); subs[newlen]='\0'; return subs; }
int Gmkdir(const char *path, bool recursive, int perms) { if (path==NULL || path[0]==0) return -1; mode_t process_mask = umask(0); //is this really needed? if (!recursive) { int r=G_mkdir(path, perms); if (r!=0) GMessage("Warning: G_mkdir(%s) failed: %s\n", path, strerror(errno)); umask(process_mask); return r; } int plen=strlen(path); char* gpath=NULL; //make sure gpath ends with / if (path[plen-1]=='/') { gpath=Gstrdup(path); } else { GMALLOC(gpath, plen+2); strcpy(gpath,path); strcat(gpath, "/"); ++plen; } //char* ss=gpath+plen-1; char* psep = gpath+plen-1; //start at the last / GDynArray<char*> dirstack(4); // stack of directories that should be created while (psep>gpath && *(psep-1)=='/') --psep; //skip double slashes *psep='\0'; int fexists=0; while ((fexists=fileExists(gpath))==0) { dirstack.Push(psep); do { --psep; } while (psep>gpath && *psep!='/'); if (psep<=gpath) { psep=NULL; break; } while (psep>gpath && *(psep-1)=='/') --psep; *psep='\0'; } if (psep) *psep='/'; while (dirstack.Count()>0) { psep=dirstack.Pop(); int mkdir_err=0; if ((mkdir_err=G_mkdir(gpath, perms))!=0) { GMessage("Warning: mkdir(%s) failed: %s\n", gpath, strerror(errno)); GFREE(gpath); umask(process_mask); return -1; } *psep='/'; } GFREE(gpath); umask(process_mask); return 0; }
char* Gsubstr(const char* str, char* from, char* to) { //extract (and allocate) a substring, including boundaries (from/to) if (str==NULL || from==NULL) return NULL; if (from[0]==0 || str[0]==0) return newEmptyStr(); if (from<str) return NULL; if (to==NULL) { to=from; while (to[1]) to++; } if (to<from) return newEmptyStr(); int newlen=to-from+1; char* subs; GMALLOC(subs, newlen); memcpy(subs, str, newlen-1); subs[newlen]='\0'; return subs; }
int saprintf(char **retp, const char *fmt, ...) { va_list argp; int len; char *buf; va_start(argp, fmt); len = vsnprintf(NULL, 0, fmt, argp); va_end(argp); GMALLOC(buf, (len + 1)); if(buf == NULL) { *retp = NULL; return -1; } va_start(argp, fmt); vsnprintf(buf, len+1, fmt, argp); va_end(argp); *retp = buf; return len; }
char* LytSeqInfo::expandGaps(char* s) { if (!hasIntrons()) return s; char* r=NULL; GMALLOC(r, xlen+1); int srcOfs=0; char* rpos=s; //source reader position char* wpos=r; //target writer position for (int i=0;i<numisegs;i++) { int wlen=intersegs[i].nextSegSeq-srcOfs; strncpy(wpos,rpos,wlen); wpos+=wlen; rpos+=wlen; srcOfs=intersegs[i].nextSegSeq; //now expand the gap wlen=intersegs[i].length(); memset((void*)wpos, '-',wlen); wpos+=wlen; } //add last exon: strcpy(wpos, rpos); return r; }
GArgs::GArgs(int argc, char* const argv[], char* format) { /* format is: <letter>[:] for e.g. p:hT <- -p testing -ptesting -h -T <string>= for e.g. PID=S= <- PID=50 S=3.5 This means that the = options, if present, must NEVER be given after dashed switches (non-value) directly */ //parse format string first: char* fstr=format; fmtcount=0; count=0; nonOptCount=0; nonOptPos=0; optPos=0; errarg=0; args=NULL; fmt=NULL; int fmtlen=strlen(format); while (fstr-format < fmtlen ) { int l=strcspn(fstr, ":="); if (fstr[l]=='\0') { //end of string reached //all previous chars are just switches: GREALLOC(fmt, (fmtcount+l)*sizeof(fmtdef)); //store each switches for (int i=0; i<l;i++) { GCALLOC(fmt[fmtcount+i].opt, 2); //one char length fmt[fmtcount+i].opt[0]=fstr[i]; fmt[fmtcount+i].type = 0; } fmtcount+=l; break; } else { if (fstr[l]==':') { //fstr[l-1] is an argument, but all the previous are just switches GREALLOC(fmt, (fmtcount+l)*sizeof(fmtdef)); //store each switches AND the option for (int i=0; i<l;i++) { GCALLOC(fmt[fmtcount+i].opt, 2); //one char length fmt[fmtcount+i].opt[0]=fstr[i]; fmt[fmtcount+i].type = (i==l-1)?1:0; } fmtcount+=l; } else { // fstr[l]=='=' case! //all these chars are one = style argument GREALLOC(fmt, (fmtcount+1)*sizeof(fmtdef)); GMALLOC(fmt[fmtcount].opt, l+1); strncpy(fmt[fmtcount].opt, fstr, l); fmt[fmtcount].opt[l]='\0'; fmt[fmtcount].type=2; fmtcount++; } fstr+=l+1; } } //---- that was the parsing of the format string //now parse the arguments based on given format specification int p=1; //skip program name int f=0; //GMessage("argc=%d\n", argc); while (p<argc) { if (argv[p][0]=='-') { //dashed argument? int cpos=1; char c=argv[p][cpos]; if (c=='\0') { //special case, plain argument '-' GREALLOC(args, (count+1)*sizeof(argdata)); args[count].opt=NULL; GCALLOC(args[count].value, 2); args[count].value[0]='-'; count++; nonOptCount++; } else { //dashed argument or switch COLLAPSED: if ((f=validOpt(c))>=0) { if (fmt[f].type==0) {//switch type GREALLOC(args, (count+1)*sizeof(argdata)); GCALLOC(args[count].opt, 2); args[count].opt[0]=c; GCALLOC(args[count].value, 1); count++; // only switches can be grouped with some other switches or options if (argv[p][cpos+1]!='\0') { cpos++; c=argv[p][cpos]; goto COLLAPSED; } } else if (fmt[f].type==1) { //dash argument GREALLOC(args, (count+1)*sizeof(argdata)); GCALLOC(args[count].opt, 2); args[count].opt[0]=c; if (argv[p][cpos+1]=='\0') { if (p+1<argc) { //value is the whole next argument p++; GMALLOC(args[count].value, strlen(argv[p])+1); strcpy(args[count].value, argv[p]); } else { errarg=p; return; } } else { //value immediately follows the dash-option GMALLOC(args[count].value, strlen(argv[p])-cpos); strcpy(args[count].value, (argv[p]+cpos+1)); //GMessage("args[%d].value = '%s'",count, args[count].value); } count++; } else {//inconsistent type errarg=p; return; } } //was validOpt else { //option not found in format definition! errarg=p; return; } } } else {//not a dashed argument char* e=strchr(argv[p],'='); if (e!=NULL && strchr(format,'=')!=NULL && e!=argv[p] && *(e-1)!='\\') { //this must be an '=' option //yet the '=' char can be preceded by a '\' in order to not be parsed //as a = option char part[30]; strncpy(part, argv[p], e-argv[p]); part[e-argv[p]]='\0'; if ((f=validOpt(part))>=0 && fmt[f].type==2) { GREALLOC(args, (count+1)*sizeof(argdata)); args[count].opt=Gstrdup(part); if (strlen(argv[p])-strlen(part)>0) { GMALLOC(args[count].value, strlen(argv[p])-strlen(part)+1); strcpy(args[count].value, e+1); } else { args[count].value=NULL; } count++; } else { //error - format does not match this '=' argument errarg=p; return; } } else { //it seems it's just a plain argument, like a filename, etc. GREALLOC(args, (count+1)*sizeof(argdata)); args[count].opt=NULL; //it's not an option args[count].value=Gstrdup(argv[p]); count++; nonOptCount++; } } p++; } }
char* AceParser::readSeq(LytSeqInfo* sqinfo) { //assumes the next line is where a sequence starts! //stops at the next empty line encountered char* buf; static char rlenbuf[12]= {0,0,0,0,0,0,0,0,0,0,0,0}; //buffer for parsing the gap length int rlenbufacc=0; //how many digits accumulated in rlenbuf so far int buflen=512; GMALLOC(buf, buflen); //this MUST be freed by the caller buf[0]='\0'; int accrd=0; //accumulated read length so far -- excludes interseg gaps! int rgpos=0; //accumulated offset including interseg gaps! char *r = linebuf->getLine(f,f_pos); int linelen=linebuf->length(); char r_splice=0, l_splice=0; while (linelen>0) { if (r==NULL) { GMessage("AceParser: error reading sequence data\n"); return NULL; } //-- pass the line content for accumulation int i=0; while (r[i]) { while (r[i] && isdigit(r[i])) { rlenbuf[rlenbufacc]=r[i]; rlenbufacc++; i++; } if (r[i]==0) break; //end of line reached already //now r[i] is surely a non-digit char if (rlenbufacc>0) { //have we just had a number before? rlenbuf[rlenbufacc]=0; if (r[i]=='=' || r[i]=='-') { i++; if (r[i]==0) break; } else { //check for splice site markers for this introns if (r[i]=='('||r[i]=='[') { l_splice=r[i]; i++; if (r[i]==0) break; } if (r[i]==')'||r[i]==']') { r_splice=r[i]; i++; if (r[i]==0) break; } }//splice check add_intron(buf, accrd, rgpos, rlenbuf, sqinfo, l_splice, r_splice); rlenbufacc=0; r_splice=0; l_splice=0; //i++;//skip the gap character } //check for digits here and break the linebuf as needed int bi=i; //start of non-digit run while (r[i] && !isdigit(r[i])) i++; int nl=(i-bi); //length of non-digit run if (nl>0) { int si=accrd; accrd+=nl; rgpos+=nl; if (accrd>=buflen-1) { buflen=accrd+512; GREALLOC(buf,buflen); } //append these non-digit chars for(int b=0; b<nl; b++) { buf[si+b]=r[bi+b]; } }//non-digit run } //while line chars /* //-- append the line to buf accrd+=linelen; if (accrd>=buflen) { buflen+=1024; GREALLOC(buf,buflen); } strcat(buf, r); */ r=linebuf->getLine(f,f_pos); linelen=linebuf->length(); }//while linelen>0 //add the 0-ending buf[accrd]=0; return buf; }
char* newEmptyStr() { char* zs=NULL; GMALLOC(zs,1); zs[0]=0; return zs; }
char* Gstrdup(const char* str) { char *copy; GMALLOC(copy, strlen(str)+1); strcpy(copy,str); return copy; }