Skip to content

Commit

Permalink
tectonic: tidy up not-so-portable printf format string specifiers
Browse files Browse the repository at this point in the history
Older MSVC compilers don't like %z; add some #define logic to make things
portable.
  • Loading branch information
pkgw committed Oct 8, 2018
1 parent 28dfbe5 commit cf49f0a
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 33 deletions.
35 changes: 35 additions & 0 deletions tectonic/core-foundation.h
Expand Up @@ -100,4 +100,39 @@ typedef SSIZE_T ssize_t;
# define M_PI 3.14159265358979
#endif

/* Portability: printf format arguments for various non-core types.
*
* <inttype.h> ought to define these for most types. We use a custom one
* for size_t since older MSVC doesn't provide %z.
*/

#ifndef PRId64
# if defined(SIZEOF_LONG)
# if SIZEOF_LONG == 8
# define PRId64 "ld"
# else
# define PRId64 "lld"
# endif
# elif defined(_WIN32)
# define PRId64 "I64d"
# else
# error "unhandled compiler/platform for PRId64 definition"
# endif
#endif

#ifndef PRIdPTR
# define PRIdPTR "ld"
#endif
#ifndef PRIxPTR
# define PRIxPTR "lx"
#endif

#ifdef _WIN32
# define PRIuZ "Iu"
# define PRIXZ "IX"
#else
# define PRIuZ "zu"
# define PRIXZ "zX"
#endif

#endif /* not TECTONIC_CORE_FOUNDATION_H */
4 changes: 2 additions & 2 deletions tectonic/dpx-cmap_read.c
Expand Up @@ -89,7 +89,7 @@ ifreader_read (ifreader *reader, size_t size)
bytesrem = (size_t) reader->endptr - (size_t) reader->cursor;
if (size > reader->max) {
if (__verbose)
dpx_message("\nExtending buffer (%ld bytes)...\n", size);
dpx_message("\nExtending buffer (%"PRIuZ" bytes)...\n", size);
reader->buf = RENEW(reader->buf, size+1, unsigned char);
reader->max = size;
}
Expand All @@ -103,7 +103,7 @@ ifreader_read (ifreader *reader, size_t size)
reader->endptr += bytesread;
reader->unread -= bytesread;
if (__verbose)
dpx_message("Reading more %ld bytes (%ld bytes remains in buffer)...\n", bytesread, bytesrem);
dpx_message("Reading more %"PRIuZ" bytes (%"PRIuZ" bytes remains in buffer)...\n", bytesread, bytesrem);
}

