Skip to content

Commit c6e6ba5

Browse files
committed
[refactor] improve code formatting and consistency in argument parsing and logging modules
1 parent 6fefd82 commit c6e6ba5

File tree

4 files changed

+87
-96
lines changed

4 files changed

+87
-96
lines changed

include/argument_parser.h

+12-13
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@
1717
typedef enum { TYPE_STRING, TYPE_INT, TYPE_LONG, TYPE_FLOAT } option_type_s;
1818

1919
typedef struct {
20-
const char *flag;
21-
const char *description;
22-
option_type_s type;
23-
const char *default_value;
24-
union {
25-
char *string_value;
26-
int int_value;
27-
long long_value;
28-
float float_value;
29-
} value;
20+
const char* flag;
21+
const char* description;
22+
option_type_s type;
23+
const char* default_value;
24+
union {
25+
char* string_value;
26+
int int_value;
27+
long long_value;
28+
float float_value;
29+
} value;
3030
} option_t;
3131

32-
int parse_arguments(int argc, char *argv[], option_t *options,
33-
int option_count);
32+
int parse_arguments(int argc, char* argv[], option_t* options, int option_count);
3433

35-
#endif // ARG_PARSER_H
34+
#endif // ARG_PARSER_H

include/logger.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212
#ifndef LOGGER_H
1313
#define LOGGER_H
1414

15+
#include <stdarg.h>
1516
#include <stdio.h>
1617
#include <stdlib.h>
1718
#include <time.h>
18-
#include <stdarg.h>
1919
#include <unistd.h>
2020

21-
typedef enum {
22-
LOG_LEVEL_ERROR,
23-
LOG_LEVEL_INFO
24-
} LogLevel;
21+
typedef enum { LOG_LEVEL_ERROR, LOG_LEVEL_INFO } LogLevel;
2522

26-
void log_message(LogLevel level, const char *format, ...);
23+
void log_message(LogLevel level, const char* format, ...);
2724

2825
#define LOGE(format, ...) log_message(LOG_LEVEL_ERROR, format, ##__VA_ARGS__)
2926
#define LOGI(format, ...) log_message(LOG_LEVEL_INFO, format, ##__VA_ARGS__)

shared/argument_parser.c

+57-62
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,66 @@
1515
#include <stdlib.h>
1616
#include <string.h>
1717

