Advertisement
Guest User

cmpayc

a guest
Oct 8th, 2009
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <locale.h>
  5. #include <wchar.h>
  6.  
  7. #define mbstowcsmacro(s,w,l) \
  8. l=mbstowcs(NULL, s, 0)+1; \
  9. w=calloc(l, sizeof(wchar_t)); \
  10. mbstowcs(w, s, l);
  11.  
  12. #define str_u_epilog(w1,w2) free(w2); free(w1);
  13.  
  14. int strcasecmp_u(const char *s1, const char *s2){
  15.     int l1,l2,result;
  16.     wchar_t *w1,*w2;
  17.     mbstowcsmacro(s1, w1, l1); mbstowcsmacro(s2, w2, l2);
  18.     result=wcscasecmp(w1, w2);
  19.     str_u_epilog(w1,w2);
  20.     return result;
  21. };
  22.  
  23. int strncasecmp_u(const char *s1, const char *s2, size_t n){
  24.     int l1,l2,result;
  25.     wchar_t *w1,*w2;
  26.     mbstowcsmacro(s1, w1, l1); mbstowcsmacro(s2, w2, l2);
  27.     result=wcsncasecmp(w1, w2, n);
  28.     str_u_epilog(w1,w2);
  29.     return result;
  30. };
  31.  
  32. size_t strcspn_u(const char *s, const char *reject){
  33.     size_t ls,lr,result;
  34.     wchar_t *ws,*wr;
  35.     mbstowcsmacro(s, ws, ls); mbstowcsmacro(reject, wr, lr);
  36.     result=wcscspn(ws, wr);
  37.     str_u_epilog(ws,wr);
  38.     return result;
  39. };
  40.  
  41. size_t strspn_u(const char *s, const char *accept) {
  42.     size_t ls, la, result;
  43.     wchar_t *ws, *wa;
  44.     mbstowcsmacro(s, ws, ls); mbstowcsmacro(accept, wa, la);
  45.     result=wcsspn(ws, wa);
  46.     str_u_epilog(ws,wa);
  47.     return result;
  48. };
  49.  
  50. size_t strlen_u(const char *s){
  51.     int l,result;
  52.     wchar_t *w;
  53.     mbstowcsmacro(s, w, l);
  54.     result=wcslen(w);
  55.     free(w);
  56.     return result;
  57. };
  58.  
  59. char *strtok_u_ptr; wchar_t *strtok_u_ptrw;
  60. //Yes, globals are wrong. But strtok() is just as wrong.
  61.  
  62. char *strtok_u(char *s, const char *delim){
  63.     size_t ls, ld, converted;
  64.     wchar_t *ws, *wd, *wresult;
  65.     char *result, buffer[MB_CUR_MAX];
  66.     int ccv, symbols_to_skip;
  67.     if (s!=NULL) {strtok_u_ptr=s;}; //otherwise use saved address
  68.     if (strtok_u_ptr==NULL) {return NULL;}; //nothing left
  69.     mbstowcsmacro(strtok_u_ptr, ws, ls); mbstowcsmacro(delim, wd, ld);
  70.     wresult=wcstok(ws, wd, &strtok_u_ptrw);
  71.     converted=wcstombs(strtok_u_ptr, ws, ls);
  72.     result=strtok_u_ptr; //correct if doesn't start with separator
  73.     // now run as many cycles, as there are separators at the start:
  74.     // wresult-ws
  75.     symbols_to_skip=((long int)wresult-(long int)ws)/sizeof(wchar_t);
  76.     for (ccv=0; ccv<symbols_to_skip; ccv++) {
  77.         //add separator length in multibyte form
  78.         result = (char*)( (long int)result + wctomb(buffer, ws[ccv]) );
  79.     };
  80.     // check if a separator was replaced with '\0'
  81.     if (converted<ls-1) {
  82.         strtok_u_ptr+=converted+1;
  83.         wcstombs(strtok_u_ptr, strtok_u_ptrw, ls-converted);
  84.     } else {
  85.         strtok_u_ptr=NULL; // no separators left
  86.     };
  87.     str_u_epilog(ws,wd);
  88.     return result;
  89. };
  90.  
  91. #define strchr_u(s,c) strstr(s,c)
  92. #define strrchr_u(s,c) rindex_u(s,c)
  93. #define index_u(s,c) strstr(s,c)
  94.  
  95. char *rindex_u(const char *s, const char *c){
  96.     const char *tmp1,*tmp2;
  97.     tmp1=s;
  98.     tmp2=NULL;
  99.     tmp1=strstr(tmp1,c);
  100.     while (tmp1!=NULL) {
  101.         tmp1=strstr(tmp1,c);
  102.         if (tmp1==NULL) { return (char *) tmp2; }
  103.         else { tmp2=tmp1; tmp1++; };
  104.     };
  105.     return (char *) tmp2;
  106. };
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement