Skip to content

Commit 2c792d0

Browse files
committed
Use enum instead of int when parse format
1 parent ffd9013 commit 2c792d0

File tree

3 files changed

+63
-57
lines changed

3 files changed

+63
-57
lines changed

main/format_output.c

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
#include <glib.h>
1818
#include "./format_output.h"
1919

20-
int
21-
format_to_int(gchar *format)
20+
FORMAT
21+
parse_format(gchar *format)
2222
{
2323
if (g_strcmp0(format, "json") == 0)
24-
return 0;
24+
return JSON;
2525
if (g_strcmp0(format, "csv") == 0)
26-
return 1;
26+
return CSV;
2727
if (g_strcmp0(format, "yaml") == 0)
28-
return 2;
28+
return YAML;
2929
else
30-
return -1;
30+
return DEFAULT;
3131
}
3232

3333
void
@@ -74,14 +74,14 @@ print_as_csv(gchar *status, gchar *start, gchar *error)
7474

7575

7676
void
77-
print_header(gchar *format)
77+
print_header(FORMAT format)
7878
{
79-
switch (format_to_int(format)) {
80-
case 0:
81-
case 2:
79+
switch (format) {
80+
case JSON:
81+
case YAML:
8282
fprintf(stdout, "[\n");
8383
break;
84-
case 1:
84+
case CSV:
8585
fprintf(stdout, "status,start,error\n");
8686
break;
8787
default:
@@ -90,11 +90,11 @@ print_header(gchar *format)
9090
}
9191

9292
void
93-
print_footer(gchar *format)
93+
print_footer(FORMAT format)
9494
{
95-
switch (format_to_int(format)) {
96-
case 0 :
97-
case 2:
95+
switch (format) {
96+
case JSON:
97+
case YAML:
9898
fprintf(stdout, "\n]\n");
9999
break;
100100
default:
@@ -104,15 +104,15 @@ print_footer(gchar *format)
104104
}
105105

106106
void
107-
print_body(gchar *format, gchar *status, gchar *start, gchar *error, gboolean first){
108-
switch (format_to_int(format)) {
109-
case 0:
107+
print_body(FORMAT format, gchar *status, gchar *start, gchar *error, gboolean first){
108+
switch (format) {
109+
case JSON:
110110
print_as_json(status, start, error, first);
111111
break;
112-
case 1:
112+
case CSV:
113113
print_as_csv(status, start, error);
114114
break;
115-
case 2:
115+
case YAML:
116116
print_as_yaml(status, start, error, first);
117117
break;
118118
default:
@@ -121,14 +121,14 @@ print_body(gchar *format, gchar *status, gchar *start, gchar *error, gboolean fi
121121
}
122122

123123
void
124-
print_status_header(gchar *format)
124+
print_status_header(FORMAT format)
125125
{
126-
switch (format_to_int(format)) {
127-
case 0:
128-
case 2:
126+
switch (format) {
127+
case JSON:
128+
case YAML:
129129
fprintf(stdout, "[\n");
130130
break;
131-
case 1:
131+
case CSV:
132132
fprintf(stdout,
133133
"key,status,pid,#start,#died,csz,ssz,mfd,since,group,cmd\n");
134134
break;
@@ -170,11 +170,11 @@ status_body_csv(gchar *fmt_line, int size)
170170
}
171171

172172
void
173-
print_status_sep(gchar *format, int count)
173+
print_status_sep(FORMAT format, int count)
174174
{
175-
switch (format_to_int(format)) {
176-
case 0:
177-
case 2:
175+
switch (format) {
176+
case JSON:
177+
case YAML:
178178
if(count)
179179
fprintf(stdout, ",\n");
180180
default:
@@ -183,16 +183,16 @@ print_status_sep(gchar *format, int count)
183183
}
184184

185185
void
186-
get_line_format(gchar *format, gchar *fmt_line, int size)
186+
get_line_format(FORMAT format, gchar *fmt_line, int size)
187187
{
188-
switch (format_to_int(format)) {
189-
case 0:
188+
switch (format) {
189+
case JSON:
190190
status_body_json(fmt_line, size);
191191
break;
192-
case 1:
192+
case CSV:
193193
status_body_csv(fmt_line, size);
194194
break;
195-
case 2:
195+
case YAML:
196196
status_body_yaml(fmt_line, size);
197197
break;
198198
default:

main/format_output.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919
# include <glib.h>
2020

21-
int format_to_int(gchar *format);
21+
typedef enum FORMAT FORMAT;
22+
enum FORMAT {CSV, JSON, YAML, DEFAULT};
23+
24+
FORMAT parse_format(gchar *format);
2225
void print_as_json(gchar *status, gchar *start, char *error, gboolean first);
2326
void print_as_yaml(gchar *status, gchar *start, char *error, gboolean first);
2427
void print_as_csv(gchar *status, gchar *start, char *error);
25-
void print_header(gchar *format);
26-
void print_footer(gchar *format);
27-
void print_body(gchar *format, gchar *status, gchar *start, gchar *error, gboolean first);
28-
void print_status_header(gchar *format);
28+
void print_header(FORMAT format);
29+
void print_footer(FORMAT format);
30+
void print_body(FORMAT format, gchar *status, gchar *start, gchar *error, gboolean first);
31+
void print_status_header(FORMAT format);
2932
void status_body_json(gchar *fmt_line, int size);
3033
void status_body_yaml(gchar *fmt_line, int size);
3134
void status_body_csv(gchar *fmt_line, int size);
32-
void print_status_sep(gchar *format, int count);
33-
void get_line_format(gchar *format, gchar *fmt_line, int size);
35+
void print_status_sep(FORMAT format, int count);
36+
void get_line_format(FORMAT format, gchar *fmt_line, int size);
3437

3538
#endif

main/gridinit_cmd.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ static GOptionEntry entries[] = {
122122
{"sock-path", 'S', 0, G_OPTION_ARG_FILENAME, &sock_path,
123123
"explicit unix socket path", "SOCKET"},
124124
{"format", 'f', 0, G_OPTION_ARG_STRING, &format,
125-
"output result by given FORMAT. Available FORMAT value are yaml, csv or json","FORMAT"},
125+
"output result by given FORMAT. Available FORMAT value are "
126+
"yaml, csv or json","FORMAT"},
126127
{"version", 'v', 0, G_OPTION_ARG_NONE, &flag_version,
127128
"Display the version of gridinit_cmd", NULL},
128129
{NULL}
@@ -267,12 +268,14 @@ dump_as(FILE *in_stream, void *udata)
267268

268269
kw = flag_color ? &KEYWORDS_COLOR : &KEYWORDS_NORMAL;
269270

270-
if(format_to_int(format) != -1)
271+
FORMAT format_t = parse_format(format);
272+
if(format_t != DEFAULT)
271273
kw = &KEYWORDS_NORMAL;
272274

273275
dump_args = udata;
274276

275-
print_header(format);
277+
278+
print_header(format_t);
276279

277280
while (!feof(in_stream) && !ferror(in_stream)) {
278281
bzero(line, sizeof(line));
@@ -289,11 +292,11 @@ dump_as(FILE *in_stream, void *udata)
289292
gchar *status = (gchar *) (code==0 ? kw->done :
290293
(code==EALREADY?kw->already:kw->failed));
291294
gchar *error = strerror(code);
292-
print_body(format, status, start, error,first);
295+
print_body(format_t, status, start, error,first);
293296
first = FALSE;
294297
}
295298
}
296-
print_footer(format);
299+
print_footer(format_t);
297300
fflush(stdout);
298301
}
299302

@@ -396,15 +399,15 @@ command_status(int lvl, int argc, char **args)
396399

397400
GList *all_jobs = _fetch_services();
398401
GList *jobs = _filter_services(all_jobs, args, counters);
399-
402+
FORMAT format_t = parse_format(format);
400403
/* compute the max length of several variable field, for well aligned
401404
* columns on the output. */
402405
const size_t maxkey = get_longest_key(jobs);
403406
const size_t maxgroup = get_longest_group(jobs);
404407

405-
if (format_to_int(format) != -1) {
406-
print_status_header(format);
407-
get_line_format(format, fmt_line, sizeof(fmt_line));
408+
if (format_t != DEFAULT) {
409+
print_status_header(format_t);
410+
get_line_format(format_t, fmt_line, sizeof(fmt_line));
408411
goto print_lines;
409412
}
410413

@@ -448,7 +451,7 @@ command_status(int lvl, int argc, char **args)
448451
struct keyword_set_s *kw;
449452
kw = flag_color ? &KEYWORDS_COLOR : & KEYWORDS_NORMAL;
450453

451-
if (format_to_int(format) != -1)
454+
if (format_t != DEFAULT)
452455
kw = &KEYWORDS_NORMAL;
453456

454457
/* iterate on the lines */
@@ -473,8 +476,8 @@ command_status(int lvl, int argc, char **args)
473476
count_broken ++;
474477
count_all ++;
475478
/* Print now! */
476-
if (format_to_int(format) != -1) {
477-
print_status_sep(format, count_all-1);
479+
if (format_t != DEFAULT) {
480+
print_status_sep(format_t, count_all-1);
478481

479482
fprintf(stdout, fmt_line, ci->key, str_status, ci->pid,
480483
ci->counter_started, ci->counter_died,
@@ -503,8 +506,8 @@ command_status(int lvl, int argc, char **args)
503506
end:;
504507
}
505508

506-
if (format_to_int(format) != -1)
507-
print_footer(format);
509+
if (format_t != DEFAULT)
510+
print_footer(format_t);
508511
fflush(stdout);
509512

510513
/* If patterns have been specified, we must find items (the user
@@ -586,10 +589,10 @@ command_stop(int argc, char **args)
586589
g_list_free(jobs);
587590
return rc;
588591
}
589-
592+
FORMAT format_t = parse_format(format);
590593
while (!_all_down()) {
591594
/* If standart output format*/
592-
if (format_to_int(format) == -1)
595+
if (format_t != DEFAULT)
593596
g_print("# Stopping...\n");
594597
int rc = command_kill(argc, args);
595598
if (rc != 0)

0 commit comments

Comments
 (0)