From b577b0655df0bcd88d28f45dfbd9ae2ce7f85c2c Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Wed, 10 Feb 2010 11:57:15 +0000 Subject: [PATCH] Abstract writing comments into NYTP_write_comment(). Sadly to keep a clean interface, this requires removing an elegant hack that took advantage of ctime() returning a string terminated with '\n'. --- FileHandle.h | 2 ++ FileHandle.xs | 29 +++++++++++++++++++++++++++++ NYTProf.xs | 11 +++++++---- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/FileHandle.h b/FileHandle.h index fbbb943..067026c 100644 --- a/FileHandle.h +++ b/FileHandle.h @@ -44,3 +44,5 @@ const char *NYTP_type_of_offset(NYTP_file file); #endif void NYTProf_croak_if_not_stdio(NYTP_file file, const char *function); + +size_t NYTP_write_comment(NYTP_file ofile, const char *format, ...); diff --git a/FileHandle.xs b/FileHandle.xs index 1578ea3..cda0759 100644 --- a/FileHandle.xs +++ b/FileHandle.xs @@ -621,6 +621,35 @@ NYTP_close(NYTP_file file, int discard) { return fclose(raw_file) == 0 ? 0 : errno; } +size_t +NYTP_write_comment(NYTP_file ofile, const char *format, ...) { + size_t retval; + size_t retval2; + va_list args; + + retval = NYTP_write(ofile, "#", 1); + if (retval != 1) + return retval; + + va_start(args, format); + + if(strEQ(format, "%s")) { + const char * const s = va_arg(args, char*); + STRLEN len = strlen(s); + retval = NYTP_write(ofile, s, len); + } else { + CROAK_IF_NOT_STDIO(ofile, "NYTP_printf"); + retval = vfprintf(ofile->file, format, args); + } + va_end(args); + + retval2 = NYTP_write(ofile, "\n", 1); + if (retval2 != 1) + return retval2; + + return retval + 2; +} + MODULE = Devel::NYTProf::FileHandle PACKAGE = Devel::NYTProf::FileHandle PREFIX = NYTP_ PROTOTYPES: DISABLE diff --git a/NYTProf.xs b/NYTProf.xs index ad2fb07..3f10268 100644 --- a/NYTProf.xs +++ b/NYTProf.xs @@ -433,6 +433,9 @@ output_header(pTHX) { SV *sv; time_t basetime = PL_basetime; + /* This comes back with a terminating \n, and we don't want that. */ + const char *const basetime_str = ctime(&basetime); + const STRLEN basetime_str_len = strlen(basetime_str); assert(out != NULL); /* File header with "magic" string, with file major and minor version */ @@ -441,8 +444,8 @@ output_header(pTHX) * comments start with '#', end with '\n', and are discarded * attributes start with ':', a word, '=', then the value, then '\n' */ - NYTP_printf(out, "# Perl profile database. Generated by Devel::NYTProf on %s", - ctime(&basetime)); /* uses \n from ctime to terminate line */ + NYTP_write_comment(out, "Perl profile database. Generated by Devel::NYTProf on %.*s", + (int)basetime_str_len - 1, basetime_str); /* XXX add options, $0, etc, but beware of embedded newlines */ /* XXX would be good to adopt a proper charset & escaping for these */ @@ -466,8 +469,8 @@ output_header(pTHX) #ifdef HAS_ZLIB if (compression_level) { const unsigned char tag = NYTP_TAG_START_DEFLATE; - NYTP_printf(out, "# Compressed at level %d with zlib %s\n", - compression_level, zlibVersion()); + NYTP_write_comment(out, "Compressed at level %d with zlib %s", + compression_level, zlibVersion()); NYTP_write(out, &tag, sizeof(tag)); NYTP_start_deflate(out, compression_level); } -- 1.6.0