Subversion Repositories filter_foundry

Rev

Rev 522 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
  3.     Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.net
  4.     Copyright (C) 2018-2022 Daniel Marschall, ViaThinkSoft
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19. */
  20.  
  21. #include "ff.h"
  22.  
  23. void strcpy_advance_id(TCHAR** str, int msgid) {
  24.     int len = FF_GetMsg(*str, msgid);
  25.     *str += len;
  26. }
  27.  
  28. // Attention: No bounds checking!
  29. int FF_GetMsg(TCHAR* ret, int MsgId) {
  30. #ifdef WIN_ENV
  31.  
  32. #ifdef UNICODE
  33.     TCHAR* szMsg;
  34.     int len;
  35.     len = LoadString(hDllInstance, MsgId, (LPTSTR)&szMsg, 0);
  36.     if (len == 0) return 0; // resource not found
  37.     if (ret != NULL) {
  38.         LoadString(hDllInstance, MsgId, ret, len + 1);
  39.     }
  40.     return len;
  41. #else
  42.     // LoadStringA is either broken or badly documented!
  43.     // The documentation says that you receive a read-only string reference as well as the length as return value if cchBufferMax==0.
  44.     // Reality shows that return value is -1 if cchBufferMax==0.
  45.     // https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/8d8c5382-1867-460a-a18f-70dc425ffe2f/loadstring-not-behaving-as-documented?forum=windowssdk
  46.     // We cannot receive a read-only memory using LoadStringA, because the WinAPI needs to do a Unicode-to-ANSI convertion,
  47.     // so we must define some max limit.
  48.     TCHAR szMsg[4096];
  49.     int len;
  50.     len = LoadString(hDllInstance, MsgId, (LPTSTR)&szMsg, 4096);
  51.     if (len == 0) return 0; // resource not found
  52.     if (ret != NULL) {
  53.         LoadString(hDllInstance, MsgId, ret, len + 1);
  54.     }
  55.     return len;
  56. #endif
  57.  
  58. #else
  59.         Str255 msg;
  60.         GetIndString(msg, 1000, MsgId);
  61.     myp2cstrcpy(ret, msg);
  62. #endif
  63. }
  64.  
  65. // Attention: Requires FF_GetMsg_Free(), otherwise memory is leaked
  66. TCHAR* FF_GetMsg_Cpy(int MsgId) {
  67. #ifdef WIN_ENV
  68.     int len;
  69.     TCHAR* ret;
  70.     len = FF_GetMsg(NULL, MsgId);
  71.     if (len == 0) return NULL; // resource not found
  72.     ret = (TCHAR*)malloc((len+1) * sizeof(TCHAR));
  73.     if (ret == NULL) return NULL;
  74.     LoadString(hDllInstance, MsgId, ret, len+1);
  75.     return ret;
  76. #else
  77.     Str255 msg;
  78.     TCHAR* ret;
  79.     ret = (TCHAR*)malloc((len + 1) * sizeof(TCHAR));
  80.     GetIndString(msg, 1000, MsgId);
  81.     myp2cstrcpy(ret, msg);
  82.     return ret;
  83. #endif
  84. }
  85.  
  86. void FF_GetMsg_Free(TCHAR* str) {
  87.     free(str);
  88. }
  89.