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
158
159
160
|
#include "utils.h"
String* string_init(void) {
String* pString = (String*) malloc(sizeof(String));
pointer_alloc_check(pString);
pString->len = 0;
pString->data = calloc(sizeof(char), 1);
pointer_alloc_check(pString->data);
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;
}
char* strip_tags(char* string) {
size_t len = strlen(string);
int i, j;
for (i = 0, j = 0; j < (int) len; i++, j++) {
if (string[j] == '<')
while (string[j] != '>')
j++;
string[i] = string[j];
}
string[i] = '\0';
return string;
}
String* file_get_string(const char* file_name) {
struct stat file_info;
if (stat(file_name, &file_info)) // If called from portfolio_modify_string, file should exist (possibly size 0)
RETNULL_MSG("File doesn't exist.");
if (file_info.st_size == 0) // Return new String if new file
return string_init();
FILE* fp = fopen(file_name, "r");
if (fp == NULL) // If file exists, but cannot be opened, usually because of permissions
RETNULL_MSG("Error opening file.")
String* pString = string_init();
pString->len = (size_t) file_info.st_size;
pString->data = realloc(pString->data, pString->len + 1); // Alloc with file size
pointer_alloc_check(pString->data);
pString->data[pString->len] = '\0';
if (fread(pString->data, sizeof(char), pString->len, fp) != pString->len) { // read file and return NULL if error
fclose(fp);
string_destroy(&pString);
RETNULL_MSG("Error reading file.")
}
fclose(fp);
return pString;
}
void string_write_file(const String* pString, const char* file_name) {
FILE* fp = fopen(file_name, "w");
if (fp == NULL)
RET_MSG("Error opening file.")
if (fwrite(pString->data, sizeof(char), pString->len, fp) != pString->len)
puts("Error writing file.");
fclose(fp);
}
void string_destroy(String** phString) {
if (*phString == NULL)
return;
String* pString = *phString;
free(pString->data);
free(*phString);
*phString = NULL;
}
void pointer_alloc_check(const void* alloced) {
if (alloced == NULL)
EXIT_MSG("alloc failed!")
}
int is_string_json_array(const String* pString) {
Json* jobj = json_tokener_parse(pString->data);
int formatted = jobj != NULL && json_object_is_type(jobj, json_type_array);
json_object_put(jobj);
return formatted;
}
int is_str_number(const char* string) {
size_t len = strlen(string);
if (len == 0)
return 0;
for (size_t i = 0; i < len; i++)
if (!isdigit(string[i]) && string[i] != '.')
return 0;
return 1;
}
double csv_read_next_double(String* pString, size_t* idx) {
char string[32];
size_t i, j = 0;
for (i = *idx; pString->data[i] != ',' && pString->data[i] != '\n' && i < pString->len; i++,
j++)
string[j] = pString->data[i];
string[j] = '\0';
*idx = i + 1;
return strtod(string, NULL);
}
int csv_goto_next_line(String* pString, size_t* idx) {
size_t i;
for (i = *idx; i < pString->len; i++) {
if (pString->data[i] == '\n') {
*idx = i + 1;
return 1;
}
}
return 0;
}
int csv_goto_next_value(String* pString, size_t* idx) {
size_t i;
for (i = *idx; i < pString->len; i++) {
if (pString->data[i] == ',') {
*idx = i + 1;
return 1;
}
}
return 0;
}
size_t string_get_num_lines(String* pString) {
size_t lines = 0, i = 0;
while (i++ < pString->len)
if (pString->data[i] == '\n')
lines++;
return lines;
}
|