Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1.  
  2. int inputString(char *buffer)
  3. {
  4.   char *input_string;
  5.   unsigned int max_length = 10, size = 10;
  6.   int ch = EOF;
  7.   int position = 0;
  8.   int length;
  9.  
  10.   input_string = malloc (sizeof(char)*BUFFER);
  11.  
  12.   if (input_string != NULL)
  13.   {
  14.     while((ch = fgetc(stdin)) != '\n' && ch != EOF && !feof(stdin))
  15.     {
  16.       input_string[position++] = ch;
  17.  
  18.       if (position == size)
  19.       {
  20.         size = position + max_length;
  21.         void *temporary = (char*)realloc(input_string, size);
  22.         if (temporary == NULL)
  23.         {
  24.           return ERROR_OUT_OF_MEMORY;
  25.         }
  26.         input_string = temporary;
  27.       }
  28.     }
  29.     input_string[position] = '\0';
  30.   }
  31.   else
  32.   {
  33.     return ERROR_OUT_OF_MEMORY;
  34.   }
  35.  
  36.   length = strlen(input_string);
  37.  
  38.   printf("\nMessage: %s\nLength: %d\n\n", input_string, length);
  39.  
  40.   if (strlen(input_string) > 106);
  41.   {
  42.     return ERROR_TEXT_TOO_LONG;
  43.   }
  44.  
  45.   *buffer = realloc(*buffer, size);
  46.   if (*buffer == NULL)
  47.   {
  48.     return ERROR_OUT_OF_MEMORY;
  49.   }
  50.  
  51.   strcpy(*buffer, input_string);
  52.  
  53.   free(input_string);
  54.  
  55.  
  56.   return 0;
  57. }
  58.  
  59. int main(int argc, char const *argv[])
  60. {
  61.   char *input = malloc(1);
  62.   struct parameters qr_info;
  63.   int error = 0;
  64.  
  65.   if(argc > 3)
  66.   {
  67.      handleError(1);
  68.      free(input);
  69.      return ERROR_TOO_MANY_ARGUMENTS;
  70.   }
  71.  
  72.   printf("--- QR-Code Encoder ---\n\nPlease enter a text:\n");
  73.  
  74.   error = inputString(input);
  75.   if (error != 0)
  76.   {
  77.     handleError(error);
  78.     free(input);
  79.     return error;
  80.   }
  81.  
  82.  
  83.   free (input);
  84.   return 0;
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement