-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg_substr_lb_cnt.c
116 lines (71 loc) · 2.33 KB
/
cg_substr_lb_cnt.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
116
/********************************************************************/
/* Function Name: cg_substr_lb_cnt
/* Purpose : Extract a string between left boundary and end of string
/* for the given occurence..
/* Input :
/* Output :
/******************************************************************/
char* cg_substr_lb_cnt(char* src,char* lbound, int cnt)
{
char* lpos;
char* newstring;
char* tmpstring;
int start,end,length,i,j,op;
int lblength = strlen(lbound);
lpos = (char *)strstr(src, lbound);
// strstr has returned the address. Now calculate * the offset from the beginning of str
start = (int)(lpos - src + 1);
if(start<0)
{
lr_output_message("-->ERROR:The left boundary value'%s' - Not found in source <--",lbound);
return("-1");
}
//lr_output_message ("The lbound \"%s\" was found at position %d", lbound, start);
start = start + lblength; // calculate the start position
length= strlen(src);
if(length<0)
{
lr_output_message("-->ERROR:Enter string with some characters<--");
return("-1");
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
tmpstring = (char*)strdup(src);
/* Msg: move pointer for the number of occcurence */
for (j=0;j<cnt;j++)
{
for(i=1;i<start;i++)
{
tmpstring++;
}
strncpy(newstring,tmpstring,length);
lpos = (char *)strstr(newstring, lbound);
start = (int)(lpos - newstring + 1);
//lr_output_message("The value of start is:%d",start);
if(start<0)
{
op=j+1;
//lr_output_message("-->Last occurence of '%s' found for occurance count %d ;the occurence value entered is %d<--",lbound,op,cnt);
if(op<cnt)
{
lr_output_message("ERROR:Left boundary not found for the given occurrence count");
return("-1");
}
//return("-1");
}
else
{
lr_output_message ("The lbound \"%s\" was found at position %d", lbound, start);
/* Calculate new start position */
start = start + lblength;
length= strlen(newstring);
if(length<0)
{
lr_output_message("-->ERROR:Enter string with some characters<--");
return("-1");
}
}
}
lr_output_message("Substring from left boundary is ::>%s",newstring);
return newstring;
}