Skip to content

Commit

Permalink
update base.c
Browse files Browse the repository at this point in the history
  • Loading branch information
4ngelf committed Mar 13, 2024
1 parent 47abb33 commit 0703b06
Showing 1 changed file with 31 additions and 51 deletions.
82 changes: 31 additions & 51 deletions base.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,30 @@

#define VERSION "v0.1.0"

This comment has been minimized.

Copy link
@4ngelf

4ngelf Mar 13, 2024

Author Owner

Test of writing comment on commit.

#define STREQ(STR1, STR2) strcmp(STR1, STR2) == 0

// used by print_help and print_version
static char* argv0;
static const char* argv0;

Check warning on line 34 in base.c

View workflow job for this annotation

GitHub Actions / C lint

base.c:34:20 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'argv0' is non-const and globally accessible, consider making it const

// parsed arguments to operate
struct ParsedArgs {
char from;
char to;
size_t count;
const char **numbers_raw;
char from;
char to;
int count;
const char **numbers_raw;
};

/* prints help on given stream */
void print_help(FILE *stream) {
fprintf(stream, "%s - Base number conversion utility\n\n", argv0);
fprintf(stream, "Usage: %s [OPTIONS] NUMBER...\n\n", argv0);
const char *options = ""
"Options:\n"
" -f, --from N convert from N base (default: 10)\n"
" -t, -to N convert to N base (default: 16)\n"
" -h, --help show this help page\n"
" -v, --version prints version\n"
"";

fprintf(stream, options);
fprintf(stream, "%s - Base number conversion utility\n\n", argv0);
fprintf(stream, "Usage: %s [OPTIONS] NUMBER...\n\n", argv0);
const char *options = ""
"Options:\n"
" -f, --from N convert from N base (default: 10)\n"
" -t, -to N convert to N base (default: 16)\n"
" -h, --help show this help page\n"
" -v, --version prints version\n"
"";

fprintf(stream, options);

Check warning on line 56 in base.c

View workflow job for this annotation

GitHub Actions / C lint

base.c:56:18 [clang-diagnostic-format-security]

format string is not a string literal (potentially insecure)
}

/* prints version on given stream */
Expand All @@ -78,33 +76,6 @@ int parseuintl(char *string, int len){
return result;
}

/* Compare two strings up to 'len' characters.
*
* returns 0 on equal.
* returns non zero on non equal */
int strcmpl(const char *str1, const char *str2, int len){
int cmp = 0;
for (int i = 0; i < len; i++){
cmp = str1[i] - str2[i];
}
return cmp;
}

/* Take argv and returns index as option.
* returns -1 if not an option.
* */
int get_option(size_t *index, int *value, const char *argv[], int size_args){
// If invalid option use this:
// print_help(stderr, argv[0]);
// exit(EXIT_FAILURE);
if (argv[*index][0] != '-' || !argv[*index][1]){
return -1;
}



(*index)++;
}

/* Process list argc and argv and return a struct of ParsedArgs
*
Expand Down Expand Up @@ -143,7 +114,7 @@ struct ParsedArgs parse_args(size_t argc, const char *argv[]){
// ---------------------------------------
// Checks
// ---------------------------------------
if (strcmpl("--", argv[index], 3) == 0){
if (strncmp("--", argv[index], 3) == 0){

Check warning on line 117 in base.c

View workflow job for this annotation

GitHub Actions / C lint

base.c:117:7 [clang-diagnostic-implicit-function-declaration]

implicitly declaring library function 'strncmp' with type 'int (const char *, const char *, unsigned long)'
index++;
break;
}
Expand All @@ -156,12 +127,11 @@ struct ParsedArgs parse_args(size_t argc, const char *argv[]){
// ---------------------------------------
// Determine which option to operate
// ---------------------------------------
int is_long = strcmpl("--", argv[index], 2) == 0;

int is_long = strncmp("--", argv[index], 2) == 0;
for (int j = 0; j < (sizeof(options) / sizeof(options[0])); j++){
char *subject = is_long ? options[j].long_name : options[j].name;
int subject_len = strlen(subject);

Check warning on line 133 in base.c

View workflow job for this annotation

GitHub Actions / C lint

base.c:133:22 [clang-diagnostic-implicit-function-declaration]

implicitly declaring library function 'strlen' with type 'unsigned long (const char *)'
if (strcmpl(subject, argv[index], subject_len) == 0){
if (strncmp(subject, argv[index], subject_len) == 0){
selected_option = j;
} else {
print_help(stderr);
Expand Down Expand Up @@ -221,7 +191,12 @@ struct ParsedArgs parse_args(size_t argc, const char *argv[]){
return parsed;
}

int main(int argc, const char *argv[]) {
/* Take the parameters processed from command arguments and print results on stdout */
int base_arg(int from, int to, const char *number_raw){

}

int main(size_t argc, const char *argv[]) {

Check failure on line 199 in base.c

View workflow job for this annotation

GitHub Actions / C lint

base.c:199:5 [clang-diagnostic-error]

first parameter of 'main' (argument count) must be of type 'int'
argv0 = argv[0];
struct ParsedArgs parsed = parse_args(argc, argv);
printf(
Expand All @@ -235,5 +210,10 @@ int main(int argc, const char *argv[]) {
}
puts("");

return 0;

for (int i = 0; i < parsed.count; i++){
base_arg(parsed.from, parsed.to, parsed.numbers_raw[i]);
}

return EXIT_SUCCESS;
}

0 comments on commit 0703b06

Please sign in to comment.