--- parser3/src/main/pa_common.C 2001/10/24 14:39:44 1.76 +++ parser3/src/main/pa_common.C 2001/10/31 14:01:45 1.80 @@ -4,7 +4,7 @@ Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: pa_common.C,v 1.76 2001/10/24 14:39:44 parser Exp $ + $Id: pa_common.C,v 1.80 2001/10/31 14:01:45 paf Exp $ */ #include "pa_common.h" @@ -16,7 +16,7 @@ #include "pa_hash.h" #include "pa_string.h" -#ifndef WIN32 +#ifdef WIN32 # ifndef _O_TEXT # define _O_TEXT 0 # endif @@ -25,6 +25,10 @@ # endif #endif +#ifndef O_TRUNC +# define O_TRUNC 0 +#endif + #if _MSC_VER int __vsnprintf(char *b, size_t s, const char *f, va_list l) { @@ -42,11 +46,11 @@ int __snprintf(char *b, size_t s, const #endif -void fix_line_breaks(char *src, size_t& size) { - char *dest=src; +void fix_line_breaks(char *cstr, size_t& size) { + char *dest=cstr; // fix DOS: \r\n -> \n // fix Macintosh: \r -> \n - char *bol=src; + char *bol=cstr; while(char *eol=strchr(bol, '\r')) { size_t len=eol-bol; if(dest!=bol) @@ -96,7 +100,7 @@ bool file_read(Pool& pool, const String& data=0; read_size=0; } else { - data=pool.malloc(max_size+(as_text?1:0)); + data=pool.malloc(max_size+(as_text?1:0), 3); if(offset) lseek(f, offset, SEEK_SET); read_size=read(f, data, max_size); @@ -140,40 +144,33 @@ static void create_dir_for_file(const St void file_write(Pool& pool, const String& file_spec, const void *data, size_t size, - bool as_text/*, + bool as_text, + bool do_append/*, bool exclusive*/) { const char *fname=file_spec.cstr(String::UL_FILE_SPEC); int f; - if(access(fname, W_OK)!=0) {/*no*/ + if(access(fname, W_OK)!=0) // no create_dir_for_file(file_spec); - if((f=open(fname, O_WRONLY|O_CREAT|_O_BINARY, 0666))>0) - close(f); - } - if(access(fname, R_OK|W_OK)==0) { - int mode=O_RDWR|(as_text?_O_TEXT:_O_BINARY) -#ifdef WIN32 - |O_TRUNC -#endif - ; - if((f=open(fname, mode, 0666))>=0) { - /*if(exclusive) - flock(f, LOCK_EX);*/ - - if(size) write(f, data, size); -#ifndef WIN32 - ftruncate(f, size); + if((f=open(fname, + O_CREAT|O_RDWR + |(as_text?_O_TEXT:_O_BINARY) + |(do_append?O_APPEND:O_TRUNC), 0666))>=0) { + /*if(exclusive) + flock(f, LOCK_EX);*/ + + if(size) write(f, data, size); +#if O_TRUNC==0 + ftruncate(f, size); #endif - /*if(exclusive) - flock(f, LOCK_UN);*/ - close(f); - return; - } - } - throw Exception(0, 0, - &file_spec, - "write failed: %s (%d), actual filename '%s'", - strerror(errno), errno, fname); + /*if(exclusive) + flock(f, LOCK_UN);*/ + close(f); + } else + throw Exception(0, 0, + &file_spec, + "write failed: %s (%d), actual filename '%s'", + strerror(errno), errno, fname); } // throws nothing! [this is required in file_move & file_delete] @@ -302,7 +299,7 @@ char *rsplit(char *string, char delim) { /// @todo less stupid type detection char *format(Pool& pool, double value, char *fmt) { - char *result=(char *)pool.malloc(MAX_NUMBER); + char *result=(char *)pool.malloc(MAX_NUMBER, 4); if(fmt) if(strpbrk(fmt, "diouxX")) if(strpbrk(fmt, "ouxX")) @@ -333,8 +330,8 @@ size_t stdout_write(const void *buf, siz #endif } -const char *unescape_chars(Pool& pool, const char *cp, int len) { - char *s=(char *)pool.malloc(len + 1); +char *unescape_chars(Pool& pool, const char *cp, int len) { + char *s=(char *)pool.malloc(len + 1, 5); enum EscapeState { EscapeRest, EscapeFirst,