-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.c
More file actions
137 lines (121 loc) · 2.61 KB
/
Copy pathHelper.c
File metadata and controls
137 lines (121 loc) · 2.61 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @file Helper.c
* @author Laksh Aggarwal (aggarwallaksh54@gmail.com)
* @version 0.1
*
*/
#include <string.h>
#include "Helper.h"
#include <stdlib.h>
string new_string()
{
string rv = malloc(sizeof(*rv) + 1);
if (rv)
{
rv->alloc = 1;
rv->len = 0;
rv->s = (char *)(rv + 1);
rv->s[0] = 0;
}
return rv;
}
string new_string_from_char(char *s)
{
unsigned long len = strlen(s);
string rv = malloc(sizeof(*rv) + 1 + len);
if (rv)
{
rv->alloc = 1 + len;
rv->len = len;
rv->s = (char *)(rv + 1);
strcpy(rv->s, s);
}
return rv;
}
void empty_s(string s)
{
s->len = 0;
s->s[0] = 0;
}
char append_char(string *sp, char c)
{
string s = *sp;
if (s->len + 2 >= s->alloc)
{
s = realloc(s, sizeof(*s) + (sizeof(char) * s->alloc * 2));
if (!s)
return 0;
s->s = (char *)(s + 1);
s->s[s->len++] = c;
s->alloc *= 2;
*sp = s;
}
else
s->s[s->len++] = c;
s->s[s->len] = 0;
return 1;
}
void trim(string s)
{
unsigned long move_from = s->len;
for (unsigned long i = 0; i < s->len; i++)
{
if (s->s[i] != ' ')
{
move_from = i;
break;
}
}
if (move_from == s->len)
{
empty_s(s);
return;
}
unsigned long end = move_from;
for (unsigned long i = s->len - 1; i >= move_from; i--)
if (s->s[i] != ' ')
{
end = i;
break;
}
memmove(s->s, s->s + move_from, end - move_from + 1);
s->len = end - move_from + 1;
s->s[s->len] = 0;
}
char string_append(string *sp, string s2)
{
string s = *sp;
if (s->len + s2->len + 1 >= s->alloc)
{
unsigned long size = s->len + s2->len + 1;
size = size > s->alloc * 2 ? size : s->alloc * 2;
s = realloc(s, sizeof(*s) + (sizeof(char) * size));
if (!s)
return 0;
s->s = (char *)(s + 1);
s->alloc = size;
*sp = s;
}
memcpy(s->s + s->len, s2->s, s2->len + 1);
s->len += s2->len;
return 1;
}
char string_append_char(string *sp, char *s2)
{
string s = *sp;
unsigned long len = strlen(s2);
if (s->len + len + 1 >= s->alloc)
{
unsigned long size = s->len + len + 1;
size = size > s->alloc * 2 ? size : s->alloc * 2;
s = realloc(s, sizeof(*s) + (sizeof(char) * size));
if (!s)
return 0;
s->s = (char *)(s + 1);
s->alloc = size;
*sp = s;
}
memcpy(s->s + s->len, s2, len + 1);
s->len += len;
return 1;
}