Advertisement
Guest User

Untitled

a guest
May 24th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <stdlib.h>
  6. int readline(char **buffer);
  7. int printtostore(void* value, char* inputstring, char *type, int inputcount);
  8. int main(void)
  9. {
  10.     char *buffer = NULL;
  11.     char aisle;
  12.     int shelf, weight, price, qty;
  13.     char *name = NULL;
  14.     int c;
  15.     while ((c = readline(&buffer)) != 0)
  16.     {
  17.         int i = printtostore(&name, buffer, "String", 0);
  18.         i = printtostore(&aisle, buffer, "char", i);
  19.         i = printtostore(&shelf, buffer, "int", i);
  20.         i = printtostore(&weight, buffer, "int", i);
  21.         i = printtostore(&price, buffer, "int", i);
  22.         i = printtostore(&qty, buffer, "int", i);
  23.         free(buffer);
  24.         printf("%s\t%c\t%d\t%d\t%d\t%d\n", name, aisle, shelf, weight, price, qty);
  25.     }
  26. }
  27. int readline(char **buffer)
  28. {
  29.     int c, i, size = 10;
  30.     *buffer = malloc(size);
  31.     assert(*buffer);
  32.     for (i = 0; (c = getchar()) != EOF && c != '\n'; i++)
  33.     {
  34.         if (i + 1 == size)
  35.         {
  36.             size *= 2;
  37.             *buffer = realloc(*buffer, size);
  38.             assert(*buffer);
  39.         }
  40.         (*buffer)[i] = (char)c;
  41.     }
  42.     (*buffer)[i] = '\0';
  43.     return i - 1;
  44. }
  45. int printtostore(void* value, char* inputstring, char *type, int inputcount)
  46. {
  47.     int buffercount = 0, bufferlength = 0, *intvalue = NULL;
  48.     char *buffer, *charbuffer;
  49.     buffer = NULL;
  50.     if (strcmp(type, "int") == 0)
  51.     {
  52.         while (inputstring[inputcount] != ',')
  53.         {
  54.             if (inputstring[inputcount] == '\0')
  55.             {
  56.                 break;
  57.             }
  58.             if (isdigit(inputstring[inputcount]) == 0)
  59.             {
  60.                 printf("ERROR: invalid input!\n");
  61.                 free(buffer);
  62.                 exit(EXIT_FAILURE);
  63.             }
  64.             if (buffercount + 1 >= bufferlength)
  65.             {
  66.                 bufferlength = (bufferlength + 1) * 10;
  67.                 buffer = realloc(buffer, bufferlength);
  68.                 assert(buffer);
  69.             }
  70.             buffer[buffercount] = inputstring[inputcount];
  71.             buffer[buffercount + 1] = '\0';
  72.             buffercount++;
  73.             inputcount++;
  74.         }
  75.         intvalue = (int*)value;
  76.         *intvalue = atoi(buffer);
  77.         inputcount++;
  78.     }
  79.     else if ((strcmp(type, "String") == 0) || (strcmp(type, "char") == 0))
  80.     {
  81.         printf("stinrg: %s\n", inputstring);
  82.         while (inputstring[inputcount] != ',')
  83.         {
  84.             if (isalpha(inputstring[inputcount]) == 0)
  85.             {
  86.                 printf("ERROR: invalid stockname!\n");
  87.                 free(buffer);
  88.                 exit(EXIT_FAILURE);
  89.             }
  90.             if (buffercount + 1 >= bufferlength)
  91.             {
  92.                 bufferlength = (bufferlength + 1) * 10;
  93.                 buffer = realloc(buffer, bufferlength);
  94.                 assert(buffer);
  95.             }
  96.             buffer[buffercount] = inputstring[inputcount];
  97.             buffercount++;
  98.             inputcount++;
  99.         }
  100.         buffer[buffercount] = '\0';
  101.         buffercount++;
  102.         printf("stinrg: %s\n", buffer);
  103.         if (strcmp(type, "String") == 0)
  104.         {
  105.             char **stringbuffer = value;
  106.             *stringbuffer = realloc(*stringbuffer, (strlen(buffer) + 1));
  107.             assert(*stringbuffer);
  108.             strcpy(*stringbuffer, buffer);
  109.             printf("stinrg: %s\n", *stringbuffer);
  110.         }
  111.         else
  112.         {
  113.             if (strlen(buffer)>1)
  114.             {
  115.                 printf("ERROR: invalid input!\n");
  116.                 exit(EXIT_FAILURE);
  117.             }
  118.             charbuffer = (char*)value;
  119.             *charbuffer = buffer[0];
  120.         }
  121.         inputcount++;
  122.     }
  123.     free(buffer);
  124.     return inputcount;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement