【HSP2.61】Unicode(UTF-16) を扱ってみるV2

使い方はほとんど一緒だけど、引数で文字列を使って呼び出すところは、すべて変数を使うように変更してあります。

使い方はほとんど一緒だけど、引数で文字列を使って呼び出すところは、すべて変数を使うように変更してあります。

  • タグ:
  • タグはありません
#include "llmod.as"
#include "unicode.as"
#module
#deffunc replaceNullW val, int, int
	mref buf, 24
	mref size, 1
	mref r, 2
	sdim _buf, size
	memcpy _buf, buf, size, 0, 0
	sdim buf, size
	// 4byte NULLだった場合に抜ける
	j = 0
	repeat
		wpeek f1, _buf, cnt
		wpeek f2, _buf, cnt+2
		peek i, _buf, cnt
		if ((f1 == 0) && (f2 == 0)) : break
		if i == r {
			poke buf, j, 0
			poke buf, j+1, 0
			j+=2
		}else{
			poke buf, j, i
			j++
		}
	loop
return
#global
#module
dummyval = ""
dummyvalint = 0
#define WM_SETTEXT  0x000C
// W is the support Unicode(UTF-16)
#deffunc objprmW int, val
	// Get the first argument
	mref p1, 0
	// Gets the second argument
	mref p2, 25
	// Gets the handle of object
	_hspobjhandle p1
	handle = stat
	if handle == 0 : return -1
	// Gets a pointer to the variable
	getptr ptr, p2
	if ptr == 0 : return -2
	// Assignment SendMessageW function arguments
	prm = handle, WM_SETTEXT, 0, ptr
	// Call the SendMessageW function of the Win32API
	dllproc "SendMessageW", prm, 4, D_USER
return 0
#deffunc titleW val
	// Get the first argument
	mref p1, 24
	// Gets the window handle
	;_get_active_window handle
	mref bmscr, 67
	handle = bmscr.13
	if handle == 0 : return -1
	// Gets a pointer to the variable
	getptr ptr, p1
	if ptr == 0 : return -2
	// Assignment SendMessageW function arguments
	prm = handle, WM_SETTEXT, 0, ptr
	// Call the SendMessageW function of the Win32API
	dllproc "SendMessageW", prm, 4, D_USER
return 0
#define global strlenW(%1 = dummyvalint,%2) _strlenW %1, %2
#deffunc _strlenW val, val
	// Get argument
	mref p1, 16
	mref p2, 25
	// Gets a pointer to the variable
	getptr ptr, p2
	if ptr == 0 : return -1
	prm = ptr

	ll_libload msvcrt,"msvcrt"
	dllproc "wcslen", prm, 1, msvcrt
	p1 = dllret
return dllret
#deffunc strcatW val, val
	// Get argument
	mref p1, 24
	mref p2, 25
	// Gets a pointer to the variable
	getptr ptr1, p1
	getptr ptr2, p2
	if (ptr1 == 0) || (ptr2 == 0) : return -1
	
	prm = ptr1, ptr2 
	ll_libload msvcrt,"msvcrt"
	dllproc "wcscat", prm, 2, msvcrt
