--- parser3/src/main/pa_common.C 2001/03/19 17:42:15 1.16 +++ parser3/src/main/pa_common.C 2001/03/21 14:06:45 1.20 @@ -5,7 +5,7 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: pa_common.C,v 1.16 2001/03/19 17:42:15 paf Exp $ + $Id: pa_common.C,v 1.20 2001/03/21 14:06:45 paf Exp $ */ #ifdef HAVE_CONFIG_H @@ -18,6 +18,12 @@ #include #include #include +#include + +#ifdef WIN32 +#else +# include +#endif #include "pa_common.h" #include "pa_types.h" @@ -45,7 +51,8 @@ int __snprintf(char *b, size_t s, const #endif -char *file_read(Pool& pool, const char *fname, bool fail_on_read_problem) { + +char *file_read_text(Pool& pool, const char *fname, bool fail_on_read_problem) { int f; struct stat finfo; if(fname && !stat(fname,&finfo) &&(f=open(fname,O_RDONLY @@ -72,6 +79,50 @@ char *file_read(Pool& pool, const char * return 0; } +void file_write(Pool& pool, + const char *fname, + const char *data, size_t size, + bool as_text/*, + bool exclusive*/) { + if(fname) { + int f; + if(access(fname, F_OK)!=0) {/*no*/ + 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 +#ifdef WIN32 + |O_TRUNC +#endif + ; + mode|=as_text?_O_TEXT:_O_BINARY; + if((f=open(fname,mode,0666))>=0) { + /*if(exclusive) + flock(f, LOCK_EX);*/ + + if(size) write(f,data,size); +#ifndef WIN32 + ftruncate(f,size); +#endif + /*if(exclusive) + flock(f, LOCK_UN);*/ + close(f); + return; + } + } + } + if(fname) + PTHROW(0, 0, + 0, + "file_write('%s'): %s (#%d)", + fname, strerror(errno), errno); + else + PTHROW(0, 0, + 0, + "file_write: no filename specified"); +} + char *getrow(char **row_ref, char delim) { char *result=*row_ref; if(result) { @@ -103,9 +154,9 @@ char *lsplit(char **string_ref, char del } char *rsplit(char *string, char delim) { - if(string){ + if(string) { char *v=strrchr(string, delim); - if (v){ + if(v) { *v=0; return v+1; } @@ -118,13 +169,13 @@ char *format(Pool& pool, double value, c if(fmt) if(strpbrk(fmt, "diouxX")) if(strpbrk(fmt, "ouxX")) - snprintf(result, MAX_NUMBER, fmt, (uint)value ); + snprintf(result, MAX_NUMBER, fmt,(uint)value ); else - snprintf(result, MAX_NUMBER, fmt, (int)value ); + snprintf(result, MAX_NUMBER, fmt,(int)value ); else snprintf(result, MAX_NUMBER, fmt, value); else - snprintf(result, MAX_NUMBER, "%d", (int)value); + snprintf(result, MAX_NUMBER, "%d",(int)value); return result; } @@ -183,7 +234,7 @@ const char *unescape_chars(Pool& pool, c return s; } -static void append_attribute_subattribute(const Hash::Key& akey, Hash::Value *avalue, +static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue, void *info) { if(akey==VALUE_NAME) return; @@ -191,21 +242,21 @@ static void append_attribute_subattribut // ...; charset=windows1251 String *string=static_cast(info); string->APPEND_CONST("; "); - string->append(akey, String::Untaint_lang::HEADER, true); + string->append(akey, String::UL_HEADER, true); string->APPEND_CONST("="); string->append(static_cast(avalue)->as_string(), - String::Untaint_lang::HEADER, true); + String::UL_HEADER, true); } -const String& attributed_meaning_string(Value *meaning) { - String &result=*new(meaning->pool()) String(meaning->pool()); - if(Hash *hash=meaning->get_hash()) { +const String& attributed_meaning_to_string(Value& meaning) { + String &result=*new(meaning.pool()) String(meaning.pool()); + if(Hash *hash=meaning.get_hash()) { // $value(value) $subattribute(subattribute value) if(Value *value=static_cast(hash->get(*value_name))) - result.append(value->as_string(), String::Untaint_lang::HEADER, true); + result.append(value->as_string(), String::UL_HEADER, true); hash->foreach(append_attribute_subattribute, &result); } else // result value - result.append(meaning->as_string(), String::Untaint_lang::HEADER, true); + result.append(meaning.as_string(), String::UL_HEADER, true); return result; } \ No newline at end of file