117fd14ceSopenharmony_ci/*
217fd14ceSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
317fd14ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
417fd14ceSopenharmony_ci * you may not use this file except in compliance with the License.
517fd14ceSopenharmony_ci * You may obtain a copy of the License at
617fd14ceSopenharmony_ci *
717fd14ceSopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
817fd14ceSopenharmony_ci *
917fd14ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1017fd14ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1117fd14ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1217fd14ceSopenharmony_ci * See the License for the specific language governing permissions and
1317fd14ceSopenharmony_ci * limitations under the License.
1417fd14ceSopenharmony_ci */
1517fd14ceSopenharmony_ci
1617fd14ceSopenharmony_ci#include <string.h>
1717fd14ceSopenharmony_ci#include "hc_string.h"
1817fd14ceSopenharmony_ci#include "hc_types.h"
1917fd14ceSopenharmony_ci
2017fd14ceSopenharmony_ciconst uint32_t STRING_ALLOC_SIZE = 10;
2117fd14ceSopenharmony_ciconst uint32_t STRING_END_CHAR_LENGTH = 1;
2217fd14ceSopenharmony_ciconst char STRING_END_CHAR = '\0';
2317fd14ceSopenharmony_ci
2417fd14ceSopenharmony_ci/*
2517fd14ceSopenharmony_ci * Append a HcString
2617fd14ceSopenharmony_ci * Notice: It will add '\0' automatically.
2717fd14ceSopenharmony_ci * @param self: self pointer.
2817fd14ceSopenharmony_ci * @param str: append string.
2917fd14ceSopenharmony_ci * @return HC_TRUE (ok), HC_FALSE (error)
3017fd14ceSopenharmony_ci */
3117fd14ceSopenharmony_ciHcBool StringAppend(HcString *self, HcString str)
3217fd14ceSopenharmony_ci{
3317fd14ceSopenharmony_ci    uint32_t length = GetParcelDataSize(&str.parcel);
3417fd14ceSopenharmony_ci    if (self != NULL && length > 0) {
3517fd14ceSopenharmony_ci        // remove '\0'
3617fd14ceSopenharmony_ci        ParcelPopBack(&self->parcel, STRING_END_CHAR_LENGTH);
3717fd14ceSopenharmony_ci        // append string(include '\0')
3817fd14ceSopenharmony_ci        return StringAppendPointer(self, GetParcelData(&str.parcel));
3917fd14ceSopenharmony_ci    }
4017fd14ceSopenharmony_ci
4117fd14ceSopenharmony_ci    return HC_FALSE;
4217fd14ceSopenharmony_ci}
4317fd14ceSopenharmony_ci
4417fd14ceSopenharmony_ci/*
4517fd14ceSopenharmony_ci * Append string pointer
4617fd14ceSopenharmony_ci * Notice: It will add '\0' automatically.
4717fd14ceSopenharmony_ci * @param self: self pointer.
4817fd14ceSopenharmony_ci * @param str: string pointer.
4917fd14ceSopenharmony_ci * @return HC_TRUE (ok), HC_FALSE (error)
5017fd14ceSopenharmony_ci */
5117fd14ceSopenharmony_ciHcBool StringAppendPointer(HcString *self, const char *str)
5217fd14ceSopenharmony_ci{
5317fd14ceSopenharmony_ci    if (self != NULL && str != NULL) {
5417fd14ceSopenharmony_ci        // remove '\0'
5517fd14ceSopenharmony_ci        ParcelPopBack(&self->parcel, STRING_END_CHAR_LENGTH);
5617fd14ceSopenharmony_ci        // append string (include '\0')
5717fd14ceSopenharmony_ci        return ParcelWrite(&self->parcel, (void *)str, HcStrlen(str) + 1);
5817fd14ceSopenharmony_ci    }
5917fd14ceSopenharmony_ci
6017fd14ceSopenharmony_ci    return HC_FALSE;
6117fd14ceSopenharmony_ci}
6217fd14ceSopenharmony_ci
6317fd14ceSopenharmony_ci/*
6417fd14ceSopenharmony_ci * Append a char
6517fd14ceSopenharmony_ci * Notice: It will add '\0' automatically.
6617fd14ceSopenharmony_ci * @param self: self pointer.
6717fd14ceSopenharmony_ci * @param str: char.
6817fd14ceSopenharmony_ci * @return HC_TRUE (ok), HC_FALSE (error)
6917fd14ceSopenharmony_ci */
7017fd14ceSopenharmony_ciHcBool StringAppendChar(HcString *self, char c)
7117fd14ceSopenharmony_ci{
7217fd14ceSopenharmony_ci    if (self != NULL && c != STRING_END_CHAR) {
7317fd14ceSopenharmony_ci        // remove '\0'
7417fd14ceSopenharmony_ci        ParcelPopBack(&self->parcel, STRING_END_CHAR_LENGTH);
7517fd14ceSopenharmony_ci
7617fd14ceSopenharmony_ci        if (ParcelWriteInt8(&self->parcel, c)) {
7717fd14ceSopenharmony_ci            return ParcelWriteInt8(&self->parcel, (uint32_t)STRING_END_CHAR);
7817fd14ceSopenharmony_ci        }
7917fd14ceSopenharmony_ci    }
8017fd14ceSopenharmony_ci
8117fd14ceSopenharmony_ci    return HC_FALSE;
8217fd14ceSopenharmony_ci}
8317fd14ceSopenharmony_ci
8417fd14ceSopenharmony_ci/*
8517fd14ceSopenharmony_ci * Assign a value to the HcString
8617fd14ceSopenharmony_ci * Notice: It will add '\0' automatically.
8717fd14ceSopenharmony_ci * @param self: self pointer.
8817fd14ceSopenharmony_ci * @param str: assign value of ta_sting.
8917fd14ceSopenharmony_ci * @return HC_TRUE (ok), HC_FALSE (error)
9017fd14ceSopenharmony_ci */
9117fd14ceSopenharmony_ciHcBool StringSet(HcString *self, HcString str)
9217fd14ceSopenharmony_ci{
9317fd14ceSopenharmony_ci    if (self != NULL) {
9417fd14ceSopenharmony_ci        DeleteParcel(&self->parcel);
9517fd14ceSopenharmony_ci        return StringAppend(self, str);
9617fd14ceSopenharmony_ci    }
9717fd14ceSopenharmony_ci
9817fd14ceSopenharmony_ci    return HC_FALSE;
9917fd14ceSopenharmony_ci}
10017fd14ceSopenharmony_ci
10117fd14ceSopenharmony_ci/*
10217fd14ceSopenharmony_ci * Assign a value to the HcString
10317fd14ceSopenharmony_ci * Notice: It will add '\0' automatically.
10417fd14ceSopenharmony_ci * @param self: self pointer.
10517fd14ceSopenharmony_ci * @param str: assign value of string pointer.
10617fd14ceSopenharmony_ci * @return HC_TRUE (ok), HC_FALSE (error)
10717fd14ceSopenharmony_ci */
10817fd14ceSopenharmony_ciHcBool StringSetPointer(HcString *self, const char *str)
10917fd14ceSopenharmony_ci{
11017fd14ceSopenharmony_ci    if (self != NULL) {
11117fd14ceSopenharmony_ci        DeleteParcel(&self->parcel);
11217fd14ceSopenharmony_ci        return StringAppendPointer(self, str);
11317fd14ceSopenharmony_ci    }
11417fd14ceSopenharmony_ci
11517fd14ceSopenharmony_ci    return HC_FALSE;
11617fd14ceSopenharmony_ci}
11717fd14ceSopenharmony_ci
11817fd14ceSopenharmony_ci/*
11917fd14ceSopenharmony_ci * Get the string pointer data
12017fd14ceSopenharmony_ci * @param self: self pointer.
12117fd14ceSopenharmony_ci * @return the pointer data of the string
12217fd14ceSopenharmony_ci */
12317fd14ceSopenharmony_ciconst char *StringGet(const HcString *self)
12417fd14ceSopenharmony_ci{
12517fd14ceSopenharmony_ci    if (self == NULL) {
12617fd14ceSopenharmony_ci        return NULL;
12717fd14ceSopenharmony_ci    }
12817fd14ceSopenharmony_ci
12917fd14ceSopenharmony_ci    return GetParcelData(&self->parcel);
13017fd14ceSopenharmony_ci}
13117fd14ceSopenharmony_ci
13217fd14ceSopenharmony_ci/*
13317fd14ceSopenharmony_ci * Get the length of the string
13417fd14ceSopenharmony_ci * @param self: self pointer.
13517fd14ceSopenharmony_ci * @return the length of the string
13617fd14ceSopenharmony_ci */
13717fd14ceSopenharmony_ciuint32_t StringLength(const HcString *self)
13817fd14ceSopenharmony_ci{
13917fd14ceSopenharmony_ci    if (self == NULL) {
14017fd14ceSopenharmony_ci        return 0;
14117fd14ceSopenharmony_ci    } else {
14217fd14ceSopenharmony_ci        uint32_t length = GetParcelDataSize(&self->parcel);
14317fd14ceSopenharmony_ci        if (length > 0) {
14417fd14ceSopenharmony_ci            return length - STRING_END_CHAR_LENGTH;
14517fd14ceSopenharmony_ci        } else {
14617fd14ceSopenharmony_ci            return 0;
14717fd14ceSopenharmony_ci        }
14817fd14ceSopenharmony_ci    }
14917fd14ceSopenharmony_ci}
15017fd14ceSopenharmony_ci
15117fd14ceSopenharmony_ci/*
15217fd14ceSopenharmony_ci * Create a string.
15317fd14ceSopenharmony_ci * Notice: You should delete_string when you don't need the string anymore.
15417fd14ceSopenharmony_ci * @return return the created string.
15517fd14ceSopenharmony_ci */
15617fd14ceSopenharmony_ciHcString CreateString(void)
15717fd14ceSopenharmony_ci{
15817fd14ceSopenharmony_ci    HcString str;
15917fd14ceSopenharmony_ci    str.parcel = CreateParcel(0, STRING_ALLOC_SIZE);
16017fd14ceSopenharmony_ci    ParcelWriteInt8(&str.parcel, STRING_END_CHAR);
16117fd14ceSopenharmony_ci    return str;
16217fd14ceSopenharmony_ci}
16317fd14ceSopenharmony_ci
16417fd14ceSopenharmony_ci/*
16517fd14ceSopenharmony_ci * Delete a string. In fact it will not destroy the string,
16617fd14ceSopenharmony_ci * but only free the allocate memory of the string and reset the member's value
16717fd14ceSopenharmony_ci * of the string.
16817fd14ceSopenharmony_ci * You can continue to use the string if you want.
16917fd14ceSopenharmony_ci * Notice: You should delete the string when you don't need it any more to avoid memory leak.
17017fd14ceSopenharmony_ci * @param str: The string you want to delete.
17117fd14ceSopenharmony_ci */
17217fd14ceSopenharmony_civoid DeleteString(HcString *str)
17317fd14ceSopenharmony_ci{
17417fd14ceSopenharmony_ci    if (str != NULL) {
17517fd14ceSopenharmony_ci        DeleteParcel(&str->parcel);
17617fd14ceSopenharmony_ci    }
17717fd14ceSopenharmony_ci}
178