--- parser3/src/main/pa_common.C 2026/04/24 20:14:33 1.342 +++ parser3/src/main/pa_common.C 2026/09/25 13:53:43 1.346 @@ -1,7 +1,7 @@ /** @file Parser: commonly functions. - Copyright (c) 2000-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2000-2026 Art. Lebedev Studio (https://www.artlebedev.com) Authors: Konstantin Morshnev , Alexandr Petrosian */ @@ -23,7 +23,7 @@ #include #endif -volatile const char * IDENT_PA_COMMON_C="$Id: pa_common.C,v 1.342 2026/04/24 20:14:33 moko Exp $" IDENT_PA_COMMON_H IDENT_PA_HASH_H IDENT_PA_INLINE_HASH_H IDENT_PA_ARRAY_H IDENT_PA_STACK_H; +volatile const char * IDENT_PA_COMMON_C="$Id: pa_common.C,v 1.346 2026/09/25 13:53:43 moko Exp $" IDENT_PA_COMMON_H IDENT_PA_HASH_H IDENT_PA_INLINE_HASH_H IDENT_PA_ARRAY_H IDENT_PA_STACK_H; // some maybe-undefined constants @@ -359,17 +359,31 @@ bool file_write_action_under_lock(const struct File_write_action_info { const char* str; size_t length; -}; +}; #endif +// a single write() must stay below INT_MAX as windows _write() reports written bytes as int +#define FILE_WRITE_CHUNK_SIZE (256*0x100000) + +static const char* bytes_left(size_t rest) { + return rest ? pa_strcat(", ", pa_uitoa(rest), " bytes left") : ""; +} + static void file_write_action(int f, void *context) { - File_write_action_info& info=*static_cast(context); - if(info.length) { - ssize_t written=write(f, info.str, info.length); + File_write_action_info& info=*static_cast(context); + + const char* data=info.str; + size_t left=info.length; + + while(left) { + size_t portion=min(left, (size_t)FILE_WRITE_CHUNK_SIZE); + ssize_t written=write(f, data, portion); if(written<0) - throw Exception("file.write", 0, "write failed: %s (%d)", strerror(errno), errno); - if((size_t)written!=info.length) - throw Exception("file.write", 0, "write failed: %u of %u bytes written", written, info.length); + throw Exception("file.write", 0, "error writing %s bytes: %s (%d)%s", pa_uitoa(portion), strerror(errno), errno, bytes_left(left-portion)); + if(written==0) + throw Exception("file.write", 0, "error writing %s bytes: zero bytes written%s", pa_uitoa(portion), bytes_left(left-portion)); + data+=written; + left-=written; } } @@ -511,8 +525,8 @@ bool dir_exists(const String& file_spec) const String* file_exist(const String& path, const String& name) { String& result=*new String(path); - if(path.last_char() != '/') - result << "/"; + if(path.last_char() != '/' && path.last_char() != '\\') + result << "/"; result << name; return file_exist(result)?&result:0; } @@ -631,9 +645,20 @@ char* getrow(char* *row_ref, char delim) return result; } +const char* strrpbrk(const char* string, const char* chars) { + if(string) { + const char* pos=string+strlen(string); + while(pos!=string) { + if(strchr(chars, *--pos)) + return pos; + } + } + return NULL; +} + char* lsplit(char* string, char delim) { if(string) { - char* v=strchr(string, delim); + char* v=strchr(string, delim); if(v) { *v=0; return v+1; @@ -644,14 +669,14 @@ char* lsplit(char* string, char delim) { char* lsplit(char* *string_ref, char delim) { char* result=*string_ref; - char* next=lsplit(*string_ref, delim); + char* next=lsplit(*string_ref, delim); *string_ref=next; return result; } char* rsplit(char* string, char delim) { if(string) { - char* v=strrchr(string, delim); + char* v=strrchr(string, delim); if(v) { *v=0; return v+1; @@ -660,6 +685,14 @@ char* rsplit(char* string, char delim) { return NULL; } +char* rsplit(char* string, const char* delims) { + if(char* v=strrpbrk(string, delims)) { + *v=0; + return v+1; + } + return NULL; +} + void pa_strncpy(char *dst, const char *src, size_t n){ size_t left = n; @@ -689,13 +722,8 @@ char *pa_strcat(const char *a, const cha } const char *pa_filename(const char *path){ - if(!path) - return NULL; - for(const char *c = path + strlen(path) - 1; c >= path; c--) { - if(*c == '/' || *c == '\\') - return c+1; - } - return path; + const char *slash=strrpbrk(path, "/\\"); + return slash ? slash+1 : path; } size_t stdout_write(const void *buf, size_t size) { @@ -1124,4 +1152,3 @@ int pa_snprintf(char* b, size_t s, const va_end(l); return r; } -