*reader->endptr = 0;
Expand Down
12 changes: 6 additions & 6 deletions tectonic/dpx-cmap_write.c
Expand Up @@ -159,8 +159,8 @@ write_map (mapDef *mtab, size_t count,
wbuf->curptr >= wbuf->limptr ) {
char fmt_buf[32];
if (count > 100)
_tt_abort("Unexpected error....: %zu", count);
sprintf(fmt_buf, "%zu beginbfchar\n", count);
_tt_abort("Unexpected error....: %"PRIuZ, count);
sprintf(fmt_buf, "%"PRIuZ" beginbfchar\n", count);
pdf_add_stream(stream, fmt_buf, strlen(fmt_buf));
pdf_add_stream(stream,
wbuf->buf, (int) (wbuf->curptr - wbuf->buf));
Expand All @@ -175,7 +175,7 @@ write_map (mapDef *mtab, size_t count,
char fmt_buf[32];

if (count > 0) {
sprintf(fmt_buf, "%zu beginbfchar\n", count);
sprintf(fmt_buf, "%"PRIuZ" beginbfchar\n", count);
pdf_add_stream(stream, fmt_buf, strlen(fmt_buf));
pdf_add_stream(stream,
wbuf->buf, (int) (wbuf->curptr - wbuf->buf));
Expand All @@ -184,7 +184,7 @@ write_map (mapDef *mtab, size_t count,
"endbfchar\n", strlen("endbfchar\n"));
count = 0;
}
sprintf(fmt_buf, "%zu beginbfrange\n", num_blocks);
sprintf(fmt_buf, "%"PRIuZ" beginbfrange\n", num_blocks);
pdf_add_stream(stream, fmt_buf, strlen(fmt_buf));
for (i = 0; i < num_blocks; i++) {
size_t j;
Expand Down Expand Up @@ -384,8 +384,8 @@ CMap_create_stream (CMap *cmap)
if (count > 0) { /* Flush */
char fmt_buf[32];
if (count > 100)
_tt_abort("Unexpected error....: %zu", count);
sprintf(fmt_buf, "%zu beginbfchar\n", count);
_tt_abort("Unexpected error....: %"PRIuZ, count);
sprintf(fmt_buf, "%"PRIuZ" beginbfchar\n", count);
pdf_add_stream(stream, fmt_buf, strlen(fmt_buf));
pdf_add_stream(stream,
wbuf.buf, (int) (wbuf.curptr - wbuf.buf));
Expand Down
8 changes: 0 additions & 8 deletions tectonic/dpx-numbers.h
Expand Up @@ -32,14 +32,6 @@

#include "core-bridge.h"

#ifndef PRId64
# if SIZEOF_LONG == 8
# define PRId64 "ld"
# else
# define PRId64 "lld"
# endif
#endif

/* When reading numbers from binary files 1, 2, or 3 bytes are
interpreted as either signed or unsigned.
Expand Down
4 changes: 2 additions & 2 deletions tectonic/dpx-pdfdev.c
Expand Up @@ -979,7 +979,7 @@ handle_multibyte_string (struct dev_font *font,
int c;
/* Check for valid surrogate pair. */
if ((p[i] & 0xfc) != 0xd8 || i + 2 >= length || (p[i+2] & 0xfc) != 0xdc) {
dpx_warning("Invalid surrogate p[%zu]=%02X...", i, p[i]);
dpx_warning("Invalid surrogate p[%"PRIuZ"]=%02X...", i, p[i]);
return -1;
}
c = (((p[i] & 0x03) << 10) | (p[i+1] << 2) | (p[i+2] & 0x03)) + 0x100;
Expand Down Expand Up @@ -1031,7 +1031,7 @@ handle_multibyte_string (struct dev_font *font,
CMap_decode(cmap,
&inbuf, &inbytesleft, &outbuf, &outbytesleft);
if (inbytesleft != 0) {
dpx_warning("CMap conversion failed. (%zu bytes remains)", inbytesleft);
dpx_warning("CMap conversion failed. (%"PRIuZ" bytes remains)", inbytesleft);
return -1;
}
length = FORMAT_BUF_SIZE - outbytesleft;
Expand Down
2 changes: 1 addition & 1 deletion tectonic/dpx-pkfont.c
Expand Up @@ -568,7 +568,7 @@ pdf_font_load_pkfont (pdf_font *font)

pkt_ptr = NEW(pkh.pkt_len, unsigned char);
if ((bytesread = fread(pkt_ptr, 1, pkh.pkt_len, fp))!= pkh.pkt_len) {
_tt_abort("Only %zu bytes PK packet read. (expected %d bytes)",
_tt_abort("Only %"PRIuZ" bytes PK packet read. (expected %d bytes)",
bytesread, pkh.pkt_len);
}
charproc = create_pk_CharProc_stream(&pkh, charwidth, pkt_ptr, bytesread);
Expand Down
4 changes: 2 additions & 2 deletions tectonic/dpx-t1_load.c
Expand Up @@ -947,7 +947,7 @@ parse_part1 (cff_font *font, char **enc_vec,
argn = parse_svalue(start, end, &strval);
CHECK_ARGN_EQ(1);
if (strlen(strval) > TYPE1_NAME_LEN_MAX) {
dpx_warning("FontName too long: %s (%zu bytes)", strval, strlen(strval));
dpx_warning("FontName too long: %s (%"PRIuZ" bytes)", strval, strlen(strval));
strval[TYPE1_NAME_LEN_MAX] = '\0';
}
cff_set_name(font, strval);
Expand Down Expand Up @@ -1165,7 +1165,7 @@ t1_get_fontname (rust_input_handle_t handle, char *fontname)

if (parse_svalue(&start, end, &strval) == 1) {
if (strlen(strval) > TYPE1_NAME_LEN_MAX) {
dpx_warning("FontName \"%s\" too long. (%zu bytes)", strval, strlen(strval));
dpx_warning("FontName \"%s\" too long. (%"PRIuZ" bytes)", strval, strlen(strval));
strval[TYPE1_NAME_LEN_MAX] = '\0';
}
strcpy(fontname, strval);
Expand Down
6 changes: 3 additions & 3 deletions tectonic/xetex-ini.c
Expand Up @@ -433,7 +433,7 @@ swap_items (char *p, size_t nitems, size_t size)
case 1:
break; /* Nothing to do. */
default:
_tt_abort("can't swap a %zu-byte item for (un)dumping", size);
_tt_abort("can't swap a %"PRIuZ"-byte item for (un)dumping", size);
}
}
#else /* not WORDS_BIGENDIAN */
Expand All @@ -452,7 +452,7 @@ do_dump (char *p, size_t item_size, size_t nitems, rust_output_handle_t out_file

ssize_t r = ttstub_output_write (out_file, p, item_size * nitems);
if (r < 0 || (size_t) r != item_size * nitems)
_tt_abort ("could not write %zu %zu-byte item(s) to %s",
_tt_abort ("could not write %"PRIuZ" %"PRIuZ"-byte item(s) to %s",
nitems, item_size, name_of_file);

/* Have to restore the old contents of memory, since some of it might
Expand All @@ -468,7 +468,7 @@ do_undump (char *p, size_t item_size, size_t nitems, rust_input_handle_t in_file
{
ssize_t r = ttstub_input_read (in_file, p, item_size * nitems);
if (r < 0 || (size_t) r != item_size * nitems)
_tt_abort("could not undump %zu %zu-byte item(s) from %s",
_tt_abort("could not undump %"PRIuZ" %"PRIuZ"-byte item(s) from %s",
nitems, item_size, name_of_file);

swap_items (p, nitems, item_size);
Expand Down
9 changes: 0 additions & 9 deletions tectonic/xetex-xetexd.h
Expand Up @@ -29,15 +29,6 @@
#define xcalloc_array(type, size) (xcalloc(size + 1, sizeof(type)))
#define xrealloc_array(ptr, type, size) ((type*) xrealloc(ptr, (size + 1) * sizeof(type)))

/* Declarations for the routines we provide ourselves in lib/. */

#ifndef PRIdPTR
#define PRIdPTR "ld"
#endif
#ifndef PRIxPTR
#define PRIxPTR "lx"
#endif

/*11:*/
#define MIN_TRIE_OP 0
#define TRIE_OP_SIZE 35111L
Expand Down

0 comments on commit cf49f0a

Please sign in to comment.