[v0.01] Initialize repository reusing stuff from cpufetch

This commit is contained in:
Dr-Noob
2021-08-10 16:54:25 +02:00
commit 5e6c3cb965
10 changed files with 387 additions and 0 deletions

99
src/common/args.c Normal file
View File

@@ -0,0 +1,99 @@
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "args.h"
#include "global.h"
struct args_struct {
bool help_flag;
bool version_flag;
};
const char args_chr[] = {
/* [ARG_CHAR_HELP] = */ 'h',
/* [ARG_CHAR_VERSION] = */ 'V',
};
const char *args_str[] = {
/* [ARG_CHAR_HELP] = */ "help",
/* [ARG_CHAR_VERSION] = */ "version",
};
static struct args_struct args;
bool show_help() {
return args.help_flag;
}
bool show_version() {
return args.version_flag;
}
int max_arg_str_length() {
int max_len = -1;
int len = sizeof(args_str) / sizeof(args_str[0]);
for(int i=0; i < len; i++) {
max_len = max(max_len, (int) strlen(args_str[i]));
}
return max_len;
}
char* build_short_options() {
const char *c = args_chr;
int len = sizeof(args_chr) / sizeof(args_chr[0]);
char* str = (char *) emalloc(sizeof(char) * (len*2 + 1));
memset(str, 0, sizeof(char) * (len*2 + 1));
sprintf(str, "%c%c",
c[ARG_HELP], c[ARG_VERSION]);
return str;
}
bool parse_args(int argc, char* argv[]) {
int opt;
int option_index = 0;
opterr = 0;
args.version_flag = false;
args.help_flag = false;
const struct option long_options[] = {
{args_str[ARG_HELP], no_argument, 0, args_chr[ARG_HELP] },
{args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] },
{0, 0, 0, 0}
};
char* short_options = build_short_options();
opt = getopt_long(argc, argv, short_options, long_options, &option_index);
while (!args.help_flag && !args.version_flag && opt != -1) {
if(opt == args_chr[ARG_HELP]) {
args.help_flag = true;
}
else if(opt == args_chr[ARG_VERSION]) {
args.version_flag = true;
}
else {
printWarn("Invalid options");
args.help_flag = true;
}
option_index = 0;
opt = getopt_long(argc, argv, short_options, long_options, &option_index);
}
if(optind < argc) {
printWarn("Invalid options");
args.help_flag = true;
}
if((args.help_flag + args.version_flag) > 1) {
printWarn("You should specify just one option");
args.help_flag = true;
}
return true;
}

19
src/common/args.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef __ARGS__
#define __ARGS__
#include <stdbool.h>
enum {
ARG_HELP,
ARG_VERSION
};
extern const char args_chr[];
extern const char *args_str[];
int max_arg_str_length();
bool parse_args(int argc, char* argv[]);
bool show_help();
bool show_version();
#endif

95
src/common/global.c Normal file
View File

@@ -0,0 +1,95 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "global.h"
#ifdef _WIN32
#define RED ""
#define BOLD ""
#define RESET ""
#else
#define RED "\x1b[31;1m"
#define BOLD "\x1b[;1m"
#define RESET "\x1b[0m"
#endif
enum {
LOG_LEVEL_NORMAL,
LOG_LEVEL_VERBOSE
};
int LOG_LEVEL;
void printWarn(const char *fmt, ...) {
if(LOG_LEVEL == LOG_LEVEL_VERBOSE) {
int buffer_size = 4096;
char* buffer = new char[buffer_size];
va_list args;
va_start(args, fmt);
vsnprintf(buffer,buffer_size, fmt, args);
va_end(args);
fprintf(stderr, BOLD "[WARNING]: " RESET "%s\n",buffer);
delete [] buffer;
}
}
void printErr(const char *fmt, ...) {
int buffer_size = 4096;
char* buffer = new char[buffer_size];
va_list args;
va_start(args, fmt);
vsnprintf(buffer,buffer_size, fmt, args);
va_end(args);
fprintf(stderr, RED "[ERROR]: " RESET "%s\n",buffer);
delete [] buffer;
}
void printBug(const char *fmt, ...) {
int buffer_size = 4096;
char* buffer = new char[buffer_size];
va_list args;
va_start(args, fmt);
vsnprintf(buffer,buffer_size, fmt, args);
va_end(args);
fprintf(stderr, RED "[ERROR]: " RESET "%s\n",buffer);
fprintf(stderr,"Please, create a new issue with this error message in https://github.com/Dr-Noob/gpufetch/issues\n");
delete [] buffer;
}
void set_log_level(bool verbose) {
if(verbose) LOG_LEVEL = LOG_LEVEL_VERBOSE;
else LOG_LEVEL = LOG_LEVEL_NORMAL;
}
int max(int a, int b) {
return a > b ? a : b;
}
void* emalloc(size_t size) {
void* ptr = malloc(size);
if(ptr == NULL) {
printErr("malloc failed: %s", strerror(errno));
exit(1);
}
return ptr;
}
void* ecalloc(size_t nmemb, size_t size) {
void* ptr = calloc(nmemb, size);
if(ptr == NULL) {
printErr("calloc failed: %s", strerror(errno));
exit(1);
}
return ptr;
}

17
src/common/global.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef __GLOBAL__
#define __GLOBAL__
#include <stdbool.h>
#include <stddef.h>
#define STRING_UNKNOWN "Unknown"
void set_log_level(bool verbose);
void printWarn(const char *fmt, ...);
void printErr(const char *fmt, ...);
void printBug(const char *fmt, ...);
int max(int a, int b);
void* emalloc(size_t size);
void* ecalloc(size_t nmemb, size_t size);
#endif

51
src/common/main.c Normal file
View File

@@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "args.h"
#include "global.h"
static const char* VERSION = "0.01";
void print_help(char *argv[]) {
const char **t = args_str;
const char *c = args_chr;
int max_len = max_arg_str_length();
printf("Usage: %s [OPTION]...\n", argv[0]);
printf("Simple yet fancy GPU architecture fetching tool\n\n");
printf("Options: \n");
printf(" -%c, --%s %*s Prints this help and exit\n", c[ARG_HELP], t[ARG_HELP], (int) (max_len-strlen(t[ARG_HELP])), "");
printf(" -%c, --%s %*s Prints cpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), "");
printf("\nBUGS: \n");
printf(" Report bugs to https://github.com/Dr-Noob/gpufetch/issues\n");
printf("\nNOTE: \n");
printf(" Peak performance information is NOT accurate. gpufetch computes peak performance using the max\n");
printf(" frequency. However, to properly compute peak performance, you need to know the frequency of the\n");
printf(" GPU running real code.\n");
printf(" For peak performance measurement see: https://github.com/Dr-Noob/peakperf\n");
}
void print_version() {
printf("gpufetch v%s\n", VERSION);
}
int main(int argc, char* argv[]) {
if(!parse_args(argc,argv))
return EXIT_FAILURE;
if(show_help()) {
print_help(argv);
return EXIT_SUCCESS;
}
if(show_version()) {
print_version();
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}