1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| #include <stdio.h> #include <stdlib.h> #include <unistd.h>
struct record { void (*print)(struct record *); void (*free)(struct record *); union { int integer; char *string; }; };
struct record *records[16];
int ask(const char * q) { char buff[32]; printf("%s > ", q); fgets(buff, sizeof(buff), stdin); return atoi(buff); }
void rec_int_print(struct record *rec) { printf("Record(Type=Integer, Value=%d)\n", rec->integer); }
void rec_str_print(struct record *rec) { printf("Record(Type=String, Value=%s)\n", rec->string); }
void rec_int_free(struct record *rec) { free(rec); puts("Record freed!"); }
void rec_str_free(struct record *rec) { free(rec->string); free(rec); puts("Record freed!"); }
void do_new() { int idx = ask("Index");
if(idx < 0 || idx > 16) { puts("Out of index!"); return; } if(records[idx]) { printf("Index #%d is used!\n", idx); return; }
struct record *r = records[idx] = (struct record *)malloc(sizeof(struct record)); r->print = rec_int_print; r->free = rec_int_free;
puts("Blob type:"); puts("1. Integer"); puts("2. Text"); int type = ask("Type"); unsigned int len;
switch(type) { case 1: r->integer = ask("Value"); break; case 2: len = ask("Length"); if(len > 1024) { puts("Length too long, please buy record service premium to store longer record!"); return; } r->string = malloc(len); printf("Value > "); fgets(r->string, len, stdin); r->print = rec_str_print; r->free = rec_str_free; break; default: puts("Invalid type!"); return; }
puts("Okey, we got your data. Here is it:"); r->print(r); }
void do_del() { int idx = ask("Index"); records[idx]->free(records[idx]); }
void do_dump() { int idx = ask("Index"); records[idx]->print(records[idx]); }
int main() { alarm(600); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdin, NULL, _IONBF, 0);
puts("Welcome to use my Record-as-a-Service (free plan)"); puts("You can only save Integer or String for 600 seconds"); puts("Pay 1,000,000,000,000,000,000,000,000 bitcoins to buy premium plan");
puts("Here is term of service. You must agree to use this service. Please read carefully!"); puts("================================================================================"); system("cat tos.txt | head -n 30 | sed -e 's/^/ /'"); puts("================================================================================");
while(1) { puts("1. New record"); puts("2. Del record"); puts("3. Show record");
switch(ask("Act")) { case 1: do_new(); break; case 2: do_del(); break; case 3: do_dump(); break; default: puts("Bye~ Thanks for using our service!"); return 0; } } }
|