Revision: 1096
Author: [email protected]
Date: Mon Mar  8 01:45:27 2010
Log: 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'.
http://code.google.com/p/perl-devel-nytprof/source/detail?r=1096

Modified:
 /trunk/FileHandle.h
 /trunk/FileHandle.xs
 /trunk/NYTProf.xs

=======================================
--- /trunk/FileHandle.h Mon Feb  8 06:49:33 2010
+++ /trunk/FileHandle.h Mon Mar  8 01:45:27 2010
@@ -44,3 +44,5 @@
 #endif

 void NYTProf_croak_if_not_stdio(NYTP_file file, const char *function);
+
+size_t NYTP_write_comment(NYTP_file ofile, const char *format, ...);
=======================================
--- /trunk/FileHandle.xs        Wed Feb 17 08:35:45 2010
+++ /trunk/FileHandle.xs        Mon Mar  8 01:45:27 2010
@@ -620,6 +620,35 @@
     }
     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_

=======================================
--- /trunk/NYTProf.xs   Fri Mar  5 02:20:14 2010
+++ /trunk/NYTProf.xs   Mon Mar  8 01:45:27 2010
@@ -434,6 +434,9 @@
 {
     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 */
@@ -442,8 +445,8 @@
      * 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 */
@@ -462,8 +465,8 @@
 #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);
     }

--
You've received this message because you are subscribed to
the Devel::NYTProf Development User group.

Group hosted at:  http://groups.google.com/group/develnytprof-dev
Project hosted at:  http://perl-devel-nytprof.googlecode.com
CPAN distribution:  http://search.cpan.org/dist/Devel-NYTProf

To post, email:  [email protected]
To unsubscribe, email:  [email protected]

Reply via email to