aboutsummaryrefslogtreecommitdiffstats
path: root/bin/common.c
blob: 0e1315aee3767dcdce27756ab47469df9339c275 (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include "common.h"

void bail_out(const char* msg)
{
	perror(msg);
	exit(-1 * errno);
}

int str2int(const char* arg, int *failure_flag)
{
	long val;
	char *end;

	val = strtol(arg, &end, 10);
	/* upon successful conversion, must point to string end */
	if (failure_flag)
		*failure_flag = *arg == '\0' || *end != '\0' ||
		                val > INT_MAX || val < INT_MIN;
	return (int) val;
}

double str2double(const char* arg, int *failure_flag)
{
	double val;
	char *end;

	val = strtod(arg, &end);
	/* upon successful conversion, must point to string end */
	if (failure_flag)
		*failure_flag = *arg == '\0' || *end != '\0';
	return val;
}

char* strsplit(char split_char, char *str)
{
	char *found = strrchr(str, split_char);
	if (found) {
		/* terminate first string and move to next char */
		*found++ = '\0';
	}
	return found;
}

/*
 * returns the character that made processing stop, newline or EOF
 */
static int skip_to_next_line(FILE *fstream)
{
	int ch;	for (ch = fgetc(fstream); ch != EOF && ch != '\n'; ch = fgetc(fstream));
	return ch;
}

/*
 * skip lines that start with '#'
 */
static void skip_comments(FILE *fstream)
{
	int ch;
	for (ch = fgetc(fstream); ch == '#'; ch = fgetc(fstream))
		skip_to_next_line(fstream);
	ungetc(ch, fstream);
}

double* csv_read_column(const char *file, int column, int *num_rows)
{
	FILE *fstream;
	int  cur_row, cur_col, ch;
	double *values = NULL;
	*num_rows = 0;

	fstream = fopen(file, "r");
	if (!fstream)
		bail_out("could not open execution time file");

	/* figure out the number of jobs */
	do {
		skip_comments(fstream);
		ch = skip_to_next_line(fstream);
		if (ch != EOF)
			++(*num_rows);
	} while (ch != EOF);

	if (-1 == fseek(fstream, 0L, SEEK_SET))
		bail_out("rewinding file failed");

	/* allocate space for exec times */
	values = calloc(*num_rows, sizeof(double));
	if (!values)
		bail_out("couldn't allocate memory");

	for (cur_row = 0; cur_row < *num_rows && !feof(fstream); ++cur_row) {

		skip_comments(fstream);

		for (cur_col = 1; cur_col < column; ++cur_col) {
			/* discard input until we get to the column we want */
			int unused __attribute__ ((unused)) = fscanf(fstream, "%*s,");
		}

		/* get the desired exec. time */
		if (1 != fscanf(fstream, "%lf", values + cur_row)) {
			fprintf(stderr, "invalid execution time near line %d\n",
					cur_row);
			exit(EXIT_FAILURE);
		}

		skip_to_next_line(fstream);
	}

	assert(cur_row == *num_rows);
	fclose(fstream);

	return values;
}