reorder

Un outil pour réarranger les lignes de stdin - retour accueil

git clone git://bebou.netlib.re/reorder
Log | Files | Refs | README |

commit 599bd4af5adda1304bc3b51741191fe96ca244ce
parent 7cdd78073c2957c9d03e535620699043bd41d1fa
Auteurice: Arthur Pons <arthur.pons@unistra.fr>
Date:   Fri, 17 Jan 2025 12:04:55 +0100

Factorisation avec fonctions et struct

Wow presque on pourrait croire que je sais ce que je fais.
Bug connu : La pile played ne fonctionne plus. A son tour !

Diffstat:
Mreorder.c | 114++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 67 insertions(+), 47 deletions(-)

diff --git a/reorder.c b/reorder.c @@ -4,7 +4,34 @@ #include <stdlib.h> #include <stdbool.h> +#define LIST_SIZE 1000 +/* Le chiffre c'est des octets. Les caractères sont encodés sur un octet chacun ? + * Donc si on met 5 la taille max serait 4 caractères ? */ +#define CARD_NAME_SIZE 20 +struct pile +{ + int cardCount; + char **cards; +}; + +void addcardtopile(char* cardname, struct pile* p) { + p->cards[p->cardCount] = malloc(CARD_NAME_SIZE); + strcpy(p->cards[p->cardCount], cardname); + p->cardCount++; +} + +struct pile* file2pile(FILE *fp) { + struct pile pi; + struct pile *p=&pi; + p->cardCount=0; + p->cards = malloc(LIST_SIZE); + char line[CARD_NAME_SIZE]; + while (fgets(line, sizeof(line), fp)) { + addcardtopile(line, p); + } + return p; +} int main(int argc, char **argv) { struct tb_event ev; @@ -12,17 +39,10 @@ int main(int argc, char **argv) { FILE *fp = fopen("deck", "r"); - size_t listSize = 1000; - int handLineCount = 0; - int playedLineCount = 0; - char **hand = malloc(listSize); - char **played = malloc(listSize); - char line[256]; - while (fgets(line, sizeof(line), fp)) { - hand[handLineCount] = malloc(256); - strcpy(hand[handLineCount], line); - handLineCount++; - } + struct pile* h = file2pile(fp); + struct pile hand = *h; + struct pile played; + played.cardCount = 0; tb_init(); bool selection=false; @@ -32,26 +52,26 @@ int main(int argc, char **argv) { while(quit) { tb_clear(); int i; int j; - for (i=0;i<handLineCount;i++) { + for (i=0;i<hand.cardCount;i++) { if (i==curline && curmenu=="hand") { if (selection) { - tb_printf(0, i, TB_BLACK, TB_BLUE, hand[i]); + tb_printf(0, i, TB_BLACK, TB_BLUE, hand.cards[i]); } else { - tb_printf(0, i, TB_BLACK, TB_WHITE, hand[i]); + tb_printf(0, i, TB_BLACK, TB_WHITE, hand.cards[i]); } } - else { tb_printf(0, i, TB_DEFAULT, TB_BLACK, hand[i]); } + else { tb_printf(0, i, TB_DEFAULT, TB_BLACK, hand.cards[i]); } } tb_printf(0, i, TB_DEFAULT, TB_BLACK, "------"); - for (j=0;j<playedLineCount;j++) { + for (j=0;j<played.cardCount;j++) { if (j==curline && curmenu=="played") { if (selection) { - tb_printf(0, i+j+1, TB_BLACK, TB_BLUE, played[j]); + tb_printf(0, i+j+1, TB_BLACK, TB_BLUE, played.cards[j]); } else { - tb_printf(0, i+j+1, TB_BLACK, TB_WHITE, played[j]); + tb_printf(0, i+j+1, TB_BLACK, TB_WHITE, played.cards[j]); } } - else { tb_printf(0, i+j+1, TB_DEFAULT, TB_BLACK, played[j]); } + else { tb_printf(0, i+j+1, TB_DEFAULT, TB_BLACK, played.cards[j]); } } tb_present(); tb_poll_event(&ev); @@ -62,9 +82,9 @@ int main(int argc, char **argv) { if (curline>0) { if (selection) { char* tmp; - tmp=hand[curline]; - hand[curline]=hand[curline-1]; - hand[curline-1]=tmp; + tmp=hand.cards[curline]; + hand.cards[curline]=hand.cards[curline-1]; + hand.cards[curline-1]=tmp; } curline--; } @@ -72,9 +92,9 @@ int main(int argc, char **argv) { if (curline>0) { if (selection) { char* tmp; - tmp=played[curline]; - played[curline]=played[curline-1]; - played[curline-1]=tmp; + tmp=played.cards[curline]; + played.cards[curline]=played.cards[curline-1]; + played.cards[curline-1]=tmp; } curline--; } @@ -83,22 +103,22 @@ int main(int argc, char **argv) { /* down */ case 65516: if (curmenu=="hand") { - if (curline<handLineCount-1) { + if (curline<hand.cardCount-1) { if (selection) { char* tmp; - tmp=hand[curline]; - hand[curline]=hand[curline+1]; - hand[curline+1]=tmp; + tmp=hand.cards[curline]; + hand.cards[curline]=hand.cards[curline+1]; + hand.cards[curline+1]=tmp; } curline++; } } else if (curmenu=="played") { - if (curline<playedLineCount-1) { + if (curline<played.cardCount-1) { if (selection) { char* tmp; - tmp=played[curline]; - played[curline]=played[curline+1]; - played[curline+1]=tmp; + tmp=played.cards[curline]; + played.cards[curline]=played.cards[curline+1]; + played.cards[curline+1]=tmp; } curline++; } @@ -108,32 +128,32 @@ int main(int argc, char **argv) { case 65515: if (curmenu=="played") { if (selection) { - hand[handLineCount] = malloc(256); - strcpy(hand[handLineCount], played[curline]); - handLineCount++; - playedLineCount--; - for(int w=curline;w<playedLineCount;w++) { - played[w]=played[w+1]; + hand.cards[hand.cardCount] = malloc(256); + strcpy(hand.cards[hand.cardCount], played.cards[curline]); + hand.cardCount++; + played.cardCount--; + for(int w=curline;w<played.cardCount;w++) { + played.cards[w]=played.cards[w+1]; } } curmenu="hand"; - curline=handLineCount-1; + curline=hand.cardCount-1; } break; /* -> */ case 65514: if (curmenu=="hand") { if (selection) { - played[playedLineCount] = malloc(256); - strcpy(played[playedLineCount], hand[curline]); - playedLineCount++; - handLineCount--; - for(int w=curline;w<handLineCount;w++) { - hand[w]=hand[w+1]; + played.cards[played.cardCount] = malloc(256); + strcpy(played.cards[played.cardCount], hand.cards[curline]); + played.cardCount++; + hand.cardCount--; + for(int w=curline;w<hand.cardCount;w++) { + hand.cards[w]=hand.cards[w+1]; } } curmenu="played"; - curline=playedLineCount-1; + curline=played.cardCount-1; } break; case 13: