14b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Copyright 2005, Google Inc.
24b6829f0d28990dd645e16386eb226d0f10c8731shiqian// All rights reserved.
34b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
44b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Redistribution and use in source and binary forms, with or without
54b6829f0d28990dd645e16386eb226d0f10c8731shiqian// modification, are permitted provided that the following conditions are
64b6829f0d28990dd645e16386eb226d0f10c8731shiqian// met:
74b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
84b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     * Redistributions of source code must retain the above copyright
94b6829f0d28990dd645e16386eb226d0f10c8731shiqian// notice, this list of conditions and the following disclaimer.
104b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     * Redistributions in binary form must reproduce the above
114b6829f0d28990dd645e16386eb226d0f10c8731shiqian// copyright notice, this list of conditions and the following disclaimer
124b6829f0d28990dd645e16386eb226d0f10c8731shiqian// in the documentation and/or other materials provided with the
134b6829f0d28990dd645e16386eb226d0f10c8731shiqian// distribution.
144b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     * Neither the name of Google Inc. nor the names of its
154b6829f0d28990dd645e16386eb226d0f10c8731shiqian// contributors may be used to endorse or promote products derived from
164b6829f0d28990dd645e16386eb226d0f10c8731shiqian// this software without specific prior written permission.
174b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
184b6829f0d28990dd645e16386eb226d0f10c8731shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
194b6829f0d28990dd645e16386eb226d0f10c8731shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
204b6829f0d28990dd645e16386eb226d0f10c8731shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
214b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
224b6829f0d28990dd645e16386eb226d0f10c8731shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
234b6829f0d28990dd645e16386eb226d0f10c8731shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
244b6829f0d28990dd645e16386eb226d0f10c8731shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
254b6829f0d28990dd645e16386eb226d0f10c8731shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
264b6829f0d28990dd645e16386eb226d0f10c8731shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
274b6829f0d28990dd645e16386eb226d0f10c8731shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
284b6829f0d28990dd645e16386eb226d0f10c8731shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
294b6829f0d28990dd645e16386eb226d0f10c8731shiqian
304b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A sample program demonstrating using Google C++ testing framework.
314b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
324b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Author: wan@google.com (Zhanyong Wan)
334b6829f0d28990dd645e16386eb226d0f10c8731shiqian
344b6829f0d28990dd645e16386eb226d0f10c8731shiqian#include "sample2.h"
354b6829f0d28990dd645e16386eb226d0f10c8731shiqian
36f904a612d9444ab36c07a8e619c113432e046f49vladlosev#include <string.h>
37f904a612d9444ab36c07a8e619c113432e046f49vladlosev
384b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Clones a 0-terminated C string, allocating memory using new.
398f3826a7fa25e245cfe1124ee303483c102236d0vladlosevconst char* MyString::CloneCString(const char* a_c_string) {
408f3826a7fa25e245cfe1124ee303483c102236d0vladlosev  if (a_c_string == NULL) return NULL;
414b6829f0d28990dd645e16386eb226d0f10c8731shiqian
428f3826a7fa25e245cfe1124ee303483c102236d0vladlosev  const size_t len = strlen(a_c_string);
438f3826a7fa25e245cfe1124ee303483c102236d0vladlosev  char* const clone = new char[ len + 1 ];
448f3826a7fa25e245cfe1124ee303483c102236d0vladlosev  memcpy(clone, a_c_string, len + 1);
454b6829f0d28990dd645e16386eb226d0f10c8731shiqian
464b6829f0d28990dd645e16386eb226d0f10c8731shiqian  return clone;
474b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
484b6829f0d28990dd645e16386eb226d0f10c8731shiqian
494b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Sets the 0-terminated C string this MyString object
504b6829f0d28990dd645e16386eb226d0f10c8731shiqian// represents.
518f3826a7fa25e245cfe1124ee303483c102236d0vladlosevvoid MyString::Set(const char* a_c_string) {
524b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Makes sure this works when c_string == c_string_
538f3826a7fa25e245cfe1124ee303483c102236d0vladlosev  const char* const temp = MyString::CloneCString(a_c_string);
544b6829f0d28990dd645e16386eb226d0f10c8731shiqian  delete[] c_string_;
554b6829f0d28990dd645e16386eb226d0f10c8731shiqian  c_string_ = temp;
564b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
57