return 0
#define global dialogW(%1 = dummyval,%2=0,%3 = dummyval) _dialogW %1, %3, %2
#define MB_OK 0x00000000
#define MB_YESNO 0x00000004
#define MB_ICONINFORMATION 0x00000040
#define MB_ICONEXCLAMATION 0x00000030
#define OFN_FILEMUSTEXIST 0x00001000
#deffunc _dialogW val, val, int
	// Gets argument
	mref p1, 24
	mref p2, 25
	mref p3, 2
	// Call the original instruction, in the case of color selection
	if (p3 == 32) || (p3 == 33) : dialog "", p3 : return
	// MessageBox
	if (p3 == 0) || (p3 == 1) || (p3 == 2) || (p3 == 3){
		// Gets the window handle
		;_get_active_window handle
		mref bmscr, 67
		handle = bmscr.13
		if handle == 0 : return -1
		// Gets a pointer to the variable
		getptr ptr1, p1
		getptr ptr2, p2
		if p3 == 0 : uType = MB_OK | MB_ICONINFORMATION
		if p3 == 1 : uType = MB_OK | MB_ICONEXCLAMATION
		if p3 == 2 : uType = MB_YESNO | MB_ICONINFORMATION
		if p3 == 3 : uType = MB_YESNO | MB_ICONEXCLAMATION
		// Assignment MessageBoxW function arguments
		prm = handle, ptr1, ptr2, uType
		// Call the MessageBoxW function of the Win32API
		dllproc "MessageBoxW", prm, 4, D_USER
		return dllret
	}
	// File selection dialog
	if (p3 == 16) || (p3 == 17){
		mref _stat, 64
		mref _stat, 65
		// OPENFILENAME structure
		dim ofn, 22
		sdim filename, 1024
		sdim aplFilter, 1024

		//
		sdim type, 1024
		sdim p1c, 1024	// Variable copy
		sdim p2c, 1024	// Variable copy
		memcpy p1c, p1, 1024, 0, 0
		memcpy p2c, p2, 1024, 0, 0
		strlenW ,p1c
		if stat == 0 {
			
			sdim tmp
			tmp = "*.*"
			to_uni type, tmp, -1
			sdim tmp2, 1024
			tmp2 = "All files (*.*)@*.*@"
			to_uni aplFilter, tmp2, -1
			
			; Not work
			;replaceNullW aplFilter, 1024, '@'
		
			size = 1024
			r = '@'
			sdim _buf, size
			memcpy _buf, aplFilter, size, 0, 0
			sdim aplFilter, size
			// If it was NULL(4 bytes) all goes out
			j = 0
			repeat size - 4
				wpeek f1, _buf, cnt
				wpeek f2, _buf, cnt+2
				peek i, _buf, cnt
				if ((f1 == 0) && (f2 == 0)) : break
				if i == r {
					poke aplFilter, j, 0
					poke aplFilter, j+1, 0
					j+=2
				}else{
					poke aplFilter, j, i
					j++
				}
			loop
			;

		}else{
			strlenW ,p1c
			mes "size:"+stat
			tmp = "*."
			to_uni type, tmp, -1
			strcatW type, p1c

			;
			sdim tmp2, 1024
			sdim tmp2_u, 1024
			tmp2 = " ("
			to_uni tmp2_u, tmp2, -1
			sdim tmp3, 1024
			sdim tmp3_u, 1024
			tmp3 = ")@"
			to_uni tmp3_u, tmp3, -1
			sdim tmp4, 1024
			sdim tmp4_u, 1024
			tmp4 = "@All files (*.*)@*.*@"
			to_uni tmp4_u, tmp4, -1

			strcatW aplFilter, p2c
			strcatW aplFilter, tmp2_u
			strcatW aplFilter, type
			strcatW aplFilter, tmp3_u
			strcatW aplFilter, type
			strcatW aplFilter, tmp4_u
			;

			; Not work
			;replaceNullW aplFilter, 1024, '@'
		
			size = 1024
			r = '@'
			sdim _buf, size
			memcpy _buf, aplFilter, size, 0, 0
			sdim aplFilter, size
			// If it was NULL(4 bytes) all goes out
			j = 0
			repeat size - 4
				wpeek f1, _buf, cnt
				wpeek f2, _buf, cnt+2
				peek i, _buf, cnt/2*2
				if ((f1 == 0) && (f2 == 0)) : break
				if i == r {
					poke aplFilter, j, 0
					//poke aplFilter, j+1, 0
					j++
				}else{
					peek i, _buf, cnt
					poke aplFilter, j, i
					j++
				}
			loop
			;
		}
		strlenW , type
		memcpy filename, type, stat * 2, 0, 0

		//
        	ofn.0 = 88 ; lStructSize
        	ofn.1 = bmscr.13 ; hwndOwner
        	ofn.2 = bmscr.14 ; hInstance
		getptr ofn.3, aplFilter ; lpstrFilter
		;getptr ofn.4, usrFilter ; lpstrCustomFilter
        	ofn.5 = 1024 ; nMaxCustFilter
        	ofn.6 = 0 ; nFilterIndex
		getptr ofn.7, filename ; lpstrFile
        	ofn.8 = 1024  ; nMaxFile
        	ofn.9 = 0 ; lpstrFileTitle
        	ofn.10 = 0 ; nMaxFileTitle
        	ofn.11 = 0 ; lpstrInitialDir
        	ofn.12 = 0 ; lpstrTitle
      		ofn.13 = OFN_FILEMUSTEXIST | 2; Flags
		// Assignment GetOpenFileNameW function argument
		getptr prm, ofn
		// Call the GetOpenFileNameW function of the Win32API
		if p3 == 16{
			dllproc "GetOpenFileNameW", prm, 1, D_COMDLG
		}else : if p3 == 17{
			dllproc "GetSaveFileNameW", prm, 1, D_COMDLG
		}
		if dllret != 0{
			sdim refstrW@, 1024
			memcpy refstrW@, filename, 1024, 0, 0
		}
	}
