Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. #if defined(UNICODE) && defined(ALLOW_WIN98)
  4. /* ***** BEGIN LICENSE BLOCK *****
  5.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6.  *
  7.  * The contents of this file are subject to the Mozilla Public License Version
  8.  * 1.1 (the "License"); you may not use this file except in compliance with
  9.  * the License. You may obtain a copy of the License at
  10.  * http://www.mozilla.org/MPL/
  11.  *
  12.  * Software distributed under the License is distributed on an "AS IS" basis,
  13.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14.  * for the specific language governing rights and limitations under the
  15.  * License.
  16.  *
  17.  * The Original Code is for Open Layer for Unicode (opencow).
  18.  *
  19.  * The Initial Developer of the Original Code is Brodie Thiesfield.
  20.  * Portions created by the Initial Developer are Copyright (C) 2004
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38. /************************************************************************
  39.  Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht
  40.  
  41.    This file is part of TZipMaster Version 1.9.
  42.  
  43.     TZipMaster is free software: you can redistribute it and/or modify
  44.     it under the terms of the GNU Lesser General Public License as published by
  45.     the Free Software Foundation, either version 3 of the License, or
  46.     (at your option) any later version.
  47.  
  48.     TZipMaster is distributed in the hope that it will be useful,
  49.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  50.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  51.     GNU Lesser General Public License for more details.
  52.  
  53.     You should have received a copy of the GNU Lesser General Public License
  54.     along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
  55.  
  56.     contact: problems@delphizip.org (include ZipMaster in the subject).
  57.     updates: http://www.delphizip.org
  58.     DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
  59. ************************************************************************/
  60.  
  61. //#include <windows.h>
  62. #include <stdlib.h>
  63.  
  64. #include "MbcsBuffer.h"
  65.  
  66. // ----------------------------------------------------------------------------
  67. // CMbcsBuffer
  68.  
  69. bool
  70. CMbcsBuffer::SetCapacity(
  71.     int aMinCapacity
  72.     )
  73. {
  74.     if (aMinCapacity > mBufferSize)
  75.     {
  76.         if (mBuffer != mStackBuffer)
  77.             ::free(mBuffer);
  78.  
  79.         mLength = 0;
  80.         mBufferSize = 0;
  81.  
  82.         mBuffer = (char *) ::malloc(aMinCapacity);
  83.         if (!mBuffer) {
  84.             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  85.             errno = ENOMEM;
  86.             return false;
  87.         }
  88.         *mBuffer = '\0';
  89.  
  90.         mBufferSize = aMinCapacity;
  91.     }
  92.  
  93.     return true;
  94. }
  95.  
  96. bool
  97. CMbcsBuffer::FromUnicode(
  98.     LPCWSTR aString,
  99.     int     aStringLen,
  100.     int     aMinCapacity)
  101. {
  102.     if (!aString) {
  103.         SetNull();
  104.         return true;
  105.     }
  106.  
  107.     if (aStringLen == -1)
  108.         aStringLen = ::lstrlenW(aString) + 1;
  109.  
  110.     int aRequiredLen = ::WideCharToMultiByte(CP_ACP, 0,
  111.         aString, aStringLen, NULL, 0, NULL, NULL);
  112.     if (aRequiredLen < 1) {
  113.         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  114.         errno = ENOMEM;
  115.         return false;
  116.     }
  117.  
  118.     if (aRequiredLen > aMinCapacity)
  119.         aMinCapacity = aRequiredLen;
  120.  
  121.     mLength = aRequiredLen - 1; // don't include the null byte
  122.  
  123.     if (!SetCapacity(aMinCapacity)) {
  124.         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  125.         errno = ENOMEM;
  126.         return false;
  127.     }
  128.  
  129.     ::WideCharToMultiByte(CP_ACP, 0, aString, aStringLen,
  130.         mBuffer, mBufferSize, NULL, NULL);
  131.  
  132.     return true;
  133. }
  134.  
  135. //int CMbcsBuffer::ToUnicode(LPWSTR lpszDst, int MaxLen)
  136. //{
  137. //    return ::MultiByteToWideChar(CP_ACP, 0, mbcsDst, -1, lpszDst, MaxLen) > 0;
  138. //}
  139. #endif
  140.  
  141.