-
Notifications
You must be signed in to change notification settings - Fork 13
/
GBase.h
769 lines (634 loc) · 22.6 KB
/
GBase.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#ifndef G_BASE_DEFINED
#define G_BASE_DEFINED
#define GCLIB_VERSION "0.12.8"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if defined(__WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_WIN64) || defined(__MINGW64__) || defined(__WINDOWS__)
#ifndef _WIN32
#define _WIN32
#endif
#ifndef _WIN64
#define _WIN64
#endif
#define __USE_MINGW_ANSI_STDIO 1
//#define __ISO_C_VISIBLE 1999
#endif
#define XSTR(x) STR(x)
#define STR(x) #x
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#define CHPATHSEP '\\'
#undef off_t
#define off_t int64_t
#ifndef popen
#define popen _popen
#endif
/*
#ifndef fseeko
#ifdef _fseeki64
#define fseeko(stream, offset, origin) _fseeki64(stream, offset, origin)
#else
#define fseeko fseek
#endif
#endif
#ifndef ftello
#ifdef _ftelli64
#define ftello(stream) _ftelli64(stream)
#else
#define ftello ftell
#endif
#endif
*/
#else
#define CHPATHSEP '/'
#ifdef __CYGWIN__
#define _BSD_SOURCE
#endif
#include <unistd.h>
#endif
#ifdef DEBUG
#undef NDEBUG
#define _DEBUG 1
#define _DEBUG_ 1
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdarg.h>
#include <type_traits>
typedef int64_t int64;
typedef uint64_t uint64;
typedef int32_t int32;
typedef uint32_t uint32;
typedef int16_t int16;
typedef uint16_t uint16;
typedef unsigned char uchar;
typedef uint8_t byte;
typedef unsigned int uint;
typedef void* pointer;
#ifndef MAXUINT
#define MAXUINT ((unsigned int)-1)
#endif
#ifndef MAXINT
#define MAXINT INT_MAX
#endif
#ifndef MAX_UINT
#define MAX_UINT ((unsigned int)-1)
#endif
#ifndef MAX_INT
#define MAX_INT INT_MAX
#endif
/****************************************************************************/
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
/****************************************************************************/
#define ERR_ALLOC "Error allocating memory.\n"
//-------------------
#define GEXIT(a) { \
fprintf(stderr, "Error: "); fprintf(stderr, a); \
GError("Exiting from line %i in file %s\n",__LINE__,__FILE__); \
}
// Debug helpers
#ifndef NDEBUG
#define GASSERT(exp) ((exp)?((void)0):(void)GAssert(#exp,__FILE__,__LINE__))
#define GVERIFY(condition) \
if (!(condition)) { \
fprintf(stderr, "Assumption \"%s\"\nFailed in file %s: at line:%i\n", \
#condition,__FILE__,__LINE__); \
GEXIT(#condition);}
#ifdef TRACE
#define GTRACE(exp) (GMessage(exp))
#else
#define GTRACE(exp)
#endif
#else
#define GASSERT(exp)
#define GTRACE(exp)
#define GVERIFY(condition)
#endif
#define GERROR(exp) (GError(exp))
// Abolute value
#define GABS(val) (((val)>=0)?(val):-(val))
// Min and Max
#define GMAX(a,b) (((a)>(b))?(a):(b))
#define GMIN(a,b) (((a)>(b))?(b):(a))
// Min of three
#define GMIN3(x,y,z) ((x)<(y)?GMIN(x,z):GMIN(y,z))
// Max of three
#define GMAX3(x,y,z) ((x)>(y)?GMAX(x,z):GMAX(y,z))
// Return minimum and maximum of a, b
#define GMINMAX(lo,hi,a,b) ((a)<(b)?((lo)=(a),(hi)=(b)):((lo)=(b),(hi)=(a)))
// Clamp value x to range [lo..hi]
#define GCLAMP(lo,x,hi) ((x)<(lo)?(lo):((x)>(hi)?(hi):(x)))
typedef int GCompareProc(const pointer item1, const pointer item2);
typedef long GFStoreProc(const pointer item1, FILE* fstorage); //for serialization
typedef pointer GFLoadProc(FILE* fstorage); //for deserialization
void GError(const char* format,...); // Error routine (aborts program)
void GMessage(const char* format,...);// Log message to stderr
// Assert failed routine:- usually not called directly but through GASSERT
void GAssert(const char* expression, const char* filename, unsigned int lineno);
typedef void GFreeProc(pointer item); //usually just delete,
//but may also support structures with embedded dynamic members
#define GMALLOC(ptr,size) if (!GMalloc((pointer*)(&ptr),size)) \
GError(ERR_ALLOC)
#define GCALLOC(ptr,size) if (!GCalloc((pointer*)(&ptr),size)) \
GError(ERR_ALLOC)
#define GREALLOC(ptr,size) if (!GRealloc((pointer*)(&ptr),size)) \
GError(ERR_ALLOC)
#define GFREE(ptr) GFree((pointer*)(&ptr))
inline char* strMin(char *arg1, char *arg2) {
return (strcmp(arg1, arg2) < 0)? arg1 : arg2;
}
inline char* strMax(char *arg1, char *arg2) {
return (strcmp(arg2, arg1) < 0)? arg1 : arg2;
}
inline int iround(double x) {
return (int)floor(x + 0.5);
}
char* Grealpath(const char *path, char *resolved_path);
int Gmkdir(const char *path, bool recursive=true, int perms = (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH));
void Gmktempdir(char* templ);
bool haveStdInput(); //if stdin is from a pipe or redirection
/****************************************************************************/
inline int Gintcmp(int a, int b) {
//return (a>b)? 1 : ((a==b)?0:-1);
return a-b;
}
int Gstrcmp(const char* a, const char* b, int n=-1);
//same as strcmp but doesn't crash on NULL pointers
int Gstricmp(const char* a, const char* b, int n=-1);
bool GstrEq(const char* a, const char* b);
bool GstriEq(const char* a, const char* b);
//basic swap template function
template<class T> void Gswap(T& lhs, T& rhs) {
T tmp=lhs; //requires copy operator
lhs=rhs;
rhs=tmp;
}
// use std::is_pointer from <type_traits> in C++11 instead
/*
template<typename T>
struct isPointer { static const bool value = false; };
template<typename T>
struct isPointer<T*> { static const bool value = true; };
*/
//check if type T is resolved as a pointer to char
template<class T>
struct is_char_ptr : std::integral_constant <
bool,
std::is_same<char const *, typename std::decay<T>::type>::value ||
std::is_same<char *, typename std::decay<T>::type>::value
> {};
inline void GFree(pointer* ptr){
GASSERT(ptr);
if (*ptr) free(*ptr);
*ptr=NULL;
}
inline bool GMalloc(pointer* ptr,unsigned long size){
//GASSERT(ptr);
if (size!=0)
*ptr=malloc(size);
return *ptr!=NULL;
}
// Allocate 0-filled memory
inline bool GCalloc(pointer* ptr,unsigned long size){
GASSERT(ptr);
*ptr=calloc(size,1);
return *ptr!=NULL;
}
// Resize memory
inline bool GRealloc(pointer* ptr,unsigned long size){
//GASSERT(ptr);
if (size==0) {
GFree(ptr);
return true;
}
if (*ptr==NULL) {//simple malloc
void *p=malloc(size);
if (p != NULL) {
*ptr=p;
return true;
}
else return false;
}//malloc
else {//realloc
void *p=realloc(*ptr,size);
if (p) {
*ptr=p;
return true;
}
return false;
}
}
template<class T> T* GDupAlloc(T& data) {
T* tmp=NULL;
if (!GMalloc((pointer*) tmp, sizeof(T)))
GError(ERR_ALLOC);
memcpy((void*)tmp, (void*)&data, sizeof(T));
return tmp;
}
// ****************** basic string manipulation *************************
char *Gstrdup(const char* str, int xtracap=0); //string duplication with extra capacity added
//duplicate a string by allocating a copy for it (+xtracap heap room) and returning the new pointer
//caller is responsible for deallocating the returned pointer!
char* Gstrdup(const char* sfrom, const char* sto);
//same as GStrdup, but with an early termination (e.g. on delimiter)
char* Gsubstr(const char* str, char* from, char* to=NULL);
//extracts a substring, allocating it, including boundaries (from/to)
char* replaceStr(char* &str, char* newvalue);
//conversion: to Lower/Upper case
// creating a new string:
char* upCase(const char* str);
char* loCase(const char* str);
// changing string in place:
char* strlower(char * str);
char* strupper(char * str);
//strstr but for memory zones: scans a memory region
//for a substring:
void* Gmemscan(void *mem, unsigned int len,
void *part, unsigned int partlen);
FILE* Gfopen(const char *path, char *mode=NULL);
// test if a char is in a string:
bool chrInStr(char c, const char* str);
char* rstrchr(char* str, char ch);
/* returns a pointer to the rightmost
occurence of ch in str - like rindex for platforms missing it*/
char* strchrs(const char* s, const char* chrs);
//strchr but with a set of chars instead of only one
char* rstrfind(const char* str, const char *substr);
// like rindex() but for strings; right side version of strstr()
char* reverseChars(char* str, int slen=0); //in place reversal of string
char* rstrstr(const char* rstart, const char *lend, const char* substr);
/*the reversed, rightside equivalent of strstr: starts searching
from right end (rstart), going back to left end (lend) and returns
a pointer to the last (right) matching character in str */
char* strifind(const char* str, const char* substr);
// case insensitive version of strstr -- finding a string within another string
// returns NULL if not found
//Determines if a string begins with a given prefix
//(returns false when any of the params is NULL,
// but true when prefix is '' (empty string)!)
bool startsWith(const char* s, const char* prefix);
bool startsiWith(const char* s, const char* prefix); //case insensitive
bool endsWith(const char* s, const char* suffix);
//Note: returns true if suffix is empty string, but false if it's NULL
bool endsiWith(const char* s, const char* suffix); //case insensitive version
//like endsWith but also remove the suffix if found
//returns true if the given suffix was found and removed
bool trimSuffix(char* s, const char* suffix);
//case insensitive version:
bool trimiSuffix(char* s, const char* suffix);
// ELF hash function for strings
int strhash(const char* str);
//alternate hash functions:
int fnv1a_hash(const char* cp);
int djb_hash(const char* cp);
//---- generic base GSeg : genomic segment (interval) --
// coordinates are considered 1-based (so 0 is invalid)
struct GSeg {
uint start; //start<end always!
uint end;
GSeg(uint s=0,uint e=0) {
if (s>e) { start=e;end=s; }
else { start=s;end=e; }
}
//check for overlap with other segment
uint len() { return end-start+1; }
bool overlap(GSeg* d) {
return (start<=d->end && end>=d->start);
}
bool overlap(GSeg& d) {
return (start<=d.end && end>=d.start);
}
bool overlap(GSeg& d, int fuzz) {
return (start<=d.end+fuzz && end+fuzz>=d.start);
}
bool overlap(uint x) {
return (start<=x && x<=end);
}
bool overlap(uint s, uint e) {
if (s>e) { Gswap(s,e); }
return (start<=e && end>=s);
}
//return the length of overlap between two segments
int overlapLen(GSeg* r) {
if (start<r->start) {
if (r->start>end) return 0;
return (r->end>end) ? end-r->start+1 : r->end-r->start+1;
}
else { //r->start<=start
if (start>r->end) return 0;
return (r->end<end)? r->end-start+1 : end-start+1;
}
}
// refstart = overlap start in ref coordinate space (1-based)
int overlapLen(uint rstart, uint rend, int* refstart=NULL) {
if (rstart>rend) { Gswap(rstart,rend); }
if (refstart) *refstart=0;
if (start<rstart) {
if (rstart>end) return 0;
if (refstart) *refstart=1;
return (rend>end) ? end-rstart+1 : rend-rstart+1;
}
else { //rstart<=start
if (start>rend) return 0;
if (refstart) *refstart=(start-rstart)+1;
return (rend<end)? rend-start+1 : end-start+1;
}
}
bool contains(GSeg* s) {
return (start<=s->start && end>=s->end);
}
bool contained(GSeg* s) {
return (s->start<=start && s->end>=end);
}
bool equals(GSeg& d){
return (start==d.start && end==d.end);
}
bool equals(GSeg* d){
return (start==d->start && end==d->end);
}
//fuzzy coordinate matching:
bool coordMatch(GSeg* s, uint fuzz=0) { //caller must check for s!=NULL
if (fuzz==0) return (start==s->start && end==s->end);
uint sd = (start>s->start) ? start-s->start : s->start-start;
uint ed = (end>s->end) ? end-s->end : s->end-end;
return (sd<=fuzz && ed<=fuzz);
}
void expand(int by) { //expand in both directions
start-=by;
end+=by;
}
void expandInclude(uint rstart, uint rend) { //expand to include given coordinates
if (rstart>rend) { Gswap(rstart,rend); }
if (rstart<start) start=rstart;
if (rend>end) end=rend;
}
//comparison operators required for sorting
bool operator==(GSeg& d){
return (start==d.start && end==d.end);
}
bool operator<(GSeg& d){
return (start==d.start)?(end<d.end):(start<d.start);
}
};
struct GRangeParser: GSeg {
char* refName=NULL;
char strand=0;
void parse(char* s);
GRangeParser(char* s=NULL):GSeg(0, 0) {
if (s) parse(s);
}
~GRangeParser() {
GFREE(refName);
}
};
//basic dynamic array template for primitive types
//which can only grow (reallocate) as needed
//optimize index test
#define GDynArray_INDEX_ERR "Error: use of index (%d) in GDynArray of size %d!\n"
#if defined(NDEBUG) || defined(NODEBUG) || defined(_NDEBUG) || defined(NO_DEBUG)
#define GDynArray_TEST_INDEX(x)
#else
#define GDynArray_TEST_INDEX(x) \
if (fCount==0 || x>=fCount) GError(GDynArray_INDEX_ERR, x, fCount)
#endif
#define GDynArray_MAXCOUNT UINT_MAX-1
#define GDynArray_NOIDX UINT_MAX
//basic dynamic array (vector) template for simple/primitive types or structs
//Warning: uses malloc so it will never call the item's default constructor when growing
template<class OBJ> class GDynArray {
protected:
bool byptr; //in-place copy (pointer) takeover of existing OBJ[]
OBJ *fArray;
uint fCount;
uint fCapacity; // size of allocated memory
const static uint dyn_array_defcap = 16; // initial capacity (in elements)
public:
GDynArray(int initcap=dyn_array_defcap):byptr(false), fArray(NULL), fCount(0),
fCapacity(initcap) { // constructor
GMALLOC(fArray, fCapacity*sizeof(OBJ));
}
GDynArray(const GDynArray &a):byptr(false), fArray(NULL),
fCount(a.fCount), fCapacity(a.fCapacity) { // copy constructor
GMALLOC(fArray, sizeof(OBJ)*a.fCapacity);
memcpy(fArray, a.fArray, sizeof(OBJ)* a.fCapacity);
}
GDynArray(OBJ* ptr, uint pcap):byptr(true), fArray(ptr), fCount(0), fCapacity(pcap) {
//this will never deallocate the passed pointer
}
virtual ~GDynArray() { if (!byptr) { GFREE(fArray); } }
GDynArray& operator = (const GDynArray &a) { // assignment operator
if (this == &a) return *this;
if (a.fCount == 0) {
Clear();
return *this;
}
growTo(a.fCapacity); //set size
memcpy(fArray, a.fArray, sizeof(OBJ)*a.fCount);
return *this;
}
OBJ& operator[] (uint idx) {// get array item
GDynArray_TEST_INDEX(idx);
return fArray[idx];
}
void Grow() {
int delta = (fCapacity>16) ? (fCapacity>>2) : 2;
if (GDynArray_MAXCOUNT-delta<=fCapacity)
delta=GDynArray_MAXCOUNT-fCapacity;
if (delta<=1) GError("Error at GDynArray::Grow(): max capacity reached!\n");
growTo(fCapacity + delta);
}
#define GDynArray_ADD(item) \
if (fCount==MAX_UINT-1) GError("Error at GDynArray: cannot add item, maximum count reached!\n"); \
if ((++fCount) > fCapacity) Grow(); \
fArray[fCount-1] = item;
uint Add(OBJ* item) { // Add item to the end of array
//element given by pointer
if (item==NULL) return GDynArray_NOIDX;
GDynArray_ADD( (*item) );
return (fCount-1);
}
uint Add(OBJ item) { // Add OBJ copy to the end of array
GDynArray_ADD(item);
return (fCount-1);
}
uint Push(OBJ item) { //same as Add
GDynArray_ADD(item);
return (fCount-1);
}
OBJ Pop() { //shoddy.. Do NOT call this for an empty array!
if (fCount==0) return (OBJ)NULL; //a NULL cast operator is required
--fCount;
return fArray[fCount];
}
uint Count() { return fCount; } // get size of array (elements)
uint Capacity() { return fCapacity; }
void growTo(uint newcap) {
if (newcap==0) { Clear(); return; }
if (newcap <= fCapacity) return; //never shrink! (use Pack() for shrinking)
GREALLOC(fArray, newcap*sizeof(OBJ));
fCapacity=newcap;
}
void append(OBJ* arr, uint count) {
//fast adding of a series of objects
growTo(fCount+count);
memcpy(fArray+fCount, arr, count*sizeof(OBJ));
fCount+=count;
}
void append(GDynArray<OBJ> arr) {
//fast adding of a series of objects
growTo(fCount+arr.fCount);
memcpy(fArray+fCount, arr.fArray, arr.fCount*sizeof(OBJ));
fCount+=arr.fCount;
}
void Trim(int tcount=1) {
//simply cut (discard) the last tcount items
//new Count is now fCount-tcount
//does NOT shrink capacity accordingly!
if (fCount>=tcount) fCount-=tcount;
}
void Pack() { //shrink capacity to fCount+dyn_array_defcap
if (fCapacity-fCount<=dyn_array_defcap) return;
int newcap=fCount+dyn_array_defcap;
GREALLOC(fArray, newcap*sizeof(OBJ));
fCapacity=newcap;
}
void zPack(OBJ z) { //shrink capacity to fCount+1 and adds a z terminator
if (fCapacity-fCount<=1) { fArray[fCount]=z; return; }
int newcap=fCount+1;
GREALLOC(fArray, newcap*sizeof(OBJ));
fCapacity=newcap;
fArray[fCount]=z;
}
inline void Shrink() { Pack(); }
void Delete(uint idx) {
GDynArray_TEST_INDEX(idx);
--fCount;
if (idx<fCount)
memmove(&fArray[idx], &fArray[idx+1], (fCount-idx)*sizeof(OBJ));
}
inline void Remove(uint idx) { Delete(idx); }
void Clear() { // clear array, shrinking its allocated memory
fCount = 0;
GREALLOC(fArray, sizeof(OBJ)*dyn_array_defcap);
// set initial memory size again
fCapacity = dyn_array_defcap;
}
void Reset() {// fast clear array WITHOUT deallocating it
fCount = 0;
}
OBJ* operator()() { return fArray; }
//use methods below in order to prevent deallocation of fArray pointer on destruct
//could be handy for adopting stack objects (e.g. cheap dynamic strings)
void ForgetPtr() { byptr=true; }
void DetachPtr() { byptr=true; }
};
int strsplit(char* str, GDynArray<char*>& fields, const char* delim, int maxfields=MAX_INT);
//splits a string by placing 0 where any of the delim chars are found, setting fields[] to the beginning
//of each field (stopping after maxfields); returns number of fields parsed
int strsplit(char* str, GDynArray<char*>& fields, const char delim, int maxfields=MAX_INT);
//splits a string by placing 0 where the delim char is found, setting fields[] to the beginning
//of each field (stopping after maxfields); returns number of fields parsed
int strsplit(char* str, GDynArray<char*>& fields, int maxfields=MAX_INT); //splits by tab or space
//splits a string by placing 0 where tab or space is found, setting fields[] to the beginning
//of each field (stopping after maxfields); returns number of fields parsed
// ************** simple line reading class for text files
//GLineReader -- text line reading/buffering class
class GLineReader {
bool closeFile;
//int len;
//int allocated;
GDynArray<char> buf;
int textlen; //length of actual text, without newline character(s)
bool isEOF;
FILE* file;
off_t filepos; //current position
bool pushed; //pushed back
int lcount; //line counter (read lines)
public:
char* chars() { return buf(); }
char* line() { return buf(); }
int readcount() { return lcount; } //number of lines read
void setFile(FILE* stream) { file=stream; }
int blength() { return buf.Count(); } //binary/buffer length, including newline character(s)
int charcount() { return buf.Count(); } //line length, including newline character(s)
int tlength() { return textlen; } //text length excluding newline character(s)
int linelen() { return textlen; } //line length, excluding newline character(s)
//int size() { return buf.Count(); } //same as size();
bool isEof() {return isEOF; }
bool eof() { return isEOF; }
off_t getfpos() { return filepos; }
off_t getFpos() { return filepos; }
char* nextLine() { return getLine(); }
char* getLine() { if (pushed) { pushed=false; return buf(); }
else return getLine(file); }
char* getLine(FILE* stream) {
if (pushed) { pushed=false; return buf(); }
else return getLine(stream, filepos); }
char* getLine(FILE* stream, off_t& f_pos); //read a line from a stream and update
// the given file position
void pushBack() { if (lcount>0) pushed=true; } // "undo" the last getLine request
// so the next call will in fact return the same line
GLineReader(const char* fname):closeFile(false),buf(1024), textlen(0),
isEOF(false),file(NULL),filepos(0), pushed(false), lcount(0) {
FILE* f=fopen(fname, "rb");
if (f==NULL) GError("Error opening file '%s'!\n",fname);
closeFile=true;
file=f;
}
GLineReader(FILE* stream=NULL, off_t fpos=0):closeFile(false),buf(1024),
textlen(0), isEOF(false),file(stream),
filepos(fpos), pushed(false), lcount(0) {
}
~GLineReader() {
if (closeFile) fclose(file);
}
};
/* extended fgets() - to read one full line from a file and
update the file position correctly !
buf will be reallocated as necessary, to fit the whole line
*/
char* fgetline(char* & buf, int& buflen, FILE* stream, off_t* f_pos=NULL, int* linelen=NULL);
//print int/values nicely formatted in 3-digit groups
char* commaprintnum(uint64 n);
/*********************** File management functions *********************/
// removes the last part (file or directory name) of a full path
// WARNING: this is a destructive operation for the given string!
void delFileName(char* filepath);
// returns a pointer to the last file or directory name in a full path
const char* getFileName(const char* filepath);
// returns a pointer to the file "extension" part in a filename
const char* getFileExt(const char* filepath);
int fileExists(const char* fname);
//returns 0 if path doesn't exist
// 1 if it's a directory
// 2 if it's a regular file
// 3 something else (but entry exists)
int64 fileSize(const char* fpath);
//write a formatted fasta record, fasta formatted
void writeFasta(FILE *fw, const char* seqid, const char* descr,
const char* seq, int linelen=60, int seqlen=0);
//parses the next number found in a string at the current position
//until a non-digit (and not a '.', 'e','E','-','+') is encountered;
//updates the char* pointer to be after the last digit parsed
bool parseNumber(char* &p, double& v);
bool parseDouble(char* &p, double& v); //just an alias for parseNumber
bool parseFloat(char* &p, float& v);
bool strToInt(char* p, int& i);
bool strToUInt(char* p, uint& i);
bool parseInt(char* &p, int& i); //advance pointer p after the number
bool parseUInt(char* &p, uint& i); //advance pointer p after the number
bool parseHex(char* &p, uint& i);
#endif /* G_BASE_DEFINED */