| 1 | /* $Id: RESSTRING.C,v 1.1 1999-05-24 20:19:59 ktk Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | *
|
|---|
| 5 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 6 | *
|
|---|
| 7 | */
|
|---|
| 8 | /*
|
|---|
| 9 | * Win32 resource string functions for OS/2
|
|---|
| 10 | *
|
|---|
| 11 | * Copyright 1998 Sander van Leeuwen
|
|---|
| 12 | *
|
|---|
| 13 | */
|
|---|
| 14 | #define INCL_BASE
|
|---|
| 15 | #define INCL_WIN
|
|---|
| 16 | #define INCL_WINERRORS
|
|---|
| 17 | #define INCL_DOSFILEMGR
|
|---|
| 18 | #include <os2.h>
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 | #include <stdlib.h>
|
|---|
| 21 | #include <string.h>
|
|---|
| 22 | #include "misc.h"
|
|---|
| 23 | #include "resstring.h"
|
|---|
| 24 | #include "win32util.h"
|
|---|
| 25 | #include "unicode.h"
|
|---|
| 26 |
|
|---|
| 27 | static char *UnicodeToAsciiN(WCHAR *wstring, char *astring, int maxlen);
|
|---|
| 28 |
|
|---|
| 29 | /******************************************************************************/
|
|---|
| 30 | //Strings stored as original: WORD len
|
|---|
| 31 | // UNICODE string (not null terminated)
|
|---|
| 32 | /******************************************************************************/
|
|---|
| 33 | int OS2LoadStringAscii(HINSTANCE hinst, UINT wID, LPSTR lpBuffer, int cchBuffer)
|
|---|
| 34 | {
|
|---|
| 35 | APIRET rc;
|
|---|
| 36 | WCHAR *szbuf;
|
|---|
| 37 | char modname[128];
|
|---|
| 38 | HMODULE hmodule;
|
|---|
| 39 |
|
|---|
| 40 | if(hinst != 0 && hinst != -1) {
|
|---|
| 41 | if(Win32QueryModuleName(hinst, modname, sizeof(modname)) == 0) {
|
|---|
| 42 | #ifdef DEBUG
|
|---|
| 43 | WriteLog("Can't determine handle of dll %X\n", hinst);
|
|---|
| 44 | #endif
|
|---|
| 45 | return(0);
|
|---|
| 46 | }
|
|---|
| 47 | rc = DosQueryModuleHandle(modname, &hmodule);
|
|---|
| 48 | if(rc) {
|
|---|
| 49 | #ifdef DEBUG
|
|---|
| 50 | WriteLog("Can't determine handle of dll %s\n", modname);
|
|---|
| 51 | #endif
|
|---|
| 52 | return(0);
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | else hmodule = 0;
|
|---|
| 56 |
|
|---|
| 57 | rc = DosGetResource(hmodule, RT_RCDATA, wID, (PPVOID)&szbuf);
|
|---|
| 58 | if(rc) {
|
|---|
| 59 | #ifdef DEBUG
|
|---|
| 60 | WriteLog("OS2LoadString failed with rc %d\n", rc);
|
|---|
| 61 | #endif
|
|---|
| 62 | if(lpBuffer) *lpBuffer = 0;
|
|---|
| 63 | return(0); //TODO: SetLastError!
|
|---|
| 64 | }
|
|---|
| 65 | UnicodeToAsciiN(szbuf+1, lpBuffer, min((*szbuf), cchBuffer-1));
|
|---|
| 66 | #ifdef DEBUG
|
|---|
| 67 | WriteLog("OS2LoadString retrieved string '%s' (size %d)\n", lpBuffer, (int)*szbuf);
|
|---|
| 68 | #endif
|
|---|
| 69 | DosFreeResource(szbuf);
|
|---|
| 70 | return(strlen(lpBuffer)); //not including null terminator!
|
|---|
| 71 | }
|
|---|
| 72 | //******************************************************************************
|
|---|
| 73 | //******************************************************************************
|
|---|
| 74 | int OS2LoadStringUnicode(HINSTANCE hinst, UINT wID, WCHAR *lpBuffer, int cchBuffer)
|
|---|
| 75 | {
|
|---|
| 76 | APIRET rc;
|
|---|
| 77 | WCHAR *szbuf;
|
|---|
| 78 | char modname[128];
|
|---|
| 79 | HMODULE hmodule;
|
|---|
| 80 | #ifdef DEBUG
|
|---|
| 81 | char *astring;
|
|---|
| 82 | #endif
|
|---|
| 83 |
|
|---|
| 84 | if(hinst != 0 && hinst != -1) {
|
|---|
| 85 | if(Win32QueryModuleName(hinst, modname, sizeof(modname)) == 0) {
|
|---|
| 86 | #ifdef DEBUG
|
|---|
| 87 | WriteLog("Can't determine handle of dll %X\n", hinst);
|
|---|
| 88 | #endif
|
|---|
| 89 | return(0);
|
|---|
| 90 | }
|
|---|
| 91 | rc = DosQueryModuleHandle(modname, &hmodule);
|
|---|
| 92 | if(rc) {
|
|---|
| 93 | #ifdef DEBUG
|
|---|
| 94 | WriteLog("Can't determine handle of dll %s\n", modname);
|
|---|
| 95 | #endif
|
|---|
| 96 | return(0);
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | else hmodule = 0;
|
|---|
| 100 |
|
|---|
| 101 | rc = DosGetResource(hmodule, RT_RCDATA, wID, (PPVOID)&szbuf);
|
|---|
| 102 | if(rc) {
|
|---|
| 103 | #ifdef DEBUG
|
|---|
| 104 | WriteLog("OS2LoadString failed with rc %d\n", rc);
|
|---|
| 105 | #endif
|
|---|
| 106 | if(lpBuffer) *lpBuffer = 0;
|
|---|
| 107 | return(0); //TODO: SetLastError!
|
|---|
| 108 | }
|
|---|
| 109 | memcpy(lpBuffer, szbuf+1, min((*szbuf), cchBuffer-1)*sizeof(WCHAR));
|
|---|
| 110 | lpBuffer[min((*szbuf), cchBuffer-1)] = 0;
|
|---|
| 111 |
|
|---|
| 112 | #ifdef DEBUG
|
|---|
| 113 | astring = UnicodeToAsciiString(lpBuffer);
|
|---|
| 114 | WriteLog("OS2LoadStringW retrieved string '%s' of size %d\n", astring, (int)*szbuf);
|
|---|
| 115 | FreeAsciiString(astring);
|
|---|
| 116 | #endif
|
|---|
| 117 | DosFreeResource(szbuf);
|
|---|
| 118 | return(UniStrlen(lpBuffer)); //not including null terminator!
|
|---|
| 119 | }
|
|---|
| 120 | //******************************************************************************
|
|---|
| 121 | //******************************************************************************
|
|---|
| 122 | static char *UnicodeToAsciiN(WCHAR *wstring, char *astring, int maxlen)
|
|---|
| 123 | {
|
|---|
| 124 | int i;
|
|---|
| 125 |
|
|---|
| 126 | for(i=0;i<maxlen;i++) {
|
|---|
| 127 | astring[i] = (UCHAR)wstring[i];
|
|---|
| 128 | }
|
|---|
| 129 | astring[maxlen] = 0;
|
|---|
| 130 | return(astring);
|
|---|
| 131 | }
|
|---|
| 132 | /******************************************************************************/
|
|---|
| 133 | /******************************************************************************/
|
|---|
| 134 |
|
|---|