Files
productManager/main.c
T
2025-10-31 14:34:27 +01:00

257 lines
5.5 KiB
C

/*
* [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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
// 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;
char *mainMenu[] = {
"Segna vendita",
"Vedi vendite",
"Vedi prodotti",
"Gestisci prodotti",
"Opzioni",
"Exit"
};
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;
height = LINES / 2;
width = COLS / 2;
starty = 0;
startx = 0;
mainWin = create_newwin(height, width, starty, startx);
int highlight = 1;
int choice = 0;
int c;
keypad(mainWin, TRUE);
mvwprintw(mainWin, 2, 2, "Usa le FRECCETTE ed INVIO per scegliere un'opzione!");
mvwprintw(mainWin, 3, 2, "In alternativa ci sono J e K con SPAZIO!");
refresh();
print_menu(mainWin, 6, width / 2 - 10, highlight);
while(1)
{ c = wgetch(mainWin);
switch(c)
{ case KEY_UP:
case 'k':
if(highlight == 1)
highlight = n_mainMenu;
else
--highlight;
break;
case KEY_DOWN:
case 'j':
if(highlight == n_mainMenu)
highlight = 1;
else
++highlight;
break;
case 10:
case 32:
choice = highlight;
break;
default:
mvprintw(LINES - 2, 1, "Il tasto '%c' non e' un tasto valido . . .", c);
refresh();
break;
}
print_menu(mainWin, 6, width / 2 - 10, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */
break;
}
mvprintw( LINES - 2 , 1, "Avvio opzione '%s' . . . ", mainMenu[choice - 1]);
clrtoeol();
refresh();
destroy_win(mainWin);
return choice;
}
void warning(char *string){
WINDOW *warningWin;
int startx, starty, width, height;
height = LINES / 2 - 2;
width = COLS / 2;
starty = LINES / 2;
startx = 0;
warningWin = create_newwin(height, width, starty, startx);
mvwprintw(warningWin, 2, 2, "ATTENZIONE! E' avvenuto un errore . . . ");
word_wrap_print(warningWin, 4, 3, width - 5, string);
wrefresh(warningWin);
getch();
destroy_win(warningWin);
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{ WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0);
wrefresh(local_win);
return local_win;
}
void destroy_win(WINDOW *local_win)
{
wborder(local_win, ':', ':', '.', '.', '+', '+', '+', '+');
wrefresh(local_win);
delwin(local_win);
}
void print_menu(WINDOW *menu_win, int y, int x, int highlight)
{
int i;
box(menu_win, 0, 0);
for(i = 0; i < n_mainMenu; ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", mainMenu[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", mainMenu[i]);
++y;
}
wrefresh(menu_win);
}
void word_wrap_print(WINDOW *win, int start_y, int start_x, int max_width, const char *str) {
int y = start_y;
int x = start_x;
int line_length = 0;
int word_length = 0;
const char *word_start = str;
while (*str) {
// Trova la prossima parola
if (*str != ' ' && *str != '\n' && *str != '\t') {
str++;
continue;
}
// Calcola lunghezza parola corrente
word_length = str - word_start;
// Gestione wrap
if (line_length + word_length > max_width) {
y++;
x = start_x;
line_length = 0;
}
// Stampa parola
for (int i = 0; i < word_length; i++) {
mvwaddch(win, y, x++, word_start[i]);
}
line_length += word_length;
// Gestione spazi
if (*str == ' ' || *str == '\t') {
if (line_length < max_width) {
mvwaddch(win, y, x++, ' ');
line_length++;
} else {
y++;
x = start_x;
line_length = 0;
}
} else if (*str == '\n') {
y++;
x = start_x;
line_length = 0;
}
word_start = ++str;
}
// Stampa ultima parola
word_length = strlen(word_start);
if (line_length + word_length > max_width) {
y++;
x = start_x;
}
mvwprintw(win, y, x, "%s", word_start);
}
int checkIfFileExists(){
return 0;
}
void changeFile(){
}
int getCount(){
return 0;
}