Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <assert.h>
- #include <stdlib.h>
- int readline(char **buffer);
- int printtostore(void* value, char* inputstring, char *type, int inputcount);
- int main(void)
- {
- char *buffer = NULL;
- char aisle;
- int shelf, weight, price, qty;
- char *name = NULL;
- int c;
- while ((c = readline(&buffer)) != 0)
- {
- int i = printtostore(&name, buffer, "String", 0);
- i = printtostore(&aisle, buffer, "char", i);
- i = printtostore(&shelf, buffer, "int", i);
- i = printtostore(&weight, buffer, "int", i);
- i = printtostore(&price, buffer, "int", i);
- i = printtostore(&qty, buffer, "int", i);
- free(buffer);
- printf("%s\t%c\t%d\t%d\t%d\t%d\n", name, aisle, shelf, weight, price, qty);
- }
- }
- int readline(char **buffer)
- {
- int c, i, size = 10;
- *buffer = malloc(size);
- assert(*buffer);
- for (i = 0; (c = getchar()) != EOF && c != '\n'; i++)
- {
- if (i + 1 == size)
- {
- size *= 2;
- *buffer = realloc(*buffer, size);
- assert(*buffer);
- }
- (*buffer)[i] = (char)c;
- }
- (*buffer)[i] = '\0';
- return i - 1;
- }
- int printtostore(void* value, char* inputstring, char *type, int inputcount)
- {
- int buffercount = 0, bufferlength = 0, *intvalue = NULL;
- char *buffer, *charbuffer;
- buffer = NULL;
- if (strcmp(type, "int") == 0)
- {
- while (inputstring[inputcount] != ',')
- {
- if (inputstring[inputcount] == '\0')
- {
- break;
- }
- if (isdigit(inputstring[inputcount]) == 0)
- {
- printf("ERROR: invalid input!\n");
- free(buffer);
- exit(EXIT_FAILURE);
- }
- if (buffercount + 1 >= bufferlength)
- {
- bufferlength = (bufferlength + 1) * 10;
- buffer = realloc(buffer, bufferlength);
- assert(buffer);
- }
- buffer[buffercount] = inputstring[inputcount];
- buffer[buffercount + 1] = '\0';
- buffercount++;
- inputcount++;
- }
- intvalue = (int*)value;
- *intvalue = atoi(buffer);
- inputcount++;
- }
- else if ((strcmp(type, "String") == 0) || (strcmp(type, "char") == 0))
- {
- printf("stinrg: %s\n", inputstring);
- while (inputstring[inputcount] != ',')
- {
- if (isalpha(inputstring[inputcount]) == 0)
- {
- printf("ERROR: invalid stockname!\n");
- free(buffer);
- exit(EXIT_FAILURE);
- }
- if (buffercount + 1 >= bufferlength)
- {
- bufferlength = (bufferlength + 1) * 10;
- buffer = realloc(buffer, bufferlength);
- assert(buffer);
- }
- buffer[buffercount] = inputstring[inputcount];
- buffercount++;
- inputcount++;
- }
- buffer[buffercount] = '\0';
- buffercount++;
- printf("stinrg: %s\n", buffer);
- if (strcmp(type, "String") == 0)
- {
- char **stringbuffer = value;
- *stringbuffer = realloc(*stringbuffer, (strlen(buffer) + 1));
- assert(*stringbuffer);
- strcpy(*stringbuffer, buffer);
- printf("stinrg: %s\n", *stringbuffer);
- }
- else
- {
- if (strlen(buffer)>1)
- {
- printf("ERROR: invalid input!\n");
- exit(EXIT_FAILURE);
- }
- charbuffer = (char*)value;
- *charbuffer = buffer[0];
- }
- inputcount++;
- }
- free(buffer);
- return inputcount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement