ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GBase.h
(Generate patch)
# Line 1 | Line 1
1   #ifndef G_BASE_DEFINED
2   #define G_BASE_DEFINED
3 <
3 > #ifndef _POSIX_SOURCE
4 > //mostly for MinGW
5 > #define _POSIX_SOURCE
6 > #endif
7 > #ifdef HAVE_CONFIG_H
8 > #include "config.h"
9 > #endif
10   #include <string.h>
11   #include <stdlib.h>
12   #include <stdio.h>
# Line 8 | Line 14
14   #include <limits.h>
15   #include <sys/types.h>
16   #include <sys/stat.h>
17 < #if defined __WIN32__ || defined _WIN32
17 > #include <stdint.h>
18 >
19 > #if defined __WIN32__ || defined WIN32 || defined _WIN32 || defined _WIN32_
20 >  #ifndef __WIN32__
21 >    #define __WIN32__
22 >  #endif
23    #include <windows.h>
24 +  #include <io.h>
25 +  #define CHPATHSEP '\\'
26 +  #undef off_t
27 +  #define off_t int64_t
28 +  #ifndef popen
29 +   #define popen _popen
30 +  #endif
31 +  #ifdef _fseeki64
32 +    #define fseeko(stream, offset, origin) _fseeki64(stream, offset, origin)
33 +  #else
34 +    /*
35 +    #define _DEFINE_WIN32_FSEEKO
36 +    int fseeko(FILE *stream, off_t offset, int whence);
37 +    */
38 +    #define fseeko fseek
39 +  #endif
40 +  #ifdef _ftelli64
41 +    #define ftello(stream) _ftelli64(stream)
42 +  #else
43 +    /*
44 +    #define _DEFINE_WIN32_FTELLO
45 +    off_t ftello(FILE *stream);
46 +    */
47 +    #define ftello ftell
48 +  #endif
49 + #else
50 +  #define CHPATHSEP '/'
51 +  #include <unistd.h>
52 + #endif
53 +
54 + #ifndef fseeko
55 + #define fseeko fseek
56 + #endif
57 + #ifndef ftello
58 + #define ftello ftell
59   #endif
60  
61   #ifdef DEBUG
62   #undef NDEBUG
63   #endif
64  
65 < typedef unsigned int uint32;
66 < typedef int int32;
65 > typedef int32_t int32;
66 > typedef uint32_t uint32;
67 > typedef int16_t int16;
68 > typedef uint16_t uint16;
69 >
70   typedef unsigned char uchar;
71   typedef unsigned char byte;
72  
24 // If long is natively 64 bit, use the regular fseek and ftell
25 #ifdef _NATIVE_64
26 #define ftello ftell
27 #define fseeko fseek
28 #endif
29
73   #ifndef MAXUINT
74   #define MAXUINT ((unsigned int)-1)
75   #endif
76  
77 < #if defined(_NATIVE_64) || defined(_LP64) || defined(__LP64__)
78 < typedef long int64;
79 < typedef unsigned long uint64;
80 < #else
81 < //assume 32bit environment with long long for int64 stuff
82 < typedef long long int64;
40 < typedef unsigned long long uint64;
77 > #ifndef MAXINT
78 > #define MAXINT INT_MAX
79 > #endif
80 >
81 > #ifndef MAX_UINT
82 > #define MAX_UINT ((unsigned int)-1)
83   #endif
84  
85 + #ifndef MAX_INT
86 + #define MAX_INT INT_MAX
87 + #endif
88 +
89 + typedef int64_t int64;
90 + typedef uint64_t uint64;
91 +
92   /****************************************************************************/
93  
94   #ifndef EXIT_FAILURE
# Line 52 | Line 101
101  
102   /****************************************************************************/
103   #define ERR_ALLOC "Error allocating memory.\n"
55 #if defined (__WIN32__) || defined (WIN32)
56  #define CHPATHSEP '\\'
57  #include <io.h>
58  #define ftello ftell
59  #define fseeko fseek
60 #else
61  #define CHPATHSEP '/'
62  #include <unistd.h>
63 #endif
104  
105   //-------------------
106  
# Line 113 | Line 153
153                                       GError(ERR_ALLOC)
154   #define GFREE(ptr)       GFree((pointer*)(&ptr))
155  
156 < inline char* min(char *arg1, char *arg2) {
156 > inline char* strMin(char *arg1, char *arg2) {
157      return (strcmp(arg1, arg2) < 0)? arg1 : arg2;
158   }
159  
160 + inline char* strMax(char *arg1, char *arg2) {
161 +    return (strcmp(arg2, arg1) < 0)? arg1 : arg2;
162 + }
163 +
164   inline int iround(double x) {
165     return (int)floor(x + 0.5);
166   }
167  
124
168   /****************************************************************************/
169  
127 inline char* max(char *arg1, char *arg2) {
128    return (strcmp(arg2, arg1) < 0)? arg1 : arg2;
129 }
130
170   inline int Gintcmp(int a, int b) {
171   //return (a>b)? 1 : ((a==b)?0:-1);
172    return a-b;
173   }
174  
175 < int Gstrcmp(char* a, char* b);
175 > int Gstrcmp(const char* a, const char* b, int n=-1);
176   //same as strcmp but doesn't crash on NULL pointers
177  
178 < int Gstricmp(const char* a, const char* b);
140 <
141 < inline void swap(int &arg1, int &arg2){
142 < arg1 ^= arg2 ^= arg1 ^= arg2;
143 < }
144 <
145 < inline void swap(char* &arg1, char* &arg2){
146 < register char* swp=arg1;
147 < arg1=arg2; arg2=swp;
148 < }
149 <
150 < inline void swap(unsigned int &arg1, unsigned int &arg2)
151 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
152 <
153 < inline void swap(short &arg1, short &arg2)
154 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
155 <
156 < inline void swap(unsigned short &arg1, unsigned short &arg2)
157 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
158 <
159 < inline void swap(long &arg1, long &arg2)
160 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
161 <
162 < inline void swap(unsigned long &arg1, unsigned long &arg2)
163 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
178 > int Gstricmp(const char* a, const char* b, int n=-1);
179  
180 < inline void swap(char &arg1, char &arg2)
181 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
182 <
183 < inline void swap(unsigned char &arg1, unsigned char &arg2)
184 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
185 <
186 < inline void swap(bool &arg1, bool &arg2)
172 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
180 > //basic swap template function
181 > template<class T> void Gswap(T& lhs, T& rhs) {
182 > //register T tmp=lhs;
183 > T tmp=lhs; //requires copy operator
184 > lhs=rhs;
185 > rhs=tmp;
186 > }
187  
188  
189   /**************** Memory management ***************************/
# Line 179 | Line 193
193   bool GRealloc(pointer* ptr,unsigned long size); // Resize memory
194   void GFree(pointer* ptr); // Free memory, resets ptr to NULL
195  
196 < /********************* debug functions *********************/
196 >
197 > int saprintf(char **retp, const char *fmt, ...);
198  
199   void GError(const char* format,...); // Error routine (aborts program)
200   void GMessage(const char* format,...);// Log message to stderr
201   // Assert failed routine:- usually not called directly but through GASSERT
202   void GAssert(const char* expression, const char* filename, unsigned int lineno);
203  
189
204   // ****************** string manipulation *************************
205   char *Gstrdup(const char* str);
206   //duplicate a string by allocating a copy for it and returning it
# Line 216 | Line 230
230                    void *part, unsigned int partlen);
231  
232   // test if a char is in a string:
233 < bool chrInStr(char c, char* str);
233 > bool chrInStr(char c, const char* str);
234  
235   char* rstrchr(char* str, char ch);
236   /* returns a pointer to the rightmost
237    occurence of ch in str - like rindex for platforms missing it*/
238  
239 < char* strchrs(char* s, const char* chrs);
239 > char* strchrs(const char* s, const char* chrs);
240   //strchr but with a set of chars instead of only one
241  
242 < char* rstrfind(char* str, const char *substr); /* like rindex() but for strings
243 < or like the right side version of strstr()
244 < */
245 < //reverse character string or
232 < char* reverseChars(char* str, int slen=0);
242 > char* rstrfind(const char* str, const char *substr);
243 > // like rindex() but for strings;  right side version of strstr()
244 >
245 > char* reverseChars(char* str, int slen=0); //in place reversal of string
246  
247 < char* rstrstr(char* rstart, char *lend, char* substr);
247 > char* rstrstr(const char* rstart, const char *lend, const char* substr);
248   /*the reversed, rightside equivalent of strstr: starts searching
249   from right end (rstart), going back to left end (lend) and returns
250   a pointer to the last (right) matching character in str */
251  
252 < char* strifind(char* str,  const char* substr);
252 > char* strifind(const char* str,  const char* substr);
253   // the case insensitive version of strstr -- finding a string within a strin
254  
255  
256   //Determines if a string begins with a given prefix
257   //(returns false when any of the params is NULL,
258   // but true when prefix is '' (empty string)!)
259 < bool startsWith(char* s, const char* prefix);
259 > bool startsWith(const char* s, const char* prefix);
260 >
261 > bool endsWith(const char* s, const char* suffix);
262 > //Note: returns true if suffix is empty string, but false if it's NULL
263 >
264  
265   // ELF hash function for strings
266   int strhash(const char* str);
# Line 263 | Line 280
280    //check for overlap with other segment
281    uint len() { return end-start+1; }
282    bool overlap(GSeg* d) {
283 <     return start<d->start ? (d->start<=end) : (start<=d->end);
283 >     //return start<d->start ? (d->start<=end) : (start<=d->end);
284 >     return (start<=d->end && end>=d->start);
285       }
286  
287    bool overlap(GSeg& d) {
288 <     return start<d.start ? (d.start<=end) : (start<=d.end);
288 >     //return start<d.start ? (d.start<=end) : (start<=d.end);
289 >     return (start<=d.end && end>=d.start);
290       }
291  
292    bool overlap(GSeg& d, int fuzz) {
293 <     return start<d.start ? (d.start<=end+fuzz) : (start<=d.end+fuzz);
293 >     //return start<d.start ? (d.start<=end+fuzz) : (start<=d.end+fuzz);
294 >     return (start<=d.end+fuzz && end+fuzz>=d.start);
295       }
296  
297    bool overlap(uint s, uint e) {
298 <    if (s>e) { swap(s,e); }
299 <     return start<s ? (s<=end) : (start<=e);
298 >     if (s>e) { Gswap(s,e); }
299 >     //return start<s ? (s<=end) : (start<=e);
300 >     return (start<=e && end>=s);
301       }
302  
303    //return the length of overlap between two segments
# Line 291 | Line 312
312          }
313       }
314    int overlapLen(uint rstart, uint rend) {
315 <     if (rstart>rend) { swap(rstart,rend); }
315 >     if (rstart>rend) { Gswap(rstart,rend); }
316       if (start<rstart) {
317          if (rstart>end) return 0;
318          return (rend>end) ? end-rstart+1 : rend-rstart+1;
# Line 313 | Line 334
334    bool operator==(GSeg& d){
335        return (start==d.start && end==d.end);
336        }
316  bool operator>(GSeg& d){
317     return (start==d.start)?(end>d.end):(start>d.start);
318     }
337    bool operator<(GSeg& d){
338       return (start==d.start)?(end<d.end):(start<d.start);
339       }
# Line 328 | Line 346
346  
347   //GLineReader -- text line reading/buffering class
348   class GLineReader {
349 +   bool closeFile;
350     int len;
351     int allocated;
352     char* buf;
# Line 340 | Line 359
359     char* chars() { return buf; }
360     char* line() { return buf; }
361     int readcount() { return lcount; } //number of lines read
362 +   void setFile(FILE* stream) { file=stream; }
363     int length() { return len; }
364     int size() { return len; } //same as size();
365     bool isEof() {return isEOF; }
# Line 356 | Line 376
376                             // the given file position
377     void pushBack() { if (lcount>0) pushed=true; } // "undo" the last getLine request
378              // so the next call will in fact return the same line
379 +   GLineReader(const char* fname) {
380 +      FILE* f=fopen(fname, "rb");
381 +      if (f==NULL) GError("Error opening file '%s'!\n",fname);
382 +      closeFile=true;
383 +      init(f);
384 +      }
385     GLineReader(FILE* stream=NULL, off_t fpos=0) {
386 +     closeFile=false;
387 +     init(stream,fpos);
388 +     }
389 +   void init(FILE* stream, off_t fpos=0) {
390       len=0;
391       isEOF=false;
392       allocated=1024;
# Line 369 | Line 399
399       }
400     ~GLineReader() {
401       GFREE(buf);
402 +     if (closeFile) fclose(file);
403       }
404   };
405  
# Line 379 | Line 410
410    */
411   char* fgetline(char* & buf, int& buflen, FILE* stream, off_t* f_pos=NULL, int* linelen=NULL);
412  
413 +
414 + //print int/values nicely formatted in 3-digit groups
415 + char* commaprint(uint64 n);
416 +
417   /*********************** File management functions *********************/
418  
419 < // removes the directory part from a full-path file name
420 < // this is a destructive operation for the given string!
419 > // removes the last part (file or directory name) of a full path
420 > // WARNING: this is a destructive operation for the given string!
421   void delFileName(char* filepath);
422  
423 < // returns a pointer to the file name part in a full-path filename
424 < char* getFileName(char* filepath);
423 > // returns a pointer to the last file or directory name in a full path
424 > const char* getFileName(const char* filepath);
425 > // returns a pointer to the file "extension" part in a filename
426 > const char* getFileExt(const char* filepath);
427 >
428  
429   int fileExists(const char* fname);
430   //returns 0 if file entry doesn't exist
# Line 394 | Line 432
432   //        2 if it's a regular file
433   //        3 otherwise (?)
434  
435 < off_t fileSize(const char* fpath);
435 > int64 fileSize(const char* fpath);
436 >
437 > //write a formatted fasta record, fasta formatted
438 > void writeFasta(FILE *fw, const char* seqid, const char* descr,
439 >        const char* seq, int linelen=60, int seqlen=0);
440  
441   //parses the next number found in a string at the current position
442   //until a non-digit (and not a '.', 'e','E','-','+') is encountered;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines