Skip to content

Commit 3d6867e

Browse files
committed
removed _POSIX_SOURCE #define which can cause problems on OS X
1 parent d912d98 commit 3d6867e

5 files changed

Lines changed: 223 additions & 80 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ endif
4343

4444

4545
BASEFLAGS := -Wall -Wextra ${INCDIRS} $(MARCH) -D_FILE_OFFSET_BITS=64 \
46-
-D_LARGEFILE_SOURCE -fno-strict-aliasing -fno-exceptions -fno-rtti -D_DARWIN_C_SOURCE
46+
-D_LARGEFILE_SOURCE -fno-strict-aliasing -fno-exceptions -fno-rtti
4747

4848
# C/C++ linker
4949

gclib/GBase.cpp

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,25 @@ int Gmkdir(const char *path, bool recursive, int perms) {
230230
else {
231231
GMALLOC(gpath, plen+2);
232232
strcpy(gpath,path);
233-
gpath[plen]='/';
234-
gpath[plen+1]=0;
233+
strcat(gpath, "/");
234+
++plen;
235235
}
236-
char* ss=gpath;
237-
char* psep = NULL;
238-
while (*ss!=0 && (psep=strchr(ss, '/'))!=NULL) {
239-
*psep=0; //now gpath is the path up to this /
240-
ss=psep; ++ss; //ss repositioned just after the /
241-
// create current level if it doesn't exist
242-
if (fileExists(gpath)) { //path exists
243-
*psep='/';
244-
continue; //assume it's a directory or a symlink to one
245-
//if not, it'll fail later
246-
}
236+
//char* ss=gpath+plen-1;
237+
char* psep = gpath+plen-1; //start at the last /
238+
GDynArray<char*> dirstack(4); // stack of directories that should be created
239+
*psep='\0';
240+
int fexists=0;
241+
while ((fexists=fileExists(gpath))!=0) {
242+
dirstack.Push(psep);
243+
GMessage("******** added %s to dirstack!\n",gpath);
244+
do { --psep; } while (psep>gpath && *psep!='/');
245+
if (psep<=gpath) break;
246+
*psep='\0';
247+
}
248+
*psep=''
249+
while (dirstack.Count()>0) {
250+
psep=dirstack.Pop();
251+
GMessage("********** popped %s from dirstack.\n",gpath);
247252
int mkdir_err=0;
248253
if ((mkdir_err=G_mkdir(gpath, perms))!=0) {
249254
GMessage("Warning: mkdir(%s) failed: %s\n", gpath, strerror(errno));
@@ -475,36 +480,40 @@ char* fgetline(char* & buf, int& buf_cap, FILE *stream, off_t* f_pos, int* linel
475480
}
476481

477482
char* GLineReader::getLine(FILE* stream, off_t& f_pos) {
478-
if (pushed) { pushed=false; return buf; }
483+
if (pushed) { pushed=false; return buf(); }
479484
//reads a char at a time until \n and/or \r are encountered
480-
len=0;
485+
//len=0;
481486
int c=0;
482487
while ((c=getc(stream))!=EOF) {
483-
if (len>=allocated-1) {
484-
allocated+=1024;
485-
GREALLOC(buf, allocated);
486-
}
488+
//if (len>=allocated-1) {
489+
// allocated+=1024;
490+
// GREALLOC(buf, allocated);
491+
//}
487492
if (c=='\n' || c=='\r') {
488-
buf[len]='\0';
493+
//buf[len]='\0';
494+
buf.Push('\0');
489495
if (c=='\r') { //DOS file -- special case
490496
if ((c=getc(stream))!='\n') ungetc(c,stream);
491497
else f_pos++;
492498
}
493499
f_pos++;
494500
lcount++;
495-
return buf;
501+
return buf();
496502
}
497503
f_pos++;
498-
buf[len]=(char)c;
499-
len++;
504+
//buf[len]=(char)c;
505+
buf.Push(c);
506+
//len++;
500507
} //while i<buf_cap-1
501508
if (c==EOF) {
502509
isEOF=true;
503-
if (len==0) return NULL;
510+
//if (len==0) return NULL;
511+
if (buf.Count()==0) return NULL;
504512
}
505-
buf[len]='\0';
513+
//buf[len]='\0';
514+
buf.Push('\0');
506515
lcount++;
507-
return buf;
516+
return buf();
508517
}
509518

510519

gclib/GBase.h

Lines changed: 128 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#ifndef G_BASE_DEFINED
22
#define G_BASE_DEFINED
3-
#ifndef _POSIX_SOURCE
4-
//mostly for MinGW
5-
#define _POSIX_SOURCE
6-
#endif
3+
//#ifndef _POSIX_SOURCE
4+
////mostly for MinGW;breaks mkdtemp and possibly other functions on OS X
5+
//#define _POSIX_SOURCE
6+
//#endif
77
#ifdef HAVE_CONFIG_H
88
#include "config.h"
99
#endif
@@ -356,64 +356,166 @@ class GSeg {
356356
}
357357
};
358358

359+
//basic dynamic array template for primitive types
360+
//which can only grow (reallocate) as needed
361+
#define GDynArray_INDEX_ERR "Error: invalid index (%d) in dynamic array!\n"
362+
#if defined(NDEBUG) || defined(NODEBUG) || defined(_NDEBUG) || defined(NO_DEBUG)
363+
#define GDynArray_TEST_INDEX(x)
364+
#else
365+
#define GDynArray_TEST_INDEX(x) \
366+
if (x>=fCount) GError(GDynArray_INDEX_ERR, x)
367+
#endif
368+
369+
#define GDynArray_MAXCOUNT UINT_MAX-1
370+
#define GDynArray_NOIDX UINT_MAX
371+
372+
template<class OBJ> class GDynArray {
373+
protected:
374+
OBJ *fArray;
375+
uint fCount;
376+
uint fCapacity; // size of allocated memory
377+
const static uint dyn_array_defcap = 16; // initial capacity (in elements)
378+
public:
379+
GDynArray(int initcap=dyn_array_defcap):fArray(NULL), fCount(0),
380+
fCapacity(initcap) { // constructor
381+
GMALLOC(fArray, fCapacity*sizeof(OBJ));
382+
}
383+
GDynArray(const GDynArray &a):fCount(a.fCount), fCapacity(a.fCapacity) { // copy constructor
384+
GMALLOC(fArray, sizeof(OBJ)*a.fCapacity);
385+
memcpy(fArray, a.fArray, sizeof(OBJ)* a.fCapacity);
386+
}
387+
virtual ~GDynArray() { GFREE(fArray); }
388+
GDynArray& operator = (const GDynArray &a) { // assignment operator
389+
if (this == &a) return *this;
390+
if (a.fCount == 0) {
391+
Clear();
392+
return *this;
393+
}
394+
setCapacity(a.fCapacity); //set size
395+
memcpy(fArray, a.fArray, sizeof(OBJ)*a.fCount);
396+
return *this;
397+
}
398+
399+
OBJ& operator [] (uint idx) {// get array item
400+
GDynArray_TEST_INDEX(idx);
401+
return fArray[idx];
402+
}
403+
404+
void Grow() {
405+
int delta = (fCapacity>16) ? (fCapacity>>2) : 2;
406+
if (GDynArray_MAXCOUNT-delta<=fCapacity)
407+
delta=GDynArray_MAXCOUNT-fCapacity;
408+
if (delta<=1) GError("Error at GDynArray::Grow(): max capacity reached!\n");
409+
setCapacity(fCapacity + delta);
410+
}
411+
#define GDYNARRAY_ADD(item) \
412+
if (fCount==MAX_UINT-1) GError("Error at GDynArray: add item failed, maximum count reached!\n"); \
413+
if ((++fCount) > fCapacity) Grow(); \
414+
fArray[fCount-1] = item;
415+
416+
uint Add(OBJ* item) { // Add item to the end of array by pointer
417+
if (item==NULL) return GDynArray_NOIDX;
418+
GDYNARRAY_ADD( (*item) );
419+
return (fCount-1);
420+
}
421+
422+
uint Add(OBJ item) { // Add item copy to the end of array
423+
GDYNARRAY_ADD(item);
424+
return (fCount-1);
425+
}
426+
427+
uint Push(OBJ item) {
428+
GDYNARRAY_ADD(item);
429+
return (fCount-1);
430+
}
431+
432+
OBJ Pop() {
433+
if (fCount==0) return (OBJ)NULL;
434+
--fCount;
435+
return fArray[fCount];
436+
}
437+
uint Count() { return fCount; } // get size of array (elements)
438+
439+
virtual void setCapacity(uint newcap) {
440+
if (newcap==0) { Clear(); return; } //better use Clear() instead
441+
if (newcap <= fCapacity) return; //never shrink -- use GVec for this
442+
GREALLOC(fArray, newcap*sizeof(OBJ));
443+
fCapacity=newcap;
444+
}
445+
446+
void Clear() { // clear array
447+
fCount = 0;
448+
GREALLOC(fArray, sizeof(OBJ)*dyn_array_defcap);
449+
// set initial memory size again
450+
fCapacity = dyn_array_defcap;
451+
}
452+
//pointer getptr() { return (pointer) fArray; }
453+
OBJ* operator()() { return fArray; }
454+
};
359455

360456

361457
//--------------------------------------------------------
362458
// ************** simple line reading class for text files
363-
364459
//GLineReader -- text line reading/buffering class
365460
class GLineReader {
366461
bool closeFile;
367-
int len;
368-
int allocated;
369-
char* buf;
462+
//int len;
463+
//int allocated;
464+
GDynArray<char> buf;
370465
bool isEOF;
371466
FILE* file;
372467
off_t filepos; //current position
373468
bool pushed; //pushed back
374469
int lcount; //line counter (read lines)
375470
public:
376-
char* chars() { return buf; }
377-
char* line() { return buf; }
471+
char* chars() { return buf(); }
472+
char* line() { return buf(); }
378473
int readcount() { return lcount; } //number of lines read
379474
void setFile(FILE* stream) { file=stream; }
380-
int length() { return len; }
381-
int size() { return len; } //same as size();
475+
int length() { return buf.Count(); }
476+
int size() { return buf.Count(); } //same as size();
382477
bool isEof() {return isEOF; }
383478
bool eof() { return isEOF; }
384479
off_t getfpos() { return filepos; }
385480
off_t getFpos() { return filepos; }
386481
char* nextLine() { return getLine(); }
387-
char* getLine() { if (pushed) { pushed=false; return buf; }
482+
char* getLine() { if (pushed) { pushed=false; return buf(); }
388483
else return getLine(file); }
389484
char* getLine(FILE* stream) {
390-
if (pushed) { pushed=false; return buf; }
485+
if (pushed) { pushed=false; return buf(); }
391486
else return getLine(stream, filepos); }
392487
char* getLine(FILE* stream, off_t& f_pos); //read a line from a stream and update
393488
// the given file position
394489
void pushBack() { if (lcount>0) pushed=true; } // "undo" the last getLine request
395490
// so the next call will in fact return the same line
396-
GLineReader(const char* fname) {
491+
GLineReader(const char* fname):closeFile(false),buf(1024),isEOF(false),file(NULL),
492+
filepos(0), pushed(false), lcount(0) {
397493
FILE* f=fopen(fname, "rb");
398494
if (f==NULL) GError("Error opening file '%s'!\n",fname);
399495
closeFile=true;
400-
init(f);
496+
file=f;
497+
//s_init(f);
401498
}
402-
GLineReader(FILE* stream=NULL, off_t fpos=0) {
403-
closeFile=false;
404-
init(stream,fpos);
499+
GLineReader(FILE* stream=NULL, off_t fpos=0):closeFile(false),buf(1024),isEOF(false),file(stream),
500+
filepos(fpos), pushed(false), lcount(0) {
501+
//closeFile=false;
502+
//s_init(stream,fpos);
405503
}
406-
void init(FILE* stream, off_t fpos=0) {
407-
len=0;
408-
isEOF=false;
409-
allocated=1024;
410-
GMALLOC(buf,allocated);
504+
/*
505+
void s_init(FILE* stream, off_t fpos=0) {
506+
//len=0;
507+
//allocated=1024;
508+
//GMALLOC(buf,allocated);
509+
//buf.Clear();
510+
//buf.setCapacity(1024);
511+
isEOF=false;
411512
lcount=0;
412-
buf[0]=0;
513+
//buf[0]=0;
413514
file=stream;
414515
filepos=fpos;
415516
pushed=false;
416517
}
518+
*/
417519
~GLineReader() {
418520
GFREE(buf);
419521
if (closeFile) fclose(file);
@@ -447,7 +549,7 @@ int fileExists(const char* fname);
447549
//returns 0 if path doesn't exist
448550
// 1 if it's a directory
449551
// 2 if it's a regular file
450-
// 3 otherwise (?)
552+
// 3 something else (but entry exists)
451553

452554
int64 fileSize(const char* fpath);
453555

0 commit comments

Comments
 (0)