-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrun.c
154 lines (134 loc) · 4.51 KB
/
rrun.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
/*
* Copyright (c) 2013, Will Dignazio
* See the file LICENSE file for copying permission.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <string.h>
#include <sqlite3.h>
#include "rstat.h"
void print_run(run_t* run) {
printf("Run Location: %s\n", run->location);
printf("Run Time (Minutes): %lf\n", run->time);
printf("Run Distance (Meters): %lf\n", run->distance);
printf("Run Temperature (Celcius): %lf\n", run->temperature);
}
void print_runs(run_t *run) {
do {
print_run(run);
printf("---------------------------------\n");
} while(run->next != NULL);
}
run_t *get_runs(runner_t *runner, sqlite3 **sqconn) {
char *sqlite3_query =
"SELECT runs.* FROM runners "
"INNER JOIN runs ON runners.uid = runs.uid "
"GROUP BY runners.uid";
run_t *run = NULL;
sqlite3_stmt *res;
const char *tail;
int perr = sqlite3_prepare_v2(*sqconn, sqlite3_query, 1000, &res, &tail);
if(perr != SQLITE_OK) {
fprintf(stderr, "Failed to prepare run join statement.");
return NULL;
}
/* Go through all the runs, building a linked
* list of them for display. */
run_t *run_head = run;
while( sqlite3_step(res) == SQLITE_ROW ) {
DEBUG(printf("Found run at %s: ", sqlite3_column_text(res, QUERY_ROW_LOCATION)));
run_head->next = build_run(sqlite3_column_int(res, QUERY_ROW_UID),
(char*)sqlite3_column_text(res, QUERY_ROW_LOCATION),
sqlite3_column_double(res, QUERY_ROW_TIME),
sqlite3_column_double(res, QUERY_ROW_DISTANCE),
sqlite3_column_double(res, QUERY_ROW_TEMPERATURE));
if(run_head->next != NULL)
run_head = run_head->next;
}
sqlite3_finalize(res);
return run;
}
run_t *build_run(uid_t uid,
char *location,
double time,
double distance,
double temperature) {
run_t *run = calloc(sizeof(run_t), 1);
run->uid = uid;
run->time = time;
run->distance = distance;
run->temperature = temperature;
run->next = NULL;
return run;
}
int put_run(run_t *run, sqlite3 **sqconn) {
if(!user_exists(sqconn)) {
fprintf(stderr, "Create a User before adding runs!\n");
return RSTAT_FAIL;
}
char *statement = sqlite3_mprintf(
"INSERT INTO runs(uid, location, time, distance, temperature) "
"VALUES(%d, \"%s\", %lf, %lf, %lf)",
run->uid,
run->location,
run->time,
run->distance,
run->temperature);
DEBUG(printf("Created sql run statement.\n"));
switch( sqlite3_exec(*sqconn, statement, 0, 0, 0) ) {
case SQLITE_OK:
DEBUG(printf("Successfully put run into table."));
sqlite3_free(statement);
return RSTAT_SUCCESS;
default:
fprintf(stderr, "Error: %s", sqlite3_errmsg(*sqconn));
sqlite3_free(statement);
return RSTAT_FAIL;
}
sqlite3_free(statement);
return RSTAT_FAIL;
}
int free_run(run_t* run) {
if(run->location != NULL)
free(run->location);
free(run);
return RSTAT_SUCCESS;
}
run_t *query_run_info() {
run_t *run = calloc(sizeof(run_t), 1);
// Get the essential user details
run->uid = getuid();
// We're going to try and to this as safely as possible...
char loc_buffer[100] = {0};
printf("Enter Run Location (100 Characters Max):\n");
while(strlen(loc_buffer) <= 0) {
printf(">");
memset(loc_buffer, 0, sizeof(loc_buffer));
fgets(loc_buffer, sizeof(loc_buffer), stdin);
if(loc_buffer[strlen(loc_buffer)-1] == '\n') // Remove trailing newline
loc_buffer[strlen(loc_buffer)-1] = '\0';
}
/* We've got the string, now copy it to dynamic memory and set it. */
char *str = malloc(strlen(loc_buffer)); // Allocate for run
strcpy(str, loc_buffer); // Copy in
run->location = str; // Set
printf("Run time (Minutes):\n");
do {
printf("> ");
scanf("%*c"); // Flush stdin
} while( scanf("%lf", &run->time) != 1);
printf("Run Distance (Meters):\n");
do {
printf("> ");
scanf("%*c");
} while( scanf("%lf", &run->distance) != 1);
printf("Run Temperature (Celcius):\n");
do {
printf("> ");
scanf("%*c");
} while( scanf("%lf", &run->temperature) != 1);
return run;
}