ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

string.c
Go to the documentation of this file.
00001 /*
00002  * COPYRIGHT:   See COPYING in the top level directory
00003  * PROJECT:     ReactOS DNS Shared Library
00004  * FILE:        lib/dnslib/string.c
00005  * PURPOSE:     functions for string manipulation and conversion.
00006  */
00007 
00008 /* INCLUDES ******************************************************************/
00009 #include "precomp.h"
00010 
00011 /* DATA **********************************************************************/
00012 
00013 /* FUNCTIONS *****************************************************************/
00014 
00015 ULONG
00016 WINAPI
00017 Dns_StringCopy(OUT PVOID Destination,
00018                IN OUT PULONG DestinationSize,
00019                IN PVOID String,
00020                IN ULONG StringSize OPTIONAL,
00021                IN DWORD InputType,
00022                IN DWORD OutputType)
00023 {
00024     ULONG DestSize;
00025     ULONG OutputSize = 0;
00026 
00027     /* Check if the caller already gave us the string size */
00028     if (!StringSize)
00029     {
00030         /* He didn't, get the input type */
00031         if (InputType == UnicodeString)
00032         {
00033             /* Unicode string, calculate the size */
00034             StringSize = (ULONG)wcslen((LPWSTR)String);
00035         }
00036         else
00037         {
00038             /* ANSI or UTF-8 sting, get the size */
00039             StringSize = (ULONG)strlen((LPSTR)String);
00040         }
00041     }
00042 
00043     /* Check if we have a limit on the desination size */
00044     if (DestinationSize)
00045     {
00046         /* Make sure that we can respect it */
00047         DestSize = Dns_GetBufferLengthForStringCopy(String,
00048                                                     StringSize,
00049                                                     InputType,
00050                                                     OutputType);
00051         if (*DestinationSize < DestSize)
00052         {
00053             /* Fail due to missing buffer space */
00054             SetLastError(ERROR_MORE_DATA);
00055 
00056             /* Return how much data we actually need */
00057             *DestinationSize = DestSize;
00058             return 0;
00059         }
00060         else if (!DestSize)
00061         {
00062             /* Fail due to invalid data */
00063             SetLastError(ERROR_INVALID_DATA);
00064             return 0;
00065         }
00066 
00067         /* Return how much data we actually need */
00068         *DestinationSize = DestSize;
00069     }
00070 
00071     /* Now check if this is a Unicode String as input */
00072     if (InputType == UnicodeString)
00073     {
00074         /* Check if the output is ANSI */
00075         if (OutputType == AnsiString)
00076         {
00077             /* Convert and return the final desination size */
00078             OutputSize = WideCharToMultiByte(CP_ACP,
00079                                              0,
00080                                              String,
00081                                              StringSize,
00082                                              Destination,
00083                                              -1,
00084                                              NULL,
00085                                              NULL) + 1;
00086         }
00087         else if (OutputType == UnicodeString)
00088         {
00089             /* Copy the string */
00090             StringSize = StringSize * sizeof(WCHAR);
00091             RtlMoveMemory(Destination, String, StringSize);
00092 
00093             /* Return output length */
00094             OutputSize = StringSize + 2;
00095         }
00096         else if (OutputType == Utf8String)
00097         {
00098             /* FIXME */
00099             OutputSize = 0;
00100         }
00101     }
00102     else if (InputType == AnsiString)
00103     {
00104         /* It's ANSI, is the output ansi too? */
00105         if (OutputType == AnsiString)
00106         {
00107             /* Copy the string */
00108             RtlMoveMemory(Destination, String, StringSize);
00109 
00110             /* Return output length */
00111             OutputSize = StringSize + 1;
00112         }
00113         else if (OutputType == UnicodeString)
00114         {
00115             /* Convert to Unicode and return size */
00116             OutputSize = MultiByteToWideChar(CP_ACP,
00117                                              0,
00118                                              String,
00119                                              StringSize,
00120                                              Destination,
00121                                              -1) * sizeof(WCHAR) + 2;
00122         }
00123         else if (OutputType == Utf8String)
00124         {
00125             /* FIXME */
00126             OutputSize = 0;
00127         }
00128     }
00129     else if (InputType == Utf8String)
00130     {
00131         /* FIXME */
00132         OutputSize = 0;
00133     }
00134 
00135     /* Return the output size */
00136     return OutputSize;
00137 }
00138 
00139 LPWSTR
00140 WINAPI
00141 Dns_CreateStringCopy_W(IN LPWSTR Name)
00142 {
00143     SIZE_T StringLength;
00144     LPWSTR NameCopy;
00145 
00146     /* Make sure that we have a name */
00147     if (!Name)
00148     {
00149         /* Fail */
00150         SetLastError(ERROR_INVALID_PARAMETER);
00151         return NULL;
00152     }
00153 
00154     /* Find out the size of the string */
00155     StringLength = (wcslen(Name) + 1) * sizeof(WCHAR);
00156 
00157     /* Allocate space for the copy */
00158     NameCopy = Dns_AllocZero(StringLength);
00159     if (NameCopy)
00160     {
00161         /* Copy it */
00162         RtlCopyMemory(NameCopy, Name, StringLength);
00163     }
00164     else
00165     {
00166         /* Fail */
00167         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
00168     }
00169 
00170     /* Return the copy */
00171     return NameCopy;
00172 }
00173 
00174 ULONG
00175 WINAPI
00176 Dns_GetBufferLengthForStringCopy(IN PVOID String,
00177                                  IN ULONG Size OPTIONAL,
00178                                  IN DWORD InputType,
00179                                  IN DWORD OutputType)
00180 {
00181     ULONG OutputSize = 0;
00182 
00183     /* Check what kind of string this is */
00184     if (InputType == UnicodeString)
00185     {
00186         /* Check if we have a size */
00187         if (!Size)
00188         {
00189             /* Get it ourselves */
00190             Size = (ULONG)wcslen(String);
00191         }
00192 
00193         /* Check the output type */
00194         if (OutputType == UnicodeString)
00195         {
00196             /* Convert the size to bytes */
00197             OutputSize = (Size + 1) * sizeof(WCHAR);
00198         }
00199         else if (OutputType == Utf8String)
00200         {
00201             /* FIXME */
00202             OutputSize = 0;
00203         }
00204         else
00205         {
00206             /* Find out how much it will be in ANSI bytes */
00207             OutputSize = WideCharToMultiByte(CP_ACP,
00208                                              0,
00209                                              String,
00210                                              Size,
00211                                              NULL,
00212                                              0,
00213                                              NULL,
00214                                              NULL) + 1;
00215         }
00216     }
00217     else if (InputType == AnsiString)
00218     {
00219         /* Check if we have a size */
00220         if (!Size)
00221         {
00222             /* Get it ourselves */
00223             Size = (ULONG)strlen(String);
00224         }
00225 
00226         /* Check the output type */
00227         if (OutputType == AnsiString)
00228         {
00229             /* Just add a byte for the null char */
00230             OutputSize = Size + 1;
00231         }
00232         else if (OutputType == UnicodeString)
00233         {
00234             /* Calculate the bytes for a Unicode string */
00235             OutputSize = (MultiByteToWideChar(CP_ACP,
00236                                               0,
00237                                               String,
00238                                               Size,
00239                                               NULL,
00240                                               0) + 1) * sizeof(WCHAR);
00241         }
00242         else if (OutputType == Utf8String)
00243         {
00244             /* FIXME */
00245             OutputSize = 0;
00246         }
00247     }
00248     else if (InputType == Utf8String)
00249     {
00250         /* FIXME */
00251         OutputSize = 0;
00252     }
00253 
00254     /* Return the size required */
00255     return OutputSize;
00256 }
00257 

Generated on Mon May 28 2012 04:17:35 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.