forked from hamon-in/cpslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
73 lines (67 loc) · 1.45 KB
/
common.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "common.h"
float percentage(uint64_t n, uint64_t d) {
/* TODO: Error check here */
float percent = ((float)n / (float)d) * 100.0;
return percent;
}
int str_comp(const void *key, const void *memb) {
const char **a = (const char **)key;
const char **b = (const char **)memb;
return strcmp(*a, *b);
}
int int_comp(const void *key, const void *memb) {
const int a = *(int *)key;
const int b = *(int *)memb;
if (a == b) {
return 0;
} else {
return -1;
}
}
char *grep_awk(FILE *fp, const char *fstr, int nfield, const char *delim) {
char *ret = NULL;
int i;
char *line = (char *)calloc(500, sizeof(char));
check_mem(line);
while (fgets(line, 400, fp) != NULL) {
if (strncasecmp(line, fstr, strlen(fstr)) == 0) {
ret = strtok(line, delim);
for (i = 0; i < nfield; i++) {
ret = strtok(NULL, delim);
}
if (ret) {
ret = strdup(ret);
check_mem(ret);
free(line);
return ret;
}
}
}
free(line);
return NULL;
error:
free(line);
return NULL;
}
char *squeeze(char *string, const char *chars) {
char *src = string;
char *target = string;
char ch;
for (; *chars; chars++) {
ch = *chars;
src = string;
target = string;
while (*src != '\0') {
if (*src != ch) {
*target = *src;
target++;
}
src++;
}
*target = '\0';
}
return string;
}