*/
void PopulateItemList(int Socket)
{
- char buffer[BUFSIZ];
- int len;
+ char *buf;
int responseCode;
char *itemType, *itemStart;
// Ask server for stock list
send(Socket, "ENUM_ITEMS\n", 11, 0);
- len = recv(Socket, buffer, BUFSIZ-1, 0);
- buffer[len] = '\0';
-
- trim(buffer);
+ buf = ReadLine(Socket);
- //printf("Output: %s\n", buffer);
+ //printf("Output: %s\n", buf);
- responseCode = atoi(buffer);
+ responseCode = atoi(buf);
if( responseCode != 201 ) {
fprintf(stderr, "Unknown response from dispense server (Response Code %i)\n", responseCode);
exit(-1);
// - Get item list -
// Expected format: 201 Items <count> <item1> <item2> ...
- RunRegex(&gArrayRegex, buffer, 4, matches, "Malformed server response");
+ RunRegex(&gArrayRegex, buf, 4, matches, "Malformed server response");
- itemType = &buffer[ matches[2].rm_so ]; buffer[ matches[2].rm_eo ] = '\0';
- count = atoi( &buffer[ matches[3].rm_so ] );
+ itemType = &buf[ matches[2].rm_so ]; buf[ matches[2].rm_eo ] = '\0';
+ count = atoi( &buf[ matches[3].rm_so ] );
// Check array type
if( strcmp(itemType, "Items") != 0 ) {
exit(-1);
}
- itemStart = &buffer[ matches[3].rm_eo ];
+ itemStart = &buf[ matches[3].rm_eo ];
gaItems = malloc( count * sizeof(tItem) );
itemStart = next;
}
+ free(buf);
+
// Fetch item information
for( i = 0; i < giNumItems; i ++ )
{
// Get item info
sendf(Socket, "ITEM_INFO %s\n", gaItems[i].Ident);
- len = recv(Socket, buffer, BUFSIZ-1, 0);
- buffer[len] = '\0';
- trim(buffer);
+ buf = ReadLine(Socket);
- responseCode = atoi(buffer);
+ responseCode = atoi(buf);
if( responseCode != 202 ) {
fprintf(stderr, "Unknown response from dispense server (Response Code %i)\n", responseCode);
exit(-1);
}
- RunRegex(&gItemRegex, buffer, 6, matches, "Malformed server response");
+ RunRegex(&gItemRegex, buf, 6, matches, "Malformed server response");
+
+ buf[ matches[3].rm_eo ] = '\0';
- buffer[ matches[3].rm_eo ] = '\0';
+ gaItems[i].Price = atoi( buf + matches[4].rm_so );
+ gaItems[i].Desc = strdup( buf + matches[5].rm_so );
- gaItems[i].Price = atoi( buffer + matches[4].rm_so );
- gaItems[i].Desc = strdup( buffer + matches[5].rm_so );
+ free(buf);
}
}