1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "ExpandableString.h" 18 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 23 void ExpandableStringInitialize(struct ExpandableString *s) { 24 memset(s, 0, sizeof(*s)); 25 } 26 27 void ExpandableStringRelease(struct ExpandableString* s) { 28 free(s->data); 29 memset(s, 0, sizeof(*s)); 30 } 31 32 bool ExpandableStringAppend(struct ExpandableString* s, const char* text) { 33 size_t textSize = strlen(text); 34 size_t requiredSize = s->dataSize + textSize + 1; 35 char* data = (char*) realloc(s->data, requiredSize); 36 if (data == NULL) { 37 return false; 38 } 39 s->data = data; 40 memcpy(s->data + s->dataSize, text, textSize + 1); 41 s->dataSize += textSize; 42 return true; 43 } 44 45 bool ExpandableStringAssign(struct ExpandableString* s, const char* text) { 46 ExpandableStringRelease(s); 47 return ExpandableStringAppend(s, text); 48 }