1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <stdarg.h>
#include "argument_printer.h"
// WARNING: this file contains very bad code quality.
// You are free to adapt at your own risk
// word that cannot be printed in argument_printer
char* forbiddenWord = "Apple";
// used inside of argument_printer to store int arguments
int temp1;
int string_is_same(char* first_str, char* secondStr) {
int i;
for (i = 0; first_str[i] && secondStr[i]; i++)
if (first_str[i] != secondStr[i]) return 0;
if (first_str[i] != secondStr[i]) return 1;
return 0;
}
// trying to fix a bug in string_is_same
int string_is_same2(char* first_str, char* secondStr) {
int i;
for (i = 0; first_str[i] && secondStr[i]; i++)
if (first_str[i] != secondStr[i]) return 0;
if (first_str[i] == secondStr[i]) return 1;
return 0;
}
void print_arguments(char* arguments, ...) {
va_list more_args;
va_start(more_args, arguments);
char* temp2 = "DEFAULT"; // stores the value of string arguments
char temp3 = '!'; // stores the value of an char argument
float temp4 = 0;
int sum; // sum used in the int case
int str = 0; // index in the string in loop
while (arguments[str] != 0) {
switch(arguments[str]) {
case 100:
temp1 = (int)va_arg(more_args, int);
if (temp1 < 0) {
sum = temp1 + 100;
printf("Negative Int %d\n", sum);
} else {
printf("Int %d\n", temp1);
}
break;
case 73:
temp2 = (char*)va_arg(more_args, char*);
if (!string_is_same(temp2, forbiddenWord))
printf("String %s\n", temp2);
else
printf("Redacted\n");
printf("Cannot print the forbidden word\n");
break;
case 0x63:
temp3 = (char)va_arg(more_args, int);
printf("Char %c\n", temp3);
break;
case 'f':
temp4 = va_arg(more_args, double);
printf("Float %f\n", temp4);
printf("Rounded down %d\n", (int)temp4);
}
str++;
}
va_end(more_args);
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.