Advertisement
jacknpoe

CONVERTER: from wchar_t* to char*, inverse + string classes

Aug 1st, 2015
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. // CONVERTER.H
  2. // namespace: jacknpoe
  3. // define JACKNPOE_CPP11 for C++11 compilers
  4.  
  5. /*########################### CONVERTER (DEPOSIT VERSION 0.0 2013-10-30)
  6. / Version: 0.0 (deposit)
  7. / Old Versions:
  8. / -
  9. */
  10.  
  11. #ifndef JACKNPOE_CONVERTER_H_
  12. #define JACKNPOE_CONVERTER_H_
  13.  
  14. #include "tjacknpoe.h"  // just definitions
  15. #include <string>
  16.  
  17. namespace jacknpoe {
  18.     char* pwchar_ttopchar( wchar_t* data);
  19.     wchar_t* pchartopwchar_t( char* data);
  20.     char* wstringtopchar( std::wstring data);
  21.      
  22. #ifdef JACKNPOE_CPP11
  23.     char* pchar16_ttopchar( char16_t* data);
  24.     char16_t* pchartopchar16_t( char* data);
  25.     char* pchar32_ttopchar( char32_t* data);
  26.     char32_t* pchartopchar32_t( char* data);
  27.     char* u16stringtopchar( std::u16string data);
  28.     char* u32stringtopchar( std::u32string data);
  29. #endif
  30. }
  31.  
  32. #endif
  33.  
  34. ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
  35. // CONVERTER.CPP (yes, it's a CPP library)
  36. // namespace: jacknpoe
  37. // define JACKNPOE_CPP11 for C++11 compilers
  38.  
  39. #include<cstdlib>
  40. #include<cstring>
  41. #include<string>
  42. #include "converter.h"
  43.  
  44. namespace jacknpoe {
  45.     char* pwchar_ttopchar( wchar_t* data) {
  46.         char *buffer; long size;
  47.         size = wcslen( data);
  48.         if( ( buffer = (char *) malloc( ( size + 1) * sizeof( char)) ) == NULL) return NULL;
  49.         for( long index = 0; index <= size; index++)
  50.             buffer[ index] = (char) data[ index];
  51.         return buffer;
  52.     }
  53.    
  54.     wchar_t* pchartopwchar_t( char* data) {
  55.         wchar_t *buffer; long size;
  56.         size = strlen( data);
  57.         if( ( buffer = (wchar_t *) malloc( ( size + 1) * sizeof( wchar_t)) ) == NULL) return NULL;
  58.         for( long index = 0; index <= size; index++)
  59.             buffer[ index] = (wchar_t) data[ index];
  60.         return buffer;
  61.     }
  62.  
  63.     char* wstringtopchar( std::wstring data) {
  64.         char *buffer; long size;
  65.         size =  data.size();
  66.         if( ( buffer = (char *) malloc( ( size + 1) * sizeof( char)) ) == NULL) return NULL;
  67.         for( long index = 0; index <= size; index++)
  68.             buffer[ index] = data[ index];
  69.         return buffer;
  70.     }
  71.  
  72.     #ifdef JACKNPOE_CPP11
  73.     char* pchar16_ttopchar( char16_t* data) {
  74.         char *buffer; long size;
  75.         size = std::char_traits<char16_t>::length( data);
  76.         if( ( buffer = (char *) malloc( ( size + 1) * sizeof( char)) ) == NULL) return NULL;
  77.         for( long index = 0; index <= size; index++)
  78.             buffer[ index] = (char) data[ index];
  79.         return buffer;
  80.     }
  81.    
  82.     char16_t* pchartopchar16_t( char* data) {
  83.         char16_t *buffer; long size;
  84.         size = strlen( data);
  85.         if( ( buffer = (char16_t *) malloc( ( size + 1) * sizeof( char16_t)) ) == NULL) return NULL;
  86.         for( long index = 0; index <= size; index++)
  87.             buffer[ index] = (char16_t) data[ index];
  88.         return buffer;
  89.     }
  90.    
  91.     char* pchar32_ttopchar( char32_t* data) {
  92.         char *buffer; long size;
  93.         size = std::char_traits<char32_t>::length( data);
  94.         if( ( buffer = (char *) malloc( ( size + 1) * sizeof( char)) ) == NULL) return NULL;
  95.         for( long index = 0; index <= size; index++)
  96.             buffer[ index] = (char) data[ index];
  97.         return buffer;
  98.     }
  99.    
  100.     char32_t* pchartopchar32_t( char* data){
  101.         char32_t *buffer; long size;
  102.         size = strlen( data);
  103.         if( ( buffer = (char32_t *) malloc( ( size + 1) * sizeof( char32_t)) ) == NULL) return NULL;
  104.         for( long index = 0; index <= size; index++)
  105.             buffer[ index] = (char32_t) data[ index];
  106.         return buffer;
  107.     }
  108.  
  109.     char* u16stringtopchar( std::u16string data) {
  110.         char *buffer; long size;
  111.         size =  data.size();
  112.         if( ( buffer = (char *) malloc( ( size + 1) * sizeof( char)) ) == NULL) return NULL;
  113.         for( long index = 0; index <= size; index++)
  114.             buffer[ index] = data[ index];
  115.         return buffer;
  116.     }
  117.  
  118.     char* u32stringtopchar( std::u32string data) {
  119.         char *buffer; long size;
  120.         size =  data.size();
  121.         if( ( buffer = (char *) malloc( ( size + 1) * sizeof( char)) ) == NULL) return NULL;
  122.         for( long index = 0; index <= size; index++)
  123.             buffer[ index] = data[ index];
  124.         return buffer;
  125.     }
  126.     #endif
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement