voltage

rk1

Mar 18th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. #include <assert.h>
  2. #include <alloca.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <getopt.h>
  8.  
  9. #define BUFFER_LENGTH 250
  10.  
  11. typedef struct _data_t
  12. {
  13.   int hours_wasted, coffee_drunk_ml, fastfood_eaten;
  14.   float completion_coeff;
  15.   char comment[BUFFER_LENGTH];
  16. }  data_t;
  17.  
  18. typedef union _mudata_t
  19. {
  20.   int i;
  21.   float f;
  22.   char s[BUFFER_LENGTH];
  23. } mudata_t;
  24.  
  25. void print_data(const data_t* data)
  26. {
  27.   printf(
  28.     "{\n %s: total wasted %d hours to complete %.3f percent of that crap\n consumed %d ml of coffee and %d kg of nasty food \n}\n",
  29.     data->comment,
  30.     data->hours_wasted,
  31.     data->completion_coeff,
  32.     data->coffee_drunk_ml,
  33.     data->fastfood_eaten
  34.   );
  35. }
  36.  
  37. void ha_modify_data(data_t* data, const char* format, const mudata_t* mudata)
  38. {
  39.   size_t i, j, n;
  40.  
  41.   j = 0;
  42.   n = strlen(format);
  43.  
  44.   for(i = 0; i < n; i++)
  45.   {
  46.     switch(format[i])
  47.     {
  48.       case 'h':
  49.         data->hours_wasted = mudata[j++].i;
  50.         break;
  51.       case 'c':
  52.         data->completion_coeff = mudata[j++].f;
  53.         break;
  54.       case 'm':
  55.         strcpy(data->comment, mudata[j++].s);
  56.         break;
  57.       case 'd':
  58.         data->coffee_drunk_ml = mudata[j++].i;
  59.         break;
  60.       case 'f':
  61.         data->fastfood_eaten = mudata[j++].i;
  62.         break;
  63.       default:
  64.         break;
  65.     }
  66.   }
  67.  
  68. }
  69.  
  70. void va_modify_data(data_t* data, const char* format, ...)
  71. {
  72.   va_list args;
  73.   size_t i, n;
  74.  
  75.   n = strlen(format);
  76.  
  77.   va_start(args, format);
  78.  
  79.   for(i = 0; i < n; i++)
  80.   {
  81.     switch(format[i])
  82.     {
  83.       case 'h':
  84.         data->hours_wasted = va_arg(args, int);
  85.         break;
  86.       case 'c':
  87.         data->completion_coeff = (float)va_arg(args, double);
  88.         break;
  89.       case 'm':
  90.         strcpy(data->comment, va_arg(args, const char*));
  91.         break;
  92.       case 'd':
  93.         data->coffee_drunk_ml = va_arg(args, int);
  94.         break;
  95.       case 'f':
  96.         data->fastfood_eaten = va_arg(args, int);
  97.         break;
  98.       default:
  99.         break;
  100.     }
  101.   }
  102.  
  103.   va_end(args);
  104. }
  105.  
  106. int main(int argc, char** argv)
  107. {
  108.   int opt;
  109.   data_t course_proj;
  110.   mudata_t* mvalues = NULL;
  111.  
  112.   printf(":: initial values ::\n");
  113.   course_proj.hours_wasted = 1;
  114.   course_proj.completion_coeff = 0.001f;
  115.   course_proj.coffee_drunk_ml = 300;
  116.   course_proj.fastfood_eaten = 0;
  117.   strcpy(course_proj.comment, "gaming is evil");
  118.   print_data(&course_proj);
  119.  
  120.   printf(":: heterogenous argument array modification ::\n");
  121.   mvalues = alloca(2 * sizeof(mudata_t));
  122.   assert(mvalues);
  123.   mvalues[0].i = 4;
  124.   mvalues[1].f = 0.002f;
  125.   ha_modify_data(&course_proj, "hc", mvalues);
  126.   print_data(&course_proj);
  127.  
  128.   printf(":: variadic function modification ::\n");
  129.   va_modify_data(&course_proj, "dmf", 2100, "I hate electrotechnics", 1);
  130.   print_data(&course_proj);
  131.  
  132.   printf(":: command line argument modification ::\n");
  133.   while((opt = getopt(argc, argv, "h:c:m:d:f:")) != -1)
  134.   {
  135.     switch(opt)
  136.     {
  137.       case 'h':
  138.         course_proj.hours_wasted = atoi(optarg);
  139.         break;
  140.       case 'c':
  141.         course_proj.completion_coeff = atof(optarg);
  142.         break;
  143.       case 'm':
  144.         strcpy(course_proj.comment, optarg);
  145.         break;
  146.       case 'd':
  147.         course_proj.coffee_drunk_ml = atoi(optarg);
  148.         break;
  149.       case 'f':
  150.         course_proj.fastfood_eaten = atoi(optarg);
  151.         break;
  152.       case '?':
  153.       default:
  154.         printf("Usage: %s [-h hrs] [-m comment] [-c coeff] [-d ml] [-f kg].\n", argv[0]);
  155.         return -1;
  156.         break;
  157.     }
  158.   }
  159.   print_data(&course_proj);
  160.  
  161.   return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment