[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[groff] 11/17: [grops]: Slightly refactor (array_length, sizeof).
From: |
G. Branden Robinson |
Subject: |
[groff] 11/17: [grops]: Slightly refactor (array_length, sizeof). |
Date: |
Sat, 17 Feb 2024 22:08:44 -0500 (EST) |
gbranden pushed a commit to branch master
in repository groff.
commit 43f517e96bc722838df86b260bc8307baeecf21c
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Sat Feb 17 17:26:25 2024 -0600
[grops]: Slightly refactor (array_length, sizeof).
* src/devices/grops/ps.cpp: Promote type definition to global scope and
deanonymize it as `proc_table_t` so we can use it with `array_length`
template.
* src/devices/grops/psrm.cpp: Promote `comment_info` type definition to
global scope so we can use it with `array_length` template.
* src/devices/grops/ps.cpp (ps_printer::special):
* src/devices/grops/psrm.cpp: Slightly refactor; migrate from `sizeof`
and division operators to groff's `array_length` template function.
(resource_manager::read_resource_arg, parse_extensions)
(resource_manager::process_file)
(resource_manager::print_extensions_comment): Switch types of loop
indices iterating over these objects from `int`s of various signedness
to `size_t`.
(resource_manager::process_file)
(resource_manager::read_download_file): Use `sizeof` operator
idiomatically; it is an operator, not a function, and should be
followed by parentheses only when making it operate on a type cast to
get the size of a non-lvalue.
---
ChangeLog | 25 +++++++++++++++++++++++++
src/devices/grops/ps.cpp | 15 +++++++++------
src/devices/grops/psrm.cpp | 34 +++++++++++++++++-----------------
3 files changed, 51 insertions(+), 23 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 39b8b473a..e221452d4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,28 @@
+2024-02-17 G. Branden Robinson <g.branden.robinson@gmail.com>
+
+ * src/devices/grops/ps.cpp: Promote type definition to global
+ scope and deanonymize it as `proc_table_t` so we can use it with
+ `array_length` template.
+
+ * src/devices/grops/psrm.cpp: Promote `comment_info` type
+ definition to global scope so we can use it with `array_length`
+ template.
+
+ * src/devices/grops/ps.cpp (ps_printer::special):
+ * src/devices/grops/psrm.cpp: Slightly refactor; migrate from
+ `sizeof` and division operators to groff's `array_length`
+ template function.
+ (resource_manager::read_resource_arg, parse_extensions)
+ (resource_manager::process_file)
+ (resource_manager::print_extensions_comment): Switch types of
+ loop indices iterating over these objects from `int`s of various
+ signedness to `size_t`.
+ (resource_manager::process_file)
+ (resource_manager::read_download_file): Use `sizeof` operator
+ idiomatically; it is an operator, not a function, and should be
+ followed by parentheses only when making it operate on a type
+ cast to get the size of a non-lvalue.
+
2024-02-17 G. Branden Robinson <g.branden.robinson@gmail.com>
* tmac/pdf.tmac (pdfbookmark): Drop unused register storing
diff --git a/src/devices/grops/ps.cpp b/src/devices/grops/ps.cpp
index e047fa85c..18fff9065 100644
--- a/src/devices/grops/ps.cpp
+++ b/src/devices/grops/ps.cpp
@@ -1523,15 +1523,18 @@ ps_printer::~ps_printer()
fclose(tempfp);
}
+typedef void (ps_printer::*SPECIAL_PROCP)(char *, const environment *);
+
+struct proc_table_t {
+ const char *name;
+ SPECIAL_PROCP proc;
+};
+
void ps_printer::special(char *arg, const environment *env, char type)
{
if (type != 'p')
return;
- typedef void (ps_printer::*SPECIAL_PROCP)(char *, const environment *);
- static const struct {
- const char *name;
- SPECIAL_PROCP proc;
- } proc_table[] = {
+ static const proc_table_t proc_table[] = {
{ "exec", &ps_printer::do_exec },
{ "def", &ps_printer::do_def },
{ "mdef", &ps_printer::do_mdef },
@@ -1560,7 +1563,7 @@ void ps_printer::special(char *arg, const environment
*env, char type)
error("empty X command ignored");
return;
}
- for (unsigned int i = 0; i < sizeof(proc_table)/sizeof(proc_table[0]); i++)
+ for (size_t i = 0; i < array_length(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 226375085..5cf9eeae8 100644
--- a/src/devices/grops/psrm.cpp
+++ b/src/devices/grops/psrm.cpp
@@ -84,7 +84,7 @@ const char *extension_table[] = {
"FileSystem",
};
-const int NEXTENSIONS = sizeof(extension_table)/sizeof(extension_table[0]);
+const size_t NEXTENSIONS = array_length(extension_table);
// this must stay in sync with 'resource_type' in 'ps.h'
const char *resource_table[] = {
@@ -97,7 +97,7 @@ const char *resource_table[] = {
"pattern",
};
-const int NRESOURCES = sizeof(resource_table)/sizeof(resource_table[0]);
+const size_t NRESOURCES = array_length(resource_table);
static int read_uint_arg(const char **pp, unsigned *res)
{
@@ -576,7 +576,7 @@ resource *resource_manager::read_resource_arg(const char
**ptr)
error("missing resource type");
return 0;
}
- int ri;
+ size_t ri;
for (ri = 0; ri < NRESOURCES; ri++)
if (strlen(resource_table[ri]) == size_t(*ptr - name)
&& strncasecmp(resource_table[ri], name, *ptr - name) == 0)
@@ -932,7 +932,7 @@ static unsigned parse_extensions(const char *ptr)
do {
++ptr;
} while (*ptr != '\0' && !white_space(*ptr));
- int i;
+ size_t i;
for (i = 0; i < NEXTENSIONS; i++)
if (strlen(extension_table[i]) == size_t(ptr - name)
&& memcmp(extension_table[i], name, ptr - name) == 0) {
@@ -955,6 +955,11 @@ static unsigned parse_extensions(const char *ptr)
// BeginResource: file should be postponed till we have seen
// the first line of the file.
+struct comment_info {
+ const char *name;
+ int (resource_manager::*proc)(const char *, int, FILE *, FILE *);
+};
+
void resource_manager::process_file(int rank, FILE *fp,
const char *filename, FILE *outfp)
{
@@ -972,12 +977,7 @@ void resource_manager::process_file(int rank, FILE *fp,
"DocumentSuppliedFiles:",
};
- const int NHEADER_COMMENTS = sizeof(header_comment_table)
- / sizeof(header_comment_table[0]);
- struct comment_info {
- const char *name;
- int (resource_manager::*proc)(const char *, int, FILE *, FILE *);
- };
+ const size_t NHEADER_COMMENTS = array_length(header_comment_table);
static const comment_info comment_table[] = {
{ "BeginResource:", &resource_manager::do_begin_resource },
@@ -997,8 +997,8 @@ void resource_manager::process_file(int rank, FILE *fp,
{ "BeginData:", &resource_manager::do_begin_data },
{ "BeginBinary:", &resource_manager::do_begin_binary },
};
-
- const int NCOMMENTS = sizeof(comment_table)/sizeof(comment_table[0]);
+
+ const size_t NCOMMENTS = array_length(comment_table);
string buf;
int saved_lineno = current_lineno;
const char *saved_filename = current_filename;
@@ -1009,8 +1009,8 @@ void resource_manager::process_file(int rank, FILE *fp,
current_lineno = saved_lineno;
return;
}
- if ((size_t)buf.length() < sizeof(PS_MAGIC)
- || memcmp(buf.contents(), PS_MAGIC, sizeof(PS_MAGIC) - 1) != 0) {
+ if ((size_t)buf.length() < sizeof PS_MAGIC
+ || memcmp(buf.contents(), PS_MAGIC, (sizeof PS_MAGIC) - 1) != 0) {
if (outfp) {
do {
if (!(broken_flags & STRIP_PERCENT_BANG)
@@ -1033,7 +1033,7 @@ void resource_manager::process_file(int rank, FILE *fp,
if (buf[0] == '%') {
if (buf[1] == '%') {
const char *ptr;
- int i;
+ size_t i;
for (i = 0; i < NCOMMENTS; i++)
if ((ptr = matches_comment(buf, comment_table[i].name))) {
copy_this_line
@@ -1095,7 +1095,7 @@ void resource_manager::read_download_file()
fatal("failed to open 'download' file: %1", strerror(errno));
char buf[512];
int lineno = 0;
- while (fgets(buf, sizeof(buf), fp)) {
+ while (fgets(buf, sizeof buf, fp)) {
lineno++;
char *p = strtok(buf, " \t\r\n");
if (p == 0 /* nullptr */ || *p == '#')
@@ -1178,7 +1178,7 @@ void resource_manager::print_extensions_comment(FILE
*outfp)
{
if (extensions) {
fputs("%%Extensions:", outfp);
- for (int i = 0; i < NEXTENSIONS; i++)
+ for (size_t i = 0; i < NEXTENSIONS; i++)
if (extensions & (1 << i)) {
putc(' ', outfp);
fputs(extension_table[i], outfp);
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [groff] 11/17: [grops]: Slightly refactor (array_length, sizeof).,
G. Branden Robinson <=