-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
175 lines (161 loc) · 4.75 KB
/
utils.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
/*
* Description:
* Find and replace text within a string.
*
* Parameters:
* src (in) - pointer to source string
* from (in) - pointer to search text
* to (in) - pointer to replacement text
*
* Returns:
* Returns a pointer to dynamically-allocated memory containing string
* with occurences of the text pointed to by 'from' replaced by with the
* text pointed to by 'to'.
*/
char
*replace(const char *src, const char *from, const char *to,size_t *sz, int type,int limit)
{
/*
* Find out the lengths of the source string, text to replace, and
* the replacement text.
*/
size_t size = strlen(src) + 1;
*sz=size;
//check for type, if its type2 cut at 200 chars
if (type==2){
if(size>limit){
//String too long ..cut it
return 0;
}
}
size_t fromlen = strlen(from);
size_t tolen = strlen(to);
/*
* Allocate the first chunk with enough for the original string.
*/
char *value = malloc(size);
/*
* We need to return 'value', so let's make a copy to mess around with.
*/
char *dst = value;
/*
* Before we begin, let's see if malloc was successful.
*/
if ( value != NULL )
{
/*
* Loop until no matches are found.
*/
for ( ;; )
{
/*
* Try to find the search text.
*/
const char *match = strstr(src, from);
if ( match != NULL )
{
/*
* Found search text at location 'match'. :)
* Find out how many characters to copy up to the 'match'.
*/
size_t count = match - src;
/*
* We are going to realloc, and for that we will need a
* temporary pointer for safe usage.
*/
char *temp;
/*
* Calculate the total size the string will be after the
* replacement is performed.
*/
size += tolen - fromlen;
/*
* Attempt to realloc memory for the new size.
*/
temp = realloc(value, size);
if ( temp == NULL )
{
/*
* Attempt to realloc failed. Free the previously malloc'd
* memory and return with our tail between our legs. :(
*/
free(value);
return NULL;
}
/*
* The call to realloc was successful. :) But we'll want to
* return 'value' eventually, so let's point it to the memory
* that we are now working with. And let's not forget to point
* to the right location in the destination as well.
*/
dst = temp + (dst - value);
value = temp;
/*
* Copy from the source to the point where we matched. Then
* move the source pointer ahead by the amount we copied. And
* move the destination pointer ahead by the same amount.
*/
memmove(dst, src, count);
src += count;
dst += count;
/*
* Now copy in the replacement text 'to' at the position of
* the match. Adjust the source pointer by the text we replaced.
* Adjust the destination pointer by the amount of replacement
* text.
*/
memmove(dst, to, tolen);
src += fromlen;
dst += tolen;
}
else /* No match found. */
{
/*
* Copy any remaining part of the string. This includes the null
* termination character.
*/
strcpy(dst, src);
break;
}
}
}
return value;
}
char* concat(int count, ...)
{
va_list ap;
int i;
// Find required length to store merged string
int len = 1; // room for NULL
va_start(ap, count);
for(i=0 ; i<count ; i++)
len += strlen(va_arg(ap, char*));
va_end(ap);
// Allocate memory to concat strings
char *merged = calloc(sizeof(char),len);
int null_pos = 0;
// Actually concatenate strings
va_start(ap, count);
for(i=0 ; i<count ; i++)
{
char *s = va_arg(ap, char*);
strcpy(merged+null_pos, s);
null_pos += strlen(s);
}
va_end(ap);
return merged;
}
char *
strLWR2(char *str){
unsigned char*p=(unsigned char*)str;
while(*p){
*p=tolower((unsigned char)*p);
p++;
}
return str;
}