Files
max668_calc/main.c

117 lines
3.2 KiB
C

/*main.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib/max_functions.h"
//global variables
static int state=0;
int main(int argc, char *argv[])
{
if (argc == 1)
printf("max668_calc: Usage: max668_calc set_output_voltage -E [E series] -V [voltage]\n");
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0) {
printf("max668_calc: Usage: max668_calc set_output_voltage -E [E series] -V [voltage]\n");
return EXIT_SUCCESS;
}
}
int command = 0;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "set_output_voltage") == 0) {
command = 1;
}
else if ((strchr(argv[i], '-') == NULL) && (strchr(argv[i-1], '-') == NULL)) {
printf("max668_calc: Usage: max668_calc set_output_voltage -E [E series] -V [voltage]\n");
return EXIT_SUCCESS;
}
}
double voltage = 0.0;
ESeries e_series;
char input[10];
int read_number;
int temp_result;
switch (command) {
case 1:
for (int i = 0; i < argc; i++) {
if (argv[i][0] == '-') {
if (strcmp(argv[i], "-V") == 0) {
if(strlen(argv[i+1]) > 9) {
printf("max668_calc: Malformed voltage format.");
return EXIT_FAILURE;
}
strncpy(argv[i+1], input, strlen(argv[i+1]));
input[strlen(argv[i+1])+1] = '\0';
if (input[strlen(input)] == 'V') {
input[strlen(input)] = '\0';
}
temp_result = sscanf(input, "%lf", &voltage);
if (temp_result == 0) {
printf("max668_calc: Malformed voltage format.");
return EXIT_FAILURE;
}
}
else if (strcmp(argv[i], "-E") == 0) {
if(strlen(argv[i+1]) > 9) {
printf("max668_calc: Malformed E series format.");
return EXIT_FAILURE;
}
strncpy(argv[i+1], input, strlen(argv[i+1]));
input[strlen(argv[i+1])+1] = '\0';
if (input[0] == 'E') {
strcpy(input, &input[1]);
}
temp_result = sscanf(input, "%i", &read_number);
if (temp_result == 0) {
printf("max668_calc: Malformed E-Series format.");
return EXIT_FAILURE;
}
e_series = get_E_Series(read_number);
}
else {
printf("max668_calc: Unknown option. Use max668_calc -h\n");
return EXIT_FAILURE;
}
}
}
set_output_voltage(voltage, e_series);
break;
default:
printf("max668_calc: Command not found. Use max668_calc -h\n");
break;
}
return EXIT_SUCCESS;
}