From: <laz...@us...> - 2004-02-29 18:15:25
|
Update of /cvsroot/rtk/rtk/src/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17958/src/core Modified Files: rchar.cpp Log Message: More string stuff... uhh.. :) Index: rchar.cpp =================================================================== RCS file: /cvsroot/rtk/rtk/src/core/rchar.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** rchar.cpp 29 Feb 2004 08:46:54 -0000 1.6 --- rchar.cpp 29 Feb 2004 17:57:10 -0000 1.7 *************** *** 10,31 **** namespace Rtk { ! RCHAR* rstrlwr(const RCHAR* str) { ! RCHAR* ret = (RCHAR*)malloc(sizeof(RCHAR)*rstrlen(str)); ! RCHAR* ptr = ret; ! while (*str) { ! *ptr++ = rtolower(*str++); } ! return ret; } ! RCHAR* rstrupr(const RCHAR* str) ! { ! RCHAR* ret = (RCHAR*)malloc(sizeof(RCHAR)*rstrlen(str)); ! RCHAR* ptr = ret; ! while (*str) { ! *ptr++ = rtoupper(*str++); } ! return ret; } --- 10,35 ---- namespace Rtk { ! RCHAR *rstrlwr(RCHAR *str) { ! RCHAR *ptr = str; ! while(*ptr) { ! if(risupper(*ptr)) { ! *ptr = rtolower(*ptr); ! } ! ptr++; } ! return str; } ! RCHAR *rstrupr(RCHAR* str) ! { ! RCHAR *ptr = str; ! while(*ptr) { ! if(rislower(*ptr)) { ! *ptr = rtoupper(*ptr); ! } ! ptr++; } ! return str; } *************** *** 57,77 **** then s_ptr[1] == '\0' ;-) */ #define CVT(fmt_len) \ RCHAR *fmt2 = (RCHAR*)alloca((fmt_len)*sizeof(RCHAR)); \ const RCHAR *s_ptr = fmt; RCHAR *d_ptr = fmt2; \ - bool check = false; \ while (*s_ptr) { \ ! if(check && *s_ptr=='s') { \ *d_ptr++ = 'S'; \ s_ptr++; \ - check = false; \ } else { \ - if(*s_ptr=='%') check = true; \ - else check = false; \ *d_ptr++ = *s_ptr++; \ } \ } \ ! *d_ptr = '\0'; \ fmt = fmt2 --- 61,81 ---- then s_ptr[1] == '\0' ;-) + Mikko: + Right :) Sorry, I forgot that we are working with null terminated strings :))) + */ #define CVT(fmt_len) \ RCHAR *fmt2 = (RCHAR*)alloca((fmt_len)*sizeof(RCHAR)); \ const RCHAR *s_ptr = fmt; RCHAR *d_ptr = fmt2; \ while (*s_ptr) { \ ! if(s_ptr[0] == '%' && s_ptr[1] == 's') { \ ! *d_ptr++ = *s_ptr++; \ *d_ptr++ = 'S'; \ s_ptr++; \ } else { \ *d_ptr++ = *s_ptr++; \ } \ } \ ! *d_ptr++ = *s_ptr++; /* Copy null */ \ fmt = fmt2 *************** *** 125,144 **** // scanf ! int rvsscanf(const RCHAR* src, const RCHAR* fmt, va_list args) ! { ! CVT(rstrlen(fmt)); // +\0 ! return vswscanf(src, fmt2, args); ! } ! ! int rvfscanf(FILE* f, const RCHAR* fmt, va_list args) { CVT(rstrlen(fmt)); // +\0 ! return vfwscanf(f, fmt2, args); } ! int rvscanf(const RCHAR* fmt, va_list args) { CVT(rstrlen(fmt)); // +\0 ! return vfwscanf(stdin, fmt2, args); } --- 129,142 ---- // scanf ! static RTK_INLINE int rvsscanf(const RCHAR* src, const RCHAR* fmt, va_list args) { CVT(rstrlen(fmt)); // +\0 ! return vswscanf(src, fmt, args); } ! static RTK_INLINE int rvfscanf(FILE* f, const RCHAR* fmt, va_list args) { CVT(rstrlen(fmt)); // +\0 ! return vfwscanf(f, fmt, args); } *************** *** 170,173 **** --- 168,217 ---- } + // File + + #define W2A(src, dst) do { \ + int src_len = rstrlen(src); \ + int dst_len = wcstombs(NULL, src, src_len); \ + if(dst_len==-1) break; \ + char *_dst = (char*)alloca(dst_len+1); \ + if(wcstombs(_dst, src, src_len)==-1) break; \ + _dst[dst_len] = '\0'; \ + dst = _dst; \ + } while(0) + + FILE *rfdopen(int fildes, const RCHAR *mode) + { + char *amode = NULL; + W2A(mode, amode); + if(amode==NULL) return NULL; + return fdopen(fildes, amode); + } + + FILE *rfopen(const RCHAR *path, const RCHAR *mode) + { + char *amode = NULL; + char *apath = NULL; + + W2A(mode, amode); + if(amode==NULL) return NULL; + W2A(path, apath); + if(apath==NULL) return NULL; + + return fopen(apath, amode); + } + + FILE *rfreopen(const RCHAR *path, const RCHAR *mode, FILE *stream) + { + char *amode = NULL; + char *apath = NULL; + + W2A(mode, amode); + if(amode==NULL) return NULL; + W2A(path, apath); + if(apath==NULL) return NULL; + + return freopen(apath, amode, stream); + } + #endif /* !UNICODE */ |