blob: 701c874631359622a600a51a074afa7b42d6f3d1 (
plain)
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
|
#include "string-tick.h"
#include "portfolio.h"
String* string_init(void) {
String* pString = (String*) malloc(sizeof(String));
if (pString != NULL) {
pString->len = 0;
pString->data = (char*) malloc(sizeof(char));
if (pString->data == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
pString->data[0] = '\0';
} else {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
return pString;
}
void strtolower(char* str) {
for (int i = 0; str[i] != '\0'; i++)
str[i] = (char) tolower(str[i]);
}
void strtoupper(char* str) {
for (int i = 0; str[i] != '\0'; i++)
str[i] = (char) toupper(str[i]);
}
char* strip_char(char* string, char c) {
size_t len = strlen(string);
int i, j;
for (i = 0, j = 0; j < (int) len; i++, j++) {
while (string[j] == c)
j++;
string[i] = string[j];
}
string[i] = '\0';
return string;
}
void string_write_portfolio(String* pString){
FILE* fp = fopen(portfolio_file, "w"); // fprintf %s won't work since there some chars are encoded to '\0', so it
for (int i = 0; i < (int) pString->len; i++) // will be null terminated several times in the middle
fputc(pString->data[i], fp);
fclose(fp);
}
void string_destroy(String** phString) {
String* pString = *phString;
free(pString->data);
free(*phString);
*phString = NULL;
}
|