-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgsub_string.c
More file actions
156 lines (125 loc) · 4.15 KB
/
Copy pathgsub_string.c
File metadata and controls
156 lines (125 loc) · 4.15 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
*
* Todo
* ----
* - Test to make sure the code handles cases where one sub-pattern creates
* - output that matches another sub-pattern, possibly causing a double substition
* - (and blowing up the final length assumptions).
*/
#include <string.h>
#include <stdlib.h>
#include "cli-sub.h"
#include "err_ref.h"
/* --- */
char *gsub_string( int *rc, char *template, struct sub_list *patts)
{
int final_len = 0, max_patt_len = 0, n_patt = 0, lrc = RC_NORMAL, seq,
next_seq, asis = 0;
char *remaining = 0, *end_of_template = 0, *eos = 0, *result = 0, *next_loc = 0;
struct sub_list *walk = 0, *next_patt = 0;
struct sub_list_reference *plist = 0;
ENTER( "gsub_string" )
end_of_template = template + strlen( template);
for( walk = patts; walk; walk = walk->next )
{
n_patt++;
if( strlen(walk->from) > max_patt_len ) max_patt_len = strlen(walk->from);
}
plist = (struct sub_list_reference *) malloc( n_patt * (sizeof *plist) );
if( !plist ) lrc = ERR_MALLOC_FAILED;
else
{
for( seq = 0, walk = patts; seq < n_patt; seq++ )
{
plist[seq].patt = walk;
plist[seq].ref = 0;
walk = walk->next;
}
}
if( lrc == RC_NORMAL )
{
/* Caculate how large the template will be after all the substitutions */
final_len = strlen(template);
for( remaining = template; remaining; )
{
next_loc = end_of_template;
next_seq = NO_PATT_MATCH;
for( seq = 0; seq < n_patt; seq++ ) if( plist[seq].patt )
{
if( plist[seq].ref < remaining )
{
plist[seq].ref = strstr( remaining, plist[seq].patt->from );
if( !plist[seq].ref ) plist[seq].patt = 0;
}
if( plist[seq].ref && plist[seq].ref < next_loc )
{
next_loc = plist[seq].ref;
next_patt = plist[seq].patt;
next_seq = seq;
}
}
if( next_seq == NO_PATT_MATCH ) remaining = 0;
else
{
remaining = next_loc + strlen(next_patt->from);
final_len += strlen(next_patt->to) - strlen(next_patt->from);
plist[next_seq].ref = 0;
}
}
/* --- */
INSUB( "gsub_string", "before-rebuild" )
result = (char *) malloc( final_len + 1);
if( !result) lrc = ERR_MALLOC_FAILED;
else
{
eos = result;
*eos = '\0';
for( seq = 0, walk = patts; seq < n_patt; seq++ )
{
plist[seq].patt = walk;
plist[seq].ref = 0;
walk = walk->next;
}
for( remaining = template; remaining; )
{
next_loc = end_of_template;
next_seq = NO_PATT_MATCH;
for( seq = 0; seq < n_patt; seq++ ) if( plist[seq].patt )
{
if( plist[seq].ref < remaining )
{
plist[seq].ref = strstr( remaining, plist[seq].patt->from );
if( !plist[seq].ref ) plist[seq].patt = 0;
}
if( plist[seq].ref && plist[seq].ref < next_loc )
{
next_loc = plist[seq].ref;
next_patt = plist[seq].patt;
next_seq = seq;
}
}
asis = next_loc - remaining;
if( asis)
{
strncpy( eos, remaining, asis );
eos += asis;
}
if( next_seq == NO_PATT_MATCH ) remaining = 0;
else
{
strcpy( eos, next_patt->to);
eos += strlen( next_patt->to);
remaining = next_loc + strlen(next_patt->from);
plist[next_seq].ref = 0;
}
}
*eos = '\0';
}
}
/* --- */
if( plist) free( plist);
if( lrc != RC_NORMAL) *rc = lrc;
/* --- */
LEAVE( "gsub_string" )
return( result);
}