-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_printer.c
More file actions
179 lines (150 loc) · 4.57 KB
/
data_printer.c
File metadata and controls
179 lines (150 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#define PROGRAM_NAME "data_printer"
#define KILOBYTE 1024
#define MEGABYTE (1024 * KILOBYTE)
#define GIGABYTE (1024 * MEGABYTE)
#define ul_t unsigned long
typedef struct {
int year;
int month;
int day;
} date;
/* for log lines */
typedef struct log_line {
date timestamp; // local date
ul_t bytes_down; // how many bytes was downloaded this calendar day
ul_t bytes_up; // how many bytes was uploaded this calendar day
} log_line;
char read_location[255];
bool display_all = false;
bool display_total = false;
bool display_less = false;
bool display_csv = false;
char *convert_to_unit = NULL;
bool no_units = false;
log_line log_file_lines[31];
int read_file (){
FILE *file = fopen(read_location, "r");
int lines = 0;
while (fscanf(file, "%d-%d-%d,%lu,%lu", &log_file_lines[lines].timestamp.year, &log_file_lines[lines].timestamp.month, &log_file_lines[lines].timestamp.day, &log_file_lines[lines].bytes_down, &log_file_lines[lines].bytes_up) == 5) {
lines++;
}
fclose(file);
return lines;
}
void printMi (unsigned long bytes, char *buffer){
char *units[] = {"kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
unsigned long unit = KILOBYTE;
int u = -1;
if (convert_to_unit != NULL){
switch (convert_to_unit[1]){
case 'k':
unit = KILOBYTE;
u = 0;
break;
case 'm':
unit = MEGABYTE;
u = 1;
break;
case 'g':
unit = GIGABYTE;
u = 2;
break;
}
}
int i = -1;
unsigned long tmp = 0;
do {
tmp = bytes % (unit == 1024 ? 1024 : (unit / 1024));
bytes = bytes / unit;
i++;
} while (bytes > unit);
if (no_units){
sprintf (buffer, "%lu.%lu", bytes, tmp);
} else {
sprintf (buffer, "%lu.%lu %s", bytes, tmp, units[u == -1 ? i : u]);
}
}
void pretty_print (const unsigned long bytes_down, const unsigned long bytes_up){
char buffer[32];
char str_down[7];
char str_up[5];
char str_total[9];
if (display_less){
strcpy(str_down, "");
strcpy(str_up, "");
strcpy(str_total, "");
}
if (display_csv){
strcpy(str_down, "");
strcpy(str_up, ",");
strcpy(str_total, ",");
} else if (!display_csv && !display_less){
strcpy(str_down, "Down: ");
strcpy(str_up, " Up: ");
strcpy(str_total, " Total: ");
}
printMi(bytes_down, buffer);
printf ("%s%s", str_down, buffer);
printMi(bytes_up, buffer);
printf ("%s%s", str_up, buffer);
if (display_total){
printMi(bytes_down + bytes_up, buffer);
printf ("%s%s", str_total, buffer);
}
printf ("\n");
}
void display_help (){
fprintf (stderr, "Usage: %s [%s]...\n%s\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n "
, PROGRAM_NAME, "OPTIONS"
, "Print data amount from log file to stdout. Parameter -p mandatory."
, " -p, --path\tmust be followed by a path to single file with data logs, it's mandatory parameter"
, " -t, --total\tprint total amount for data (download + upload)"
, " -l, --less\tprint only numbers with units without the Down/Up/Total strings"
, " -L, --LESS\tprint only number with no units"
, " -c, --csv\tprint in .csv format"
, " -kb, -mb, -gb\tprint in kilobytes, megabytes or gigabytes respectively, these are mutually exclusive"
, " -h, --help\tdisplay usage information");
}
int main (int argc, char **argv){
if (argc == 1){
fprintf(stderr, "%s\n", "Parameter -p is mandatory. Use --help for more information.");
return 0;
} else {
for (int i = 1; i < argc; i++){
if ((strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--path") == 0) && argc > (i + 1)){
strcpy(read_location, argv[i + 1]);
i++;
} else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--all") == 0){
display_all = true;
} else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--total") == 0){
display_total = true;
} else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--less") == 0){
display_less = true;
} else if (strcmp(argv[i], "-L") == 0 || strcmp(argv[i], "--LESS") == 0){
display_less = true;
no_units = true;
} else if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--csv") == 0){
display_csv = true;
} else if (strcmp(argv[i], "-kb") == 0 || strcmp(argv[i], "-mb") == 0 || strcmp(argv[i], "-gb") == 0){
convert_to_unit = argv[i];
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0){
display_help();
return 0;
}
}
}
int lines = read_file ();
if (display_all){
for (int i = 0; i < lines; i++){
pretty_print(log_file_lines[i].bytes_down, log_file_lines[i].bytes_up);
}
} else {
pretty_print(log_file_lines[lines - 1].bytes_down, log_file_lines[lines - 1].bytes_up);
}
exit(0);
}