summaryrefslogtreecommitdiff
path: root/src/get_file.c
blob: 914163169b9a4ed0355f47b50104535cf0e25ffa (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

#include "fracture.h"

FILE *get_file(const char *prefix, unsigned int width, unsigned int crack,
							 double beta, unsigned int iter, unsigned int num_iter,
							 unsigned int num, bool read) {
	int prefix_len = strlen(prefix);
	int width_len = 1 + (int)log10(width);
	int crack_len;
	if (crack != 0) {
		crack_len = 1 + (int)log10(crack);
	} else {
		crack_len = 1;
	}
	int beta_len;
	if (beta > 1) {
		beta_len = 1 + (int)log10(beta) + 3;
	} else {
		beta_len = 4;
	}
	int iter_len = 1 + (int)log10(num_iter);
	int num_len = 1 + (int)log10(num);
	int num_underscores = 5;

	int len = prefix_len + width_len + crack_len + beta_len + iter_len + num_len +
						num_underscores + 4;

	char filename[len + 1];
	snprintf(filename, sizeof(filename), "%s_%u_%u_%.2f_%0*u_%u.txt", prefix,
					 width, crack, beta, iter_len, iter + 1, num);

	char *mode = "w";
	if (read) {
		mode = "r";
	}
	FILE *file = fopen(filename, mode);
	return file;
}