-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-funcs.c
128 lines (112 loc) · 3.47 KB
/
string-funcs.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
/*
* Copyright (c) 2013 The Board of Trustees of Carnegie Mellon University.
*
* Author: Chris Rapier <rapier@psc.edu>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT License.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the MIT License for more details.
*
* You should have received a copy of the MIT License along with this library;
* if not, see http://opensource.org/licenses/MIT.
*
*/
/* this file provides the methods used to handle strings that are not found in
* the standard c strings library
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// use this to return the index position of the first appearance of the needle
// -1 means it does not exist
int strpos (char *haystack, char *needle) {
char *p = strstr(haystack, needle);
if (p)
return p - haystack;
return -1;
}
// take in incoming string and split by the delimiter and place the
// results into an array.
char** str_split( char* str, char delim, int* numSplits )
{
char** ret;
int retLen;
char* c;
if ((str == NULL) || (delim == '\0')) {
/* Either of those will cause problems */
ret = NULL;
retLen = -1;
} else {
retLen = 0;
c = str;
/* Pre-calculate number of elements */
while ( *c != '\0' ) {
if ( *c == delim )
retLen++;
c++;
}
// malloced but not freed! We need to pass a buffer in
// that we can free from the caller.
ret = malloc((retLen + 1) * sizeof(*ret));
ret[retLen] = NULL;
c = str;
retLen = 1;
ret[0] = str;
while (*c != '\0') {
if (*c == delim) {
ret[retLen++] = &c[1];
*c = '\0';
}
c++;
}
}
if ( numSplits != NULL )
*numSplits = retLen;
return ret;
}
// strip leading and trailing whitespace
char *strip(char *string) {
char *start = string;
while(isblank(*start)) start++;
int end = strlen(start);
if(start != string) {
memmove(string, start, end);
string[end] = '\0';
}
while(isblank(*(string+end-1))) end--;
string[end] = '\0';
return string;
}
// strip leading and trailing whitespace
char *noquotes(char *string) {
char *start = string;
while(*start == '"') start++;
int end = strlen(start);
if(start != string) {
memmove(string, start, end);
string[end] = '\0';
}
while(*(string+end-1) == '"') end--;
string[end] = '\0';
return string;
}
void join_strings(char **buf, char **strings, char *seperator, int count) {
size_t total_length = 0; /* Total length of joined strings */
int i = 0; /* Loop counter */
/* Find total length of joined strings */
for (i = 0; i < count; i++) total_length += strlen(strings[i]);
total_length++; /* For joined string terminator */
total_length += strlen(seperator) * (count - 1); // for seperators
*buf = malloc((total_length+1) * sizeof(char)); /* Allocate memory for joined strings */
*buf[0] = '\0'; /* Empty string we can append to */
/* Append all the strings */
for (i = 0; i < count; i++) {
strcat(*buf, strings[i]);
if (i < (count - 1)) strcat(*buf, seperator);
}
// return str;
}