forked from rgcgithub/clamms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
60 lines (53 loc) · 1.39 KB
/
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
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void missing_value_error(char *arg) {
fprintf(stderr, "Missing value for argument: %s\n", arg);
exit(1);
}
void invalid_value_error(char *arg) {
fprintf(stderr, "Invalid value for argument: %s\n", arg);
exit(1);
}
// used by qsort
int double_comp(const void *a, const void *b) {
if (*((const double *) a) < *((const double *) b))
return -1;
return *((const double *) a) > *((const double *) b);
}
// input must be sorted
double median(double *arr, int len) {
if (len % 2 == 1)
return arr[(len-1)/2];
else
return (arr[len/2] + arr[len/2 - 1]) / 2.0;
}
FILE *open_file(char *path) {
FILE *file = fopen(path, "r");
if (file == NULL) {
fprintf(stderr, "Cannot read file: %s\n", path);
exit(1);
}
return file;
}
int count_lines_in_file(FILE *file) {
int n_lines = 0;
int tmp_char;
while ((tmp_char = getc(file)) != EOF) {
if (tmp_char == '\n') n_lines++;
} rewind(file);
return n_lines;
}
void read_sample_name(char *dest, char *src) {
char *slash_pos;
while ((slash_pos = strchr(src, '/')) != NULL)
src = slash_pos + 1;
char *first_dot = strchr(src, '.');
if (first_dot == NULL) {
strcpy(dest, src);
} else {
int len = first_dot - src;
strncpy(dest, src, len);
dest[len] = '\0';
}
}