ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GBase.cpp
(Generate patch)
# Line 1 | Line 1
1 + #include "GBase.h"
2   #include <stdarg.h>
3   #include <ctype.h>
3 #include "GBase.h"
4   #include <sys/stat.h>
5  
6 + #ifndef S_ISDIR
7 + #define S_ISDIR(mode)  (((mode) & S_IFMT) == S_IFDIR)
8 + #endif
9 +
10 + #ifndef S_ISREG
11 + #define S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
12 + #endif
13 +
14   static char msg[4069];
15 + /*
16 + #ifdef _DEFINE_WIN32_FSEEKO
17 + int fseeko(FILE *stream, off_t offset, int whence) {
18 +  
19 +   }
20 + #endif
21 +
22 + #ifdef _DEFINE_WIN32_FTELLO
23 + off_t ftello(FILE *stream) {
24 +  
25 +  }
26 + #endif
27 + */
28 +
29 +
30 + int saprintf(char **retp, const char *fmt, ...) {
31 +  va_list argp;
32 +  int len;
33 +  char *buf;
34 +
35 +  va_start(argp, fmt);
36 +  len = vsnprintf(NULL, 0, fmt, argp);
37 +  va_end(argp);
38 +  GMALLOC(buf, (len + 1));
39 +  if(buf == NULL)
40 +    {
41 +    *retp = NULL;
42 +    return -1;
43 +    }
44 +
45 +  va_start(argp, fmt);
46 +  vsnprintf(buf, len+1, fmt, argp);
47 +  va_end(argp);
48 +
49 +  *retp = buf;
50 +  return len;
51 + }
52 +
53   //************************* Debug helpers **************************
54   // Assert failed routine
55   void GAssert(const char* expression, const char* filename, unsigned int lineno){
# Line 33 | Line 79
79    #endif
80      exit(1);
81    }
82 +  
83   // Warning routine (just print message without exiting)
84   void GMessage(const char* format,...){
85    va_list arguments;
# Line 93 | Line 140
140  
141   char* Gstrdup(const char* str) {
142    if (str==NULL) return NULL;
143 <  char *copy;
143 >  char *copy=NULL;
144    GMALLOC(copy, strlen(str)+1);
145    strcpy(copy,str);
146    return copy;
147    }
148  
149   char* newEmptyStr() {
150 <  char* zs;
150 >  char* zs=NULL;
151    GMALLOC(zs,1);
152    zs[0]=0;
153    return zs;
# Line 108 | Line 155
155  
156   char* Gstrdup(const char* sfrom, const char* sto) {
157    if (sfrom==NULL || sto==NULL) return NULL;
158 <  char *copy;
158 >  char *copy=NULL;
159    if (sfrom[0]==0) return newEmptyStr();
160    GMALLOC(copy, sto-sfrom+2);
161    strncpy(copy, sfrom, sto-sfrom+1);
# Line 333 | Line 380
380  
381  
382   //strchr but with a set of chars instead of only one
383 < char* strchrs(char* s, const char* chrs) {
383 > char* strchrs(const char* s, const char* chrs) {
384    if (s==NULL || chrs==NULL || *chrs=='\0' || *s=='\0')
385           return NULL;
386    unsigned int l=strlen(s);
387    unsigned int r=strcspn(s, chrs);
388    if (r==l) return NULL;
389 <  return (s+r);
389 >  return ((char*)s+r);
390   }
391  
392   char* upCase(const char* str) {
# Line 379 | Line 426
426  
427  
428   //test if a char is in a given string (set)
429 < bool chrInStr(char c, char* str) {
429 > bool chrInStr(char c, const char* str) {
430   if (str==NULL || *str=='\0') return false;
431 < for (char* p=str; (*p)!='\0'; p++) {
431 > for (const char* p=str; (*p)!='\0'; p++) {
432     if ((*p)==c) return true;
433     }
434   return false;
# Line 389 | Line 436
436  
437  
438  
439 < char* rstrfind(char* str, const char* substr) {
439 > char* rstrfind(const char* str, const char* substr) {
440   /* like rindex() for a string */
441   int l,i;
442   if (str==NULL || *str=='\0') return NULL;
443   if (substr==NULL || *substr=='\0') return NULL;
444   l=strlen(substr);
445 < char* p=str+strlen(str)-l;
445 > char* p=(char*)str+strlen(str)-l;
446     //rightmost position that could match
447  
448   while (p>=str) {
449 <    for (i=0; i<l && *(p+i) == *(substr+i); i++);
449 >    for (i=0; i<l && *(p+i) == *(substr+i); i++) ;
450      if (i==l) return p; //found!
451      p--;
452      }
# Line 407 | Line 454
454   }
455  
456  
457 < char* strifind(char* str,  const char* substr) {
457 > char* strifind(const char* str,  const char* substr) {
458   // the case insensitive version of strstr -- finding a string within a strin
459    int l,i;
460    if (str==NULL || *str==0) return NULL;
461    if (substr==NULL || *substr==0) return NULL;
462    l=strlen(substr);
463 <  char* smax=str+strlen(str)-l;
463 >  char* smax=(char*)str+strlen(str)-l;
464    //rightmost position that could match
465 <  char* p=str;
465 >  char* p=(char*)str;
466    while (p<=smax) {
467 <     for (i=0; i<l && tolower(*(p+i))==tolower(*(substr+i)); i++);
467 >     for (i=0; i<l && tolower(*(p+i))==tolower(*(substr+i)); i++) ;
468       if (i==l) return p; //found!
469       p++;
470       }
# Line 427 | Line 474
474  
475  
476   // tests if string s has the given prefix
477 < bool startsWith(char* s, const char* prefix) {
477 > bool startsWith(const char* s, const char* prefix) {
478   if (prefix==NULL || s==NULL) return false;
479   int i=0;
480   while (prefix[i]!='\0' && prefix[i]==s[i]) i++;
481   return (prefix[i]=='\0');
482   }
483  
484 + // tests if string s ends with given suffix
485 + bool endsWith(const char* s, const char* suffix) {
486 + if (suffix==NULL || s==NULL) return false;
487 + if (suffix[0]==0) return true; //special case: empty suffix
488 + int j=strlen(suffix)-1;
489 + int i=strlen(s)-1;
490 + if (i<j) return false;
491 + while (j>=0 && s[i]==suffix[j]) { i--; j--; }
492 + return (j==-1);
493 + }
494 +
495  
496   char* reverseChars(char* str, int slen) {
497    if (slen==0) slen=strlen(str);
498    int l=0;
499    int r=slen-1;
500 <  register char c;
500 >  char c;
501    while (l<r) {
502       c=str[l];str[l]=str[r];
503       str[r]=c;
446     //swap(str[l],str[r]);
504       l++;r--;
505       }
506    return str;
507   }
508  
509  
510 < char* rstrstr(char* rstart, char *lend, char* substr) {  /*like strstr, but starts searching
510 > char* rstrstr(const char* rstart, const char *lend, const char* substr) {  /*like strstr, but starts searching
511   from right end, going up to lend and returns a pointer to the last (right)
512   matching character in str */
513   char *p;
514   int l,i;
515   l=strlen(substr);
516 < p=rstart-l+1;
516 > p=(char*)rstart-l+1;
517   while (p>=lend) {
518      for (i=0;i<l;i++) if (*(p+i) != *(substr+i)) break;
519      if (i==l) return p+l-1;
# Line 480 | Line 537
537    return h;
538    }
539  
540 < // removes the directory part from a full-path file name
540 > // removes the last part (file or directory name) of a full path
541   // this is a destructive operation for the given string!!!
542   // the trailing '/' is guaranteed to be there
543   void delFileName(char* filepath) {
544   char *p, *sep;
545   if (filepath==NULL) return;
546   for (p=filepath, sep=filepath;*p!='\0';p++)
547 <     if (*p==CHPATHSEP) sep=p+1;
547 >     if (*p=='/' || *p=='\\') sep=p+1;
548   *sep='\0'; // truncate filepath
549   }
550  
551 < // returns a pointer to the file name part in a full-path filename
552 < char* getFileName(char* filepath) {
553 < char *p, *sep;
551 > // returns a pointer to the last file or directory name in a full path
552 > const char* getFileName(const char* filepath) {
553 > const char *p, *sep;
554   if (filepath==NULL) return NULL;
555   for (p=filepath, sep=filepath;*p!='\0';p++)
556 <     if (*p==CHPATHSEP) sep=p+1;
556 >     if (*p=='/' || *p=='\\') sep=p+1;
557   return sep;
558   }
559  
560 + // returns a pointer to the file "extension" part in a filename
561 + const char* getFileExt(const char* filepath) {
562 + const char *p, *dp, *sep;
563 + if (filepath==NULL) return NULL;
564 + for (p=filepath, dp=filepath, sep=filepath;*p!='\0';p++) {
565 +     if (*p=='.') dp=p+1;
566 +       else if (*p=='/' || *p=='\\')
567 +                  sep=p+1;
568 +     }
569 + return (dp>sep) ? dp : NULL ;
570 + }
571 +
572   int fileExists(const char* fname) {
573    struct stat stFileInfo;
574    int r=0;
# Line 527 | Line 596
596    return true;
597   }
598   */
599 < off_t fileSize(const char* fpath) {
599 > int64 fileSize(const char* fpath) {
600    struct stat results;
601    if (stat(fpath, &results) == 0)
602        // The size of the file in bytes is in
603 <      return results.st_size;
603 >      return (int64)results.st_size;
604    else
605        // An error occurred
606 <    //GError("Error at stat(%s)!\n", fpath)
606 >    //GMessage("Error at stat(%s)!\n", fpath);
607      return 0;
608   }
609  
# Line 620 | Line 689
689   return true;
690   }
691  
692 + //write a formatted fasta record, fasta formatted
693 + void writeFasta(FILE *fw, const char* seqid, const char* descr,
694 +        const char* seq, int linelen, int seqlen) {
695 +  fflush(fw);
696 +  // write header line only if given!
697 +  if (seqid!=NULL) {
698 +    if (descr==NULL || descr[0]==0)
699 +             fprintf(fw,">%s\n",seqid);
700 +        else fprintf(fw,">%s %s\n",seqid, descr);
701 +    }
702 +  fflush(fw);
703 +  if (seq==NULL || *seq==0) return; //nothing to print
704 +  if (linelen==0) { //unlimited line length: write the whole sequence on a line
705 +     if (seqlen>0)
706 +             fwrite((const void*)seq, 1, seqlen,fw);
707 +        else fprintf(fw,"%s",seq);
708 +     fprintf(fw,"\n");
709 +     fflush(fw);
710 +     return;
711 +     }
712 +  int ilen=0;
713 +  if (seqlen>0) { //seq length given, so we know when to stop
714 +    for (int i=0; i < seqlen; i++, ilen++) {
715 +            if (ilen == linelen) {
716 +                 fputc('\n', fw);
717 +                 ilen = 0;
718 +                 }
719 +            fputc(seq[i], fw);
720 +            }
721 +    fputc('\n', fw);
722 +    }
723 +  else { //seq length not given, stop when 0 encountered
724 +    for (int i=0; seq[i]!=0; i++, ilen++) {
725 +            if (ilen == linelen) {
726 +                 fputc('\n', fw);
727 +                 ilen = 0;
728 +                 }
729 +            fputc(seq[i], fw);
730 +            } //for
731 +    fputc('\n', fw);
732 +    }
733 +  fflush(fw);
734 + }
735 +
736 + char* commaprint(uint64 n) {
737 +  static int comma = '\0';
738 +  static char retbuf[48];
739 +  char *p = &retbuf[sizeof(retbuf)-1];
740 +  int i = 0;
741 +  if(comma == '\0') {
742 +    /* struct lconv *lcp = localeconv();
743 +    if(lcp != NULL) {
744 +      if(lcp->thousands_sep != NULL &&
745 +        *lcp->thousands_sep != '\0')
746 +        comma = *lcp->thousands_sep;
747 +      else  */
748 +                          comma = ',';
749 +     // }
750 +    }
751 +  *p = '\0';
752 +  do {
753 +    if(i%3 == 0 && i != 0)
754 +      *--p = comma;
755 +    *--p = '0' + n % 10;
756 +    n /= 10;
757 +    i++;
758 +  } while(n != 0);
759 +  return p;
760 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines