--- parser3/src/main/pa_common.C 2001/10/19 12:43:30 1.73 +++ parser3/src/main/pa_common.C 2001/11/14 13:07:41 1.90 @@ -2,9 +2,9 @@ Parser: commonly functions. Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Author: Alexander Petrosyan (http://paf.design.ru) - $Id: pa_common.C,v 1.73 2001/10/19 12:43:30 parser Exp $ + $Id: pa_common.C,v 1.90 2001/11/14 13:07:41 paf Exp $ */ #include "pa_common.h" @@ -16,22 +16,56 @@ #include "pa_hash.h" #include "pa_string.h" -#ifndef WIN32 -# ifndef _O_TEXT -# define _O_TEXT 0 -# endif -# ifndef _O_BINARY -# define _O_BINARY 0 -# endif +#ifndef _O_TEXT +# define _O_TEXT 0 +#endif +#ifndef _O_BINARY +# define _O_BINARY 0 +#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) { - int r=_vsnprintf(b, --s, f, l); - b[s]=0; + if(!s) + return 0; + + int r; + // note: on win32& maybe somewhere else + // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing + --s; +#if _MSC_VER + /* + win32: + mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm + + if the number of bytes to write exceeds buffer, then count bytes are written and –1 is returned + */ + r=_vsnprintf(b, s, f, l); + if(r<0) + r=s; +#else + r=_vsnprintf(b, s, f, l); + /* + solaris: + man vsnprintf + + The snprintf() function returns the number of characters + formatted, that is, the number of characters that would have + been written to the buffer if it were large enough. If the + value of n is 0 on a call to snprintf(), an unspecified + value less than 1 is returned. + */ + + if(r<0) + r=0; + else if(r>s) + r=s; +#endif + b[r]=0; return r; } + int __snprintf(char *b, size_t s, const char *f, ...) { va_list l; va_start(l, f); @@ -40,22 +74,30 @@ int __snprintf(char *b, size_t s, const return r; } -#endif +static char *strnchr(char *buf, size_t size, char c) { + for(; size-->0; buf++) { + if(*buf==c) + return buf; + } + + return 0; +} -// under WIN32 "t" mode fixes DOS chars OK, can't say that about other systems/ line break styles -static void fix_line_breaks(char *src, size_t& size) { - char *dest=src; +void fix_line_breaks(char *buf, size_t& size) { + //_asm int 3; + const char * const eob=buf+size; + char *dest=buf; // fix DOS: \r\n -> \n // fix Macintosh: \r -> \n - char *bol=src; - while(char *eol=strchr(bol, '\r')) { + char *bol=buf; + while(char *eol=strnchr(bol, eob -bol, '\r')) { size_t len=eol-bol; if(dest!=bol) memcpy(dest, bol, len); dest+=len; *dest++='\n'; - if(eol[1]=='\n') { // \r,\n = DOS + if(&eol[1]=0 && read_size<=max_size) { - if(as_text) - ((char*&)data)[read_size]=0; - } else + if(read_size<0 || read_size>max_size) throw Exception(0, 0, &file_spec, "read failed: actually read %d bytes count not in [0..%lu] valid range", read_size, (unsigned long)max_size); //never - - if(as_text) - fix_line_breaks((char *)data, read_size); + + data_size=read_size; + if(as_text) { + fix_line_breaks((char *)data, data_size); + // note: after fixing + ((char*&)data)[data_size]=0; + } return true; } if(fail_on_read_problem) @@ -141,40 +189,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] @@ -303,7 +344,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")) @@ -334,8 +375,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, @@ -393,7 +434,7 @@ static void append_attribute_subattribut const String& attributed_meaning_to_string(Value& meaning, String::Untaint_lang lang) { String &result=*new(meaning.pool()) String(meaning.pool()); - if(Hash *hash=meaning.get_hash()) { + if(Hash *hash=meaning.get_hash(0)) { // $value(value) $subattribute(subattribute value) if(Value *value=static_cast(hash->get(*value_name))) result.append(value->as_string(), lang, true); @@ -445,7 +486,7 @@ bool StrEqNc(const char *s1, const char } } -static int isLeap(int year) { +static bool isLeap(int year) { return !( (year % 4) || ((year % 400) && !(year % 100)) ); @@ -475,4 +516,4 @@ void remove_crlf(char *start, char *end) case '\n': *p='|'; break; case '\r': *p=' '; break; } -} \ No newline at end of file +}