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 116 | Line 163
163    return copy;
164    }
165  
166 < int Gstrcmp(char* a, char* b) {
166 > int Gstrcmp(const char* a, const char* b, int n) {
167   if (a==NULL || b==NULL) {
168     return a==NULL ? -1 : 1;
169     }
170 < else return strcmp(a,b);
170 > else {
171 >   if (n<0) return strcmp(a,b);
172 >       else return strncmp(a,b,n);
173 > }
174 >
175   }
176  
177 < int Gstricmp(const char* a, const char* b) {
177 > int Gstricmp(const char* a, const char* b, int n) {
178   if (a==NULL || b==NULL) return a==NULL ? -1 : 1;
179   register int ua, ub;
180 < while ((*a!=0) && (*b!=0)) {
181 <  ua=tolower((unsigned char)*a);
182 <  ub=tolower((unsigned char)*b);
183 <  a++;b++;
184 <  if (ua!=ub) return ua < ub ? -1 : 1;
180 > if (n<0) {
181 >   while ((*a!=0) && (*b!=0)) {
182 >    ua=tolower((unsigned char)*a);
183 >    ub=tolower((unsigned char)*b);
184 >    a++;b++;
185 >    if (ua!=ub) return ua < ub ? -1 : 1;
186 >    }
187 >    return (*a == 0) ? ( (*b == 0) ? 0 : -1 ) : 1 ;
188 >  }
189 > else {
190 >   while (n && (*a!=0) && (*b!=0)) {
191 >    ua=tolower((unsigned char)*a);
192 >    ub=tolower((unsigned char)*b);
193 >    a++;b++;n--;
194 >    if (ua!=ub) return ua < ub ? -1 : 1;
195 >    }
196 >    //return (*a == 0) ? ( (*b == 0) ? 0 : -1 ) : 1 ;
197 >   if (n==0) return 0;
198 >   else { return (*a == 0) ? ( (*b == 0) ? 0 : -1 ) : 1 ; }
199    }
135  return (*a == 0) ? ( (*b == 0) ? 0 : -1 ) : 1 ;
200   }
201  
202   int strsplit(char* str, char** fields, int maxfields, const char* delim) {
# Line 333 | Line 397
397  
398  
399   //strchr but with a set of chars instead of only one
400 < char* strchrs(char* s, const char* chrs) {
400 > char* strchrs(const char* s, const char* chrs) {
401    if (s==NULL || chrs==NULL || *chrs=='\0' || *s=='\0')
402           return NULL;
403    unsigned int l=strlen(s);
404    unsigned int r=strcspn(s, chrs);
405    if (r==l) return NULL;
406 <  return (s+r);
406 >  return ((char*)s+r);
407   }
408  
409   char* upCase(const char* str) {
# Line 379 | Line 443
443  
444  
445   //test if a char is in a given string (set)
446 < bool chrInStr(char c, char* str) {
446 > bool chrInStr(char c, const char* str) {
447   if (str==NULL || *str=='\0') return false;
448 < for (char* p=str; (*p)!='\0'; p++) {
448 > for (const char* p=str; (*p)!='\0'; p++) {
449     if ((*p)==c) return true;
450     }
451   return false;
# Line 389 | Line 453
453  
454  
455  
456 < char* rstrfind(char* str, const char* substr) {
456 > char* rstrfind(const char* str, const char* substr) {
457   /* like rindex() for a string */
458   int l,i;
459   if (str==NULL || *str=='\0') return NULL;
460   if (substr==NULL || *substr=='\0') return NULL;
461   l=strlen(substr);
462 < char* p=str+strlen(str)-l;
462 > char* p=(char*)str+strlen(str)-l;
463     //rightmost position that could match
464  
465   while (p>=str) {
466 <    for (i=0; i<l && *(p+i) == *(substr+i); i++);
466 >    for (i=0; i<l && *(p+i) == *(substr+i); i++) ;
467      if (i==l) return p; //found!
468      p--;
469      }
# Line 407 | Line 471
471   }
472  
473  
474 < char* strifind(char* str,  const char* substr) {
474 > char* strifind(const char* str,  const char* substr) {
475   // the case insensitive version of strstr -- finding a string within a strin
476    int l,i;
477    if (str==NULL || *str==0) return NULL;
478    if (substr==NULL || *substr==0) return NULL;
479    l=strlen(substr);
480 <  char* smax=str+strlen(str)-l;
480 >  char* smax=(char*)str+strlen(str)-l;
481    //rightmost position that could match
482 <  char* p=str;
482 >  char* p=(char*)str;
483    while (p<=smax) {
484 <     for (i=0; i<l && tolower(*(p+i))==tolower(*(substr+i)); i++);
484 >     for (i=0; i<l && tolower(*(p+i))==tolower(*(substr+i)); i++) ;
485       if (i==l) return p; //found!
486       p++;
487       }
# Line 427 | Line 491
491  
492  
493   // tests if string s has the given prefix
494 < bool startsWith(char* s, const char* prefix) {
494 > bool startsWith(const char* s, const char* prefix) {
495   if (prefix==NULL || s==NULL) return false;
496   int i=0;
497   while (prefix[i]!='\0' && prefix[i]==s[i]) i++;
498   return (prefix[i]=='\0');
499   }
500  
501 + // tests if string s ends with given suffix
502 + bool endsWith(const char* s, const char* suffix) {
503 + if (suffix==NULL || s==NULL) return false;
504 + if (suffix[0]==0) return true; //special case: empty suffix
505 + int j=strlen(suffix)-1;
506 + int i=strlen(s)-1;
507 + if (i<j) return false;
508 + while (j>=0 && s[i]==suffix[j]) { i--; j--; }
509 + return (j==-1);
510 + }
511 +
512  
513   char* reverseChars(char* str, int slen) {
514    if (slen==0) slen=strlen(str);
515    int l=0;
516    int r=slen-1;
517 <  register char c;
517 >  char c;
518    while (l<r) {
519       c=str[l];str[l]=str[r];
520       str[r]=c;
446     //swap(str[l],str[r]);
521       l++;r--;
522       }
523    return str;
524   }
525  
526  
527 < char* rstrstr(char* rstart, char *lend, char* substr) {  /*like strstr, but starts searching
527 > char* rstrstr(const char* rstart, const char *lend, const char* substr) {  /*like strstr, but starts searching
528   from right end, going up to lend and returns a pointer to the last (right)
529   matching character in str */
530   char *p;
531   int l,i;
532   l=strlen(substr);
533 < p=rstart-l+1;
533 > p=(char*)rstart-l+1;
534   while (p>=lend) {
535      for (i=0;i<l;i++) if (*(p+i) != *(substr+i)) break;
536      if (i==l) return p+l-1;
# Line 480 | Line 554
554    return h;
555    }
556  
557 < // removes the directory part from a full-path file name
557 > // removes the last part (file or directory name) of a full path
558   // this is a destructive operation for the given string!!!
559   // the trailing '/' is guaranteed to be there
560   void delFileName(char* filepath) {
561   char *p, *sep;
562   if (filepath==NULL) return;
563   for (p=filepath, sep=filepath;*p!='\0';p++)
564 <     if (*p==CHPATHSEP) sep=p+1;
564 >     if (*p=='/' || *p=='\\') sep=p+1;
565   *sep='\0'; // truncate filepath
566   }
567  
568 < // returns a pointer to the file name part in a full-path filename
569 < char* getFileName(char* filepath) {
570 < char *p, *sep;
568 > // returns a pointer to the last file or directory name in a full path
569 > const char* getFileName(const char* filepath) {
570 > const char *p, *sep;
571   if (filepath==NULL) return NULL;
572   for (p=filepath, sep=filepath;*p!='\0';p++)
573 <     if (*p==CHPATHSEP) sep=p+1;
573 >     if (*p=='/' || *p=='\\') sep=p+1;
574   return sep;
575   }
576  
577 + // returns a pointer to the file "extension" part in a filename
578 + const char* getFileExt(const char* filepath) {
579 + const char *p, *dp, *sep;
580 + if (filepath==NULL) return NULL;
581 + for (p=filepath, dp=filepath, sep=filepath;*p!='\0';p++) {
582 +     if (*p=='.') dp=p+1;
583 +       else if (*p=='/' || *p=='\\')
584 +                  sep=p+1;
585 +     }
586 + return (dp>sep) ? dp : NULL ;
587 + }
588 +
589   int fileExists(const char* fname) {
590    struct stat stFileInfo;
591    int r=0;
# Line 527 | Line 613
613    return true;
614   }
615   */
616 < off_t fileSize(const char* fpath) {
616 > int64 fileSize(const char* fpath) {
617    struct stat results;
618    if (stat(fpath, &results) == 0)
619        // The size of the file in bytes is in
620 <      return results.st_size;
620 >      return (int64)results.st_size;
621    else
622        // An error occurred
623 <    //GError("Error at stat(%s)!\n", fpath)
623 >    //GMessage("Error at stat(%s)!\n", fpath);
624      return 0;
625   }
626  
# Line 620 | Line 706
706   return true;
707   }
708  
709 + //write a formatted fasta record, fasta formatted
710 + void writeFasta(FILE *fw, const char* seqid, const char* descr,
711 +        const char* seq, int linelen, int seqlen) {
712 +  fflush(fw);
713 +  // write header line only if given!
714 +  if (seqid!=NULL) {
715 +    if (descr==NULL || descr[0]==0)
716 +             fprintf(fw,">%s\n",seqid);
717 +        else fprintf(fw,">%s %s\n",seqid, descr);
718 +    }
719 +  fflush(fw);
720 +  if (seq==NULL || *seq==0) return; //nothing to print
721 +  if (linelen==0) { //unlimited line length: write the whole sequence on a line
722 +     if (seqlen>0)
723 +             fwrite((const void*)seq, 1, seqlen,fw);
724 +        else fprintf(fw,"%s",seq);
725 +     fprintf(fw,"\n");
726 +     fflush(fw);
727 +     return;
728 +     }
729 +  int ilen=0;
730 +  if (seqlen>0) { //seq length given, so we know when to stop
731 +    for (int i=0; i < seqlen; i++, ilen++) {
732 +            if (ilen == linelen) {
733 +                 fputc('\n', fw);
734 +                 ilen = 0;
735 +                 }
736 +            fputc(seq[i], fw);
737 +            }
738 +    fputc('\n', fw);
739 +    }
740 +  else { //seq length not given, stop when 0 encountered
741 +    for (int i=0; seq[i]!=0; i++, ilen++) {
742 +            if (ilen == linelen) {
743 +                 fputc('\n', fw);
744 +                 ilen = 0;
745 +                 }
746 +            fputc(seq[i], fw);
747 +            } //for
748 +    fputc('\n', fw);
749 +    }
750 +  fflush(fw);
751 + }
752 +
753 + char* commaprint(uint64 n) {
754 +  static int comma = '\0';
755 +  static char retbuf[48];
756 +  char *p = &retbuf[sizeof(retbuf)-1];
757 +  int i = 0;
758 +  if(comma == '\0') {
759 +    /* struct lconv *lcp = localeconv();
760 +    if(lcp != NULL) {
761 +      if(lcp->thousands_sep != NULL &&
762 +        *lcp->thousands_sep != '\0')
763 +        comma = *lcp->thousands_sep;
764 +      else  */
765 +                          comma = ',';
766 +     // }
767 +    }
768 +  *p = '\0';
769 +  do {
770 +    if(i%3 == 0 && i != 0)
771 +      *--p = comma;
772 +    *--p = '0' + n % 10;
773 +    n /= 10;
774 +    i++;
775 +  } while(n != 0);
776 +  return p;
777 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines