OSDN Git Service

amodiからの文字位置情報が二桁以上あると正しく取得できていなかった
[dokopop/dokopop.git] / DCHookTest / mustr.cpp
1 #include <windows.h>
2 #pragma hdrstop
3 #include "mustr.h"
4
5 __mustr::__mustr( const char *str )
6 {
7         constructor(str, str ? strlen(str) : 0);
8 }
9 __mustr::__mustr( const wchar_t *str )
10 {
11         constructor(str, str ? wcslen(str) : 0);
12 }
13 void __mustr::constructor(const char *str, int len)
14 {
15         buffer = (void*)str;
16         length = len;
17         type = 1;
18         newbuffer = NULL;
19 }
20 void __mustr::constructor(const wchar_t *str, int len)
21 {
22         buffer = (void*)str;
23         length = len;
24         type = 2;
25         newbuffer = NULL;
26 }
27 __mustr::~__mustr()
28 {
29         if ( newbuffer ) delete[] newbuffer;
30 }
31 __mustr::operator char *()
32 {
33         if ( type == 1 ) return (char*)buffer;
34         if ( newbuffer ) return (char*)newbuffer;
35
36         // Unicode to Ansi
37
38         newbuffer = new char[ length*2 + 1 ];
39         int size = WideCharToMultiByte( CP_ACP, 0, (wchar_t*)buffer, length, newbuffer, length*2, NULL, NULL );
40         newbuffer[size] = '\0';
41         return (char*)newbuffer;
42 }
43 __mustr::operator wchar_t *()
44 {
45         if ( type == 2 ) return (wchar_t*)buffer;
46         if ( newbuffer ) return (wchar_t*)newbuffer;
47
48         // Ansi to Unicode
49
50         *(wchar_t**)&newbuffer = new wchar_t[ length*2 + 1 ];
51         int size = MultiByteToWideChar( CP_ACP, 0, (char*)buffer, length, (wchar_t*)newbuffer, length*2 );
52         ((wchar_t*)newbuffer)[size] = '\0';
53         return (wchar_t*)newbuffer;
54 }