[PATCH v5 2/4] src/: Use countof() instead of array_length()
Alejandro Colomar <[email protected]>
| Newsgroups | gmane.comp.printing.groff.general |
|---|---|
| Message-ID | <7348d3a1ff2a95f3fcc4fafe91ec6cb29281df13.1765122824.git.alx@kernel.org> |
countof() is standard (ISO C2y). Signed-off-by: Alejandro Colomar <[email protected]> --- src/devices/grolbp/lbp.cpp | 5 +++-- src/devices/grolj4/lj4.cpp | 5 +++-- src/devices/grops/ps.cpp | 3 ++- src/devices/grops/psrm.cpp | 11 ++++++----- src/include/lib.h | 8 -------- src/libs/libgroff/font.cpp | 6 +++--- src/libs/libgroff/glyphuni.cpp | 4 +++- src/libs/libgroff/make-uniuni | 4 +++- src/libs/libgroff/uniuni.cpp | 4 +++- src/preproc/eqn/box.cpp | 14 ++++++++------ src/preproc/eqn/lex.cpp | 11 ++++++----- src/preproc/pic/pic.ypp | 7 ++++--- src/preproc/preconv/preconv.cpp | 5 +++-- src/roff/troff/input.cpp | 4 ++-- src/utils/addftinfo/addftinfo.cpp | 5 +++-- 15 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/devices/grolbp/lbp.cpp b/src/devices/grolbp/lbp.cpp index 44783da4e..cf43a30bf 100644 --- a/src/devices/grolbp/lbp.cpp +++ b/src/devices/grolbp/lbp.cpp @@ -31,6 +31,7 @@ TODO #include <errno.h> #include <limits.h> // INT_MAX #include <math.h> // fabs(), sqrt() +#include <stdcountof.h> #include <stdlib.h> // abs(), EXIT_SUCCESS, exit(), strtol() #include <string.h> // strcmp(), strcpy(), strlen(), strncpy() #include <strings.h> // strcasecmp() @@ -42,7 +43,7 @@ TODO #include "charset.h" #include "driver.h" #include "lbp.h" -#include "lib.h" // array_length(), strsave() +#include "lib.h" // strsave() #include "paper.h" extern "C" const char *Version_string; @@ -599,7 +600,7 @@ static int set_papersize(const char *paperformat) unsigned int i; // First, test for a standard (i.e. supported directly by the printer) // paper format. - for (i = 0 ; i < array_length(lbp_papersizes); i++) + for (i = 0 ; i < countof(lbp_papersizes); i++) { if (strcasecmp(lbp_papersizes[i].name,paperformat) == 0) return lbp_papersizes[i].code; diff --git a/src/devices/grolj4/lj4.cpp b/src/devices/grolj4/lj4.cpp index eb6f9274a..34ebb4b48 100644 --- a/src/devices/grolj4/lj4.cpp +++ b/src/devices/grolj4/lj4.cpp @@ -37,6 +37,7 @@ X command to include bitmap graphics #include <assert.h> #include <locale.h> // setlocale() #include <math.h> // atan2(), floor() +#include <stdcountof.h> #include <stdio.h> // EOF, FILE, fflush(), fprintf(), printf(), // setbuf(), stderr, stdout #include <stdlib.h> // exit(), EXIT_SUCCESS, strtol() @@ -144,7 +145,7 @@ void lj4_font::handle_unknown_font_command(const char *command, const char *fn, int lineno) { - for (size_t i = 0; i < array_length(command_table); i++) { + for (size_t i = 0; i < countof(command_table); i++) { if (strcmp(command, command_table[i].s) == 0) { if (0 /* nullptr */ == arg) fatal_with_file_and_line(fn, lineno, @@ -179,7 +180,7 @@ void lj4_font::handle_unknown_font_command(const char *command, static ssize_t lookup_paper_size(const char *s) { // C++11: constexpr - const size_t paper_table_length = array_length(paper_table); + const size_t paper_table_length = countof(paper_table); // ...and once it's a constexpr, we can do this... //static_assert(paper_table_length < INT_MAX); // ...but until then... diff --git a/src/devices/grops/ps.cpp b/src/devices/grops/ps.cpp index ba42ad2b0..79bd769a7 100644 --- a/src/devices/grops/ps.cpp +++ b/src/devices/grops/ps.cpp @@ -30,6 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <errno.h> #include <locale.h> // setlocale() #include <math.h> // atan2(), sqrt(), tan() +#include <stdcountof.h> #include <stdint.h> // uint16_t #include <stdio.h> // EOF, FILE, fclose(), fgets(), fileno(), fseek(), // getc(), SEEK_SET, setbuf(), stderr, stdout @@ -1639,7 +1640,7 @@ void ps_printer::special(char *arg, const environment *env, char type) error("empty X command ignored"); return; } - for (size_t i = 0; i < array_length(proc_table); i++) + for (size_t i = 0; i < countof(proc_table); i++) if (strncmp(command, proc_table[i].name, p - command) == 0) { flush_sbuf(); if (sbuf_color != *env->col) diff --git a/src/devices/grops/psrm.cpp b/src/devices/grops/psrm.cpp index 5957d0dd9..b5a75a92e 100644 --- a/src/devices/grops/psrm.cpp +++ b/src/devices/grops/psrm.cpp @@ -21,13 +21,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #endif #include <errno.h> +#include <stdcountof.h> #include <stdio.h> // EOF, FILE, fclose(), fgets(), getc(), ungetc() #include <stdlib.h> // getenv(), putenv(), strtoul() #include <string.h> // strerror(), strtok() #include "cset.h" #include "driver.h" -#include "lib.h" // array_length(), strsave() +#include "lib.h" // strsave() #include "stringclass.h" #include "ps.h" @@ -72,7 +73,7 @@ const char *extension_table[] = { "FileSystem", }; -const size_t NEXTENSIONS = array_length(extension_table); +const size_t NEXTENSIONS = countof(extension_table); // this must stay in sync with 'resource_type' in 'ps.h' const char *resource_table[] = { @@ -85,7 +86,7 @@ const char *resource_table[] = { "pattern", }; -const size_t NRESOURCES = array_length(resource_table); +const size_t NRESOURCES = countof(resource_table); static bool read_uint_arg(const char **pp, unsigned *res) { @@ -952,7 +953,7 @@ void resource_manager::process_file(int rank, FILE *fp, "DocumentSuppliedFiles:", }; - const size_t NHEADER_COMMENTS = array_length(header_comment_table); + const size_t NHEADER_COMMENTS = countof(header_comment_table); static const comment_info comment_table[] = { { "BeginResource:", &resource_manager::do_begin_resource }, @@ -973,7 +974,7 @@ void resource_manager::process_file(int rank, FILE *fp, { "BeginBinary:", &resource_manager::do_begin_binary }, }; - const size_t NCOMMENTS = array_length(comment_table); + const size_t NCOMMENTS = countof(comment_table); string buf; int saved_lineno = current_lineno; const char *saved_filename = current_filename; diff --git a/src/include/lib.h b/src/include/lib.h index 5e92e3f4d..6b8ca2a59 100644 --- a/src/include/lib.h +++ b/src/include/lib.h @@ -121,14 +121,6 @@ int mksdir(char *tmpl); static const double PI = 3.14159265358979323846; -#ifdef __cplusplus -template <typename T, size_t N> -// constexpr // C++11 -size_t array_length(T(&)[N]) { - return N; -} -#endif - #endif /* GROFF_LIB_H */ // Local Variables: diff --git a/src/libs/libgroff/font.cpp b/src/libs/libgroff/font.cpp index faef870fe..bbb275661 100644 --- a/src/libs/libgroff/font.cpp +++ b/src/libs/libgroff/font.cpp @@ -25,11 +25,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <errno.h> #include <limits.h> // INT_MAX, INT_MIN, LONG_MAX #include <math.h> +#include <stdcountof.h> #include <stdlib.h> #include <string.h> // strerror() #include <wchar.h> -#include "lib.h" // array_length() +#include "lib.h" #include "errarg.h" #include "error.h" @@ -1288,8 +1289,7 @@ const char *font::load_desc() bool numeric_directive_found = false; size_t idx; for (idx = 0; - (!numeric_directive_found - && (idx < array_length(numeric_directive_table))); + !numeric_directive_found && (idx < countof(numeric_directive_table)); idx++) if (strcmp(numeric_directive_table[idx].name, p) == 0) numeric_directive_found = true; diff --git a/src/libs/libgroff/glyphuni.cpp b/src/libs/libgroff/glyphuni.cpp index 2de37f50c..850df0c99 100644 --- a/src/libs/libgroff/glyphuni.cpp +++ b/src/libs/libgroff/glyphuni.cpp @@ -20,6 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <config.h> #endif +#include <stdcountof.h> + #include "lib.h" #include "stringclass.h" @@ -509,7 +511,7 @@ static struct glyph_to_unicode_init { glyph_to_unicode_init::glyph_to_unicode_init() { - for (size_t i = 0; i < array_length(glyph_to_unicode_list); i++) { + for (size_t i = 0; i < countof(glyph_to_unicode_list); i++) { glyph_to_unicode_map *gtu = new glyph_to_unicode_map[1]; gtu->value = (char *)glyph_to_unicode_list[i].value; glyph_to_unicode_table.define(glyph_to_unicode_list[i].key, gtu); diff --git a/src/libs/libgroff/make-uniuni b/src/libs/libgroff/make-uniuni index c14ca170e..c63c54d2e 100755 --- a/src/libs/libgroff/make-uniuni +++ b/src/libs/libgroff/make-uniuni @@ -89,6 +89,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <config.h> #endif +#include <stdcountof.h> + #include "lib.h" #include "stringclass.h" @@ -137,7 +139,7 @@ static struct unicode_decompose_init { unicode_decompose_init::unicode_decompose_init() { - for (size_t i = 0; i < array_length(unicode_decompose_list); i++) { + for (size_t i = 0; i < countof(unicode_decompose_list); i++) { unicode_decompose *dec = new unicode_decompose[1]; dec->value = const_cast<char *>(unicode_decompose_list[i].value); unicode_decompose_table.define(unicode_decompose_list[i].key, dec); diff --git a/src/libs/libgroff/uniuni.cpp b/src/libs/libgroff/uniuni.cpp index dd0d3d62c..f274074a1 100644 --- a/src/libs/libgroff/uniuni.cpp +++ b/src/libs/libgroff/uniuni.cpp @@ -20,6 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <config.h> #endif +#include <stdcountof.h> + #include "lib.h" #include "stringclass.h" @@ -2137,7 +2139,7 @@ static struct unicode_decompose_init { unicode_decompose_init::unicode_decompose_init() { - for (size_t i = 0; i < array_length(unicode_decompose_list); i++) { + for (size_t i = 0; i < countof(unicode_decompose_list); i++) { unicode_decompose *dec = new unicode_decompose[1]; dec->value = const_cast<char *>(unicode_decompose_list[i].value); unicode_decompose_table.define(unicode_decompose_list[i].key, dec); diff --git a/src/preproc/eqn/box.cpp b/src/preproc/eqn/box.cpp index 5000e5787..7d56ff2d8 100644 --- a/src/preproc/eqn/box.cpp +++ b/src/preproc/eqn/box.cpp @@ -20,6 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <config.h> #endif +#include <stdcountof.h> + #include <assert.h> #include "eqn.h" @@ -139,7 +141,7 @@ struct param *param_table = 0 /* nullptr */; void set_param(const char *name, int value) { - for (size_t i = 0; i <= array_length(default_param_table); i++) + for (size_t i = 0; i <= countof(default_param_table); i++) if (strcmp(param_table[i].name, name) == 0) { *(param_table[i].ptr) = value; return; @@ -149,7 +151,7 @@ void set_param(const char *name, int value) void reset_param(const char *name) { - for (size_t i = 0; i < array_length(default_param_table); i++) + for (size_t i = 0; i < countof(default_param_table); i++) if (strcmp(param_table[i].name, name) == 0) { *param_table[i].ptr = *(default_param_table[i].ptr); return; @@ -160,7 +162,7 @@ void reset_param(const char *name) int get_param(const char *name) { - for (size_t i = 0; i < array_length(default_param_table); i++) + for (size_t i = 0; i < countof(default_param_table); i++) if (strcmp(param_table[i].name, name) == 0) return *(param_table[i].ptr); assert(0 == "attempted to access parameter not in table"); @@ -169,8 +171,8 @@ int get_param(const char *name) void init_param_table() { - param_table = new param[array_length(default_param_table)]; - for (size_t i = 0; i < array_length(default_param_table); i++) { + param_table = new param[countof(default_param_table)]; + for (size_t i = 0; i < countof(default_param_table); i++) { param_table[i].name = default_param_table[i].name; param_table[i].ptr = new int(*(default_param_table[i].ptr)); } @@ -179,7 +181,7 @@ void init_param_table() void free_param_table() { if (param_table != 0 /* nullptr */) { - for (size_t i = 0; i < array_length(default_param_table); i++) + for (size_t i = 0; i < countof(default_param_table); i++) delete param_table[i].ptr; delete[] param_table; param_table = 0 /* nullptr */; diff --git a/src/preproc/eqn/lex.cpp b/src/preproc/eqn/lex.cpp index 44aa576cd..cb49e0799 100644 --- a/src/preproc/eqn/lex.cpp +++ b/src/preproc/eqn/lex.cpp @@ -20,7 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <config.h> #endif -#include "lib.h" // array_length() +#include <stdcountof.h> + #include "ptable.h" #include "stringclass.h" @@ -285,13 +286,13 @@ static struct builtin_def mathml_defs[] = { void init_table(const char *device) { unsigned int i; - for (i = 0; i < array_length(token_table); i++) { + for (i = 0; i < countof(token_table); i++) { definition *def = new definition[1]; def->is_macro = 0; def->tok = token_table[i].token; macro_table.define(token_table[i].name, def); } - for (i = 0; i < array_length(common_defs); i++) { + for (i = 0; i < countof(common_defs); i++) { definition *def = new definition[1]; def->is_macro = 1; def->contents = strsave(common_defs[i].def); @@ -299,7 +300,7 @@ void init_table(const char *device) macro_table.define(common_defs[i].name, def); } if (output_format == troff) { - for (i = 0; i < array_length(troff_defs); i++) { + for (i = 0; i < countof(troff_defs); i++) { definition *def = new definition[1]; def->is_macro = 1; def->contents = strsave(troff_defs[i].def); @@ -308,7 +309,7 @@ void init_table(const char *device) } } else if (output_format == mathml) { - for (i = 0; i < array_length(mathml_defs); i++) { + for (i = 0; i < countof(mathml_defs); i++) { definition *def = new definition[1]; def->is_macro = 1; def->contents = strsave(mathml_defs[i].def); diff --git a/src/preproc/pic/pic.ypp b/src/preproc/pic/pic.ypp index fdd0d197f..4d4e98b7c 100644 --- a/src/preproc/pic/pic.ypp +++ b/src/preproc/pic/pic.ypp @@ -25,6 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <errno.h> // EDOM, ERANGE, errno #include <math.h> // atan2(), cos(), floor(), fmod(), log10(), pow(), // sin(), sqrt() +#include <stdcountof.h> #include <stdio.h> // fflush(), fprintf(), snprintf(), sprintf(), stderr #include <stdlib.h> // rand(), srand(), system() #include <string.h> // strcat(), strchr(), strcmp(), strcpy(), strlen() @@ -1855,7 +1856,7 @@ void define_variable(const char *name, double val) if (strcmp(name, "scale") == 0) { // When the scale changes, reset all scaled predefined variables to // their default values. - for (size_t i = 0; i < array_length(defaults_table); i++) + for (size_t i = 0; i < countof(defaults_table); i++) if (defaults_table[i].scaled) define_variable(defaults_table[i].name, (val * defaults_table[i].val)); @@ -1875,7 +1876,7 @@ void parse_init() void reset(const char *nm) { - for (size_t i = 0; i < array_length(defaults_table); i++) + for (size_t i = 0; i < countof(defaults_table); i++) if (strcmp(nm, defaults_table[i].name) == 0) { double val = defaults_table[i].val; if (defaults_table[i].scaled) { @@ -1895,7 +1896,7 @@ void reset_all() // aren't scaled because 'scale' is not scaled, and changing the // value of 'scale' will reset all the predefined variables that // are scaled. - for (size_t i = 0; i < array_length(defaults_table); i++) + for (size_t i = 0; i < countof(defaults_table); i++) if (!defaults_table[i].scaled) define_variable(defaults_table[i].name, defaults_table[i].val); } diff --git a/src/preproc/preconv/preconv.cpp b/src/preproc/preconv/preconv.cpp index 0ca91a62d..f0f154be6 100644 --- a/src/preproc/preconv/preconv.cpp +++ b/src/preproc/preconv/preconv.cpp @@ -31,6 +31,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ # endif #endif #include <locale.h> // setlocale() +#include <stdcountof.h> #include <stdio.h> // EOF, FILE, fclose(), ferror(), fflush(), fileno(), // fopen(), fprintf(), fread(), fseek(), ftell(), // getc(), printf(), putchar(), rewind(), SEEK_SET, @@ -44,7 +45,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <getopt.h> // getopt_long() -#include "lib.h" // array_length() +#include "lib.h" #include "errarg.h" #include "error.h" @@ -809,7 +810,7 @@ get_BOM(FILE *fp, string &BOM, string &data) // UTF-8: 0xEFBBBF // UTF-16: 0xFEFF or 0xFFFE // UTF-32: 0x0000FEFF or 0xFFFE0000 - const int BOM_table_len = array_length(BOM_table); + const int BOM_table_len = countof(BOM_table); char BOM_string[4]; const char *retval = 0 /* nullptr */; int len; diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp index 49c7a5217..541cc5e51 100644 --- a/src/roff/troff/input.cpp +++ b/src/roff/troff/input.cpp @@ -23,6 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <assert.h> #include <errno.h> // ENOENT, errno #include <locale.h> // setlocale() +#include <stdcountof.h> #include <stdio.h> // EOF, FILE, clearerr(), fclose(), fflush(), // fileno(), fopen(), fprintf(), fseek(), getc(), // pclose(), popen(), printf(), SEEK_SET, snprintf(), @@ -37,7 +38,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <stack> #include "json-encode.h" // json_encode_char() -#include "lib.h" // array_length() #include "troff.h" #include "dictionary.h" @@ -10003,7 +10003,7 @@ static struct warning_category { static unsigned int lookup_warning(const char *name) { - for (unsigned int i = 0U; i < array_length(warning_table); i++) + for (unsigned int i = 0U; i < countof(warning_table); i++) if (strcmp(name, warning_table[i].name) == 0) return warning_table[i].mask; return 0U; diff --git a/src/utils/addftinfo/addftinfo.cpp b/src/utils/addftinfo/addftinfo.cpp index 1ba334b36..900e87a69 100644 --- a/src/utils/addftinfo/addftinfo.cpp +++ b/src/utils/addftinfo/addftinfo.cpp @@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #endif #include <errno.h> +#include <stdcountof.h> #include <stdlib.h> // exit(), EXIT_SUCCESS, strtol() #include "lib.h" @@ -115,7 +116,7 @@ int main(int argc, char **argv) usage("option requires argument"); size_t j; for (j = 0;; j++) { - if (j >= array_length(param_table)) + if (j >= countof(param_table)) fatal("parameter '%1' not recognized", argv[i] + 1); if (strcmp(param_table[j].name, argv[i] + 1) == 0) break; @@ -137,7 +138,7 @@ int main(int argc, char **argv) static void usage(FILE *stream) { fprintf(stream, "usage: %s", program_name); - size_t len = array_length(param_table); + size_t len = countof(param_table); for (size_t i = 0; i < len; i++) fprintf(stream, " [-%s n]", param_table[i].name); fputs(" resolution unit-width font\n", stream); -- 2.51.0