Un outil pour réarranger les lignes de stdin - retour accueil
git clone git://bebou.netlib.re/reorder
Log | Files | Refs | README |
reorder.c (4737B)
1 #define TB_IMPL 2 #include "termbox2.h" 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <stdbool.h> 6 7 #define LIST_SIZE 1000 8 /* Le chiffre c'est des octets. Les caractères sont encodés sur un octet chacun ? 9 * Donc si on met 5 la taille max serait 4 caractères ? */ 10 #define CARD_NAME_SIZE 20 11 12 struct pile 13 { 14 int cardCount; 15 char **cards; 16 int curline; 17 }; 18 19 int max(int a, int b) { return a > b ? a : b; } 20 21 void addcardtopile(char* cardname, struct pile* p) { 22 p->cards[p->cardCount] = malloc(CARD_NAME_SIZE); 23 strcpy(p->cards[p->cardCount], cardname); 24 p->cardCount++; 25 p->curline=p->cardCount-1; 26 } 27 28 void removecardofpile(int cardposition, struct pile* p) { 29 p->cardCount--; 30 for(int i=cardposition;i<p->cardCount;i++) { 31 p->cards[i]=p->cards[i+1]; 32 } 33 if (p->curline>0) { p->curline--; } 34 } 35 36 void moveselection(struct pile* p, char* direction, bool selection) { 37 int step; 38 if (direction == "up") { step = -1; } 39 else { step = 1; } 40 if (p->curline+step >= 0 && p->curline+step < p->cardCount) { 41 if (selection) { 42 char* tmp; 43 tmp=p->cards[p->curline]; 44 p->cards[p->curline]=p->cards[p->curline+step]; 45 p->cards[p->curline+step]=tmp; 46 } 47 p->curline=p->curline+step; 48 } 49 } 50 51 void updatecmd(char* cmd, const struct pile p) { 52 if (p.cardCount == 0 ) { return; } 53 strcat(cmd,"( cat ./"); 54 strcat(cmd,p.cards[0]); 55 for (int i=1;i<p.cardCount;i++) { 56 strcat(cmd," | \n./"); 57 strcat(cmd,p.cards[i]); 58 } 59 strcat(cmd," ) 2> /dev/null"); 60 } 61 62 void updateres(char *res, const char *cmd, FILE *fp) { 63 fp = popen(cmd,"r"); 64 char line[1000]; 65 while(fgets(line, sizeof(line), fp)) { 66 strcat(res, line); 67 } 68 } 69 70 struct pile file2pile(FILE *fp) { 71 struct pile pi; 72 struct pile *p=π 73 p->cardCount=0; 74 p->curline=0; 75 p->cards = malloc(LIST_SIZE); 76 char line[CARD_NAME_SIZE]; 77 while (fgets(line, sizeof(line), fp)) { 78 /* Retire le retour à la ligne à la fin de line */ 79 line[strcspn(line, "\n")] = '\0'; 80 addcardtopile(line, p); 81 } 82 return pi; 83 } 84 85 86 void display(struct pile* h, struct pile* p, int handcurline, int playedcurline, char* curmenu,bool selection, char* cmd, char* res) { 87 tb_clear(); 88 int i; int j; int w; 89 int biggestpile=max(p->cardCount,h->cardCount); 90 for (i=0;i<h->cardCount;i++) { 91 if (i==handcurline && curmenu=="hand") { 92 if (selection) { 93 tb_printf(0, i, TB_BLACK, TB_BLUE, h->cards[i]); 94 } else { 95 tb_printf(0, i, TB_BLACK, TB_WHITE, h->cards[i]); 96 } 97 } 98 else { tb_printf(0, i, TB_DEFAULT, TB_BLACK, h->cards[i]); } 99 } 100 for (int w=0;w<biggestpile;w++) { 101 tb_printf(CARD_NAME_SIZE,w,TB_DEFAULT, TB_BLACK, "|"); 102 } 103 for (j=0;j<p->cardCount;j++) { 104 if (j==playedcurline && curmenu=="played") { 105 if (selection) { 106 tb_printf(CARD_NAME_SIZE+2, j, TB_BLACK, TB_BLUE, p->cards[j]); 107 } else { 108 tb_printf(CARD_NAME_SIZE+2, j, TB_BLACK, TB_WHITE, p->cards[j]); 109 } 110 } 111 else { tb_printf(CARD_NAME_SIZE+2, j, TB_DEFAULT, TB_BLACK, p->cards[j]); } 112 } 113 tb_printf(0, biggestpile+1, TB_DEFAULT, TB_BLACK, cmd); 114 tb_printf(0, biggestpile+10, TB_DEFAULT, TB_BLACK, res); 115 tb_present(); 116 } 117 118 int main(int argc, char **argv) { 119 struct tb_event ev; 120 121 FILE *fp = fopen("deck", "r"); 122 char res[2000]; 123 FILE *fp2; 124 125 char cmd[2000]; 126 cmd[0]='\0'; 127 128 struct pile hand = file2pile(fp); 129 struct pile *h = &hand; 130 struct pile played; 131 struct pile *p=&played; 132 played.cards=malloc(LIST_SIZE); 133 played.cardCount=0; 134 played.curline=0; 135 hand.curline=0; 136 137 tb_init(); 138 bool selection=false; 139 bool quit=false; 140 char* curmenu="hand"; 141 142 while(!quit) { 143 cmd[0]='\0';res[0]='\0'; 144 updatecmd(cmd, played); 145 updateres(res, cmd, fp2); 146 display(h,p,hand.curline,played.curline,curmenu,selection,cmd,res); 147 tb_poll_event(&ev); 148 switch(ev.key) { 149 /* up */ 150 case 65517: 151 if (curmenu=="hand") { 152 moveselection(h,"up",selection); 153 } else if (curmenu=="played") { 154 moveselection(p,"up",selection); 155 } 156 break; 157 /* down */ 158 case 65516: 159 if (curmenu=="hand") { 160 moveselection(h,"down",selection); 161 } else if (curmenu=="played") { 162 moveselection(p,"down",selection); 163 } 164 break; 165 /* <- */ 166 case 65515: 167 if (curmenu=="played") { 168 if (selection) { 169 addcardtopile(played.cards[played.curline],h); 170 removecardofpile(played.curline,p); 171 selection=!selection; 172 } 173 curmenu="hand"; 174 } 175 break; 176 /* -> */ 177 case 65514: 178 if (curmenu=="hand") { 179 if (selection) { 180 addcardtopile(hand.cards[hand.curline],p); 181 removecardofpile(hand.curline,h); 182 } 183 curmenu="played"; 184 } 185 break; 186 case 13: 187 selection=!selection; 188 break; 189 case 0: 190 switch(ev.ch) { 191 case 113: 192 quit=true; 193 break; 194 default: 195 break; 196 } 197 default: 198 break; 199 } 200 } 201 202 tb_shutdown(); 203 204 return 0; 205 }