#include #include #include #include struct entity { float x, y; char *label; struct entity *next; }; static struct entity *entity_head = NULL; int board_width = 300, board_height = 300; static char *vaprintf(const char *fmt, va_list ap) { int len; char scratch; char *ret; va_list ap2; va_copy(ap2, ap); len = vsnprintf(&scratch, 1, fmt, ap2); va_end(ap2); if (len < 0) { perror(__func__); return NULL; } ret = malloc(len + 1); vsnprintf(ret, len + 1, fmt, ap); return ret; } static struct entity *entity_create(const char *fmt, ...) __attribute__((format(printf,1,2))); static struct entity *entity_create(const char *fmt, ...) { va_list ap; struct entity *e; e = calloc(1, sizeof *e); va_start(ap, fmt); e->label = vaprintf(fmt, ap); va_end(ap); e->next = NULL; e->x = 0; e->y = 0; return e; } static void entity_move(struct entity *e, int x, int y) { e->x = x; e->y = y; } static void entity_insert(struct entity **head, struct entity *e) { e->next = *head; *head = e; } static void generate(void) { int i, max; struct entity *e; max = rand() % 30; for (i = 0; i < max; i++) { e = entity_create("ent%03d", i); if (!e) exit(1); entity_insert(&entity_head, e); entity_move(e, rand() % board_width, rand() % board_height); } } static void svg_begin(FILE *f) { fprintf(f, "\n"); fprintf(f, "\n"); fprintf(f, "\n"); } static void svg_end(FILE *f) { fprintf(f, "\n"); } static void svg_rect(FILE *f, double w, double h, unsigned color) { fprintf(f, "> 16) & 255, (color >> 8) & 255, color & 255); fprintf(f, "stroke:rgb(0,0,0)\"/>\n"); } static void svg_circle(FILE *f, double r, double x, double y, unsigned color) { fprintf(f, "\n", (color >> 16) & 255, (color >> 8) & 255, color & 255); } static void svg_text(FILE *f, double x, double y, const char *fmt, ...) __attribute__((format(printf,4,5))); static void svg_text(FILE *f, double x, double y, const char *fmt, ...) { char *buf; va_list ap; va_start(ap, fmt); buf = vaprintf(fmt, ap); va_end(ap); fprintf(f, "\n"); fprintf(f, "%s\n\n", buf); free(buf); } static void svg_print(void) { struct entity *e; FILE *f = stdout; double r = (board_width + board_height) / 50.; svg_begin(f); svg_rect(f, board_width, board_height, 0xff0088); for (e = entity_head; e; e = e->next) { svg_circle(f, r, e->x, e->y, 0x00ff88); svg_text(f, e->x, e->y + r, "%s", e->label); } svg_end(f); } int main() { generate(); svg_print(); return 0; }