X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fclient%2Fmain.c;h=acef5b5021c2f1f4ff08787da8c74dadbb68f79a;hb=1e3c14b4fe54108b8bebbc000a93f7eb30284c67;hp=e1de523b9f3472790b151d2395766457a51c91cc;hpb=d4239b1fab5e6bc01212bdd34e74d4edd2f72503;p=tpg%2Fopendispense2.git diff --git a/src/client/main.c b/src/client/main.c index e1de523..acef5b5 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -686,7 +686,9 @@ void PopulateItemList(int Socket) // - Get item list - - // Expected format: 201 Items ... + // Expected format: + // 201 Items + // 202 Item RunRegex(&gArrayRegex, buf, 4, matches, "Malformed server response"); itemType = &buf[ matches[2].rm_so ]; buf[ matches[2].rm_eo ] = '\0'; @@ -701,29 +703,21 @@ void PopulateItemList(int Socket) } itemStart = &buf[ matches[3].rm_eo ]; - - gaItems = malloc( count * sizeof(tItem) ); - - for( giNumItems = 0; giNumItems < count && itemStart; giNumItems ++ ) - { - char *next = strchr( ++itemStart, ' ' ); - if( next ) *next = '\0'; - gaItems[giNumItems].Ident = strdup(itemStart); - itemStart = next; - } free(buf); + giNumItems = count; + gaItems = malloc( giNumItems * sizeof(tItem) ); + // Fetch item information for( i = 0; i < giNumItems; i ++ ) { regmatch_t matches[6]; // Get item info - sendf(Socket, "ITEM_INFO %s\n", gaItems[i].Ident); buf = ReadLine(Socket); - responseCode = atoi(buf); + if( responseCode != 202 ) { fprintf(stderr, "Unknown response from dispense server (Response Code %i)\n", responseCode); exit(-1); @@ -733,11 +727,25 @@ void PopulateItemList(int Socket) buf[ matches[3].rm_eo ] = '\0'; + gaItems[i].Ident = strdup( buf + matches[3].rm_so ); gaItems[i].Price = atoi( buf + matches[4].rm_so ); gaItems[i].Desc = strdup( buf + matches[5].rm_so ); free(buf); } + + // Read end of list + buf = ReadLine(Socket); + responseCode = atoi(buf); + + if( responseCode != 200 ) { + fprintf(stderr, "Unknown response from dispense server %i\n'%s'", + responseCode, buf + ); + exit(-1); + } + + free(buf); } /**