summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlouie <lshprung@yahoo.com>2020-06-16 21:23:02 -0700
committerlouie <lshprung@yahoo.com>2020-06-16 21:23:02 -0700
commit96d5b7878f70b93651106b19768edfba7ba3dafd (patch)
tree4891b8f14b1595b269a70bbf9fa019c765f5449c
parentbad94ac2786a4e765707b5766ab3999e604e9d0b (diff)
Printing group column
-rw-r--r--draw.c131
-rw-r--r--entry.c88
-rw-r--r--entry.h18
-rw-r--r--group.c146
-rw-r--r--group.h26
-rw-r--r--read_cfg.c93
-rw-r--r--read_cfg.h8
-rw-r--r--structures.c184
8 files changed, 510 insertions, 184 deletions
diff --git a/draw.c b/draw.c
new file mode 100644
index 0000000..afd64bb
--- /dev/null
+++ b/draw.c
@@ -0,0 +1,131 @@
+#include <ncurses.h>
+#include <stdbool.h>
+#include <string.h>
+#include "entry.h"
+#include "group.h"
+#include "read_cfg.h"
+#define MAX_LEN 6
+#define BUF_LEN 1024
+
+void draw_title(int width);
+void draw_win(char *title, int x, int y, int width, int height);
+void fill_groups(GROUP **group_arr, int count, int startx, int starty, int maxx, int maxy);
+char *trim_group_name(GROUP *g, int max_len);
+
+int main(){
+ static int width;
+ static int height;
+ bool tall = true; //is the window a certain height (tbd what the threshold should be TODO)
+ bool wide = true; //is the window a certain width (tbd what the threshold should be TODO)
+ GROUP **g;
+ int g_count;
+
+ initscr();
+
+ width = getmaxx(stdscr);
+ height = getmaxy(stdscr);
+
+ //title at the top (Terminal Media Launcher) (23 chars)
+ draw_title(width);
+
+ //Draw Search Bar 2 spaces (3 spaces if window is big enough) under the title
+ move(3, width/4);
+ printw("[ Search: ");
+ move(3, (width*3)/4);
+ printw("]");
+ move(3, (width/4)+10);
+
+ //Draw Columns
+ //TODO create conditionals based on size of window)
+ draw_win("GROUP", 0, 4, width/3, height-4);
+ draw_win("ENTRY", (width/3), 4, width/3, height-4);
+ draw_win("INFO", (2*width/3), 4, width/3, height-4);
+
+ //Fill Groups
+ cfg_interp(); //read the contents of the cfg file
+ g = get_groups(); //retrieve results of previous function
+ g_count = get_gcount(g); //retrieve number of groups in g
+ fill_groups(g, g_count, 1, 5, ((width/3)-1), height);
+
+ /*
+ //DEBUG
+ move(5, 1);
+ printw("This is test output to test the limits of a window");
+ //END DEBUG
+ */
+
+ getch();
+ endwin();
+ return 0;
+}
+
+void draw_title(int width){
+ WINDOW *title;
+
+ title = newwin(2, width, 0, 0);
+ refresh();
+ attron(A_BOLD | A_UNDERLINE);
+ move(0, (width-23)/2);
+ printw("Terminal Media Launcher");
+ attroff(A_BOLD | A_UNDERLINE);
+ box(title, 0, 0);
+ wrefresh(title);
+
+ return;
+}
+
+void draw_win(char *title, int x, int y, int width, int height){
+ WINDOW *new;
+ int title_len = strlen(title);
+
+ new = newwin(height, width, y, x);
+ refresh();
+ attron(A_UNDERLINE);
+ move(y, x+(width-title_len)/2);
+ printw("%s", title);
+ attroff(A_UNDERLINE);
+ box(new, 0, 0);
+ wrefresh(new);
+
+ return;
+}
+
+void fill_groups(GROUP **group_arr, int count, int startx, int starty, int maxx, int maxy){
+ int i;
+ int max_len = maxx - startx; //longest possible string length that can be displayed in the window
+ char *name;
+
+ for(i = 0; i < count; i++){
+ name = get_gname(group_arr[i]);
+
+ //the name is too long, take the group to the trimming function
+ if(strlen(name) > max_len) name = trim_group_name(group_arr[i], max_len);
+ move(starty, startx);
+ printw("%s", name);
+ starty++;
+ }
+
+ return;
+}
+
+char *trim_group_name(GROUP *g, int max_len){
+ char *name = get_gname(g);
+ char *tok; //for use in finding relative path name
+ char *tok_ahead;
+ char *delims = "/\t\n";
+
+ //group name and path are equivalent: special procedure
+ if(!(strcmp(name, get_gpath(g)))){
+ //find relative path name
+ tok_ahead = strtok(name, delims);
+ while(tok_ahead != NULL){
+ tok = tok_ahead;
+ tok_ahead = strtok(NULL, delims);
+ }
+ name = tok;
+ if(strlen(name) <= max_len) return name;
+ }
+
+ name[max_len] = '\0';
+ return name;
+}
diff --git a/entry.c b/entry.c
new file mode 100644
index 0000000..d5f1425
--- /dev/null
+++ b/entry.c
@@ -0,0 +1,88 @@
+#include <assert.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "entry.h"
+#include "group.h"
+#define BUF_LEN 1024
+
+typedef struct entry{
+ char name[BUF_LEN];
+ char path[BUF_LEN];
+ struct entry *next;
+} ENTRY;
+
+ENTRY *create_entry(char *new_path, char *new_group);
+ENTRY *entry_add_last(ENTRY *tail, ENTRY *add);
+ENTRY **get_entries(ENTRY *head, int count);
+char *get_ename(ENTRY *e);
+char *get_epath(ENTRY *e);
+
+ENTRY *create_entry(char *new_path, char *new_group){
+ ENTRY *new;
+ char full_path[BUF_LEN] = "";
+
+ if(new_group != NULL) strcat(full_path, new_group);
+ strcat(full_path, new_path);
+
+ //check if file exists
+ if(access(full_path, F_OK) == -1){
+ printf("Error: Invalid File Name \"%s\"\n", full_path);
+ return NULL;
+ }
+
+ new = malloc(sizeof(ENTRY));
+
+ strcpy(new->name, new_path);
+ strcpy(new->path, full_path);
+ new->next = NULL;
+
+ return new;
+}
+
+ENTRY *entry_add_last(ENTRY *tail, ENTRY *add){
+ assert(add != NULL);
+
+ if(tail == NULL) tail = add;
+ else{
+ tail->next = add;
+ tail = add;
+ }
+
+ return tail;
+}
+
+ENTRY **get_entries(ENTRY *head, int count){
+ ENTRY **arr = malloc(sizeof(ENTRY *) * count);
+ ENTRY *trav = head;
+ int i;
+
+ for(i = 0; i < count; i++){
+ arr[i] = trav;
+ trav = trav->next;
+ }
+
+ return arr;
+}
+
+char *get_ename(ENTRY *e){
+ assert(e != NULL);
+ return e->name;
+}
+char *get_epath(ENTRY *e){
+ assert(e != NULL);
+ return e->name;
+ return e->path;
+}
+
+void entry_debug(ENTRY *trav){
+
+ while(trav != NULL){
+ printf("%s, \n", trav->name);
+ trav = trav->next;
+ }
+
+ return;
+}
diff --git a/entry.h b/entry.h
new file mode 100644
index 0000000..3c8a366
--- /dev/null
+++ b/entry.h
@@ -0,0 +1,18 @@
+#ifndef ENTRY_H
+#define ENTRY_H
+
+typedef struct entry ENTRY;
+
+ENTRY *create_entry(char *new_path, char *new_group);
+
+ENTRY *entry_add_last(ENTRY *tail, ENTRY *add);
+
+ENTRY **get_entries(ENTRY *head, int count);
+
+char *get_ename(ENTRY *e);
+
+char *get_epath(ENTRY *e);
+
+void entry_debug(ENTRY *trav);
+
+#endif
diff --git a/group.c b/group.c
new file mode 100644
index 0000000..b78f054
--- /dev/null
+++ b/group.c
@@ -0,0 +1,146 @@
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "entry.h"
+#include "group.h"
+#define BUF_LEN 1024
+
+typedef struct group{
+ char name[BUF_LEN];
+ char path[BUF_LEN];
+ char program[BUF_LEN];
+ struct entry *head;
+ struct entry *tail;
+ struct group *next;
+ int entry_count;
+} GROUP;
+
+GROUP *create_group(char *new_path);
+void group_add(char *gname, ENTRY *addme);
+GROUP **get_groups();
+char *get_gname(GROUP *g);
+char *get_gpath(GROUP *g);
+char *get_gprog(GROUP *g);
+ENTRY *get_ghead(GROUP *g);
+int get_ecount(GROUP *g);
+int get_gcount();
+void group_debug(); //debug function to output all groups
+
+GROUP *groups_head;
+GROUP *gp; //pointer to remember last group that was looked at
+int group_count = 0;
+int total_count = 0;
+
+GROUP *create_group(char *new_path){
+ GROUP *new = malloc(sizeof(GROUP));
+
+ strcpy(new->name, new_path); //by default, group name is equivalent to the path
+ strcpy(new->path, new_path);
+ strcpy(new->program, "./"); //by default, launch an entry by executing it
+ new->head = NULL;
+ new->tail = NULL;
+ new->next = NULL;
+ new->entry_count = 0;
+
+ group_count++;
+ return new;
+}
+
+//FIXME maybe make this function part of a seperate file to handle a tree (AVL?)
+//for now, simple linked list implementation
+void group_add(char *gname, ENTRY *addme){
+ int i;
+ GROUP *new;
+ GROUP *last = NULL; //last element in an existing group list (NULL to start)
+
+ //The previous group is not the same as the new group to add to
+ if(!(gp != NULL && (!(strcmp(gp->name, gname)) || !(strcmp(gp->path, gname))))){
+ gp = groups_head;
+ while(gp != NULL){
+ //gname matches groups[i]'s name or path, add entry here
+ if(!(strcmp(gp->name, gname)) || !(strcmp(gp->path, gname))) break;
+
+ last = gp;
+ gp = gp->next;
+ }
+ }
+
+ //was unable to find a matching existing group
+ //need to create new group to insert the entry into
+ if(gp == NULL){
+ new = create_group(gname);
+
+ //first group
+ if(last == NULL) groups_head = new;
+
+ //add to the end of the groups
+ else last->next = new;
+
+ gp = new;
+ }
+
+ //add the entry to the list of entries in the group
+ gp->tail = entry_add_last(gp->tail, addme);
+ if(gp->head == NULL) gp->head = gp->tail; //first element of the new list
+
+ gp->entry_count++;
+ total_count++;
+ return;
+}
+
+GROUP **get_groups(){
+ GROUP **arr = malloc(sizeof(GROUP *) * group_count);
+ GROUP *trav = groups_head;
+ int i;
+
+ for(i = 0; i < group_count; i++){
+ arr[i] = trav;
+ trav = trav->next;
+ }
+
+ return arr;
+}
+
+char *get_gname(GROUP *g){
+ assert(g != NULL);
+ return g->name;
+}
+
+char *get_gpath(GROUP *g){
+ assert(g != NULL);
+ return g->path;
+}
+
+char *get_gprog(GROUP *g){
+ assert(g != NULL);
+ return g->program;
+}
+
+ENTRY *get_ghead(GROUP *g){
+ assert(g != NULL);
+ return g->head;
+}
+
+int get_ecount(GROUP *g){
+ assert(g != NULL);
+ return g->entry_count;
+}
+
+int get_gcount(){
+ return group_count;
+}
+
+void group_debug(){
+ GROUP *trav = groups_head;
+
+ while(trav != NULL){
+ entry_debug(trav->head);
+ printf("\tfrom group %s\n", trav->name);
+ trav = trav->next;
+ }
+
+ return;
+}
diff --git a/group.h b/group.h
new file mode 100644
index 0000000..1afb63d
--- /dev/null
+++ b/group.h
@@ -0,0 +1,26 @@
+#ifndef GROUP_H
+#define GROUP_H
+
+typedef struct group GROUP;
+
+GROUP *create_group(char *new_path);
+
+void group_add(char *gname, ENTRY *addme);
+
+GROUP **get_groups();
+
+char *get_gname(GROUP *g);
+
+char *get_gpath(GROUP *g);
+
+char *get_gprog(GROUP *g);
+
+ENTRY *get_ghead(GROUP *g);
+
+int get_ecount(GROUP *g);
+
+int get_gcount();
+
+void group_debug();
+
+#endif
diff --git a/read_cfg.c b/read_cfg.c
new file mode 100644
index 0000000..8def4ef
--- /dev/null
+++ b/read_cfg.c
@@ -0,0 +1,93 @@
+#include <assert.h>
+#include <ctype.h>
+#include <dirent.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "entry.h"
+#include "group.h"
+#define BUF_LEN 1024 //maybe move this line to the header file
+
+void cfg_interp();
+void check_line(char *buffer);
+
+void cfg_interp(){
+ FILE *fp;
+ char buffer[BUF_LEN];
+ GROUP **g;
+ ENTRY **e;
+ int count;
+ int e_count;
+ int i;
+ int j;
+
+ //TODO have this check in certain locations for a config file, give error message if "config" does not exist
+ fp = fopen("config", "r");
+ assert(fp != NULL);
+
+ //Read each line of "config"
+ while(fgets(buffer, BUF_LEN, fp)){
+ check_line(buffer);
+ }
+
+ /*
+ //DEBUG: test to see if the list was added to properly
+ g = get_groups();
+ count = get_gcount();
+ for(i = 0; i < count; i++){
+ printf("Looking at group %s\n", get_gname(g[i]));
+ e_count = get_ecount(g[i]);
+ e = get_entries(get_ghead(g[i]), e_count);
+ for(j = 0; j < e_count; j++){
+ printf("\t%s\n", get_ename(e[j]));
+ }
+ }
+ //END DEBUG
+ */
+
+ return;
+}
+
+void check_line(char *buffer){
+ char *delims = " \t\n";
+ char *tok = strtok(buffer, delims);
+ DIR *dp;
+ struct dirent *d_entry;
+ ENTRY *new;
+
+ //add a path to the entry list
+ //TODO add potential dash functions
+ //TODO account for spaces in file name
+ if(!(strcmp(tok, "add"))){
+ tok = strtok(NULL, delims);
+ new = create_entry(tok, NULL);
+ if(new != NULL) group_add(tok, new);
+ }
+
+ //recursively add entries from a directory
+ if(!(strcmp(tok, "addDir"))){
+ tok = strtok(NULL, delims);
+ dp = opendir(tok);
+
+ //check if the directory is valid
+ if(dp == NULL) printf("Error: Invalid Directory \"%s\"\n", tok);
+
+ else{
+ while((d_entry = readdir(dp))){
+ //only add if its a file (d_type = 8)
+ if(d_entry->d_type == 8){
+ new = create_entry(d_entry->d_name, tok);
+ if(new != NULL) group_add(tok, new);
+ }
+ }
+ }
+
+ //TODO add option to also add files from directories inside directory (-r)
+
+ closedir(dp);
+ }
+
+ return;
+}
diff --git a/read_cfg.h b/read_cfg.h
new file mode 100644
index 0000000..2e8c708
--- /dev/null
+++ b/read_cfg.h
@@ -0,0 +1,8 @@
+#ifndef READ_CFG_H
+#define READ_CFG_H
+
+void cfg_interp();
+
+void check_line(char *buffer);
+
+#endif
diff --git a/structures.c b/structures.c
deleted file mode 100644
index e8d42dd..0000000
--- a/structures.c
+++ /dev/null
@@ -1,184 +0,0 @@
-#include <assert.h>
-#include <ctype.h>
-#include <dirent.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#define BUF_LEN 1024
-#define MAX_GRPS 50
-
-typedef struct entry{
- char name[BUF_LEN];
- char path[BUF_LEN];
- struct entry *next;
-} ENTRY;
-
-typedef struct group{
- char name[BUF_LEN];
- char path[BUF_LEN];
- struct entry *head;
- struct entry *tail;
- struct group *next;
-} GROUP;
-
-void check_line(char *buffer);
-ENTRY *create_entry(char *new_path, char *new_group);
-GROUP *create_group(char *new_path);
-void group_add(char *gname, ENTRY *addme);
-
-GROUP *groups_head;
-GROUP *gp; //pointer to remember last group that was looked at
-
-int main(){
- FILE *fp;
- char buffer[BUF_LEN];
-
- fp = fopen("config", "r");
- assert(fp != NULL);
-
- while(fgets(buffer, BUF_LEN, fp)){
- check_line(buffer);
- }
-
- //DEBUG: test to see if the list was added to properly
- GROUP *gtrav = groups_head;
- ENTRY *etrav;
- while(gtrav != NULL){
- etrav = gtrav->head;
- while(etrav != NULL){
- printf("%s, from group %s\n", etrav->name, gtrav->name);
- etrav = etrav->next;
- }
- gtrav = gtrav->next;
- }
-
- return 0;
-}
-
-void check_line(char *buffer){
- char *delims = " \t\n";
- char *tok = strtok(buffer, delims);
- DIR *dp;
- struct dirent *entry;
- ENTRY *new;
-
- //add a path to the entry list
- //TODO add potential dash functions
- //TODO account for spaces in file name
- if(!(strcmp(tok, "add"))){
- tok = strtok(NULL, delims);
- new = create_entry(tok, NULL);
- if(new != NULL) group_add(tok, new);
- }
-
- //recursively add entries from a directory
- if(!(strcmp(tok, "addDir"))){
- tok = strtok(NULL, delims);
- dp = opendir(tok);
-
- //check if the directory is valid
- if(dp == NULL) printf("Error: Invalid Directory \"%s\"\n", tok);
-
- else{
- while((entry = readdir(dp))){
- //only add if its a file (d_type = 8)
- if(entry->d_type == 8){
- new = create_entry(entry->d_name, tok);
- if(new != NULL) group_add(tok, new);
- }
- }
- }
-
- //TODO add option to also add files from directories inside directory (-r)
-
- closedir(dp);
- }
-
- return;
-}
-
-ENTRY *create_entry(char *new_path, char *new_group){
- ENTRY *new;
- char full_path[BUF_LEN] = "";
-
- if(new_group != NULL) strcat(full_path, new_group);
- strcat(full_path, new_path);
-
- //check if file exists
- if(access(full_path, F_OK) == -1){
- printf("Error: Invalid File Name \"%s\"\n", full_path);
- return NULL;
- }
-
- new = malloc(sizeof(ENTRY));
-
- strcpy(new->name, new_path);
- strcpy(new->path, full_path);
- new->next = NULL;
-
- return new;
-}
-
-GROUP *create_group(char *new_path){
- GROUP *new = malloc(sizeof(GROUP));
-
- strcpy(new->name, new_path); //by default, group name is equivalent to the path
- strcpy(new->path, new_path);
- new->head = NULL;
- new->tail = NULL;
- new->next = NULL;
-
- return new;
-}
-
-//FIXME maybe make this function part of a seperate file to handle a tree (AVL?)
-//for now, simple linked list implementation
-void group_add(char *gname, ENTRY *addme){
- int i;
- bool new_group = false; //do we need to create a new group?
- GROUP *new;
- GROUP *last = NULL; //last element in an existing group list (NULL to start)
-
- //The previous group is not the same as the new group to add to
- if(!(gp != NULL && (!(strcmp(gp->name, gname)) || !(strcmp(gp->path, gname))))){
- gp = groups_head;
- while(gp != NULL){
- //gname matches groups[i]'s name or path, add entry here
- if(!(strcmp(gp->name, gname)) || !(strcmp(gp->path, gname))) break;
-
- last = gp;
- gp = gp->next;
- }
- }
-
- //was unable to find a matching existing group
- if(gp == NULL) new_group = true;
-
- //need to create new group to insert the entry into
- if(new_group){
- new = create_group(gname);
-
- //first group
- if(last == NULL) groups_head = new;
-
- //add to the end of the groups
- else last->next = new;
-
- gp = new;
- }
-
- //add the entry to the list of entries in the group
- if(gp->head == NULL){
- gp->head = addme;
- gp->tail = addme;
- }
-
- else{
- gp->tail->next = addme;
- gp->tail = addme;
- }
-
- return;
-}