-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.c
More file actions
157 lines (133 loc) · 3.46 KB
/
Copy pathstrings.c
File metadata and controls
157 lines (133 loc) · 3.46 KB
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
#include "main.h"
char* substring(char* a, const char* b) {
/* Declare variables */
size_t i = 0, length = strlen(a);
char* c = NULL, * result = NULL;
/* Create a temporary string where we will backup "a" */
c = calloc(length + 1, sizeof(char));
if (c == NULL) {
show_error("MEMORY ERROR");
return result;
}
/* Copy a into c, and make a all-caps */
for (; i<length; i++) {
c[i] = a[i];
a[i] = toupper(a[i]);
}
/* Figure out where the result pointer should be */
result = strstr(a, b);
/* copy c back into a, free c and we're done */
strncpy(a, c, length);
free(c);
return result;
}
bool STRING_EQUALS(char* a, const char* b) {
bool result = false;
size_t i = 0, length = strlen(a);
char* c = NULL;
c = calloc(length + 1, sizeof(char));
if (c == NULL) {
show_error("MEMORY ERROR");
return result;
}
for (; i<length; i++)
c[i] = toupper(a[i]);
result = strcmp(c, b) == 0;
free(c);
return result;
}
bool STRING_STARTS_WITH(char* a, const char* b) {
bool result = false;
size_t i = 0, length = strlen(a);
char* c = NULL;
c = calloc(length + 1, sizeof(char));
if (c == NULL) {
show_error("MEMORY ERROR");
return result;
}
for (; i<length; i++)
c[i] = toupper(a[i]);
result = strstr(c, b) == c;
free(c);
return result;
}
void replace_with_float(char* line, size_t from, size_t to, float value) {
char temp[15];
Line copy;
size_t i, length;
memset(temp, 0, 15);
memset(copy, 0, LINE_SIZE);
snprintf(temp, 15, "%g", value);
length = strlen(temp);
for (i=0; i<from; i++)
copy[i] = line[i];
for (i=from; i<from + length; i++)
copy[i] = temp[i - from];
strncat(copy, line + to, LINE_SIZE - 1);
strncpy(line, copy, LINE_SIZE);
}
void replace_with_string(char* line, size_t start, size_t end, char* replacement) {
Line copy;
size_t i;
/* We start with a blank line. Into this copy,
add the text to the left of the replacement */
memset(copy, 0, LINE_SIZE);
for (i=0; i<start; i++)
copy[i] = line[i];
/* Copy the replacement into the string */
strncat(copy, replacement, LINE_SIZE - 1);
/* Copy the text after the replacement */
strncat(copy, line + end, LINE_SIZE - 1);
/* And update the "line" with the new string */
strncpy(line, copy, LINE_SIZE);
}
void shift_left(char* string, size_t start, size_t length) {
size_t i;
for (i = start + 1; i < length; i++) {
string[i - 1] = string[i];
string[i] = ' ';
}
}
void strip_spaces(char* string) {
size_t i = 0, length = strlen(string);
bool in_quotes = false;
for (; i<length; i++) {
if (string[i] == '"')
in_quotes = !in_quotes;
if (in_quotes) continue;
if (string[i] == ' ')
shift_left(string, i, length);
}
}
void combine_strings(char* line) {
size_t start, end, i, j, length;
length = strlen(line);
for (i = 0; i<length; i++) {
if (line[i] != '"') continue;
start = i;
i++;
while(line[i] == ' ') i++;
if (line[i] != '+') continue;
i++;
while(line[i] == ' ') i++;
if (line[i] != '"') continue;
i++;
end = i - start;
for (j = 0; j < end; j++) {
shift_left(line, start, LINE_SIZE);
}
length = strlen(line);
i = -1;
}
}
void print_centered(const char* string) {
int rows = 48, columns = 25;
size_t i = 0, length = 40;
GetScreenSize(&rows, &columns);
length = (columns / 2) - strlen(string) / 2;
for (i=0; i<length; i++) printf(" ");
printf("%s", string);
if (length + strlen(string) + length > columns) length--;
for (i=0; i<length; i++) printf(" ");
}
// End of string functions