Read the habits from file

This commit is contained in:
Ngô Ngọc Đức Huy 2021-06-23 15:51:29 +07:00
parent 21d9060e9f
commit dbb1aeac63
Signed by: huyngo
GPG Key ID: 904AF1C7CDF695C3
7 changed files with 102 additions and 64 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.out
*.o
build/

16
Makefile Normal file
View File

@ -0,0 +1,16 @@
all: generate wochabit clean
generate:
@echo "Generating binaries"
gcc -Wall -c date.c
gcc -Wall -c habit.c
gcc -Wall -c wochabit.c
wochabit:
@echo "Building executable"
mkdir -p build
gcc -o build/wochabit date.o habit.o wochabit.o
clean:
@echo "Cleaning up..."
rm *.o

View File

@ -20,6 +20,6 @@ static const char FAIL_CHAR[] = "\u2588\u2588\u2588";
// The number of days for habit tracking
static const int TRACK_LENGTH = 7;
static const char HABIT_FILE[] = "~/.wochabit";
static const char HABIT_FILE[] = "/.wochabit";
#endif

View File

@ -16,5 +16,6 @@ static const char COLOR_PREFIX[] = "\033[3%d;1m";
static const char COLOR_SUFFIX[] = "\033[0m";
static const int DATE_SIZE = 11; // "YYYY-MM-DD\0" = 11 chars
static const int HABIT_NAME_SIZE = 64; // A habit name shouldn't be that long
#endif

67
habit.c
View File

@ -3,33 +3,86 @@
#include <string.h>
#include <time.h>
#include "date.h"
#include "habit.h"
#include "config.h"
#include "constants.h"
int
add_habit(struct habit_tracker *tracker, char *name, int stati)
{
struct habit *first = habit_tracker->first;
struct habit *first = tracker->first;
struct habit *new_habit;
new_habit = malloc(sizeof(struct habit));
if (new_habit == NULL) {
return -1;
}
new_habit->stati = stati;
new_habit->name = name;
new_habit->name = strdup(name);
new_habit->next = first;
habit_tracker->first = new_habit;
tracker->first = new_habit;
return 0;
}
int
habit_from_file(struct habit_tracker *tracker, FILE *fp)
{
int stat_buf;
char *ptr, *endptr;
char linebuf[HABIT_NAME_SIZE];
char linebuf[HABIT_NAME_SIZE + 8];
char date[DATE_SIZE];
int stati;
char name[HABIT_NAME_SIZE];
int first = 1;
tracker->first = NULL;
if
tracker->size = 0;
while(fgets(linebuf, HABIT_NAME_SIZE, fp)) {
if (first) {
sscanf(linebuf, "%s\n", &date);
tracker->date = strdup(date);
first = 0;
continue;
}
sscanf(linebuf, "%d\t%[^\t\n]", &stati, &name);
add_habit(tracker, name, stati);
++(tracker->size);
}
return 0;
}
void
print_status(int status)
{
if (status) {
printf(COLOR_PREFIX, DONE_COLOR);
printf(DONE_CHAR);
} else {
printf(COLOR_PREFIX, FAIL_COLOR);
printf(FAIL_CHAR);
}
printf(COLOR_SUFFIX);
}
void
print_habit(int habit)
{
for (int i = 0; i < TRACK_LENGTH; ++i) {
int status = habit % 2;
print_status(status);
habit >>= 1;
}
}
void
print_habits(struct habit_tracker *tracker)
{
struct habit *current = tracker->first;
printf("%s\n", tracker->date);
print_days();
for (int i = 0; i < tracker->size && current != NULL; ++i) {
print_habit(current->stati);
printf(" (%d) %s\n", i, current->name);
current = current->next;
}
}

16
habit.h
View File

@ -1,18 +1,24 @@
const int HABIT_NAME_SIZE = 64; // A habit name shouldn't be that long
#ifndef HABIT_H
#define HABIT_H
struct habit {
int stati; // Plural of status :)
char name;
char *name;
struct habit *prev;
struct habit *next;
}
};
struct habit_tracker {
char date[DATE_SIZE];
char *date;
int size;
struct habit *first;
}
};
int add_habit(struct habit_tracker *tracker, char *name, int stati);
int del_habit(struct habit_tracker *tracker, int position);
int habit_from_file(struct habit_tracker *tracker, FILE *fp);
void print_status(int status); // Print the status for a day of a habit
void print_habit(int habit); // Print the habit stati during the tracked duration
void print_habits(struct habit_tracker *tracker);
#endif

View File

@ -18,66 +18,27 @@
* */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "constants.h"
#include "config.h"
#include "date.h"
#include "habit.h"
void print_status(int status); // Print the status for a day of a habit
void print_habit(int habit); // Print the habit stati during the tracked duration
// Like print_habit, but for all habits from the tracker file
void print_habits(const char *fname);
void
print_status(int status)
{
if (status) {
printf(COLOR_PREFIX, DONE_COLOR);
printf(DONE_CHAR);
} else {
printf(COLOR_PREFIX, FAIL_COLOR);
printf(FAIL_CHAR);
}
printf(COLOR_SUFFIX);
}
void
print_habit(int habit)
{
for (int i = 0; i < TRACK_LENGTH; ++i) {
int status = habit % 2;
print_status(status);
habit >>= 1;
}
printf("\n");
}
void
print_habits(const char *fname)
{
FILE *fp;
char date_buf[DATE_SIZE];
fp = fopen(fname, "r");
if (fp == NULL) {
fprintf(stderr, "Cannot read file %s: %s", fname,
strerror(errno));
}
}
int
main(int argc, char *argv[]){
print_habit(5);
print_habit(49);
print_habit(1 << TRACK_LENGTH - 2);
print_days();
struct tm date = parse_date("2020-11-19");
printf("%d\n", date.tm_year);
printf("%d\n", date.tm_mon);
printf("%d\n", date.tm_mday);
int diff = get_day_diff(&date);
printf("%d\n", diff);
printf("Bye\n");
struct habit_tracker tracker;
FILE *fp;
char *fname;
fname = malloc(strlen(getenv("HOME")) + strlen(HABIT_FILE) + 1);
strcpy(fname, getenv("HOME"));
strcat(fname, HABIT_FILE);
fp = fopen(fname, "r");
habit_from_file(&tracker, fp);
fclose(fp);
print_habits(&tracker);
return 0;
}