📄 wydefs.cpp
字号:
/* Copyright is licensed under GNU LGPL. by I.J.Wang 2003 File is intented to define runtime code required in wydefs.h. */#define WYLIB_SOURCE#include "wyreqs.h"#include <exception>#include "wydefs.h"#include <string.h>#include <cstdlib>void Wy__Base::terminate(void) WY__NOTHROW__{ std::terminate(); // std::abort(); // added to force compiler silent (RH9.0)};Wy__Base::NoCancel::NoCancel(){ const int v=::pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_oldstate); if(v!=0) { // man. says only parameter error. which should not happen. // throw(v); }};Wy__Base::NoCancel::~NoCancel(){ const int v=::pthread_setcancelstate(_oldstate,NULL); if(v!=0) { // man. says only parameter error. which should not happen. // throw(v); }};void Wy__Base::memswp(void* p1,void* p2,size_t n) WY__TSPC(){ char* cp1=static_cast<char*>(p1); char* cp2=static_cast<char*>(p2); for(size_t i=0; i<n; ++i,++cp1,++cp2) { const char tmp(*cp1); *cp1=*cp2; *cp2=tmp; }};// [Syn] Write message to STDERR_FILENO//// [Ret] 0= Ok// otherwise= errno//int Wy__Base::put_errmsg(const char* msg) WY__TSPC()#ifndef WYLIB_CONFIG_THROWSPC try#endif{ if(msg==0) { return(EFAULT); } const int mlen(::strlen(msg)); int oldstate; int v( Wy__Base::pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &oldstate) ); if(v!=0) { // man. says only parameter error. which should not happen. Wy__Base::terminate(); } v=(::write(STDERR_FILENO,msg,mlen)==mlen)? 0:errno; if(::pthread_setcancelstate(oldstate,NULL)!=0) { // man. says only parameter error. which should not happen. Wy__Base::terminate(); } return(v);}#ifndef WYLIB_CONFIG_THROWSPC catch(...) { std::unexpected(); }#endif;// [Syn] Print abnormal terminate message//// [Ret] errno encountered//int Wy__Base::put_abnmsg(const char* expr,const char *file_name, unsigned int lnum) WY__TSPC()#ifndef WYLIB_CONFIG_THROWSPC try#endif{ static const char EMsg1[]="Abnormal Terminate: "; char obuf[256],*zptr; ssize_t wrlen; int oldstate; int v( Wy__Base::pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &oldstate) ); if(v!=0) { // man. says only parameter error. which should not happen. Wy__Base::terminate(); } // RESET "Abnormal Terminate: " // ::memcpy(obuf,EMsg1,sizeof(EMsg1)-1); zptr=obuf+sizeof(EMsg1)-1; if(expr!=NULL) { // APPEND expr const size_t len(::strlen(expr)); const size_t lft(sizeof(obuf)-(zptr-obuf)); if(len>0) { if(len>lft) { ::memcpy(zptr,expr,lft); zptr+=lft; goto lm_exit; } ::memcpy(zptr,expr,len); zptr+=len; } }; if(sizeof(obuf)-(zptr-obuf)<2) { goto lm_exit; } if(file_name!=0) { // APPEND ", " file_name const size_t len(::strlen(file_name)); if(len>0) { if((expr!=NULL)&&(*expr!=0)) { *zptr=','; zptr++; *zptr=' '; zptr++; } const size_t lft=sizeof(obuf)-(zptr-obuf); if(len>lft) { ::memcpy(zptr,file_name,lft); zptr+=lft; goto lm_exit; } ::memcpy(zptr,file_name,len); zptr+=len; } }; // Convert lnum number and append to zptr // // APPEND "(lnum)" // { // enough for unsigned int +2 char dbuf[(CHAR_BIT*sizeof(unsigned int)*3011+9999)/10000+2]; std::div_t dt; int n; dt.quot=lnum; n=0; do { dt=std::div(dt.quot,10); dbuf[n++]=(char)(dt.rem); } while(dt.quot>0); if((sizeof(obuf)-(zptr-obuf))<size_t(n+2)) { goto lm_exit; } *zptr='('; ++zptr; for(n--; n>=0; n--) { const char ch=dbuf[n]; *zptr= (ch>9)? (ch+'7'):(ch+'0'); ++zptr; } *zptr=')'; ++zptr; } wrlen=zptr-obuf; if(wrlen<ssize_t(sizeof(obuf))) { *zptr='\n'; ++zptr; // optional ++wrlen; } v=(::write(STDERR_FILENO,obuf,wrlen)==wrlen) ?0:errno; if(::pthread_setcancelstate(oldstate,NULL)!=0) { // man. says only parameter error. which should not happen. Wy__Base::terminate(); } return(v);lm_exit:; wrlen=zptr-obuf; ::write(STDERR_FILENO,obuf,wrlen); if(::pthread_setcancelstate(oldstate,NULL)!=0) { // man. says only parameter error. which should not happen. Wy__Base::terminate(); } return(ENAMETOOLONG);}#ifndef WYLIB_CONFIG_THROWSPC catch(...) { std::unexpected(); }#endif;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -