Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.5 KiB

  1. /*****************************************************************/
  2. /** Microsoft Windows for Workgroups **/
  3. /** Copyright (C) Microsoft Corp., 1991-1992 **/
  4. /*****************************************************************/
  5. /*
  6. strncpy.c
  7. NLS/DBCS-aware string class: strncpy method
  8. This file contains the implementation of the strncpy method
  9. for the STRING class. It is separate so that clients of STRING which
  10. do not use this operator need not link to it.
  11. FILE HISTORY:
  12. gregj 04/08/93 Created
  13. */
  14. #include "npcommon.h"
  15. extern "C"
  16. {
  17. #include <netlib.h>
  18. }
  19. #if defined(DEBUG)
  20. static const CHAR szFileName[] = __FILE__;
  21. #define _FILENAME_DEFINED_ONCE szFileName
  22. #endif
  23. #include <npassert.h>
  24. #include <npstring.h>
  25. /*******************************************************************
  26. NAME: NLS_STR::strncpy
  27. SYNOPSIS: Copy non-null-terminated string
  28. ENTRY: pchSource - string to copy
  29. cbSource - number of bytes to copy
  30. EXIT: If successful, contents of string overwritten.
  31. If failed, the original contents of the string remain.
  32. RETURNS: Reference to self.
  33. HISTORY:
  34. gregj 04/08/93 Created
  35. ********************************************************************/
  36. NLS_STR& NLS_STR::strncpy( const CHAR *pchSource, UINT cbSource )
  37. {
  38. if ( cbSource == 0)
  39. pchSource = NULL;
  40. if ( pchSource == NULL )
  41. {
  42. if ( !IsOwnerAlloc() && !QueryAllocSize() )
  43. {
  44. if ( !Alloc(1) )
  45. ReportError( WN_OUT_OF_MEMORY );
  46. return *this;
  47. }
  48. UIASSERT( QueryAllocSize() > 0 );
  49. *_pchData = '\0';
  50. _cchLen = 0;
  51. }
  52. else
  53. {
  54. if ( !IsOwnerAlloc() )
  55. {
  56. if ( (UINT)QueryAllocSize() < cbSource + 1 )
  57. {
  58. CHAR * pchNew = new CHAR[cbSource + 1];
  59. if ( pchNew == NULL )
  60. {
  61. ReportError( WN_OUT_OF_MEMORY );
  62. return *this;
  63. }
  64. delete _pchData;
  65. _pchData = pchNew;
  66. _cbData = cbSource + 1;
  67. }
  68. }
  69. else
  70. {
  71. if ((UINT)QueryAllocSize() < cbSource + 1)
  72. cbSource = QueryAllocSize() - 1;
  73. }
  74. ::strncpyf( _pchData, pchSource, cbSource );
  75. /*
  76. * Get the new length of the string. It may not necessarily be
  77. * cbSource because if the string is getting truncated, cbSource
  78. * might be halfway through a double-byte character.
  79. */
  80. _pchData[cbSource] = '\0';
  81. _cchLen = ::strlenf( _pchData );
  82. }
  83. IncVers();
  84. /* Reset the error state, since the string is now valid.
  85. */
  86. ReportError( WN_SUCCESS );
  87. return *this;
  88. }
  89.