-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstring-helpers.c
131 lines (115 loc) · 2.72 KB
/
string-helpers.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "string-helpers.h"
#if defined(_WIN32) || defined(WIN32)
#define strcasecmp _stricmp
#endif
// Overriding getline_custom
// https://stackoverflow.com/a/3501681/16642426
size_t readLine(char** lineptr, size_t* n, FILE* stream) {
char* bufptr = NULL;
char* p = bufptr;
size_t size;
int c;
if (lineptr == NULL) {
return -1;
}
if (stream == NULL) {
return -1;
}
if (n == NULL) {
return -1;
}
bufptr = *lineptr;
size = *n;
c = fgetc(stream);
if (c == EOF) {
return -1;
}
if (bufptr == NULL) {
bufptr = malloc(128);
if (bufptr == NULL) {
return -1;
}
size = 128;
}
p = bufptr;
while (c != EOF) {
if ((p - bufptr) > (size - 1)) {
size = size + 128;
bufptr = realloc(bufptr, size);
if (bufptr == NULL) {
return -1;
}
}
*p++ = c;
if (c == '\n') {
break;
}
c = fgetc(stream);
}
*p++ = '\0';
*lineptr = bufptr;
*n = size;
return p - bufptr - 1;
}
// Check if a string starts with a given substring
// https://stackoverflow.com/a/15515276/16642426
int startsWith(const char* a, const char* b)
{
if (strncmp(a, b, strlen(b)) == 0) return 1;
return 0;
}
// Compare function for qsort
// https://stackoverflow.com/a/4061231/16642426
int compareStrings(const void* p1, const void* p2)
{
const char** ia = (const char**)p1;
const char** ib = (const char**)p2;
return strcasecmp(*ia, *ib);
}
// Case insensitive strstr
char* strstr_i(char* p1, const char* p2) {
while (*p1) {
for (size_t i = 0;; i++) {
if (p2[i] == '\0')
return (char*)p1;
if (tolower(p1[i]) != tolower(p2[i]))
break;
}
++p1;
}
return 0;
}
// Replace a substring in a string with another string
char* strreplace(char* origString, const char* searchString, const char* replaceString) {
char* searchIndex = origString;
char* tmpString = malloc(strlen(origString) + 1);
strcpy(tmpString, origString);
while (strstr(searchIndex, searchString) != NULL) {
searchIndex = strstr(searchIndex, searchString);
*searchIndex = '\0';
*(tmpString + (searchIndex - origString)) = '\0';
tmpString = realloc(tmpString, strlen(tmpString) + strlen(replaceString) + 1);
strcat(tmpString, replaceString);
searchIndex += strlen(searchString);
tmpString = realloc(tmpString, strlen(tmpString) + strlen(searchIndex) + 1);
strcat(tmpString, searchIndex);
}
origString = realloc(origString, strlen(tmpString) + 1);
strcpy(origString, tmpString);
free(tmpString);
return origString;
}
// Case-insensitive string comparison
// https://stackoverflow.com/a/5820991/16642426
int strcmp_i(const char * a, const char * b)
{
for (;; a++, b++)
{
int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
if (d != 0 || !*a)
return d;
}
}