> I'm currently using:
>
> php_stream_write(stream, Z_STRVAL_P(*data), 30);
>
> As the string is terminated with a zero-byte, it already works, but it
> would be better to fill the unused space with zero-bytes.
>
Actually, that'll also open the door to a potential segfault since you may
overrun the allocated block of memory.

Try:

/* Not binary safe, but depending on your data that may be okay */
if (Z_STRLEN_PP(data) > 30) {
  php_stream_write(stream, Z_STRVAL_PP(data), 30);
} else {
  php_stream_printf(stream TSRMLS_CC, "%-30s", Z_STRVAL_PP(data));
}

or:

/* This is binary safe */
if (Z_STRLEN_PP(data) > 30) {
  php_stream_write(stream, Z_STRVAL_PP(data), 30);
} else {
  php_stream_write(stream, Z_STRVAL_PP(data), Z_STRLEN_PP(data));
  /* That's 30 spaces in that string constant */
  php_stream_write(stream, "                              ", 30 -
Z_STRLEN_PP(data));
}

-Sara

P.S. - As you might have noticed in the above code  Z_STRVAL_PP(data) is the
same as Z_STRVAL_P(*data).  The same is true of the Z_LVAL_P(*data) you used
in your previous question.   I don't recall if using the _PP is dictated by
the coding standards, but it's certainly in line with established
convention.  You'll probably find your code becomes more readable by others
who are accostomed to the PHP Core if you stick to the secondary indirection
macros.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to