-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbstring.c
116 lines (101 loc) · 1.8 KB
/
vbstring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* File: vbstring.c */
#include <stdio.h>
#include "cutils.h"
#include "vbstring.h"
/* Declaration Section
*-------------------------------------------------------------------
*/
/* Public (extern) Functions Section
*-------------------------------------------------------------------
*/
int init_vbstring(char *src, size_t size)
{
if (src == NULL || size < 2)
{
return 0;
}
init_string(src, size, ' ');
return 1;
}
// 0 1 2 3 4
// [1] [2] [3] [0] [E]
int ToCStyleArray(char *src, const size_t size)
{
size_t i;
if (src == NULL)
{
return 0;
}
i = (size-2);
while (1)
{
if (src[i] != ' ')
{
break;
}
src[i] = '\0';
if (i == 0)
{
break;
}
i--;
}
return 1;
}
int ToVBStyleString(char *src, const size_t size)
{
size_t firstzero, i;
int ret;
ret = findfirstzero(src, size, &firstzero);
printf("first zero = %u (return code: %d)\n", firstzero, ret);
for (i = firstzero; i < (size-1); i++)
{
src[i] = ' ';
}
return 1;
}
char *copy_vbstring(char *dest, const size_t size, const char *src)
{
size_t i, j;
if (dest == NULL || src == NULL || size < 2)
{
return NULL;
}
for (i = 0, j = 0; i < (size - 1); i++)
{
if (src[j] != '\0')
{
dest[i] = src[j];
j++;
continue;
}
dest[i] = ' ';
}
dest[size-1] = '\0';
return dest;
}
extern char *append_vbstring(char *dest, const size_t size, const char *src)
{
size_t i, j, pos;
if (src == NULL || dest == NULL || size < 2)
{
return NULL;
}
if (findfirstspace(dest, size, &pos) != 1)
{
return NULL;
}
printf("pos: %u\n", pos);
for (i = pos, j = 0; i < (size - 1); i++, j++)
{
if (src[j] == '\0') // end of string
{
break;
}
dest[i] = src[j];
}
return dest;
}
/* Private (static) Functions Section
*-------------------------------------------------------------------
*/