I am new to C and want to know if how I am handling something is correct. I am creating a module for http://nginx.com/, and I am creating a status page for my module.
Now the status page will consist of some basic HTML & tables. Here is some of my code to create this.
// Get size
size =
sizeof("<table>") +
sizeof("<tr><td align=\"right\">enabled:</td><td>YES</td></tr>") +
sizeof("<tr><td align=\"right\">activated:</td><td>YES</td></tr>") +
sizeof("<tr><td align=\"right\">connections/lt:</td><td>") + NGX_ATOMIC_T_LEN + sizeof(" / ") + NGX_ATOMIC_T_LEN + sizeof("</td></tr>") +
sizeof("<tr><td align=\"right\">remain on: xxxx-xx-xx xx:xx:xx GMT</td><td></td></tr>") +
sizeof("</table>");
// Start buffer
b = ngx_create_temp_buf(r->pool, size);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// Start chain
out.buf = b;
out.next = NULL;
// Finish buffer
b->last = ngx_sprintf(b->last, "<table>");
b->last = ngx_sprintf(b->last, "<tr><td align=\"right\">enabled:</td><td>%s</td></tr>", alcf->enabled ? "YES" : "NO");
b->last = ngx_sprintf(b->last, "<tr><td align=\"right\">activated:</td><td>%s</td></tr>", alcf->activated ? "YES" : "NO");
b->last = ngx_sprintf(b->last, "<tr><td align=\"right\">connections/lt:</td><td>%uA / %uA</td></tr>", ac, alcf->connections_activate);
b->last = ngx_sprintf(b->last, "<tr><td align=\"right\">remain on:</td><td>");
b->last = !alcf->activatedEndTime ? ngx_sprintf(b->last,"") : ngx_http_cookie_time(b->last, alcf->activatedEndTime);
b->last = ngx_sprintf(b->last, "</td></tr>");
b->last = ngx_sprintf(b->last, "<table>");
Is this the only effective way of doing this, I feel like it would be wrong to have to write the HTML code twice, once to get the size to inflate the buffer, and one to actually store in the buffer. Would there be any other solution to this. I am trying to keep it as memory efficient as possible.