reading from file
[taskasaur.git] / main.c
diff --git a/main.c b/main.c
index 83b5aa0..3b219ec 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,10 +1,15 @@
 #include <stdio.h>
 #include <ncurses.h>
 #include <signal.h>
+#include <stdlib.h>
 #include <unistd.h>
 
+#define MAX_TODO_LENGTH 200
+
 void winch_handler(int sig); 
 
+char* read_todo(FILE* file);
+
 WINDOW* create_list_win(int height, int width, int y, int x);
 
 #include "config.h"
@@ -12,6 +17,7 @@ WINDOW* create_list_win(int height, int width, int y, int x);
 int 
 main(int argc, char** argv) 
 {
+    int flag;
     FILE* board_file;
     int height, width;
     int x, y;
@@ -20,13 +26,47 @@ main(int argc, char** argv)
 
     signal(SIGWINCH, winch_handler);
 
-    // read from task file
-    board_file = fopen(default_board_file, "r");
-    if (!board_file) {
-        printf("File does not exist\n");
-        return 1;
-    }   
-    fclose(board_file);
+    // read command line args
+    flag = getopt(argc, argv, "o:n:");
+    switch (flag) {
+        case 'o':
+
+            // read from task file (might need to check for read and write permissions)
+            board_file = fopen(optarg, "r");
+            if (!board_file) {
+                printf("%s does not exist\n", optarg);
+                return 1;
+            }   
+            
+            char* lineptr = read_todo(board_file);
+            printf("lineptr: %p\n", lineptr);
+            printf("first: %c\n", *lineptr);
+            free(lineptr);
+            fclose(board_file);
+
+            break;
+
+        case 'n':
+
+            // make sure file does not exist
+            // however, it maybe be possible that an different error has occured (besides the file not existing)
+            if (access(optarg, F_OK) == 0) { 
+                printf("%s already exists\n", optarg);
+                return 1;
+            }
+            // create a file here
+            printf("Successfully created %s\n", optarg);
+            break;
+
+        case -1:
+        case '?':
+            printf("Help string\n");
+            return 2;
+    }
+
+
+
+    return 0;
 
     // start ncurses 
     initscr();
@@ -79,6 +119,24 @@ winch_handler(int sig)
     refresh();
 }
 
+
+char*
+read_todo(FILE* file) 
+{ // apparently getline isn't rly that portable, so consider other options
+    char* lineptr;
+    size_t len;
+    ssize_t nread;
+
+    lineptr = NULL;
+    len = 0;
+
+    while ((nread = getline(&lineptr, &len, file)) != -1) {
+        printf("Read line of size %zd\n", nread);
+    }
+
+    return lineptr;
+}
+
 WINDOW* 
 create_list_win(int height, int width, int y, int x)
 {