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 +  #ifdef _fseeki64
29 +    #define fseeko(stream, offset, origin) _fseeki64(stream, offset, origin)
30 +  #else
31 +    /*
32 +    #define _DEFINE_WIN32_FSEEKO
33 +    int fseeko(FILE *stream, off_t offset, int whence);
34 +    */
35 +    #define fseeko fseek
36 +  #endif
37 +  #ifdef _ftelli64
38 +    #define ftello(stream) _ftelli64(stream)
39 +  #else
40 +    /*
41 +    #define _DEFINE_WIN32_FTELLO
42 +    off_t ftello(FILE *stream);
43 +    */
44 +    #define ftello ftell
45 +  #endif
46 + #else
47 +  #define CHPATHSEP '/'
48 +  #include <unistd.h>
49   #endif
50  
51 +
52   #ifdef DEBUG
53   #undef NDEBUG
54   #endif
55  
56 < typedef unsigned int uint32;
57 < typedef int int32;
56 > typedef int32_t int32;
57 > typedef uint32_t uint32;
58 >
59   typedef unsigned char uchar;
60   typedef unsigned char byte;
61  
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
62   #ifndef MAXUINT
63   #define MAXUINT ((unsigned int)-1)
64   #endif
65  
66 < #if defined(_NATIVE_64) || defined(_LP64) || defined(__LP64__)
67 < typedef long int64;
68 < typedef unsigned long uint64;
69 < #else
70 < //assume 32bit environment with long long for int64 stuff
71 < typedef long long int64;
40 < typedef unsigned long long uint64;
66 > #ifndef MAXINT
67 > #define MAXINT INT_MAX
68 > #endif
69 >
70 > #ifndef MAX_UINT
71 > #define MAX_UINT ((unsigned int)-1)
72   #endif
73  
74 + #ifndef MAX_INT
75 + #define MAX_INT INT_MAX
76 + #endif
77 +
78 + typedef int64_t int64;
79 + typedef uint64_t uint64;
80 +
81   /****************************************************************************/
82  
83   #ifndef EXIT_FAILURE
# Line 52 | Line 90
90  
91   /****************************************************************************/
92   #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
93  
94   //-------------------
95  
# Line 113 | Line 142
142                                       GError(ERR_ALLOC)
143   #define GFREE(ptr)       GFree((pointer*)(&ptr))
144  
145 < inline char* min(char *arg1, char *arg2) {
145 > inline char* strMin(char *arg1, char *arg2) {
146      return (strcmp(arg1, arg2) < 0)? arg1 : arg2;
147   }
148  
149 + inline char* strMax(char *arg1, char *arg2) {
150 +    return (strcmp(arg2, arg1) < 0)? arg1 : arg2;
151 + }
152 +
153   inline int iround(double x) {
154     return (int)floor(x + 0.5);
155   }
156  
124
157   /****************************************************************************/
158  
127 inline char* max(char *arg1, char *arg2) {
128    return (strcmp(arg2, arg1) < 0)? arg1 : arg2;
129 }
130
159   inline int Gintcmp(int a, int b) {
160   //return (a>b)? 1 : ((a==b)?0:-1);
161    return a-b;
# Line 139 | Line 167
167   int Gstricmp(const char* a, const char* b);
168  
169   inline void swap(int &arg1, int &arg2){
170 < arg1 ^= arg2 ^= arg1 ^= arg2;
170 > //arg1 ^= arg2;
171 > //arg2 ^= arg1;
172 > //arg1 ^= arg2;
173 > register int swp=arg1;
174 > arg1=arg2; arg2=swp;
175   }
176  
177 < inline void swap(char* &arg1, char* &arg2){
177 > inline void swap(char* &arg1, char* &arg2){ //swap pointers!
178   register char* swp=arg1;
179   arg1=arg2; arg2=swp;
180   }
181  
182 < inline void swap(unsigned int &arg1, unsigned int &arg2)
183 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
184 <
185 < inline void swap(short &arg1, short &arg2)
186 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
187 <
188 < inline void swap(unsigned short &arg1, unsigned short &arg2)
189 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
190 <
191 < inline void swap(long &arg1, long &arg2)
192 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
193 <
194 < inline void swap(unsigned long &arg1, unsigned long &arg2)
195 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
196 <
197 < inline void swap(char &arg1, char &arg2)
198 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
199 <
200 < inline void swap(unsigned char &arg1, unsigned char &arg2)
201 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
202 <
203 < inline void swap(bool &arg1, bool &arg2)
204 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
205 <
182 > inline void swap(uint &arg1, uint &arg2) {
183 >  register uint swp=arg1;
184 >  arg1=arg2; arg2=swp;
185 >  }
186 >
187 > inline void swap(short &arg1, short &arg2) {
188 >  register short swp=arg1;
189 >  arg1=arg2; arg2=swp;
190 >  }
191 >
192 > inline void swap(unsigned short &arg1, unsigned short &arg2) {
193 >  register unsigned short swp=arg1;
194 >  arg1=arg2; arg2=swp;
195 >  }
196 >
197 > inline void swap(long &arg1, long &arg2) {
198 >  register long swp=arg1;
199 >  arg1=arg2; arg2=swp;
200 >  }
201 >
202 > inline void swap(unsigned long &arg1, unsigned long &arg2) {
203 >  register unsigned long swp=arg1;
204 >  arg1=arg2; arg2=swp;
205 >  }
206 >
207 >
208 > inline void swap(char &arg1, char &arg2) {
209 >  register char swp=arg1;
210 >  arg1=arg2; arg2=swp;
211 >  }
212 >
213 > inline void swap(unsigned char &arg1, unsigned char &arg2) {
214 >  register unsigned char swp=arg1;
215 >  arg1=arg2; arg2=swp;
216 >  }
217  
218   /**************** Memory management ***************************/
219  
# Line 179 | Line 222
222   bool GRealloc(pointer* ptr,unsigned long size); // Resize memory
223   void GFree(pointer* ptr); // Free memory, resets ptr to NULL
224  
225 < /********************* debug functions *********************/
225 >
226 > int saprintf(char **retp, const char *fmt, ...);
227  
228   void GError(const char* format,...); // Error routine (aborts program)
229   void GMessage(const char* format,...);// Log message to stderr
230   // Assert failed routine:- usually not called directly but through GASSERT
231   void GAssert(const char* expression, const char* filename, unsigned int lineno);
232  
189
233   // ****************** string manipulation *************************
234   char *Gstrdup(const char* str);
235   //duplicate a string by allocating a copy for it and returning it
# Line 216 | Line 259
259                    void *part, unsigned int partlen);
260  
261   // test if a char is in a string:
262 < bool chrInStr(char c, char* str);
262 > bool chrInStr(char c, const char* str);
263  
264   char* rstrchr(char* str, char ch);
265   /* returns a pointer to the rightmost
266    occurence of ch in str - like rindex for platforms missing it*/
267  
268 < char* strchrs(char* s, const char* chrs);
268 > char* strchrs(const char* s, const char* chrs);
269   //strchr but with a set of chars instead of only one
270  
271 < char* rstrfind(char* str, const char *substr); /* like rindex() but for strings
271 > char* rstrfind(const char* str, const char *substr); /* like rindex() but for strings
272   or like the right side version of strstr()
273   */
274   //reverse character string or
275   char* reverseChars(char* str, int slen=0);
276  
277 < char* rstrstr(char* rstart, char *lend, char* substr);
277 > char* rstrstr(const char* rstart, const char *lend, const char* substr);
278   /*the reversed, rightside equivalent of strstr: starts searching
279   from right end (rstart), going back to left end (lend) and returns
280   a pointer to the last (right) matching character in str */
281  
282 < char* strifind(char* str,  const char* substr);
282 > char* strifind(const char* str,  const char* substr);
283   // the case insensitive version of strstr -- finding a string within a strin
284  
285  
286   //Determines if a string begins with a given prefix
287   //(returns false when any of the params is NULL,
288   // but true when prefix is '' (empty string)!)
289 < bool startsWith(char* s, const char* prefix);
289 > bool startsWith(const char* s, const char* prefix);
290 >
291 > bool endsWith(const char* s, const char* suffix);
292 > //Note: returns true if suffix is empty string, but false if it's NULL
293 >
294  
295   // ELF hash function for strings
296   int strhash(const char* str);
# Line 263 | Line 310
310    //check for overlap with other segment
311    uint len() { return end-start+1; }
312    bool overlap(GSeg* d) {
313 <     return start<d->start ? (d->start<=end) : (start<=d->end);
313 >     //return start<d->start ? (d->start<=end) : (start<=d->end);
314 >     return (start<=d->end && end>=d->start);
315       }
316  
317    bool overlap(GSeg& d) {
318 <     return start<d.start ? (d.start<=end) : (start<=d.end);
318 >     //return start<d.start ? (d.start<=end) : (start<=d.end);
319 >     return (start<=d.end && end>=d.start);
320       }
321  
322    bool overlap(GSeg& d, int fuzz) {
323 <     return start<d.start ? (d.start<=end+fuzz) : (start<=d.end+fuzz);
323 >     //return start<d.start ? (d.start<=end+fuzz) : (start<=d.end+fuzz);
324 >     return (start<=d.end+fuzz && end+fuzz>=d.start);
325       }
326  
327    bool overlap(uint s, uint e) {
328 <    if (s>e) { swap(s,e); }
329 <     return start<s ? (s<=end) : (start<=e);
328 >     if (s>e) { swap(s,e); }
329 >     //return start<s ? (s<=end) : (start<=e);
330 >     return (start<=e && end>=s);
331       }
332  
333    //return the length of overlap between two segments
# Line 328 | Line 379
379  
380   //GLineReader -- text line reading/buffering class
381   class GLineReader {
382 +   bool closeFile;
383     int len;
384     int allocated;
385     char* buf;
# Line 340 | Line 392
392     char* chars() { return buf; }
393     char* line() { return buf; }
394     int readcount() { return lcount; } //number of lines read
395 +   void setFile(FILE* stream) { file=stream; }
396     int length() { return len; }
397     int size() { return len; } //same as size();
398     bool isEof() {return isEOF; }
# Line 356 | Line 409
409                             // the given file position
410     void pushBack() { if (lcount>0) pushed=true; } // "undo" the last getLine request
411              // so the next call will in fact return the same line
412 +   GLineReader(const char* fname) {
413 +      FILE* f=fopen(fname, "rb");
414 +      if (f==NULL) GError("Error opening file '%s'!\n",fname);
415 +      closeFile=true;
416 +      init(f);
417 +      }
418     GLineReader(FILE* stream=NULL, off_t fpos=0) {
419 +     closeFile=false;
420 +     init(stream,fpos);
421 +     }
422 +   void init(FILE* stream, off_t fpos=0) {
423       len=0;
424       isEOF=false;
425       allocated=1024;
# Line 369 | Line 432
432       }
433     ~GLineReader() {
434       GFREE(buf);
435 +     if (closeFile) fclose(file);
436       }
437   };
438  
# Line 379 | Line 443
443    */
444   char* fgetline(char* & buf, int& buflen, FILE* stream, off_t* f_pos=NULL, int* linelen=NULL);
445  
446 +
447 + //print int/values nicely formatted in 3-digit groups
448 + char* commaprint(uint64 n);
449 +
450   /*********************** File management functions *********************/
451  
452 < // removes the directory part from a full-path file name
453 < // this is a destructive operation for the given string!
452 > // removes the last part (file or directory name) of a full path
453 > // WARNING: this is a destructive operation for the given string!
454   void delFileName(char* filepath);
455  
456 < // returns a pointer to the file name part in a full-path filename
457 < char* getFileName(char* filepath);
456 > // returns a pointer to the last file or directory name in a full path
457 > const char* getFileName(const char* filepath);
458 > // returns a pointer to the file "extension" part in a filename
459 > const char* getFileExt(const char* filepath);
460 >
461  
462   int fileExists(const char* fname);
463   //returns 0 if file entry doesn't exist
# Line 394 | Line 465
465   //        2 if it's a regular file
466   //        3 otherwise (?)
467  
468 < off_t fileSize(const char* fpath);
468 > int64 fileSize(const char* fpath);
469 >
470 > //write a formatted fasta record, fasta formatted
471 > void writeFasta(FILE *fw, const char* seqid, const char* descr,
472 >        const char* seq, int linelen=60, int seqlen=0);
473  
474   //parses the next number found in a string at the current position
475   //until a non-digit (and not a '.', 'e','E','-','+') is encountered;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines