[v0.98][Refactoring] Use array of colors instead of fixed structure of colors in args

This commit is contained in:
Dr-Noob
2021-08-07 10:00:00 +02:00
parent a03f296390
commit 2e08b10652
4 changed files with 48 additions and 69 deletions

View File

@@ -5,6 +5,8 @@
#include "args.h" #include "args.h"
#include "global.h" #include "global.h"
#define NUM_COLORS 4
#define COLOR_STR_INTEL "intel" #define COLOR_STR_INTEL "intel"
#define COLOR_STR_AMD "amd" #define COLOR_STR_AMD "amd"
#define COLOR_STR_IBM "ibm" #define COLOR_STR_IBM "ibm"
@@ -25,7 +27,7 @@ struct args_struct {
bool verbose_flag; bool verbose_flag;
bool version_flag; bool version_flag;
STYLE style; STYLE style;
struct colors* colors; struct color** colors;
}; };
const char args_chr[] = { const char args_chr[] = {
@@ -54,7 +56,7 @@ STYLE get_style() {
return args.style; return args.style;
} }
struct colors* get_colors() { struct color** get_colors() {
return args.colors; return args.colors;
} }
@@ -100,24 +102,19 @@ STYLE parse_style(char* style) {
return i; return i;
} }
void free_colors_struct(struct colors* cs) { void free_colors_struct(struct color** cs) {
free(cs->c1); for(int i=0; i < NUM_COLORS; i++) {
free(cs->c2); free(cs[i]);
free(cs->c3); }
free(cs->c4);
free(cs); free(cs);
} }
bool parse_color(char* optarg_str, struct colors** cs) { bool parse_color(char* optarg_str, struct color*** cs) {
*cs = emalloc(sizeof(struct colors)); for(int i=0; i < NUM_COLORS; i++) {
(*cs)->c1 = emalloc(sizeof(struct color)); (*cs)[i] = emalloc(sizeof(struct color));
(*cs)->c2 = emalloc(sizeof(struct color)); }
(*cs)->c3 = emalloc(sizeof(struct color));
(*cs)->c4 = emalloc(sizeof(struct color)); struct color** c = *cs;
struct color** c1 = &((*cs)->c1);
struct color** c2 = &((*cs)->c2);
struct color** c3 = &((*cs)->c3);
struct color** c4 = &((*cs)->c4);
int32_t ret; int32_t ret;
char* str_to_parse = NULL; char* str_to_parse = NULL;
bool free_ptr; bool free_ptr;
@@ -148,40 +145,29 @@ bool parse_color(char* optarg_str, struct colors** cs) {
} }
ret = sscanf(str_to_parse, "%d,%d,%d:%d,%d,%d:%d,%d,%d:%d,%d,%d", ret = sscanf(str_to_parse, "%d,%d,%d:%d,%d,%d:%d,%d,%d:%d,%d,%d",
&(*c1)->R, &(*c1)->G, &(*c1)->B, &c[0]->R, &c[0]->G, &c[0]->B,
&(*c2)->R, &(*c2)->G, &(*c2)->B, &c[1]->R, &c[1]->G, &c[1]->B,
&(*c3)->R, &(*c3)->G, &(*c3)->B, &c[2]->R, &c[2]->G, &c[2]->B,
&(*c4)->R, &(*c4)->G, &(*c4)->B); &c[3]->R, &c[3]->G, &c[3]->B);
if(ret != 12) { if(ret != 12) {
printErr("Expected to read 12 values for color but read %d", ret); printErr("Expected to read 12 values for color but read %d", ret);
return false; return false;
} }
//TODO: Refactor c1->R c2->R ... to c[i]->R for(int i=0; i < NUM_COLORS; i++) {
if((*c1)->R < 0 || (*c1)->R > 255) { if(c[i]->R < 0 || c[i]->R > 255) {
printErr("Red in color 1 is invalid. Must be in range (0, 255)"); printErr("Red in color %d is invalid: %d; must be in range (0, 255)", i+1, c[i]->R);
return false; return false;
} }
if((*c1)->G < 0 || (*c1)->G > 255) { if(c[i]->G < 0 || c[i]->G > 255) {
printErr("Green in color 1 is invalid. Must be in range (0, 255)"); printErr("Green in color %d is invalid: %d; must be in range (0, 255)", i+1, c[i]->G);
return false; return false;
} }
if((*c1)->B < 0 || (*c1)->B > 255) { if(c[i]->B < 0 || c[i]->B > 255) {
printErr("Blue in color 1 is invalid. Must be in range (0, 255)"); printErr("Blue in color %d is invalid: %d; must be in range (0, 255)", i+1, c[i]->B);
return false; return false;
} }
if((*c2)->R < 0 || (*c2)->R > 255) {
printErr("Red in color 2 is invalid. Must be in range (0, 255)");
return false;
}
if((*c2)->G < 0 || (*c2)->G > 255) {
printErr("Green in color 2 is invalid. Must be in range (0, 255)");
return false;
}
if((*c2)->B < 0 || (*c2)->B > 255) {
printErr("Blue in color 2 is invalid. Must be in range (0, 255)");
return false;
} }
if(free_ptr) free (str_to_parse); if(free_ptr) free (str_to_parse);
@@ -244,8 +230,8 @@ bool parse_args(int argc, char* argv[]) {
return false; return false;
} }
color_flag = true; color_flag = true;
args.colors = emalloc(sizeof(struct color *) * NUM_COLORS);
if(!parse_color(optarg, &args.colors)) { if(!parse_color(optarg, &args.colors)) {
printErr("Color parsing failed");
return false; return false;
} }
} }

View File

@@ -10,13 +10,6 @@ struct color {
int32_t B; int32_t B;
}; };
struct colors {
struct color* c1;
struct color* c2;
struct color* c3;
struct color* c4;
};
enum { enum {
STYLE_EMPTY, STYLE_EMPTY,
STYLE_FANCY, STYLE_FANCY,
@@ -48,8 +41,8 @@ bool show_raw();
bool show_debug(); bool show_debug();
bool show_version(); bool show_version();
bool verbose_enabled(); bool verbose_enabled();
void free_colors_struct(struct colors* cs); void free_colors_struct(struct color** cs);
struct colors* get_colors(); struct color** get_colors();
STYLE get_style(); STYLE get_style();
#endif #endif

View File

@@ -150,7 +150,7 @@ char* rgb_to_ansi(struct color* c, bool background, bool bold) {
return str; return str;
} }
struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) { struct ascii* set_ascii(VENDOR vendor, STYLE style, struct color** cs) {
char *COL_FANCY_1, *COL_FANCY_2, *COL_FANCY_3, *COL_FANCY_4, *COL_RETRO_1, *COL_RETRO_2, *COL_RETRO_3, *COL_RETRO_4; char *COL_FANCY_1, *COL_FANCY_2, *COL_FANCY_3, *COL_FANCY_4, *COL_RETRO_1, *COL_RETRO_2, *COL_RETRO_3, *COL_RETRO_4;
struct ascii* art = emalloc(sizeof(struct ascii)); struct ascii* art = emalloc(sizeof(struct ascii));
art->n_attributes_set = 0; art->n_attributes_set = 0;
@@ -278,10 +278,10 @@ struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) {
break; break;
case STYLE_FANCY: case STYLE_FANCY:
if(cs != NULL) { if(cs != NULL) {
COL_FANCY_1 = rgb_to_ansi(cs->c1, true, true); COL_FANCY_1 = rgb_to_ansi(cs[0], true, true);
COL_FANCY_2 = rgb_to_ansi(cs->c2, true, true); COL_FANCY_2 = rgb_to_ansi(cs[1], true, true);
COL_FANCY_3 = rgb_to_ansi(cs->c3, false, true); COL_FANCY_3 = rgb_to_ansi(cs[2], false, true);
COL_FANCY_4 = rgb_to_ansi(cs->c4, false, true); COL_FANCY_4 = rgb_to_ansi(cs[3], false, true);
} }
art->ascii_chars[0] = ' '; art->ascii_chars[0] = ' ';
art->ascii_chars[1] = ' '; art->ascii_chars[1] = ' ';
@@ -298,10 +298,10 @@ struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) {
break; break;
case STYLE_RETRO: case STYLE_RETRO:
if(cs != NULL) { if(cs != NULL) {
COL_RETRO_1 = rgb_to_ansi(cs->c1, false, true); COL_RETRO_1 = rgb_to_ansi(cs[0], false, true);
COL_RETRO_2 = rgb_to_ansi(cs->c2, false, true); COL_RETRO_2 = rgb_to_ansi(cs[1], false, true);
COL_RETRO_3 = rgb_to_ansi(cs->c3, false, true); COL_RETRO_3 = rgb_to_ansi(cs[2], false, true);
COL_RETRO_4 = rgb_to_ansi(cs->c4, false, true); COL_RETRO_4 = rgb_to_ansi(cs[3], false, true);
} }
strcpy(art->color1_ascii,COL_RETRO_1); strcpy(art->color1_ascii,COL_RETRO_1);
strcpy(art->color2_ascii,COL_RETRO_2); strcpy(art->color2_ascii,COL_RETRO_2);
@@ -441,7 +441,7 @@ void print_ascii(struct ascii* art) {
} }
bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs) { bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct color** cs) {
struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs); struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs);
if(art == NULL) if(art == NULL)
return false; return false;
@@ -562,7 +562,7 @@ void print_ascii(struct ascii* art) {
print_ascii_ppc(art, longest_attribute); print_ascii_ppc(art, longest_attribute);
} }
bool print_cpufetch_ppc(struct cpuInfo* cpu, STYLE s, struct colors* cs) { bool print_cpufetch_ppc(struct cpuInfo* cpu, STYLE s, struct color** cs) {
struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs); struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs);
if(art == NULL) if(art == NULL)
return false; return false;
@@ -727,7 +727,7 @@ void print_ascii(struct ascii* art) {
} }
bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) { bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct color** cs) {
struct ascii* art = set_ascii(get_soc_vendor(cpu->soc), s, cs); struct ascii* art = set_ascii(get_soc_vendor(cpu->soc), s, cs);
if(art == NULL) if(art == NULL)
return false; return false;
@@ -812,7 +812,7 @@ bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
} }
#endif #endif
bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct colors* cs) { bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct color** cs) {
// Sanity check of ASCII arts // Sanity check of ASCII arts
int len = sizeof(ASCII_ARRAY) / sizeof(ASCII_ARRAY[0]); int len = sizeof(ASCII_ARRAY) / sizeof(ASCII_ARRAY[0]);
for(int i=0; i < len; i++) { for(int i=0; i < len; i++) {

View File

@@ -22,6 +22,6 @@ typedef int STYLE;
void print_levels(struct cpuInfo* cpu); void print_levels(struct cpuInfo* cpu);
#endif #endif
bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct colors* cs); bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct color** cs);
#endif #endif