18-
int parse_arguments(int argc, char *argv[], option_t *options,
19-
int option_count) {
20-
LOGI("Parsing arguments... count: %d opt count %d", argc, option_count);
18+
int parse_arguments(int argc, char* argv[], option_t* options, int option_count) {
19+
LOGI("Parsing arguments... count: %d opt count %d", argc, option_count);
2120

22-
for (int i = 0; i < option_count; i++) {
23-
if (options[i].default_value) {
24-
switch (options[i].type) {
25-
case TYPE_STRING:
26-
options[i].value.string_value =
27-
strdup(options[i].default_value);
28-
break;
29-
case TYPE_INT:
30-
options[i].value.int_value = atoi(options[i].default_value);
31-
break;
32-
case TYPE_LONG:
33-
options[i].value.long_value =
34-
atol(options[i].default_value);
35-
break;
36-
case TYPE_FLOAT:
37-
options[i].value.float_value =
38-
atof(options[i].default_value);
39-
break;
40-
}
41-
}
42-
}
21+
for (int i = 0; i < option_count; i++) {
22+
if (options[i].default_value) {
23+
switch (options[i].type) {
24+
case TYPE_STRING:
25+
options[i].value.string_value = strdup(options[i].default_value);
26+
break;
27+
case TYPE_INT:
28+
options[i].value.int_value = atoi(options[i].default_value);
29+
break;
30+
case TYPE_LONG:
31+
options[i].value.long_value = atol(options[i].default_value);
32+
break;
33+
case TYPE_FLOAT:
34+
options[i].value.float_value = atof(options[i].default_value);
35+
break;
36+
}
37+
}
38+
}
4339

44-
for (int i = 1; i < argc; i++) {
45-
if (strcmp(argv[i], "-h") == 0) {
46-
return 1;
47-
}
40+
for (int i = 1; i < argc; i++) {
41+
if (strcmp(argv[i], "-h") == 0) {
42+
return 1;
43+
}
4844

49-
int found = 0;
50-
for (int j = 0; j < option_count; j++) {
51-
if (strcmp(argv[i], options[j].flag) == 0) {
52-
found = 1;
53-
if (i + 1 < argc) {
54-
i++;
55-
switch (options[j].type) {
56-
case TYPE_STRING:
57-
options[j].value.string_value = strdup(argv[i]);
58-
break;
59-
case TYPE_INT:
60-
options[j].value.int_value = atoi(argv[i]);
61-
break;
62-
case TYPE_LONG:
63-
options[j].value.long_value = atol(argv[i]);
64-
break;
65-
case TYPE_FLOAT:
66-
options[j].value.float_value = atof(argv[i]);
67-
break;
68-
}
69-
} else {
70-
LOGE("Error: Missing value for option '%s'",
71-
options[j].flag);
72-
return -1;
73-
}
74-
break;
75-
}
76-
}
45+
int found = 0;
46+
for (int j = 0; j < option_count; j++) {
47+
if (strcmp(argv[i], options[j].flag) == 0) {
48+
found = 1;
49+
if (i + 1 < argc) {
50+
i++;
51+
switch (options[j].type) {
52+
case TYPE_STRING:
53+
options[j].value.string_value = strdup(argv[i]);
54+
break;
55+
case TYPE_INT:
56+
options[j].value.int_value = atoi(argv[i]);
57+
break;
58+
case TYPE_LONG:
59+
options[j].value.long_value = atol(argv[i]);
60+
break;
61+
case TYPE_FLOAT:
62+
options[j].value.float_value = atof(argv[i]);
63+
break;
64+
}
65+
} else {
66+
LOGE("Error: Missing value for option '%s'", options[j].flag);
67+
return -1;
68+
}
69+
break;
70+
}
71+
}
7772

78-
if (!found) {
79-
fprintf(stderr, "Error: Unknown option '%s'\n", argv[i]);
80-
continue;
81-
}
82-
}
73+
if (!found) {
74+
LOGE("Error: Unknown option '%s'\n", argv[i]);
75+
continue;
76+
}
77+
}
8378

84-
return 0;
79+
return 0;
8580
}

shared/logger.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@
1111

1212
#include "logger.h"
1313

14-
static char *get_timestamp() {
15-
static char timestamp[64];
16-
time_t now = time(NULL);
17-
struct tm *tm_info = localtime(&now);
18-
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", tm_info);
19-
return timestamp;
14+
static char* get_timestamp() {
15+
static char timestamp[64];
16+
time_t now = time(NULL);
17+
struct tm* tm_info = localtime(&now);
18+
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", tm_info);
19+
return timestamp;
2020
}
2121

22-
void log_message(LogLevel level, const char *format, ...) {
23-
const char *level_str = (level == LOG_LEVEL_ERROR) ? "E" : "I";
24-
FILE *output = (level == LOG_LEVEL_ERROR) ? stderr : stdout;
22+
void log_message(LogLevel level, const char* format, ...) {
23+
const char* level_str = (level == LOG_LEVEL_ERROR) ? "E" : "I";
24+
FILE* output = (level == LOG_LEVEL_ERROR) ? stderr : stdout;
2525

26-
fprintf(output, "%s | %s | %d | ", get_timestamp(), level_str, getpid());
26+
fprintf(output, "%s | %s | %d | ", get_timestamp(), level_str, getpid());
2727

28-
va_list args;
29-
va_start(args, format);
30-
vfprintf(output, format, args);
31-
va_end(args);
28+
va_list args;
29+
va_start(args, format);
30+
vfprintf(output, format, args);
31+
va_end(args);
3232

33-
fprintf(output, "\n");
33+
fprintf(output, "\n");
3434
}

0 commit comments

Comments
 (0)