Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating size of C string before writing to buffer
    text
    copied!<p>I am new to C and want to know if how I am handling something is correct. I am creating a module for <a href="http://nginx.com/" rel="nofollow">http://nginx.com/</a>, and I am creating a status page for my module.</p> <p>Now the status page will consist of some basic HTML &amp; tables. Here is some of my code to create this.</p> <pre><code>// Get size size = sizeof("&lt;table&gt;") + sizeof("&lt;tr&gt;&lt;td align=\"right\"&gt;enabled:&lt;/td&gt;&lt;td&gt;YES&lt;/td&gt;&lt;/tr&gt;") + sizeof("&lt;tr&gt;&lt;td align=\"right\"&gt;activated:&lt;/td&gt;&lt;td&gt;YES&lt;/td&gt;&lt;/tr&gt;") + sizeof("&lt;tr&gt;&lt;td align=\"right\"&gt;connections/lt:&lt;/td&gt;&lt;td&gt;") + NGX_ATOMIC_T_LEN + sizeof(" / ") + NGX_ATOMIC_T_LEN + sizeof("&lt;/td&gt;&lt;/tr&gt;") + sizeof("&lt;tr&gt;&lt;td align=\"right\"&gt;remain on: xxxx-xx-xx xx:xx:xx GMT&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;") + sizeof("&lt;/table&gt;"); // Start buffer b = ngx_create_temp_buf(r-&gt;pool, size); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } // Start chain out.buf = b; out.next = NULL; // Finish buffer b-&gt;last = ngx_sprintf(b-&gt;last, "&lt;table&gt;"); b-&gt;last = ngx_sprintf(b-&gt;last, "&lt;tr&gt;&lt;td align=\"right\"&gt;enabled:&lt;/td&gt;&lt;td&gt;%s&lt;/td&gt;&lt;/tr&gt;", alcf-&gt;enabled ? "YES" : "NO"); b-&gt;last = ngx_sprintf(b-&gt;last, "&lt;tr&gt;&lt;td align=\"right\"&gt;activated:&lt;/td&gt;&lt;td&gt;%s&lt;/td&gt;&lt;/tr&gt;", alcf-&gt;activated ? "YES" : "NO"); b-&gt;last = ngx_sprintf(b-&gt;last, "&lt;tr&gt;&lt;td align=\"right\"&gt;connections/lt:&lt;/td&gt;&lt;td&gt;%uA / %uA&lt;/td&gt;&lt;/tr&gt;", ac, alcf-&gt;connections_activate); b-&gt;last = ngx_sprintf(b-&gt;last, "&lt;tr&gt;&lt;td align=\"right\"&gt;remain on:&lt;/td&gt;&lt;td&gt;"); b-&gt;last = !alcf-&gt;activatedEndTime ? ngx_sprintf(b-&gt;last,"") : ngx_http_cookie_time(b-&gt;last, alcf-&gt;activatedEndTime); b-&gt;last = ngx_sprintf(b-&gt;last, "&lt;/td&gt;&lt;/tr&gt;"); b-&gt;last = ngx_sprintf(b-&gt;last, "&lt;table&gt;"); </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload