Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Tools/Xabsl2/yabsl/vsyabsl/text.cpp

Go to the documentation of this file.

    00001
    00002 /***************************************************************************
    00003          Copyright (c) Microsoft Corporation, All rights reserved.
    00004     This code sample is provided "AS IS" without warranty of any kind,
    00005     it is not recommended for use in a production environment.
    00006 ***************************************************************************/
    00007
    00008 /*---------------------------------------------------------
    00009  String function library.
    00010 ---------------------------------------------------------*/
    00011 #include "common.h"
    00012 #include <windows.h>
    00013 #include "text.h"
    00014
    00015 UniStr  uniCopy( UniStr dest, CUniStr src, int maxLen )
    00016 {
    00017     if (!dest) return NULL;
    00018     if (!src)  return dest;
    00019     if (maxLen < 0) maxLen = uniLen(src);
    00020     if (maxLen <= 0) return dest;
    00021
    00022     wcsncpy( dest, src, maxLen );
    00023     dest[maxLen] = UNI('\0');
    00024     return dest;
    00025 }
    00026
    00027 AnsiStr  ansiCopy( AnsiStr dest, CAnsiStr src, int maxLen )
    00028 {
    00029     if (!dest) return NULL;
    00030     if (!src)  return dest;
    00031     if (maxLen < 0) maxLen = ansiLen(src);
    00032     if (maxLen <= 0) return dest;
    00033
    00034     strncpy( dest, src, maxLen );
    00035     dest[maxLen] = '\0';
    00036     return dest;
    00037 }
    00038
    00039 UniStr  uniCat( UniStr dest, CUniStr src, int maxLen )
    00040 {
    00041     if (!dest) return NULL;
    00042     if (!src)  return dest;
    00043
    00044     int len = uniLen(dest);
    00045     if (maxLen < 0)  maxLen = len + uniLen(src);
    00046     if (maxLen <= 0 || maxLen <= len) return dest;
    00047
    00048     wcsncat( dest, src, maxLen - len );
    00049     dest[maxLen] = UNI('\0');
    00050     return dest;
    00051 }
    00052
    00053 AnsiStr  ansiCat( AnsiStr dest, CAnsiStr src, int maxLen )
    00054 {
    00055     if (!dest) return NULL;
    00056     if (!src)  return dest;
    00057
    00058     int len = ansiLen(dest);
    00059     if (maxLen < 0)  maxLen = len + ansiLen(src);
    00060     if (maxLen <= 0 || maxLen <= len) return dest;
    00061
    00062     strncat( dest, src, maxLen - len );
    00063     dest[maxLen] = '\0';
    00064     return dest;
    00065 }
    00066
    00067
    00068 int ansiToUniLen( CAnsiStr s )
    00069 {
    00070     if (s == NULL)
    00071         return 0;
    00072     return (int)mbstowcs( NULL, s, -1 );
    00073 }
    00074
    00075 int uniToAnsiLen( CUniStr s )
    00076 {
    00077     if (s == NULL)
    00078         return 0;
    00079     return (int)wcstombs( NULL, s, -1 );
    00080 }
    00081
    00082 UniStr ansiToUniCopy( UniStr dest, CAnsiStr src, int maxLen )
    00083 {
    00084     if (!dest) return NULL;
    00085     dest[0] = UNI('\0');
    00086     if (!src || maxLen <= 0)  return dest;
    00087
    00088     mbstowcs( dest, src, maxLen+1 );
    00089     dest[maxLen] = UNI('\0');
    00090     return dest;
    00091 }
    00092
    00093 AnsiStr uniToAnsiCopy( AnsiStr dest, CUniStr src, int maxLen )
    00094 {
    00095     if (!dest) return NULL;
    00096     dest[0] = '\0';
    00097     if (!src || maxLen <= 0)  return dest;
    00098
    00099     wcstombs( dest, src, maxLen+1 );
    00100     dest[maxLen] = '\0';
    00101     return dest;
    00102 }
    00103
    00104 UniStr  ansiToUniCat( UniStr dest, CAnsiStr src, int maxLen )
    00105 {
    00106     if (!dest) return NULL;
    00107     if (!src)  return dest;
    00108
    00109     int len = uniLen(dest);
    00110     ansiToUniCopy( dest+len, src, maxLen - len );
    00111     return dest;
    00112 }
    00113
    00114 AnsiStr uniToAnsiCat( AnsiStr dest, CUniStr src, int maxLen )
    00115 {
    00116     if (!dest) return NULL;
    00117     if (!src)  return NULL;
    00118
    00119     int len = ansiLen(dest);
    00120     uniToAnsiCopy( dest+len, src, maxLen - len );
    00121     return dest;
    00122 }
    00123
    00124
    00125 UniStr ansiToUniDup( CAnsiStr s )
    00126 {
    00127     if (!s) return NULL;
    00128
    00129     int    len  = ansiToUniLen(s);
    00130     UniStr dest = (UniStr) malloc( (len+1)*sizeof(UniChar) );
    00131
    00132     return ansiToUniCopy( dest, s, len );
    00133 }
    00134
    00135 AnsiStr uniToAnsiDup( CUniStr s )
    00136 {
    00137     if (!s) return NULL;
    00138
    00139     int     len  = uniToAnsiLen(s);
    00140     AnsiStr dest = (AnsiStr) malloc( (len+1)*sizeof(AnsiChar) );
    00141
    00142     return uniToAnsiCopy( dest, s, len );
    00143 }
    00144
    00145
    00146
    00147 void bstrFree( BSTR& bstr )
    00148 {
    00149     if (bstr) SysFreeString(bstr);
    00150     bstr = NULL;
    00151 }
    00152
    00153 BSTR bstrCopy( BSTR& bstr, COleStr src, int maxLen )
    00154 {
    00155     if (maxLen<0)  maxLen = oleLen(src);
    00156     if (maxLen<=0) return NULL;
    00157
    00158     SysReAllocStringLen( &bstr, src, maxLen );
    00159     return bstr;
    00160 }
    00161
    00162 BSTR bstrCat( BSTR& bstr, COleStr ostr, int maxLen )
    00163 {
    00164     if (!ostr) return bstr;
    00165     if (maxLen < 0) maxLen = oleLen(ostr);
    00166     if (maxLen <= 0) return bstr;
    00167
    00168     int bstrlen = bstrLen(bstr);
    00169
    00170     BSTR temp = SysAllocStringLen( NULL,
    00171                     bstrlen + maxLen + 1 );
    00172     if (!temp) return bstr;
    00173
    00174     oleCopy( temp, bstr, bstrlen );
    00175     oleCat( temp, ostr, maxLen );
    00176     bstrFree(bstr);
    00177     bstr = temp;
    00178
    00179     return bstr;
    00180 }
    00181