Technology

A better output_iterator

I was using the std::stream_iterator, for example:

    std::vector    vals;
    vals.push_back (12);
    vals.push_back (22);
    vals.push_back (5);
    vals.push_back (30);
    // The std::ostream_iterator appends the ","
    std::ostringstream  str3;
    std::copy (vals.begin(), vals.end(), std::ostream_iterator (str3, ","));
    CPPUNIT_ASSERT_EQUAL (std::string ("12,22,5,30,"), str3.str());

It has the “,” after the last item.
So here’s an implementation that doesn’t append the seperator after the last one:

#ifndef INCLUDED_OUTITER_H
#define INCLUDED_OUTITER_H
#include 
/**
 * A replacement for std::ostream_iterator that doesn't put the
 * seperator after the last item.
 */
template >
class outiter : public std::iterator
{
public:
    typedef CharType            char_type;
    typedef Traits          traits_type;
    typedef std::basic_ostream    ostream_type;
    /// Initialize from a stream
    outiter (ostream_type &stream)
    : m_stream (&stream),
      m_string (0),
      m_started (false)
    {}
    /// Copy constructor
    outiter (const outiter ©)
    : m_stream (copy.m_stream),
      m_string (copy.m_string),
      m_started (copy.m_started)
    {}
    /// Initialize from a stream and the seperator
    outiter (ostream_type &stream, const CharType *str)
    : m_stream (&stream),
      m_string (str),
      m_started (false)
    {}
    /// Assignment actually does the output
    outiter &
    operator=(const Type &value)
    {
    if (!m_started)
    {
        m_started = true;
    }
    else if (m_string)
    {
            (*m_stream) << m_string;
    }
    (*m_stream) << value;
    return *this;
    }
    /// Just return a reference to this
    outiter &
    operator*()
    {
    return *this;
    }
    /// Just return a reference to this
    outiter &
    operator++()
    {
    return *this;
    }
    /// Just return a reference to this
    outiter &
    operator++(int)
    {
    return *this;
    }
private:
    /// The stream to write to
    ostream_type *      m_stream;
    /// The seperator (may be NULL)
    const char_type *       m_string;
    /// Flag to indicate if we've output anything
    bool            m_started;
};
#endif /* INCLUDED_OUTITER_H */

And here's the corresponding code to use it:

    std::ostringstream  str;
    std::copy (vals.begin(), vals.end(), outiter (str, ","));
    CPPUNIT_ASSERT_EQUAL (std::string ("12,22,5,30"), str.str());

Closer to a new car?

It’s one step closer to happening! Getting a new car based on a trade in might actually happen. It hasn’t passed the House yet (much less the Senate) but the leadership of both parties agree on doing it.
From House Reaches a Deal on ‘Cash for Clunkers’ Program:

Under the House plan, a car trade-in that improves fuel efficiency by at least 10 miles per gallon would qualify for a $4,500 voucher, as would the trade-in of a small truck that improves efficiency by 5 miles per gallon. The new vehicle must have a minimum fuel efficiency rating of 22 miles per gallon for cars and 18 miles per gallon for small trucks.

Setting the timezone

I’ve gotten pretty careful about keeping time in UTC and then converting it to localtime for the user to understand. For the first time, I actually had to find the localtime in a non-local timezone. It’s ugly. It seems you have to mess with the TZ environment variable. Here’s what I wrote:

#include 
#include 
/**
 * This modifies the TZ environment variable and calls
 * tzset() to update the global timezone flags.  The
 * previous setting of TZ is returned.  An empty timezone
 * implies the system default value.
 *
 * Changing this affects all threads in this process.  See
 * zdump(1) to check timezone names.
 *
 * @param tzname Name of the new timezone ("US/Eastern").
 * @return The old value of TZ.
 */
std::string changeTimeZone (const std::string &tzname)
{
    char *tzptr = getenv ("TZ");
    std::string  tzold;
    if (tzptr)
        tzold = tzptr;
    if (tzname.empty())
        unsetenv ("TZ");
    else
        setenv ("TZ", tzname.c_str(), 1);
    tzset();
    return tzold;
}

And here’s a code fragment that uses it:

std::string oldzone = changeTimeZone("US/Pacific");
time_t  seconds = ::time(0);
struct tm   tm_time;
localtime_r (&seconds, tm_time);
changeTimeZone(oldzone);

Cars and Batteries

Fascinating profile of
Shai Agassi who’s introducing replaceable batteries for the auto industry. Part of his theory is that selling the “energy” is very profitable.
>But Agassi realized he needed one more breakthrough: some way to rapidly charge a vehicle. No drivers, he knew, will tolerate a two-hour wait to recharge when they’re on a 500-mile haul. Then one day, he and an automotive engineer were chewing over an impractical method for quickly replenishing batteries. The engineer wondered aloud: Wouldn’t the fastest way to charge an electric car be to simply replace the battery?

4C6C9BC9-7284-4EB2-910B-F0AB01834593.jpg