return dllret
#global
#module draw
#define global printW mesW
#define global mesW(%1) draw_init : getptr string_ptr@draw, %1 : _mesW string_ptr@draw
#deffunc draw_init
	string_ptr = 0
return
#deffunc _mesW
	mref bmscr, 67
	lpRect = csrx, csry, winx, winy
	getptr lpRect_ptr, lpRect
	prm = bmscr.4, string_ptr, -1, lpRect_ptr, 0
	dllproc "DrawTextW", prm, 5, D_USER
	pos csrx, csry + stat
return
#global
#module file
#define GENERIC_WRITE 0x40000000
#define GENERIC_READ 0x80000000
#define OPEN_EXISTING 3
#define FILE_ATTRIBUTE_NORMAL 0x00000080
#define FILE_BEGIN 1
#define FILE_CURRENT 1
#deffunc init_file
	file_u_ptr = 0
	buf_ptr = 0
	size = 0
	offset = 0
return
#define global bloadW(%1,%2,%3,%4=0) init_file : getptr file_u_ptr@file, %1 : getptr buf_ptr@file, %2 : size@file = %3 : offset@file = %4 : _bloadW 0
#define global bsaveW(%1,%2,%3,%4=0) init_file : getptr file_u_ptr@file, %1 : getptr buf_ptr@file, %2 : size@file = %3 : offset@file = %4 : _bloadW 1
#define global existW(%1) init_file : getptr file_u_ptr@file, %1 : _bloadW 2
#deffunc _bloadW int//, int, int, int
/*
	mref file_u_ptr, 0
	mref buf_ptr, 1
	mref size, 2
	mref offset, 3
*/
	mref mode, 0

	// Assignment CreateFileW function arguments
	if mode == 0{
		prm = file_u_ptr.0, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
	}
	if mode == 1{
		prm = file_u_ptr.0, GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
	}
	if mode == 2{
		prm = file_u_ptr.0, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
	}
	// Call the CreateFileW function of the Win32API

	dllproc "CreateFileW", prm, 7, D_KERNEL
	hFile = stat
	if hFile == 0 : return -1

	if mode == 2{
		prm = hFile, 0
		dllproc "GetFileSize", prm, 2, D_KERNEL
		fsize = stat
		prm = hFile
		dllproc "CloseHandle", prm, 1, D_KERNEL
		strsizeW@ = fsize
		return fsize
	}

	readsize = 0
	getptr readsize_ptr, readsize

	prm = hFile, offset, 0, FILE_BEGIN
	dllproc "SetFilePointer", prm, 4, D_KERNEL

	prm = hFile, buf_ptr, size, readsize_ptr, 0
	if mode == 0{
		dllproc "ReadFile", prm, 5, D_KERNEL
	}else{
		dllproc "WriteFile", prm, 5, D_KERNEL
	}
	
	prm = hFile
	dllproc "CloseHandle", prm, 1, D_KERNEL

	strsizeW@ = readsize
	
return readsize
#global