Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h>
- #include <wchar.h>
- #define mbstowcsmacro(s,w,l) \
- l=mbstowcs(NULL, s, 0)+1; \
- w=calloc(l, sizeof(wchar_t)); \
- mbstowcs(w, s, l);
- #define str_u_epilog(w1,w2) free(w2); free(w1);
- int strcasecmp_u(const char *s1, const char *s2){
- int l1,l2,result;
- wchar_t *w1,*w2;
- mbstowcsmacro(s1, w1, l1); mbstowcsmacro(s2, w2, l2);
- result=wcscasecmp(w1, w2);
- str_u_epilog(w1,w2);
- return result;
- };
- int strncasecmp_u(const char *s1, const char *s2, size_t n){
- int l1,l2,result;
- wchar_t *w1,*w2;
- mbstowcsmacro(s1, w1, l1); mbstowcsmacro(s2, w2, l2);
- result=wcsncasecmp(w1, w2, n);
- str_u_epilog(w1,w2);
- return result;
- };
- size_t strcspn_u(const char *s, const char *reject){
- size_t ls,lr,result;
- wchar_t *ws,*wr;
- mbstowcsmacro(s, ws, ls); mbstowcsmacro(reject, wr, lr);
- result=wcscspn(ws, wr);
- str_u_epilog(ws,wr);
- return result;
- };
- size_t strspn_u(const char *s, const char *accept) {
- size_t ls, la, result;
- wchar_t *ws, *wa;
- mbstowcsmacro(s, ws, ls); mbstowcsmacro(accept, wa, la);
- result=wcsspn(ws, wa);
- str_u_epilog(ws,wa);
- return result;
- };
- size_t strlen_u(const char *s){
- int l,result;
- wchar_t *w;
- mbstowcsmacro(s, w, l);
- result=wcslen(w);
- free(w);
- return result;
- };
- char *strtok_u_ptr; wchar_t *strtok_u_ptrw;
- //Yes, globals are wrong. But strtok() is just as wrong.
- char *strtok_u(char *s, const char *delim){
- size_t ls, ld, converted;
- wchar_t *ws, *wd, *wresult;
- char *result, buffer[MB_CUR_MAX];
- int ccv, symbols_to_skip;
- if (s!=NULL) {strtok_u_ptr=s;}; //otherwise use saved address
- if (strtok_u_ptr==NULL) {return NULL;}; //nothing left
- mbstowcsmacro(strtok_u_ptr, ws, ls); mbstowcsmacro(delim, wd, ld);
- wresult=wcstok(ws, wd, &strtok_u_ptrw);
- converted=wcstombs(strtok_u_ptr, ws, ls);
- result=strtok_u_ptr; //correct if doesn't start with separator
- // now run as many cycles, as there are separators at the start:
- // wresult-ws
- symbols_to_skip=((long int)wresult-(long int)ws)/sizeof(wchar_t);
- for (ccv=0; ccv<symbols_to_skip; ccv++) {
- //add separator length in multibyte form
- result = (char*)( (long int)result + wctomb(buffer, ws[ccv]) );
- };
- // check if a separator was replaced with '\0'
- if (converted<ls-1) {
- strtok_u_ptr+=converted+1;
- wcstombs(strtok_u_ptr, strtok_u_ptrw, ls-converted);
- } else {
- strtok_u_ptr=NULL; // no separators left
- };
- str_u_epilog(ws,wd);
- return result;
- };
- #define strchr_u(s,c) strstr(s,c)
- #define strrchr_u(s,c) rindex_u(s,c)
- #define index_u(s,c) strstr(s,c)
- char *rindex_u(const char *s, const char *c){
- const char *tmp1,*tmp2;
- tmp1=s;
- tmp2=NULL;
- tmp1=strstr(tmp1,c);
- while (tmp1!=NULL) {
- tmp1=strstr(tmp1,c);
- if (tmp1==NULL) { return (char *) tmp2; }
- else { tmp2=tmp1; tmp1++; };
- };
- return (char *) tmp2;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement