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 + #ifndef fseeko
52 + #define fseeko fseek
53 + #endif
54 + #ifndef ftello
55 + #define ftello ftell
56   #endif
57  
58   #ifdef DEBUG
59   #undef NDEBUG
60   #endif
61  
62 < typedef unsigned int uint32;
63 < typedef int int32;
62 > typedef int32_t int32;
63 > typedef uint32_t uint32;
64 >
65   typedef unsigned char uchar;
66   typedef unsigned char byte;
67  
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
68   #ifndef MAXUINT
69   #define MAXUINT ((unsigned int)-1)
70   #endif
71  
72 < #if defined(_NATIVE_64) || defined(_LP64) || defined(__LP64__)
73 < typedef long int64;
74 < typedef unsigned long uint64;
75 < #else
76 < //assume 32bit environment with long long for int64 stuff
77 < typedef long long int64;
40 < typedef unsigned long long uint64;
72 > #ifndef MAXINT
73 > #define MAXINT INT_MAX
74 > #endif
75 >
76 > #ifndef MAX_UINT
77 > #define MAX_UINT ((unsigned int)-1)
78   #endif
79  
80 + #ifndef MAX_INT
81 + #define MAX_INT INT_MAX
82 + #endif
83 +
84 + typedef int64_t int64;
85 + typedef uint64_t uint64;
86 +
87   /****************************************************************************/
88  
89   #ifndef EXIT_FAILURE
# Line 52 | Line 96
96  
97   /****************************************************************************/
98   #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
99  
100   //-------------------
101  
# Line 113 | Line 148
148                                       GError(ERR_ALLOC)
149   #define GFREE(ptr)       GFree((pointer*)(&ptr))
150  
151 < inline char* min(char *arg1, char *arg2) {
151 > inline char* strMin(char *arg1, char *arg2) {
152      return (strcmp(arg1, arg2) < 0)? arg1 : arg2;
153   }
154  
155 + inline char* strMax(char *arg1, char *arg2) {
156 +    return (strcmp(arg2, arg1) < 0)? arg1 : arg2;
157 + }
158 +
159   inline int iround(double x) {
160     return (int)floor(x + 0.5);
161   }
162  
124
163   /****************************************************************************/
164  
127 inline char* max(char *arg1, char *arg2) {
128    return (strcmp(arg2, arg1) < 0)? arg1 : arg2;
129 }
130
165   inline int Gintcmp(int a, int b) {
166   //return (a>b)? 1 : ((a==b)?0:-1);
167    return a-b;
# Line 139 | Line 173
173   int Gstricmp(const char* a, const char* b);
174  
175   inline void swap(int &arg1, int &arg2){
176 < arg1 ^= arg2 ^= arg1 ^= arg2;
176 > //arg1 ^= arg2;
177 > //arg2 ^= arg1;
178 > //arg1 ^= arg2;
179 > register int swp=arg1;
180 > arg1=arg2; arg2=swp;
181   }
182  
183 < inline void swap(char* &arg1, char* &arg2){
183 > inline void swap(char* &arg1, char* &arg2){ //swap pointers!
184   register char* swp=arg1;
185   arg1=arg2; arg2=swp;
186   }
187  
188 < inline void swap(unsigned int &arg1, unsigned int &arg2)
189 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
190 <
191 < inline void swap(short &arg1, short &arg2)
192 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
193 <
194 < inline void swap(unsigned short &arg1, unsigned short &arg2)
195 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
196 <
197 < inline void swap(long &arg1, long &arg2)
198 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
199 <
200 < inline void swap(unsigned long &arg1, unsigned long &arg2)
201 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
202 <
203 < inline void swap(char &arg1, char &arg2)
204 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
205 <
206 < inline void swap(unsigned char &arg1, unsigned char &arg2)
207 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
208 <
209 < inline void swap(bool &arg1, bool &arg2)
210 < { arg1 ^= arg2 ^= arg1 ^= arg2; }
211 <
188 > inline void swap(uint &arg1, uint &arg2) {
189 >  register uint swp=arg1;
190 >  arg1=arg2; arg2=swp;
191 >  }
192 >
193 > inline void swap(short &arg1, short &arg2) {
194 >  register short swp=arg1;
195 >  arg1=arg2; arg2=swp;
196 >  }
197 >
198 > inline void swap(unsigned short &arg1, unsigned short &arg2) {
199 >  register unsigned short swp=arg1;
200 >  arg1=arg2; arg2=swp;
201 >  }
202 >
203 > inline void swap(long &arg1, long &arg2) {
204 >  register long swp=arg1;
205 >  arg1=arg2; arg2=swp;
206 >  }
207 >
208 > inline void swap(unsigned long &arg1, unsigned long &arg2) {
209 >  register unsigned long swp=arg1;
210 >  arg1=arg2; arg2=swp;
211 >  }
212 >
213 >
214 > inline void swap(char &arg1, char &arg2) {
215 >  register char swp=arg1;
216 >  arg1=arg2; arg2=swp;
217 >  }
218 >
219 > inline void swap(unsigned char &arg1, unsigned char &arg2) {
220 >  register unsigned char swp=arg1;
221 >  arg1=arg2; arg2=swp;
222 >  }
223  
224   /**************** Memory management ***************************/
225  
# Line 179 | Line 228
228   bool GRealloc(pointer* ptr,unsigned long size); // Resize memory
229   void GFree(pointer* ptr); // Free memory, resets ptr to NULL
230  
231 < /********************* debug functions *********************/
231 >
232 > int saprintf(char **retp, const char *fmt, ...);
233  
234   void GError(const char* format,...); // Error routine (aborts program)
235   void GMessage(const char* format,...);// Log message to stderr
236   // Assert failed routine:- usually not called directly but through GASSERT
237   void GAssert(const char* expression, const char* filename, unsigned int lineno);
238  
189
239   // ****************** string manipulation *************************
240   char *Gstrdup(const char* str);
241   //duplicate a string by allocating a copy for it and returning it
# Line 216 | Line 265
265                    void *part, unsigned int partlen);
266  
267   // test if a char is in a string:
268 < bool chrInStr(char c, char* str);
268 > bool chrInStr(char c, const char* str);
269  
270   char* rstrchr(char* str, char ch);
271   /* returns a pointer to the rightmost
272    occurence of ch in str - like rindex for platforms missing it*/
273  
274 < char* strchrs(char* s, const char* chrs);
274 > char* strchrs(const char* s, const char* chrs);
275   //strchr but with a set of chars instead of only one
276  
277 < char* rstrfind(char* str, const char *substr); /* like rindex() but for strings
277 > char* rstrfind(const char* str, const char *substr); /* like rindex() but for strings
278   or like the right side version of strstr()
279   */
280   //reverse character string or
281   char* reverseChars(char* str, int slen=0);
282  
283 < char* rstrstr(char* rstart, char *lend, char* substr);
283 > char* rstrstr(const char* rstart, const char *lend, const char* substr);
284   /*the reversed, rightside equivalent of strstr: starts searching
285   from right end (rstart), going back to left end (lend) and returns
286   a pointer to the last (right) matching character in str */
287  
288 < char* strifind(char* str,  const char* substr);
288 > char* strifind(const char* str,  const char* substr);
289   // the case insensitive version of strstr -- finding a string within a strin
290  
291  
292   //Determines if a string begins with a given prefix
293   //(returns false when any of the params is NULL,
294   // but true when prefix is '' (empty string)!)
295 < bool startsWith(char* s, const char* prefix);
295 > bool startsWith(const char* s, const char* prefix);
296 >
297 > bool endsWith(const char* s, const char* suffix);
298 > //Note: returns true if suffix is empty string, but false if it's NULL
299 >
300  
301   // ELF hash function for strings
302   int strhash(const char* str);
# Line 263 | Line 316
316    //check for overlap with other segment
317    uint len() { return end-start+1; }
318    bool overlap(GSeg* d) {
319 <     return start<d->start ? (d->start<=end) : (start<=d->end);
319 >     //return start<d->start ? (d->start<=end) : (start<=d->end);
320 >     return (start<=d->end && end>=d->start);
321       }
322  
323    bool overlap(GSeg& d) {
324 <     return start<d.start ? (d.start<=end) : (start<=d.end);
324 >     //return start<d.start ? (d.start<=end) : (start<=d.end);
325 >     return (start<=d.end && end>=d.start);
326       }
327  
328    bool overlap(GSeg& d, int fuzz) {
329 <     return start<d.start ? (d.start<=end+fuzz) : (start<=d.end+fuzz);
329 >     //return start<d.start ? (d.start<=end+fuzz) : (start<=d.end+fuzz);
330 >     return (start<=d.end+fuzz && end+fuzz>=d.start);
331       }
332  
333    bool overlap(uint s, uint e) {
334 <    if (s>e) { swap(s,e); }
335 <     return start<s ? (s<=end) : (start<=e);
334 >     if (s>e) { swap(s,e); }
335 >     //return start<s ? (s<=end) : (start<=e);
336 >     return (start<=e && end>=s);
337       }
338  
339    //return the length of overlap between two segments
# Line 328 | Line 385
385  
386   //GLineReader -- text line reading/buffering class
387   class GLineReader {
388 +   bool closeFile;
389     int len;
390     int allocated;
391     char* buf;
# Line 340 | Line 398
398     char* chars() { return buf; }
399     char* line() { return buf; }
400     int readcount() { return lcount; } //number of lines read
401 +   void setFile(FILE* stream) { file=stream; }
402     int length() { return len; }
403     int size() { return len; } //same as size();
404     bool isEof() {return isEOF; }
# Line 356 | Line 415
415                             // the given file position
416     void pushBack() { if (lcount>0) pushed=true; } // "undo" the last getLine request
417              // so the next call will in fact return the same line
418 +   GLineReader(const char* fname) {
419 +      FILE* f=fopen(fname, "rb");
420 +      if (f==NULL) GError("Error opening file '%s'!\n",fname);
421 +      closeFile=true;
422 +      init(f);
423 +      }
424     GLineReader(FILE* stream=NULL, off_t fpos=0) {
425 +     closeFile=false;
426 +     init(stream,fpos);
427 +     }
428 +   void init(FILE* stream, off_t fpos=0) {
429       len=0;
430       isEOF=false;
431       allocated=1024;
# Line 369 | Line 438
438       }
439     ~GLineReader() {
440       GFREE(buf);
441 +     if (closeFile) fclose(file);
442       }
443   };
444  
# Line 379 | Line 449
449    */
450   char* fgetline(char* & buf, int& buflen, FILE* stream, off_t* f_pos=NULL, int* linelen=NULL);
451  
452 +
453 + //print int/values nicely formatted in 3-digit groups
454 + char* commaprint(uint64 n);
455 +
456   /*********************** File management functions *********************/
457  
458 < // removes the directory part from a full-path file name
459 < // this is a destructive operation for the given string!
458 > // removes the last part (file or directory name) of a full path
459 > // WARNING: this is a destructive operation for the given string!
460   void delFileName(char* filepath);
461  
462 < // returns a pointer to the file name part in a full-path filename
463 < char* getFileName(char* filepath);
462 > // returns a pointer to the last file or directory name in a full path
463 > const char* getFileName(const char* filepath);
464 > // returns a pointer to the file "extension" part in a filename
465 > const char* getFileExt(const char* filepath);
466 >
467  
468   int fileExists(const char* fname);
469   //returns 0 if file entry doesn't exist
# Line 394 | Line 471
471   //        2 if it's a regular file
472   //        3 otherwise (?)
473  
474 < off_t fileSize(const char* fpath);
474 > int64 fileSize(const char* fpath);
475 >
476 > //write a formatted fasta record, fasta formatted
477 > void writeFasta(FILE *fw, const char* seqid, const char* descr,
478 >        const char* seq, int linelen=60, int seqlen=0);
479  
480   //parses the next number found in a string at the current position
481   //until a non-digit (and not a '.', 'e','E','-','+') is encountered;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines