diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..dc80d73 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "makefile.configureOnOpen": false, + "files.associations": { + "ncurses.h": "c", + "logic.h": "c", + "stdio.h": "c", + "globals.h": "c", + "windows.h": "c", + "stdlib.h": "c" + } +} \ No newline at end of file diff --git a/build/globals.d b/build/globals.d new file mode 100644 index 0000000..49d7c7a --- /dev/null +++ b/build/globals.d @@ -0,0 +1,2 @@ +build/globals.o: src/globals.c src/../include/globals.h +src/../include/globals.h: diff --git a/build/globals.o b/build/globals.o new file mode 100644 index 0000000..98323c6 Binary files /dev/null and b/build/globals.o differ diff --git a/build/logic.d b/build/logic.d new file mode 100644 index 0000000..ff34596 --- /dev/null +++ b/build/logic.d @@ -0,0 +1,4 @@ +build/logic.o: src/logic.c src/../include/logic.h \ + src/../include/globals.h +src/../include/logic.h: +src/../include/globals.h: diff --git a/build/logic.o b/build/logic.o new file mode 100644 index 0000000..d3f0250 Binary files /dev/null and b/build/logic.o differ diff --git a/build/main.d b/build/main.d new file mode 100644 index 0000000..767f5ec --- /dev/null +++ b/build/main.d @@ -0,0 +1,3 @@ +build/main.o: src/main.c include/windows.h include/logic.h +include/windows.h: +include/logic.h: diff --git a/build/main.o b/build/main.o new file mode 100644 index 0000000..6565e06 Binary files /dev/null and b/build/main.o differ diff --git a/build/windows.d b/build/windows.d new file mode 100644 index 0000000..f019f6a --- /dev/null +++ b/build/windows.d @@ -0,0 +1,5 @@ +build/windows.o: src/windows.c src/../include/windows.h \ + src/../include/logic.h src/../include/globals.h +src/../include/windows.h: +src/../include/logic.h: +src/../include/globals.h: diff --git a/build/windows.o b/build/windows.o new file mode 100644 index 0000000..9a1144b Binary files /dev/null and b/build/windows.o differ diff --git a/include/globals.h b/include/globals.h new file mode 100644 index 0000000..a429d6e --- /dev/null +++ b/include/globals.h @@ -0,0 +1,11 @@ +#ifndef GLOBALS_H +#define GLOBALS_H + +#include +#include +#include +#include + +extern char productPricesFileLocation[64]; // Dichiarazione extern + +#endif \ No newline at end of file diff --git a/include/logic.h b/include/logic.h new file mode 100644 index 0000000..27a283a --- /dev/null +++ b/include/logic.h @@ -0,0 +1,15 @@ +#ifndef LOGIC_H +#define LOGIC_H + +#include +#include +#include +#include + +// TODO: QUI CI VA LA STRUCT DATI + +int getCount(); +void changeFile(); +int exists(const char *fname); + +#endif \ No newline at end of file diff --git a/include/windows.h b/include/windows.h new file mode 100644 index 0000000..ab4f2c1 --- /dev/null +++ b/include/windows.h @@ -0,0 +1,20 @@ +#ifndef WINDOWS_H +#define WINDOWS_H + +#include +#include +#include +#include + +int runMainMenu(); +void runOptionsMenu(); +void warningf(const char *fmt, ...); +void warning(char *string); +void statusf(const char *fmt, ...); +void status(char *msg); +WINDOW *create_newwin(int height, int width, int starty, int startx); +void destroy_win(WINDOW *local_win); +void print_menu(WINDOW *menu_win, int y, int x, int highlight); +void word_wrap_print(WINDOW *win, int start_y, int start_x, int max_width, const char *str); + +#endif \ No newline at end of file diff --git a/makefile b/makefile index d0ae140..c19a008 100644 --- a/makefile +++ b/makefile @@ -1,33 +1,26 @@ -# Makefile per un programma C con ncurses su Linux x64 - -# Variabili CC = gcc -CFLAGS = -Wall -g -std=c11 -NCLUDE_DIRS = ./ -LDLIBS = -lncurses -lncursesw +CFLAGS = -I./include -Wall -Wextra -MMD -MP +LDLIBS = -lncurses +SRC_DIR = src +BUILD_DIR = build +SOURCES = $(wildcard $(SRC_DIR)/*.c) +OBJECTS = $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SOURCES)) +EXEC = newCounter -# Nome del programma -TARGET = newCounter +DEPS = $(OBJECTS:.o=.d) -# File sorgente -SRC_FILES = main.c +all: $(EXEC) -# Nome dell'eseguibile -EXECUTABLE = $(TARGET) +$(EXEC): $(OBJECTS) + $(CC) $^ -o $@ $(LDLIBS) -# Default target -all: $(EXECUTABLE) - -# Regole di compilazione -$(EXECUTABLE): $(SRC_FILES) - $(CC) $(CFLAGS) $(SRC_FILES) -o $(EXECUTABLE) $(LDLIBS) - -# Regole per la pulizia (rimuove l'eseguibile e i file oggetto) -clean: - rm -f $(EXECUTABLE) *.o - -# Regole per la creazione di file oggetto (opzionale, ma consigliata per una compilazione più veloce) -%.o: %.c +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c + @mkdir -p $(@D) $(CC) $(CFLAGS) -c $< -o $@ +-include $(DEPS) +clean: + rm -rf $(BUILD_DIR) $(EXEC) + +.PHONY: all clean \ No newline at end of file diff --git a/newCounter b/newCounter index c508b7b..9536579 100755 Binary files a/newCounter and b/newCounter differ diff --git a/productPricesSettings.txt b/productPricesSettings.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/globals.c b/src/globals.c new file mode 100644 index 0000000..d524946 --- /dev/null +++ b/src/globals.c @@ -0,0 +1,3 @@ +#include "../include/globals.h" + +char productPricesFileLocation[64]="./productPricesSettings.txt"; \ No newline at end of file diff --git a/src/logic.c b/src/logic.c new file mode 100644 index 0000000..9aac599 --- /dev/null +++ b/src/logic.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include "../include/logic.h" +#include "../include/globals.h" + +FILE *products; + +int exists(const char *fname) { + FILE *file; + if ((file = fopen(fname, "r"))) + { + fclose(file); + return 1; + } + return 0; +} + +void changeFile(){ + +} + +int getCount(){ + + return 0; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..53743a1 --- /dev/null +++ b/src/main.c @@ -0,0 +1,66 @@ +/* +* [Guida NCURSES](https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/intro.html) +* Product Manager +* +* Controllare se file esiste +* Listare prodotti con prezzo da file +* Aggiungere prodotto con prezzo a file +* Listare copie vendute di prodotto da file +* Aggiungere copie venduto di prodotto a file +* Cambiare nome dei file con interagisce +* Calcolare la somma delle entrate dati i file +*/ + +#include +#include +#include +#include + +#include +#include + +int main(){ + initscr(); + if(has_colors() == FALSE) + { endwin(); + warning("Your terminal does not support color"); + exit(1); + } + start_color(); + //initialize options + noecho(); + curs_set(0); + raw(); + mvprintw(LINES -1, COLS / 2 - 25, "Product Manager by Vincenzo Nikodem Cesqui Podiani"); + refresh(); + //initialize colors + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_BLACK, COLOR_BLUE); + init_pair(3, COLOR_BLACK, COLOR_YELLOW); + statusf(""); + + int running = 0; + while(running != 6){ + running = runMainMenu(); + switch(running) { + case 1: + warning("Questa e' una stringa molto lunga . . . Ma anche un test, che ti devo dire, ci proviamo a fare cose che non sono mai state fatte, oltretutto che divertimento ci sarebbe senza esperienze nuove?"); + break; + case 2: + break; + case 3: + break; + case 4: + break; + case 5: + runOptionsMenu(); + break; + case 6: + break; + } + } + + endwin(); + return 0; +} + diff --git a/main.c b/src/windows.c similarity index 59% rename from main.c rename to src/windows.c index 15dff74..a45b815 100644 --- a/main.c +++ b/src/windows.c @@ -1,37 +1,12 @@ -/* -* [Guida NCURSES](https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/intro.html) -* Product Manager -* -* Controllare se file esiste -* Listare prodotti con prezzo da file -* Aggiungere prodotto con prezzo a file -* Listare copie vendute di prodotto da file -* Aggiungere copie venduto di prodotto a file -* Cambiare nome dei file con interagisce -* Calcolare la somma delle entrate dati i file -*/ - #include #include #include #include -// Prototipi -int runMainMenu(); -void warning(char *string); -WINDOW *create_newwin(int height, int width, int starty, int startx); -void destroy_win(WINDOW *local_win); -void print_menu(WINDOW *menu_win, int y, int x, int highlight); -void word_wrap_print(WINDOW *win, int start_y, int start_x, int max_width, const char *str); -int getCount(); -void changeFile(); -int checkIfFileExists(); - -// Globali -char productPricesFileLocation[64]="./productPricesSetting.txt"; -char countFileLocation[64]="./count.txt"; -FILE *productPrices; -FILE *count; +#include "../include/windows.h" +#include "../include/logic.h" +#include "../include/globals.h" +#define MAX_LEN 64 char *mainMenu[] = { "Segna vendita", @@ -44,36 +19,6 @@ char *mainMenu[] = { int n_mainMenu = sizeof(mainMenu) / sizeof(char *); - - - -// Funzioni -int main(){ - initscr(); - //initialize options - noecho(); - curs_set(0); - raw(); - refresh(); - - int running = 0; - while(running != 6){ - running = runMainMenu(); - switch(running) { - case 5: - warning("Questa è una stringa molto lunga . . . Ma anche un test, che ti devo dire, ci proviamo a fare cose che non sono mai state fatte, oltretutto che divertimento ci sarebbe senza esperienze nuove?"); - break; - case 6: - break; - } - } - - endwin(); - return 0; -} - - - int runMainMenu(){ WINDOW *mainWin; int startx, starty, width, height; @@ -114,7 +59,7 @@ int runMainMenu(){ choice = highlight; break; default: - mvprintw(LINES - 2, 1, "Il tasto '%c' non e' un tasto valido . . .", c); + statusf("Il tasto '%c' non e' un tasto valido", c); refresh(); break; } @@ -122,7 +67,7 @@ int runMainMenu(){ if(choice != 0) /* User did a choice come out of the infinite loop */ break; } - mvprintw( LINES - 2 , 1, "Avvio opzione '%s' . . . ", mainMenu[choice - 1]); + statusf("Avvio opzione '%s'", mainMenu[choice - 1]); clrtoeol(); refresh(); destroy_win(mainWin); @@ -130,24 +75,109 @@ int runMainMenu(){ return choice; } +void runOptionsMenu(){ + WINDOW *optionsWin; + int startx, starty, width, height; + height = LINES / 2; + width = COLS / 2; + starty = 0; + startx = COLS / 2; + optionsWin = create_newwin(height, width, starty, startx); + + char input[MAX_LEN + 1]; + strcpy(input, productPricesFileLocation); + int ch, pos = strlen(input); + + mvwprintw(optionsWin, 1, 2, "Posizione file prodotti:"); + wattron(optionsWin, COLOR_PAIR(2)); + for (int i = 0; i < MAX_LEN; i++) { + mvwaddch(optionsWin, 2, 2 + i, ' '); + } + mvwprintw(optionsWin, 2, 2, "%s", input); + wmove(optionsWin, 2, 2 + strlen(input)); + wrefresh(optionsWin); + + pos = strlen(input); + + // Input nella finestra + curs_set(1); + while ((ch = wgetch(optionsWin)) != '\n') { + if (ch == KEY_BACKSPACE || ch == 127 || ch == '\b') { + if (pos > 0) { + pos--; + input[pos] = '\0'; + mvwaddch(optionsWin, 2, 2 + pos, ' '); + wmove(optionsWin, 2, 2 + pos); + } + } else if (pos < MAX_LEN && ch >= 32 && ch <= 126) { + input[pos++] = ch; + input[pos] = '\0'; + mvwaddch(optionsWin, 2, 2 + pos - 1, ch); + } + wrefresh(optionsWin); + } + curs_set(0); + wattroff(optionsWin, COLOR_PAIR(2)); + + if (exists(input)) { + statusf("Il file '%s' esiste e verra' utilizzato al posto di '%s'", input, productPricesFileLocation); + strcpy(productPricesFileLocation, input); + } + else { + warningf("Il file '%s' non esiste, utilizzo il vecchio file '%s'", input, productPricesFileLocation); + } + + werase(optionsWin); + destroy_win(optionsWin); +} + +void warningf(const char *fmt, ...) { + char buf[256]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + warning(buf); +} + void warning(char *string){ WINDOW *warningWin; int startx, starty, width, height; - height = LINES / 2 - 2; + height = LINES / 2 - 3; width = COLS / 2; starty = LINES / 2; startx = 0; warningWin = create_newwin(height, width, starty, startx); + wbkgd(warningWin, COLOR_PAIR(1)); - mvwprintw(warningWin, 2, 2, "ATTENZIONE! E' avvenuto un errore . . . "); + mvwprintw(warningWin, 2, 2, "ATTENZIONE!!! E' avvenuto un errore! "); word_wrap_print(warningWin, 4, 3, width - 5, string); + mvwprintw(warningWin, height - 1, width /2, " Premi un tasto per continuare . . . "); wrefresh(warningWin); getch(); + werase(warningWin); destroy_win(warningWin); } +void statusf(const char *fmt, ...) { + char buf[256]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + status(buf); +} + +void status(char *msg) { + attron(COLOR_PAIR(3)); + mvprintw(LINES -2, 0, " %-*s", COLS -1, msg); + attroff(COLOR_PAIR(3)); + refresh(); +} + + WINDOW *create_newwin(int height, int width, int starty, int startx) { WINDOW *local_win; @@ -160,7 +190,7 @@ WINDOW *create_newwin(int height, int width, int starty, int startx) void destroy_win(WINDOW *local_win) { - wborder(local_win, ':', ':', '.', '.', '+', '+', '+', '+'); + wborder(local_win, ':', ':', '.', '.', ' ', ' ', ' ', ' '); wrefresh(local_win); delwin(local_win); } @@ -240,17 +270,3 @@ void word_wrap_print(WINDOW *win, int start_y, int start_x, int max_width, const mvwprintw(win, y, x, "%s", word_start); } -int checkIfFileExists(){ - - return 0; -} - -void changeFile(){ - -} - -int getCount(){ - - return 0; -} -