#include "rect.h" #include #include #include #define max_rects 32 const char* next_line(const char* line) { const char* c; for (c = line; *c != '\n'; c++) { if (!*c) return 0; } return c + 1; } int line_len(const char* line) { const char* c; int r = 0; for (c = line; *c && *c != '\n'; c++, r++); return r; } int main(int argc, char** argv) { FILE* infile; const char* line; char* src; int size; int speed; Rect rects[max_rects]; Rect r; int fc = 0; if (argc < 3) { fprintf(stderr, "Usage: %s infile outfile.\n", argv[0]); return 1; } infile = fopen(argv[1], "rb"); if (!infile) { fprintf(stderr, "Failed to open %s.\n", argv[1]); return 2; } fseek(infile, 0, SEEK_END); size = ftell(infile); rewind(infile); src = malloc(size + 1); fread(src, 1, size, infile); src[size] = 0; fclose(infile); line = src; speed = (int)strtol(line, 0, 10); line = next_line(line); for (; line; line = next_line(line)) { int ll = line_len(line); char* line2 = malloc(ll + 1); line2[ll] = 0; /* lmao */ memcpy(line2, line, ll); if (sscanf(line, "{%d,%d,%d,%d}", &r.x, &r.y, &r.w, &r.h ) != 4) break; rects[fc++] = r; } free(src); { FILE* outfile = fopen(argv[2], "wb"); if (!outfile) { fprintf(stderr, "Failed to open %s.\n", argv[2]); return 2; } fwrite(&fc, 1, 4, outfile); fwrite(&speed, 1, 4, outfile); fwrite(rects, fc, sizeof r, outfile); fclose(outfile); } return 0; }