/* compiled with: * clang `pkg-config --libs json-c` fm.c */ #include #include #include #define MAX_NAME 30 #define ESC_FIN "\x1b[0m" char *rgb_to_esc(const char *rgb) { int r, g, b; char *esc = malloc(24); sscanf(rgb, "#%02x%02x%02x", &r, &g, &b); sprintf(esc, "\x1b[38;2;%d;%d;%dm", r, g, b); return esc; } char *make_name(json_object *name) { const char *name_raw = json_object_get_string(name); char *name_str = malloc(MAX_NAME); int name_len= strlen(name_raw); if (name_len < MAX_NAME) { strcpy(name_str, name_raw); memset(&name_str[name_len], ' ', MAX_NAME-name_len); } else { strncpy(name_str, name_raw, MAX_NAME-3); strcat(name_str, "..."); } return name_str; } void print_line(json_object *team) { json_object *name= json_object_object_get(team, "name"); json_object *pts = json_object_object_get(team, "pts"); json_object *pos = json_object_object_get(team, "idx"); json_object *qualColor = json_object_object_get(team, "qualColor"); char *col; if (!json_object_is_type(qualColor, json_type_null)) col = rgb_to_esc(json_object_get_string(qualColor)); else col = rgb_to_esc("#FFFFFF"); printf("%s%2d %s%3d%s\n", col, json_object_get_int(pos), make_name(name), json_object_get_int(pts), ESC_FIN); } int main(void) { json_object *root = json_object_from_file("laliga.json"); if (!root) return 1; json_object *table_data = json_object_array_get_idx(json_object_object_get(root, "table"), 0); table_data = json_object_object_get(table_data, "data"); json_object *legend = json_object_object_get(table_data, "legend"); json_object *table = json_object_object_get(table_data, "table"); json_object *table_all = json_object_object_get(table, "all"); int n = json_object_array_length(table_all); for (int i = 0; i < n; i++) { json_object *team; team = json_object_array_get_idx(table_all, i); print_line(team); free(team); } json_object_put(root); return 0; }