-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmystring.c
125 lines (109 loc) · 2.86 KB
/
mystring.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int pos(char *haystack, char *needle)
{
if (strstr(haystack, needle)) {
return (int) strstr(haystack, needle) - (int) haystack;
} else {
return -1;
}
}
void cutto(char *str, int len)
{
memmove(str, str + len, strlen(str) - len + 1);
}
void mystrncpy(char *dest, char *src, int len)
{
strncpy(dest, src, len);
*(dest + len) = 0;
}
/*
Search through str looking for what. Replace any what with
the contents of by. Do not let the string get longer than max_length.
*/
int replace(char *str, char *what, char *by, int max_length)
{
char *foo, *bar = str;
int i = 0;
int str_length, what_length, by_length;
/* do a sanity check */
if (! str) return 0;
if (! what) return 0;
if (! by) return 0;
what_length = strlen(what);
by_length = strlen(by);
str_length = strlen(str);
foo = strstr(bar, what);
/* keep replacing if there is somethign to replace and it
will no over-flow
*/
while ( (foo) &&
( (str_length + by_length - what_length) < (max_length - 1) ) )
{
bar = foo + strlen(by);
memmove(bar, foo + strlen(what), strlen(foo + strlen(what)) + 1);
memcpy(foo, by, strlen(by));
i++;
foo = strstr(bar, what);
str_length = strlen(str);
}
return i;
}
/* int_from_list(char *list, int n)
* returns the n'th integer element from a string like '2,5,12-15,20-23'
* if n is out of range or *list is out of range, -1 is returned.
*/
int int_from_list(char *list, int n)
{
char *str, *tok;
int count = -1, firstrun = 1;
str = (char *) malloc(sizeof(char) * (strlen(list) + 2));
if (!str)
return -1;
memset(str, 0, strlen(list) + 2);
strncpy(str, list, strlen(list));
/* apppend ',' to the string so we can always use strtok() */
str[strlen(list)] = ',';
for (;;) {
if (firstrun)
tok = strtok(str, ",");
else
tok = strtok(NULL, ",");
if (!tok || *tok == '\0') {
free(str);
return -1;
}
if (strchr(tok, '-')) {
char *start, *end;
int s, e;
start = tok;
end = strchr(tok, '-');
*end = '\0';
end++;
if (!*start || !*end) {
free(str);
return -1;
}
s = atoi(start);
e = atoi(end);
if (s < 1 || e < 1 || e < s) {
free(str);
return -1;
}
count += e - s + 1;
if (count >= n) {
free(str);
return (e - (count - n));
}
} else {
count++;
if (count == n) {
int val = atoi(tok);
free(str);
return val;
}
}
firstrun = 0;
}
}