Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As the other people answering the question have shown, there isn't really a clean ("D.R.Y.") way to do this using the C preprocessor alone. The problem is that you need to define an array of size of your enum containing strings corresponding to each enum value, and the C preprocessor isn't smart enough to be able to do that. What I do is to create a text file something like this:</p> <pre><code>%status ok %meaning The routine completed its work successfully. % %status eof_reading_content %meaning The routine encountered the end of the input before it expected to. % </code></pre> <p>Here %'s mark delimiters.</p> <p>Then a Perl script, the working part of which looks like this,</p> <pre><code>sub get_statuses { my ($base_name, $prefix) = @_; my @statuses; my $status_txt_file = "$base_name.txt"; my $status_text = file_slurp ($status_txt_file); while ($status_text =~ m/ \%status\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\n \%meaning\s*(.*?)\s*\n\%\s*\n /gxs) { my ($code, $meaning) = ($1, $2); $code = $prefix."_$code"; $meaning =~ s/\s+/ /g; push @statuses, [$code, $meaning]; } return @statuses; } </code></pre> <p>reads this file and writes a header file:</p> <pre><code>typedef enum kinopiko_status { kinopiko_status_ok, kinopiko_status_eof_reading_content, </code></pre> <p>and a C file:</p> <pre><code>/* Generated by ./kinopiko-status.pl at 2009-11-09 23:45. */ #include "kinopiko-status.h" const char * kinopiko_status_strings[26] = { "The routine completed its work successfully.", "The routine encountered the end of the input before it expected to. ", </code></pre> <p>using the input file at the top. It also calculates the number 26 here by counting the input lines. (There are twenty-six possible statuses in fact.)</p> <p>Then the construction of the status string file is automated using <code>make</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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