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);
# Line 450 | Line 508
508   }
509  
510  
511 < char* rstrstr(char* rstart, char *lend, char* substr) {  /*like strstr, but starts searching
511 > char* rstrstr(const char* rstart, const char *lend, const char* substr) {  /*like strstr, but starts searching
512   from right end, going up to lend and returns a pointer to the last (right)
513   matching character in str */
514   char *p;
515   int l,i;
516   l=strlen(substr);
517 < p=rstart-l+1;
517 > p=(char*)rstart-l+1;
518   while (p>=lend) {
519      for (i=0;i<l;i++) if (*(p+i) != *(substr+i)) break;
520      if (i==l) return p+l-1;
# Line 480 | Line 538
538    return h;
539    }
540  
541 < // removes the directory part from a full-path file name
541 > // removes the last part (file or directory name) of a full path
542   // this is a destructive operation for the given string!!!
543   // the trailing '/' is guaranteed to be there
544   void delFileName(char* filepath) {
545   char *p, *sep;
546   if (filepath==NULL) return;
547   for (p=filepath, sep=filepath;*p!='\0';p++)
548 <     if (*p==CHPATHSEP) sep=p+1;
548 >     if (*p=='/' || *p=='\\') sep=p+1;
549   *sep='\0'; // truncate filepath
550   }
551  
552 < // returns a pointer to the file name part in a full-path filename
553 < char* getFileName(char* filepath) {
554 < char *p, *sep;
552 > // returns a pointer to the last file or directory name in a full path
553 > const char* getFileName(const char* filepath) {
554 > const char *p, *sep;
555   if (filepath==NULL) return NULL;
556   for (p=filepath, sep=filepath;*p!='\0';p++)
557 <     if (*p==CHPATHSEP) sep=p+1;
557 >     if (*p=='/' || *p=='\\') sep=p+1;
558   return sep;
559   }
560  
561 + // returns a pointer to the file "extension" part in a filename
562 + const char* getFileExt(const char* filepath) {
563 + const char *p, *dp, *sep;
564 + if (filepath==NULL) return NULL;
565 + for (p=filepath, dp=filepath, sep=filepath;*p!='\0';p++) {
566 +     if (*p=='.') dp=p+1;
567 +       else if (*p=='/' || *p=='\\')
568 +                  sep=p+1;
569 +     }
570 + return (dp>sep) ? dp : NULL ;
571 + }
572 +
573   int fileExists(const char* fname) {
574    struct stat stFileInfo;
575    int r=0;
# Line 527 | Line 597
597    return true;
598   }
599   */
600 < off_t fileSize(const char* fpath) {
600 > int64 fileSize(const char* fpath) {
601    struct stat results;
602    if (stat(fpath, &results) == 0)
603        // The size of the file in bytes is in
604 <      return results.st_size;
604 >      return (int64)results.st_size;
605    else
606        // An error occurred
607 <    //GError("Error at stat(%s)!\n", fpath)
607 >    //GMessage("Error at stat(%s)!\n", fpath);
608      return 0;
609   }
610  
# Line 620 | Line 690
690   return true;
691   }
692  
693 + //write a formatted fasta record, fasta formatted
694 + void writeFasta(FILE *fw, const char* seqid, const char* descr,
695 +        const char* seq, int linelen, int seqlen) {
696 +  fflush(fw);
697 +  // write header line only if given!
698 +  if (seqid!=NULL) {
699 +    if (descr==NULL || descr[0]==0)
700 +             fprintf(fw,">%s\n",seqid);
701 +        else fprintf(fw,">%s %s\n",seqid, descr);
702 +    }
703 +  fflush(fw);
704 +  if (seq==NULL || *seq==0) return; //nothing to print
705 +  if (linelen==0) { //unlimited line length: write the whole sequence on a line
706 +     if (seqlen>0)
707 +             fwrite((const void*)seq, 1, seqlen,fw);
708 +        else fprintf(fw,"%s",seq);
709 +     fprintf(fw,"\n");
710 +     fflush(fw);
711 +     return;
712 +     }
713 +  int ilen=0;
714 +  if (seqlen>0) { //seq length given, so we know when to stop
715 +    for (int i=0; i < seqlen; i++, ilen++) {
716 +            if (ilen == linelen) {
717 +                 fputc('\n', fw);
718 +                 ilen = 0;
719 +                 }
720 +            fputc(seq[i], fw);
721 +            }
722 +    fputc('\n', fw);
723 +    }
724 +  else { //seq length not given, stop when 0 encountered
725 +    for (int i=0; seq[i]!=0; i++, ilen++) {
726 +            if (ilen == linelen) {
727 +                 fputc('\n', fw);
728 +                 ilen = 0;
729 +                 }
730 +            fputc(seq[i], fw);
731 +            } //for
732 +    fputc('\n', fw);
733 +    }
734 +  fflush(fw);
735 + }
736 +
737 + char* commaprint(uint64 n) {
738 +  static int comma = '\0';
739 +  static char retbuf[48];
740 +  char *p = &retbuf[sizeof(retbuf)-1];
741 +  int i = 0;
742 +  if(comma == '\0') {
743 +    /* struct lconv *lcp = localeconv();
744 +    if(lcp != NULL) {
745 +      if(lcp->thousands_sep != NULL &&
746 +        *lcp->thousands_sep != '\0')
747 +        comma = *lcp->thousands_sep;
748 +      else  */
749 +                          comma = ',';
750 +     // }
751 +    }
752 +  *p = '\0';
753 +  do {
754 +    if(i%3 == 0 && i != 0)
755 +      *--p = comma;
756 +    *--p = '0' + n % 10;
757 +    n /= 10;
758 +    i++;
759 +  } while(n != 0);
760 +  return p;
761 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines