-
Notifications
You must be signed in to change notification settings - Fork 4
/
cue_utils.c
193 lines (166 loc) · 3.65 KB
/
cue_utils.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* File : utils.c
* Author : Vincent Cheung
* Date : Jul. 13, 2015
* Description : Contains some utils used in lib cue.
*
* Copyright (C) 2015, Vincet Cheung<coolingfall@gmail.com>
*/
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "cue_utils.h"
/**
* Get file size with specified filename.
*
* @param filename filename
* @return file size
*/
long get_file_size(const char *filename)
{
struct stat statbuf;
return stat(filename, &statbuf) < 0 ? -1 : statbuf.st_size;
}
/**
* Get sub string from source string.
*
* @param dest dest poniter to save string
* @param src source string poniter
* @param start the start index
* @param end the end index
*/
void substring(char *dest, const char *src, int start, int end)
{
int i = start;
if (start > strlen(src))
{
return;
}
if (end > strlen(src))
{
end = strlen(src);
}
while (i < end)
{
dest[i - start] = src[i];
i ++;
}
dest[i - start] = '\0';
}
/**
* To check if the string start with specified string.
*
* @param src source string pointer
* @param prefix prefix string poniter
* @return ture if start with specified string, otherwise return false
*/
bool starts_with(const char *src, const char *prefix)
{
int len = strlen(prefix);
char buf[len];
substring(buf, src, 0, len);
return !strcmp(buf, prefix);
}
/**
* To check if the string end with specified string.
*
* @param src source string pointer
* @param suffix suffix string poniter
* @return ture if end with specified string, otherwise return false
*/
bool ends_with(const char *src, const char *suffix)
{
int len = strlen(suffix);
char buf[len];
substring(buf, src, strlen(src) - len, strlen(src));
return !strcmp(buf, suffix);
}
/**
* To get the index when sub string first appear in src.
*
* @param src src string
* @param sub the string to search
* @return the index of substring in source string,
* otherwise return -1 if not exists
*/
int index_of(const char *src, const char *sub)
{
char *result = strstr(src, sub);
return result ? strlen(src) - strlen(result) : -1;
}
/**
* To get the index when need string last appear in src.
*
* @param src src string
* @param need the string to search
* @return the index of substring in source string,
* otherwise return -1 if not exists
*/
int last_index_of(const char *src, const char *need)
{
int i;
const char *p = src + strlen(src);
size_t len = strlen(need);
char *buf;
for(i = 0; i < strlen(src); i ++)
{
buf = strchr(p --, *need);
if (!buf)
{
continue;
}
if (strncmp(buf, need, len) == 0)
{
return strlen(src) - strlen((char *)buf);
}
}
return -1;
}
/**
* Remove space from string at the beginning or end.
*
* @param src pointer to source string
*/
char *trim(char *src)
{
int i;
int len = strlen(src);
int start = 0, end = len - 1;
while (start < end && (src[start] == ' ' || src[start] == '\t'))
{
start ++;
}
while (end >= start && (src[end] <= ' ' || src[end] == '\t'))
{
end --;
}
memmove(src, src + start, end - start + 1);
src[end - start + 1] = '\0';
return src;
}
/**
* Read line from input file poniter.
*
* @param buf the buffer to save result
* @param bufsize the size of buffer
* @param stream the pointer to file structure
*/
char *read_line(char *buf, int bufsize, FILE *stream)
{
char *result = fgets(buf, bufsize, stream);
if (result != NULL)
{
trim(buf);
}
return result;
}
/**
* Write string into file.
*
* @param intput the string to write
* @param fp pointer to file
*/
void write_string(const char *input, FILE *fp)
{
fwrite(input, strlen(input), 1, fp);
}