blob: 471cd999f43cf3d5a27227ff227bbaee880dac84 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/******************************************************************************
2*(C) Copyright 2014 Marvell International Ltd.
3* All Rights Reserved
4******************************************************************************/
5/*******************************************************************************
6 *
7 * Filename: String.c
8 *
9 * Authors: Boaz Sommer
10 *
11 * Description: A String class - holds a string
12 *
13 * HISTORY:
14 *
15 *
16 *
17 * Notes:
18 *
19 ******************************************************************************/
20#include <stdarg.h>
21#include <stdlib.h>
22#include <string.h>
23#include <stdio.h>
24
25#include "String.h"
26#include "mgui_utils.h"
27
28typedef struct S_STRING_PRIV
29{
30 char *text;
31 int alloc_length;
32}STRING_PRIV;
33
34
35static void StringSetLen (STRING_PRIV *ptp, int len)
36{
37 char *tmp_str;
38 if (ptp) {
39 if (ptp->text)
40 {
41 if (len > 0)
42 {
43 tmp_str = strdup(ptp->text);
44 free(ptp->text);
45 ptp->text = (char *)malloc(len + 1);
46 ptp->alloc_length = len;
47 (void)tmp_str;
48 //strcpy(ptp->text, tmp_str);
49 }
50 else
51 {
52 free(ptp->text);
53 ptp->text = NULL;
54 ptp->alloc_length = 0;
55 }
56 }
57 else if (len > 0)
58 {
59 ptp->text = (char *)malloc(len + 1);
60 ptp->alloc_length = len;
61 }
62 }
63}
64
65
66
67
68STRING *StringInit (STRING *pt)
69{
70 STRING_PRIV *ptp = (STRING_PRIV *)pt;
71
72 ptp = (STRING_PRIV *)calloc(1, sizeof(*ptp));
73 ptp->text = NULL;
74 ptp->alloc_length = 0;
75
76 return (STRING *)ptp;
77}
78
79void StringDeinit (STRING *pt)
80{
81 STRING_PRIV *ptp = (STRING_PRIV *)pt;
82 if (ptp)
83 free(ptp);
84}
85
86void StringSet (STRING *pt, const char *s)
87{
88 STRING_PRIV *ptp = (STRING_PRIV *)pt;
89 int src_len;
90
91 if (ptp)
92 if (s)
93 {
94 src_len = strlen(s);
95 if (src_len > ptp->alloc_length + 1)
96 StringSetLen(ptp, src_len);
97
98 strncpy(ptp->text, s, src_len);
99 ptp->text[src_len] = 0;
100 }
101
102}
103char *StringGet (STRING *pt)
104{
105 STRING_PRIV *ptp = (STRING_PRIV *)pt;
106
107 if (ptp)
108 return ptp->text;
109 else
110 return NULL;
111}