State is now being saved between sessions. Needs cleanup.
This commit is contained in:
32
main.c
32
main.c
@@ -149,7 +149,7 @@ Color activity_type_color_representation[activity_type_COUNT] = {
|
||||
// TODO: String representation.
|
||||
|
||||
typedef struct Activity {
|
||||
enum activity_type activity; // TODO: Rename to kind or type.
|
||||
activity_type activity; // TODO: Rename to kind or type.
|
||||
Color color;
|
||||
U64 began;
|
||||
U64 ended;
|
||||
@@ -220,7 +220,7 @@ int main(int argc, char *argv[])
|
||||
// WARNING: Not bullet proof, yet.
|
||||
FILE *f;
|
||||
if(path_exists(file_buffer)) {
|
||||
f = fopen(file_buffer, "r+");
|
||||
f = fopen(file_buffer, "r");
|
||||
if(!f) {
|
||||
fprintf(stderr, "fopen(%s) failed: %s\n", file_buffer, strerror(errno));
|
||||
return 1;
|
||||
@@ -278,6 +278,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
/* return 0; */
|
||||
|
||||
U64 last_save = 0;
|
||||
|
||||
U64 lower_bound_s = (U64)ts;
|
||||
U64 upper_bound_s = lower_bound_s + 86400;
|
||||
// U64 upper_bound_s = lower_bound_s + 3600;
|
||||
@@ -425,11 +427,35 @@ int main(int argc, char *argv[])
|
||||
for(U32 i = 0; i <= current_activity; i++) {
|
||||
activities_stats[activities[i].activity].total_seconds = 0;
|
||||
}
|
||||
|
||||
// Save the state every 5 seconds.
|
||||
if((now_s - last_save) >= 5) {
|
||||
f = freopen(file_buffer, "w", f); // WARNING: TODO: Might fail.
|
||||
|
||||
char write_buffer[128];
|
||||
for(U32 i = 0; i <= current_activity; i++) {
|
||||
snprintf(write_buffer, sizeof(write_buffer),
|
||||
"%s %llu %llu\n",
|
||||
activity_type_string_representation[activities[i].activity],
|
||||
activities[i].began,
|
||||
activities[i].ended);
|
||||
|
||||
size_t write_buffer_len = strlen(write_buffer);
|
||||
if(fwrite(write_buffer, 1, write_buffer_len, f) != write_buffer_len) {
|
||||
perror("fwrite");
|
||||
}
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
if (fflush(f) != 0)
|
||||
perror("fflush");
|
||||
|
||||
last_save = now_s;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
CloseWindow();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user