aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--later.c134
-rw-r--r--later.h14
3 files changed, 141 insertions, 9 deletions
diff --git a/Makefile b/Makefile
index d201105..a7fb15f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
.PHONY: build
build:
- cc later.c -Wall -Werror -o later
+ cc -g later.c -Wall -Werror -o later
diff --git a/later.c b/later.c
index 81eda3b..5dc7303 100644
--- a/later.c
+++ b/later.c
@@ -5,7 +5,6 @@
#include "later.h"
-
LaterItem_t* new_item() {
LaterItem_t* item = (LaterItem_t*)malloc(sizeof(LaterItem_t));
item->directory = NULL;
@@ -14,6 +13,99 @@ LaterItem_t* new_item() {
return item;
}
+LaterFile_t* new_LaterFile() {
+ LaterFile_t* file = (LaterFile_t*)malloc(sizeof(LaterFile_t));
+ file->filename = NULL;
+ file->file = NULL;
+ file->capacity = 8;
+ file->items = (LaterItem_t**)malloc(sizeof(LaterItem_t*) * file->capacity);
+ file->size = 0;
+ file->loaded = false;
+ file->modified = false;
+ return file;
+}
+
+bool build_LaterFile(LaterFile_t* file, char* laterfile_path) {
+ // open the file for reading/appending (so it gets created if necessary)
+ FILE* laterfile = fopen(laterfile_path, "a+");
+ fseek(laterfile, 0, SEEK_END);
+ int filesize = ftell(laterfile);
+ rewind(laterfile);
+
+ file->filename = laterfile_path;
+ file->file = laterfile;
+
+ // if the file is empty, we're done
+ if (filesize == 0) {
+ printf("empty file!\n");
+ return true;
+ }
+
+ // start reading in entries
+ char buf[2048];
+ char* ret;
+ LaterItem_t* curitem = NULL;
+ while (true) {
+ // get the next line
+ ret = fgets(buf, sizeof(buf), laterfile);
+ if (ret == NULL) {
+ break;
+ } else if (ret[strlen(ret)-1] == '\n') {
+ ret[strlen(ret)-1] = '\0';
+ }
+
+ // are we starting a new item?
+ if (curitem == NULL) {
+ curitem = new_item();
+ }
+
+ // if we've reached the end of a definition
+ if (strcmp(ret, "--") == 0) {
+ file->items[file->size] = curitem;
+ file->size++;
+ curitem = NULL;
+ } else if (strncmp(ret, "DIR ", 4) == 0) {
+ // directory
+ curitem->directory = strdup(ret+4);
+ } else if (strncmp(ret, "PATH ", 5) == 0) {
+ //path
+ curitem->path = strdup(ret+5);
+ } else if (strncmp(ret, "DO ", 3) == 0) {
+ // command
+ curitem->command = strdup(ret+3);
+ } else {
+ // fallthrough
+ printf("unrecognized: %s\n", ret);
+ break;
+ }
+ }
+
+ if (curitem != NULL) {
+ free(curitem);
+ return false;
+ }
+
+ return true;
+}
+
+void add_item(LaterFile_t* file, LaterItem_t* item) {
+ // TODO capacity things
+ file->items[file->size] = item;
+ file->size++;
+ file->modified = true;
+}
+
+void free_LaterFile(LaterFile_t* file) {
+ fclose(file->file);
+ LaterItem_t* cur;
+ for (int i = 0; i < file->capacity; i++) {
+ cur = file->items[i];
+ if (cur != NULL) {
+ free(cur);
+ }
+ }
+}
+
void free_item(LaterItem_t* item) {
if (item->directory) free(item->directory);
if (item->path) free(item->path);
@@ -45,7 +137,7 @@ void build_item(LaterItem_t* item, char** raw_command, int raw_command_length) {
item->command = command;
}
-void print_item(LaterItem_t* item) {
+void debug_item(LaterItem_t* item) {
/*
* Format:
* DIR <pwd>
@@ -56,6 +148,15 @@ void print_item(LaterItem_t* item) {
printf("DIR %s\nPATH %s\nDO %s\n--\n", item->directory, item->path, item->command);
}
+void write_file(LaterFile_t* file) {
+ // reopen the file for writing
+ FILE* newfp = freopen(file->filename, "w", file->file);
+ LaterItem_t* curitem;
+ for (int i = 0; i < file->size; i++) {
+ curitem = file->items[i];
+ fprintf(newfp, "DIR %s\nPATH %s\nDO %s\n--\n", curitem->directory, curitem->path, curitem->command);
+ }
+}
int main(int argc, char** argv) {
/* how it could work
@@ -70,9 +171,19 @@ int main(int argc, char** argv) {
return 1;
}
- // open the file for reading/appending
- FILE* laterfile = fopen(laterfile_path, "a+");
- fseek(laterfile, 0, SEEK_END);
+ LaterFile_t* file = new_LaterFile();
+ bool ret = build_LaterFile(file, laterfile_path);
+ if (!ret) {
+ printf("couldn't build file, exiting\n");
+ return 1;
+ }
+
+ // debug the existing file
+ LaterItem_t* cur;
+ for (int i = 0; i < file->size; i++) {
+ cur = file->items[i];
+ debug_item(cur);
+ }
// loop through the arguments
int argi = 1;
@@ -86,9 +197,16 @@ int main(int argc, char** argv) {
argi++; // go past the --
- LaterItem_t* item = new_item();
- build_item(item, argv+argi, argc-argi);
- print_item(item);
+ // build new item
+ cur = new_item();
+ build_item(cur, argv+argi, argc-argi);
+ add_item(file, cur);
+
+ // write the file if necessary
+ if (file->modified)
+ write_file(file);
+ // cleanup
+ free(file);
return 0;
}
diff --git a/later.h b/later.h
index 875b81c..6fd665d 100644
--- a/later.h
+++ b/later.h
@@ -1,6 +1,8 @@
#ifndef __LATER_H__
#define __LATER_H__
+#include <stdbool.h>
+
typedef struct LaterItem LaterItem_t;
struct LaterItem {
@@ -9,4 +11,16 @@ struct LaterItem {
char* command;
};
+typedef struct LaterFile LaterFile_t;
+
+struct LaterFile {
+ char* filename;
+ FILE* file;
+ LaterItem_t** items;
+ int size;
+ int capacity;
+ bool loaded;
+ bool modified;
+};
+
#endif