-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg_substr.c
102 lines (61 loc) · 1.9 KB
/
cg_substr.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
/********************************************************************/
/* Function Name: cg_substr
/* Purpose : Extract a string between left boundary and right boundary.
/* Input :
/* Output :
/******************************************************************/
char* cg_substr(char* source,char* lbound, char* rbound)
{
char* lposition;
char* rposition;
int begin,end;
int length;
char* newstring;
char* tmpstring;
char* tmp;
int i;
int lblength = strlen(lbound);
lposition = (char *)strstr(source, lbound);
// strstr has returned the address. Now calculate * the offset from the beginning of str
begin = (int)(lposition - source + 1);
//lr_output_message ("The lbound \"%s\" was found at position %d", lbound, begin);
if(begin<0)
{
lr_output_message("ERROR:Verify Left boundary");
return("-1");
}
begin = begin + lblength;
rposition = (char *)strstr(source, rbound);
// strstr has returned the address. Now calculate * the offset from the beginning of str
end = (int)(rposition - source + 1);
//lr_output_message ("The rbound \"%s\" was found at position %d", rbound, end);
length= end-begin;
if(length<0)
{
tmp = (char*)malloc(length + 1);
tmp = (char*)strdup(source);
for(i=1;i<begin;i++)
{
tmp++;
}
rposition = (char *)strstr(tmp, rbound);
end = (int)(rposition - tmp + 1);
//lr_output_message ("The new rbound \"%s\" was found at position %d", rbound, end);
length= end - 1;
if(length<0)
{
lr_output_message("ERROR: Verify right boundary value");
return("-1");
}
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
tmpstring = (char*)strdup(source);
for(i=1;i<begin;i++)
{
tmpstring++;
}
strncpy(newstring,tmpstring,length);
lr_output_message("Substring is ::>%s",newstring);
return newstring;
}