Module CharTranslate.pkg

     1//*************************************************************************
     2//*
     3//*  Copyright (c) 2001 Data Access Corporation, Miami Florida,
     4//*  All rights reserved.
     5//*  DataFlex is a registered trademark of Data Access Corporation.
     6//*
     7//*************************************************************************
     8//*
     9//*  Module Name:
    10//*      CharTranslate.pkg
    11//*
    12//*  Creator:
    13//*      SF
    14//*
    15//*
    16//*  Purpose:
    17//*      Various global functions to translate OEM to UTF.
    18//*
    19//*************************************************************************
    20
    21Use Windows.pkg
    22
    23#REPLACE CP_ACP                    0           // default to ANSI code page
    24#REPLACE CP_OEMCP                  1           // default to OEM  code page
    25#REPLACE CP_MACCP                  2           // default to MAC  code page
    26#REPLACE CP_THREAD_ACP             3           // current thread's ANSI code page
    27#REPLACE CP_SYMBOL                 42          // SYMBOL translations
    28#REPLACE CP_UTF7                   65000       // UTF-7 translation
    29#REPLACE CP_UTF8                   65001       // UTF-8 translation
    30
    31External_Function32 MultiByteToWideChar "MultiByteToWideChar" kernel32.dll;
    32    DWORD dwCodePage;
    33    DWORD dwFlags;
    34    Pointer pMultiByteStr;
    35    Integer cbMultiByte;
    36    Pointer pWideStr;
    37    Integer cchWideChar;
    38    Returns Integer
    39
    40External_Function32 WideCharToMultiByte "WideCharToMultiByte" kernel32.dll;
    41    DWORD dwCodePage;
    42    DWORD dwFlags;
    43    Pointer pWideStr;
    44    Integer cchWideChar;
    45    Pointer pMultiByteStr;
    46    Integer cbMultiByte;
    47    Pointer pDefaultChar;
    48    Pointer pUsedDefaultChar;
    49    Returns Integer
    50
    51//dwCodePage = Multi Byte Code Page to convert from
    52//pMultiBuf  = Pointer to Multi Byte string
    53//iLen       = Length in bytes of Multi Byte string
    54//ppWideBuf  = Contents of ppWideBuf contains a pointer to the destination buffer on return
    55Function MultiToWideBuffer Global DWORD dwCodePage Address pMultiBuf Integer iLen Address ppWideBuf Returns Integer
    56    Integer iWideSize
    57    Integer iVoid
    58    Address pWideBuf
    59    Move (MultiByteToWideChar(dwCodePage,0,pMultiBuf,iLen,0,0)) to iWideSize
    60    If (iWideSize=0);
    61        Function_Return 0
    62    Move (Alloc((iWideSize+1)*2)) to pWideBuf
    63    If (pWideBuf=0);
    64        Function_Return 0
    65    Move (MultiByteToWideChar(dwCodePage,0,pMultiBuf,iLen,pWideBuf,iWideSize)) to iWideSize
    66    If (iWideSize=0) ;
    67        Move (Free(pWideBuf)) to iVoid
    68    else begin
    69        Move (StoreW(pWideBuf,(iWideSize*2),0)) to iVoid
    70        Move (StoreDW(ppWideBuf,0,pWideBuf)) to iVoid
    71    end
    72    Function_Return iWideSize
    73End_Function
    74
    75//dwCodePage = Multi Byte Code Page to convert to
    76//pWideBuf   = Pointer to Wide Character string
    77//iLen       = Length in characters of Wide Character string
    78//ppMultiBuf = Contents of ppMultiBuf contains a pointer to the destination buffer on return
    79Function WideToMultiBuffer Global DWORD dwCodePage Address pWideBuf Integer iLen Address ppMultiBuf Returns Integer
    80    Integer iMultiSize
    81    Address pMultiBuf
    82    Integer iVoid
    83    Move (WideCharToMultiByte(dwCodePage,0,pWideBuf,iLen,0,0,0,0)) to iMultiSize
    84    If (iMultiSize=0);
    85        Function_Return 0
    86    Move (Alloc(iMultiSize+1)) to pMultiBuf
    87    If (pMultiBuf=0);
    88        Function_Return 0
    89    Move (WideCharToMultiByte(dwCodePage,0,pWideBuf,iLen,pMultiBuf,iMultiSize,0,0)) to iMultiSize
    90    If (iMultiSize=0) ;
    91        Move (Free(pMultiBuf)) to iVoid
    92    else begin
    93        Move (StoreC(pMultiBuf,iMultiSize,0)) to iVoid
    94        Move (StoreDW(ppMultiBuf,0,pMultiBuf)) to iVoid
    95    end
    96    Function_Return iMultiSize
    97End_Function
    98
    99//Converts a UTF-8 string to OEM
   100//pUtf8Buf   = Pointer to UTF-8 string
   101//iLen       = Length of UTF-8 string in bytes
   102//Returns pointer to OEM string, use free() to deallocate buffer when it's no longer needed
   103Function Utf8ToOemBuffer Global Address pUtf8Buf Integer iLen Returns Address
   104    Address pWideBuf
   105    Address pOemBuf
   106    Integer iWideBufLen
   107    Integer iMultiBufLen
   108    Integer iVoid
   109    Move 0 to pWideBuf
   110    Move 0 to pOemBuf
   111    Move (MultiToWideBuffer(CP_UTF8,pUtf8Buf,iLen,AddressOf(pWideBuf))) to iWideBufLen
   112    If (iWideBufLen=0);
   113        Function_Return 0
   114    Move (WideToMultiBuffer(CP_OEMCP,pWideBuf,iWideBufLen,AddressOf(pOemBuf))) to iMultiBufLen
   115    Move (Free(pWideBuf)) to iVoid
   116    Function_Return pOemBuf
   117End_Function
   118
   119//Converts a OEM string to UTF-8
   120//pOemBuf    = Pointer to OEM string
   121//iLen       = Length of OEM string in bytes
   122//Returns pointer to UTF-8 string, use free() to deallocate buffer when it's no longer needed
   123Function OemToUtf8Buffer Global Address pOemBuf Integer iLen Returns Address
   124    Address pWideBuf
   125    Address pUtf8Buf
   126    Integer iWideBufLen
   127    Integer iMultiBufLen
   128    Integer iVoid
   129    Move 0 to pWideBuf
   130    Move 0 to pUtf8Buf
   131    Move (MultiToWideBuffer(CP_OEMCP,pOemBuf,iLen,AddressOf(pWideBuf))) to iWideBufLen
   132    If (iWideBufLen=0);
   133        Function_Return 0
   134    Move (WideToMultiBuffer(CP_UTF8,pWideBuf,iWideBufLen,AddressOf(pUtf8Buf))) to iMultiBufLen
   135    Move (Free(pWideBuf)) to iVoid
   136    Function_Return pUtf8Buf
   137End_Function
   138
   139
   140//Converts a OEM string to UTF-16
   141//pOemBuf    = Pointer to OEM string
   142//iLen       = Length of OEM string in bytes
   143//Returns pointer to UTF-16 string, use free() to deallocate buffer when it's no longer needed
   144Function OemToUtf16Buffer Global Address pOemBuf Integer iLen Returns Address
   145    Address pWideBuf
   146    Integer iWideBufLen
   147    Move 0 to pWideBuf
   148    Move (MultiToWideBuffer(CP_OEMCP,pOemBuf,iLen,AddressOf(pWideBuf))) to iWideBufLen
   149    If (iWideBufLen=0) Begin
   150        Function_Return 0
   151    End
   152    Function_Return pWideBuf
   153End_Function
   154
   155//Converts a UTF-16 string to OEM
   156//pWideBuf     = Pointer to UTF-16 string
   157//iWideBufLen  = Length of UTF-16 in characters
   158//Returns pointer to OEM string, use free() to deallocate buffer when it's no longer needed
   159Function Utf16ToOemBuffer Global Address pWideBuf Integer iWideBufLen Returns Address
   160    Address pOemBuf
   161    Integer iMultiBufLen
   162    Move 0 to pOemBuf
   163    Move (WideToMultiBuffer(CP_OEMCP,pWideBuf,iWideBufLen,AddressOf(pOemBuf))) to iMultiBufLen
   164    If (iMultiBufLen=0) Begin
   165        Function_Return 0
   166    End
   167    Function_Return pOemBuf
   168End_Function
   169
   170