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   static char msg[4069];
7 + /*
8 + #ifdef _DEFINE_WIN32_FSEEKO
9 + int fseeko(FILE *stream, off_t offset, int whence) {
10 +  
11 +   }
12 + #endif
13 +
14 + #ifdef _DEFINE_WIN32_FTELLO
15 + off_t ftello(FILE *stream) {
16 +  
17 +  }
18 + #endif
19 + */
20 +
21 +
22 + int saprintf(char **retp, const char *fmt, ...) {
23 +  va_list argp;
24 +  int len;
25 +  char *buf;
26 +
27 +  va_start(argp, fmt);
28 +  len = vsnprintf(NULL, 0, fmt, argp);
29 +  va_end(argp);
30 +  GMALLOC(buf, (len + 1));
31 +  if(buf == NULL)
32 +    {
33 +    *retp = NULL;
34 +    return -1;
35 +    }
36 +
37 +  va_start(argp, fmt);
38 +  vsnprintf(buf, len+1, fmt, argp);
39 +  va_end(argp);
40 +
41 +  *retp = buf;
42 +  return len;
43 + }
44 +
45   //************************* Debug helpers **************************
46   // Assert failed routine
47   void GAssert(const char* expression, const char* filename, unsigned int lineno){
# Line 33 | Line 71
71    #endif
72      exit(1);
73    }
74 +  
75   // Warning routine (just print message without exiting)
76   void GMessage(const char* format,...){
77    va_list arguments;
# Line 93 | Line 132
132  
133   char* Gstrdup(const char* str) {
134    if (str==NULL) return NULL;
135 <  char *copy;
135 >  char *copy=NULL;
136    GMALLOC(copy, strlen(str)+1);
137    strcpy(copy,str);
138    return copy;
139    }
140  
141   char* newEmptyStr() {
142 <  char* zs;
142 >  char* zs=NULL;
143    GMALLOC(zs,1);
144    zs[0]=0;
145    return zs;
# Line 108 | Line 147
147  
148   char* Gstrdup(const char* sfrom, const char* sto) {
149    if (sfrom==NULL || sto==NULL) return NULL;
150 <  char *copy;
150 >  char *copy=NULL;
151    if (sfrom[0]==0) return newEmptyStr();
152    GMALLOC(copy, sto-sfrom+2);
153    strncpy(copy, sfrom, sto-sfrom+1);
# Line 333 | Line 372
372  
373  
374   //strchr but with a set of chars instead of only one
375 < char* strchrs(char* s, const char* chrs) {
375 > char* strchrs(const char* s, const char* chrs) {
376    if (s==NULL || chrs==NULL || *chrs=='\0' || *s=='\0')
377           return NULL;
378    unsigned int l=strlen(s);
379    unsigned int r=strcspn(s, chrs);
380    if (r==l) return NULL;
381 <  return (s+r);
381 >  return ((char*)s+r);
382   }
383  
384   char* upCase(const char* str) {
# Line 379 | Line 418
418  
419  
420   //test if a char is in a given string (set)
421 < bool chrInStr(char c, char* str) {
421 > bool chrInStr(char c, const char* str) {
422   if (str==NULL || *str=='\0') return false;
423 < for (char* p=str; (*p)!='\0'; p++) {
423 > for (const char* p=str; (*p)!='\0'; p++) {
424     if ((*p)==c) return true;
425     }
426   return false;
# Line 389 | Line 428
428  
429  
430  
431 < char* rstrfind(char* str, const char* substr) {
431 > char* rstrfind(const char* str, const char* substr) {
432   /* like rindex() for a string */
433   int l,i;
434   if (str==NULL || *str=='\0') return NULL;
435   if (substr==NULL || *substr=='\0') return NULL;
436   l=strlen(substr);
437 < char* p=str+strlen(str)-l;
437 > char* p=(char*)str+strlen(str)-l;
438     //rightmost position that could match
439  
440   while (p>=str) {
441 <    for (i=0; i<l && *(p+i) == *(substr+i); i++);
441 >    for (i=0; i<l && *(p+i) == *(substr+i); i++) ;
442      if (i==l) return p; //found!
443      p--;
444      }
# Line 407 | Line 446
446   }
447  
448  
449 < char* strifind(char* str,  const char* substr) {
449 > char* strifind(const char* str,  const char* substr) {
450   // the case insensitive version of strstr -- finding a string within a strin
451    int l,i;
452    if (str==NULL || *str==0) return NULL;
453    if (substr==NULL || *substr==0) return NULL;
454    l=strlen(substr);
455 <  char* smax=str+strlen(str)-l;
455 >  char* smax=(char*)str+strlen(str)-l;
456    //rightmost position that could match
457 <  char* p=str;
457 >  char* p=(char*)str;
458    while (p<=smax) {
459 <     for (i=0; i<l && tolower(*(p+i))==tolower(*(substr+i)); i++);
459 >     for (i=0; i<l && tolower(*(p+i))==tolower(*(substr+i)); i++) ;
460       if (i==l) return p; //found!
461       p++;
462       }
# Line 427 | Line 466
466  
467  
468   // tests if string s has the given prefix
469 < bool startsWith(char* s, const char* prefix) {
469 > bool startsWith(const char* s, const char* prefix) {
470   if (prefix==NULL || s==NULL) return false;
471   int i=0;
472   while (prefix[i]!='\0' && prefix[i]==s[i]) i++;
473   return (prefix[i]=='\0');
474   }
475  
476 + // tests if string s ends with given suffix
477 + bool endsWith(const char* s, const char* suffix) {
478 + if (suffix==NULL || s==NULL) return false;
479 + if (suffix[0]==0) return true; //special case: empty suffix
480 + int j=strlen(suffix)-1;
481 + int i=strlen(s)-1;
482 + if (i<j) return false;
483 + while (j>=0 && s[i]==suffix[j]) { i--; j--; }
484 + return (j==-1);
485 + }
486 +
487  
488   char* reverseChars(char* str, int slen) {
489    if (slen==0) slen=strlen(str);
# Line 450 | Line 500
500   }
501  
502  
503 < char* rstrstr(char* rstart, char *lend, char* substr) {  /*like strstr, but starts searching
503 > char* rstrstr(const char* rstart, const char *lend, const char* substr) {  /*like strstr, but starts searching
504   from right end, going up to lend and returns a pointer to the last (right)
505   matching character in str */
506   char *p;
507   int l,i;
508   l=strlen(substr);
509 < p=rstart-l+1;
509 > p=(char*)rstart-l+1;
510   while (p>=lend) {
511      for (i=0;i<l;i++) if (*(p+i) != *(substr+i)) break;
512      if (i==l) return p+l-1;
# Line 480 | Line 530
530    return h;
531    }
532  
533 < // removes the directory part from a full-path file name
533 > // removes the last part (file or directory name) of a full path
534   // this is a destructive operation for the given string!!!
535   // the trailing '/' is guaranteed to be there
536   void delFileName(char* filepath) {
537   char *p, *sep;
538   if (filepath==NULL) return;
539   for (p=filepath, sep=filepath;*p!='\0';p++)
540 <     if (*p==CHPATHSEP) sep=p+1;
540 >     if (*p=='/' || *p=='\\') sep=p+1;
541   *sep='\0'; // truncate filepath
542   }
543  
544 < // returns a pointer to the file name part in a full-path filename
545 < char* getFileName(char* filepath) {
546 < char *p, *sep;
544 > // returns a pointer to the last file or directory name in a full path
545 > const char* getFileName(const char* filepath) {
546 > const char *p, *sep;
547   if (filepath==NULL) return NULL;
548   for (p=filepath, sep=filepath;*p!='\0';p++)
549 <     if (*p==CHPATHSEP) sep=p+1;
549 >     if (*p=='/' || *p=='\\') sep=p+1;
550   return sep;
551   }
552  
553 + // returns a pointer to the file "extension" part in a filename
554 + const char* getFileExt(const char* filepath) {
555 + const char *p, *dp, *sep;
556 + if (filepath==NULL) return NULL;
557 + for (p=filepath, dp=filepath, sep=filepath;*p!='\0';p++) {
558 +     if (*p=='.') dp=p+1;
559 +       else if (*p=='/' || *p=='\\')
560 +                  sep=p+1;
561 +     }
562 + return (dp>sep) ? dp : NULL ;
563 + }
564 +
565   int fileExists(const char* fname) {
566    struct stat stFileInfo;
567    int r=0;
# Line 527 | Line 589
589    return true;
590   }
591   */
592 < off_t fileSize(const char* fpath) {
592 > int64 fileSize(const char* fpath) {
593    struct stat results;
594    if (stat(fpath, &results) == 0)
595        // The size of the file in bytes is in
596 <      return results.st_size;
596 >      return (int64)results.st_size;
597    else
598        // An error occurred
599 <    //GError("Error at stat(%s)!\n", fpath)
599 >    //GMessage("Error at stat(%s)!\n", fpath);
600      return 0;
601   }
602  
# Line 620 | Line 682
682   return true;
683   }
684  
685 + //write a formatted fasta record, fasta formatted
686 + void writeFasta(FILE *fw, const char* seqid, const char* descr,
687 +        const char* seq, int linelen, int seqlen) {
688 +  fflush(fw);
689 +  // write header line only if given!
690 +  if (seqid!=NULL) {
691 +    if (descr==NULL || descr[0]==0)
692 +             fprintf(fw,">%s\n",seqid);
693 +        else fprintf(fw,">%s %s\n",seqid, descr);
694 +    }
695 +  fflush(fw);
696 +  if (seq==NULL || *seq==0) return; //nothing to print
697 +  if (linelen==0) { //unlimited line length: write the whole sequence on a line
698 +     if (seqlen>0)
699 +             fwrite((const void*)seq, 1, seqlen,fw);
700 +        else fprintf(fw,"%s",seq);
701 +     fprintf(fw,"\n");
702 +     fflush(fw);
703 +     return;
704 +     }
705 +  int ilen=0;
706 +  if (seqlen>0) { //seq length given, so we know when to stop
707 +    for (int i=0; i < seqlen; i++, ilen++) {
708 +            if (ilen == linelen) {
709 +                 fputc('\n', fw);
710 +                 ilen = 0;
711 +                 }
712 +            fputc(seq[i], fw);
713 +            }
714 +    fputc('\n', fw);
715 +    }
716 +  else { //seq length not given, stop when 0 encountered
717 +    for (int i=0; seq[i]!=0; i++, ilen++) {
718 +            if (ilen == linelen) {
719 +                 fputc('\n', fw);
720 +                 ilen = 0;
721 +                 }
722 +            fputc(seq[i], fw);
723 +            } //for
724 +    fputc('\n', fw);
725 +    }
726 +  fflush(fw);
727 + }
728 +
729 + char* commaprint(uint64 n) {
730 +  static int comma = '\0';
731 +  static char retbuf[48];
732 +  char *p = &retbuf[sizeof(retbuf)-1];
733 +  int i = 0;
734 +  if(comma == '\0') {
735 +    /* struct lconv *lcp = localeconv();
736 +    if(lcp != NULL) {
737 +      if(lcp->thousands_sep != NULL &&
738 +        *lcp->thousands_sep != '\0')
739 +        comma = *lcp->thousands_sep;
740 +      else  */
741 +                          comma = ',';
742 +     // }
743 +    }
744 +  *p = '\0';
745 +  do {
746 +    if(i%3 == 0 && i != 0)
747 +      *--p = comma;
748 +    *--p = '0' + n % 10;
749 +    n /= 10;
750 +    i++;
751 +  } while(n != 0);
752 +  return p;
753 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines