Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <alloca.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <string.h>
- #include <getopt.h>
- #define BUFFER_LENGTH 250
- typedef struct _data_t
- {
- int hours_wasted, coffee_drunk_ml, fastfood_eaten;
- float completion_coeff;
- char comment[BUFFER_LENGTH];
- } data_t;
- typedef union _mudata_t
- {
- int i;
- float f;
- char s[BUFFER_LENGTH];
- } mudata_t;
- void print_data(const data_t* data)
- {
- printf(
- "{\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",
- data->comment,
- data->hours_wasted,
- data->completion_coeff,
- data->coffee_drunk_ml,
- data->fastfood_eaten
- );
- }
- void ha_modify_data(data_t* data, const char* format, const mudata_t* mudata)
- {
- size_t i, j, n;
- j = 0;
- n = strlen(format);
- for(i = 0; i < n; i++)
- {
- switch(format[i])
- {
- case 'h':
- data->hours_wasted = mudata[j++].i;
- break;
- case 'c':
- data->completion_coeff = mudata[j++].f;
- break;
- case 'm':
- strcpy(data->comment, mudata[j++].s);
- break;
- case 'd':
- data->coffee_drunk_ml = mudata[j++].i;
- break;
- case 'f':
- data->fastfood_eaten = mudata[j++].i;
- break;
- default:
- break;
- }
- }
- }
- void va_modify_data(data_t* data, const char* format, ...)
- {
- va_list args;
- size_t i, n;
- n = strlen(format);
- va_start(args, format);
- for(i = 0; i < n; i++)
- {
- switch(format[i])
- {
- case 'h':
- data->hours_wasted = va_arg(args, int);
- break;
- case 'c':
- data->completion_coeff = (float)va_arg(args, double);
- break;
- case 'm':
- strcpy(data->comment, va_arg(args, const char*));
- break;
- case 'd':
- data->coffee_drunk_ml = va_arg(args, int);
- break;
- case 'f':
- data->fastfood_eaten = va_arg(args, int);
- break;
- default:
- break;
- }
- }
- va_end(args);
- }
- int main(int argc, char** argv)
- {
- int opt;
- data_t course_proj;
- mudata_t* mvalues = NULL;
- printf(":: initial values ::\n");
- course_proj.hours_wasted = 1;
- course_proj.completion_coeff = 0.001f;
- course_proj.coffee_drunk_ml = 300;
- course_proj.fastfood_eaten = 0;
- strcpy(course_proj.comment, "gaming is evil");
- print_data(&course_proj);
- printf(":: heterogenous argument array modification ::\n");
- mvalues = alloca(2 * sizeof(mudata_t));
- assert(mvalues);
- mvalues[0].i = 4;
- mvalues[1].f = 0.002f;
- ha_modify_data(&course_proj, "hc", mvalues);
- print_data(&course_proj);
- printf(":: variadic function modification ::\n");
- va_modify_data(&course_proj, "dmf", 2100, "I hate electrotechnics", 1);
- print_data(&course_proj);
- printf(":: command line argument modification ::\n");
- while((opt = getopt(argc, argv, "h:c:m:d:f:")) != -1)
- {
- switch(opt)
- {
- case 'h':
- course_proj.hours_wasted = atoi(optarg);
- break;
- case 'c':
- course_proj.completion_coeff = atof(optarg);
- break;
- case 'm':
- strcpy(course_proj.comment, optarg);
- break;
- case 'd':
- course_proj.coffee_drunk_ml = atoi(optarg);
- break;
- case 'f':
- course_proj.fastfood_eaten = atoi(optarg);
- break;
- case '?':
- default:
- printf("Usage: %s [-h hrs] [-m comment] [-c coeff] [-d ml] [-f kg].\n", argv[0]);
- return -1;
- break;
- }
- }
- print_data(&course_proj);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment