Ristrutturato in più file
This commit is contained in:
1 parent
3607f14f86
commit
7e6ae04dc4
19 files changed
+280
-103
No files matched your search
@@ -0,0 +1,3 @@
|
||||
#include "../include/globals.h"
|
||||
|
||||
char productPricesFileLocation[64]="./productPricesSettings.txt";
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
+66
@@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <logic.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
+272
@@ -0,0 +1,272 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
#include "../include/windows.h"
|
||||
#include "../include/logic.h"
|
||||
#include "../include/globals.h"
|
||||
#define MAX_LEN 64
|
||||
|
||||
char *mainMenu[] = {
|
||||
"Segna vendita",
|
||||
"Vedi vendite",
|
||||
"Vedi prodotti",
|
||||
"Gestisci prodotti",
|
||||
"Opzioni",
|
||||
"Exit"
|
||||
};
|
||||
int n_mainMenu = sizeof(mainMenu) / sizeof(char *);
|
||||
|
||||
|
||||
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:
|
||||
statusf("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;
|
||||
}
|
||||
statusf("Avvio opzione '%s'", mainMenu[choice - 1]);
|
||||
clrtoeol();
|
||||
refresh();
|
||||
destroy_win(mainWin);
|
||||
|
||||
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 - 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! ");
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user