Category Archives: Programming

How to get file names under a folder in C++

As per my earlier post How to transform between a double date-time and std::string in C++, some things are surprisingly hard in C++, especially compared to writing in a .NET language. I recently tracked down this useful snippet of code on the internet in order to recursively build a list of files matching some file extension under a folder. This only compiles on Windows – if you want something cross-platform, look at boost (I wanted to avoid bringing the boost filesystem library into my project as yet another dependency).

void accumulate_files(
  const std::string& folder,
  const std::string& extension,
  std::vector<std::string>& file_names )
{
  std::ostringstream oss;
  oss << folder << "\\";
  std::string search_path = oss.str();

  WIN32_FIND_DATA fd;
  HANDLE hFind = ::FindFirstFile( search_path.c_str(), &fd );
  if( hFind != INVALID_HANDLE_VALUE)
  {
    do
    {
      std::string file_name = fd.cFileName;
      std::ostringstream full_file_name;
      full_file_name << folder << "\\" << file_name;

      if ( boost::algorithm::ends_with( file_name, extension ) )
      {
        file_names.push_back( file_name );
      }
      else if ( file_name != "." && file_name != ".." && !file_name.empty() )
      {
        // Recursively call into next directory
        accumulate_files( full_file_name.c_str(), extension, file_names );
      }
    }
    while( ::FindNextFile(hFind, &fd) );

    ::FindClose(hFind);
  }
}

For production code, you’d also want to wrap the file handle in a smart pointer to ensure it gets closed properly for exception safety.

4 Comments

Filed under C++, C++ Code, Programming

cpplinq – functional style for C++ using lambdas

I’ve just tried out CppLinq, a C++11 library that brings LINQ-style syntax into scope for C++ programmers that are used to writing code in a functional style. I’ve been using C++11 lambdas with STL algorithms like foreach, transform, accumulate – but this syntax using where, for_each, sum and ‘>>’ to chain commands together is so much neater. In fact, it brings C++11 style very close to the succinct F# piping style that is so popular.

To use cpplinq, you can just download a single header file and include it in your code. Awesome – having just battled for hours to use some other 3rd party library which required multiple libs, source files and compiler switches, this is so easy by comparison.

This Dr Dobbs article has several code examples which act as an simple tutorial.

1 Comment

Filed under C++, Programming

How to emulate C++11’s ‘enum class’ in C++03

One of many neat features in C++11 is the introduction of ‘enum class’ – this addresses problems arising from enum declarations such as scoping (the enum cases leak in the same scope as the enum type) and implicit conversion to integer. See Stroustrup’s C++11 FAQ for more details.

However, in the papers that motivated the new language feature (N1513 and N2347) they discuss the current workarounds for C++03. One such workaround is to define a class to represent the enum type – it’s much more verbose than the new C++11 feature, but it solves the scoping and conversion issues.

// Header file
class Weekday
{
private:
  // Note that the private enum cases have underscores to differentiate
  // from the public cases
  typedef enum Weekday_ { Mon_, Tues_, Wed_, Thurs_, Fri_, Sat_, Sun_ };
  Weekday_ value_;

public:
  static const Weekday Mon, Tues, Wed, Thurs, Fri, Sat, Sun;
  explicit Weekday( const Weekday_& value ) : value_( value ){}

  bool operator<( const Weekday& rhs ) const { return this->value_ < rhs.value_; }
  bool operator==( const Weekday& rhs ) const { return this->value_ == rhs.value_; }
  bool operator!=( const Weekday& rhs ) const { return !(this->operator==(rhs)); }
};

// Source file
// Definitions for the public Weekday instances 
// in terms of the private enum
const Weekday Weekday::Mon( Mon_ );
const Weekday Weekday::Tues( Tues_ );
const Weekday Weekday::Wed( Wed_ );
const Weekday Weekday::Thurs( Thurs_ );
const Weekday Weekday::Fri( Fri_ );
const Weekday Weekday::Sat( Sat_ );
const Weekday Weekday::Sun( Sun_ );

That’s it – I’ve been using this recently and it works pretty well. I also added a “to_int()” member and a static “from_int()” member to explicitly convert bewteen the enum class and integers – the implementation is simply a switch over the enum cases.

8 Comments

Filed under C++, C++ Code, Programming

How to transform between a double date-time and std::string in C++

One of the attractions of writing software using the .NET framework is the wealth of support for doing simple things like translating between different data formats. These tasks are typically much harder to achieve in C++ due to the lack of an equivalent framework. One such task that I came across the other day is that date-times are often represented by a double in Windows, where the integer part represents the date since some epoch and the fractional part is the time as a fraction of 24 hours. Even with access to the Boost library, I still had to do some work to produce a simple transformation in C++.

#include <boost/format.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

typedef double DateTime;

namespace
{
  boost::gregorian::date parse_date( DateTime date_time )
  {
      boost::gregorian::date dt = boost::date_time::parse_date<boost::gregorian::date>( "1899-12-30", boost::date_time::ymd_order_iso );
      dt += boost::gregorian::date_duration( static_cast<long>( floor(date_time) ) );
  }

  boost::posix_time::time_duration parse_time( DateTime date_time )
  {
    double fractionalDay = date_time - floor(date_time);
    long milliseconds = static_cast<long>( floor( fractionalDay * 24.0 * 60.0 * 60.0 * 1000.0 + 0.5) );
    return boost::posix_time::milliseconds( milliseconds );
  }
}

std::string to_date_string( DateTime date_time )
{
  boost::gregorian::date dt = parse_date( date_time );
  return (boost::format( "%4-%02d-%02d" ) % dt.year() % dt.month().as_number() % dt.day().as_number()).str();
}

DateTime from_date_string( const std::string& value )
{
  boost::gregorian::date epoch = boost::date_time::parse_date<boost::gregorian::date>( "1899-12-30", boost::date_time::ymd_order_iso);
  boost::gregorian::date dt = boost::date_time::parse_date<boost::gregorian::date>( value, boost::date_time::ymd_order_iso);

  boost::gregorian::date_duration diff = dt - epoch;
  return diff.days();
}

std::string to_date_time_string( DateTime date_time )
{
  boost::gregorian::date date_part = parse_date( date_time );
  boost::posix_time::time_duration time_part = parse_time( date_time );

  long long fractional_seconds = time_part.fractional_seconds();
  boost::date_time::time_resolutions resolution = time_part.resolution();
  if ( resolution == boost::date_time::micro )
  {
    fractional_seconds /= 1000;
  } 
  else
  {
    if (resolution != boost::date_time::milli)
      throw std::logic_error( "Unexpected time resolution" );
  }

  return (boost::format( "%d-%02d-%02d %02d:%02d:%02d.%03d" )
    % date_part.Year() % date_part.month().as_number() % date_part.day().as_number()
    % time_part.hours() % time_part.minutes() % time_part.seconds() % fractional_seconds ).str();
}

DateTime from_date_time_string( const std::string& value )
{
  DateTime date = from_date_string( value );
 
  boost::posix_time::ptime t = boost::posix_time::time_from_string( value );
  double milliseconds = static_cast<double>(t.time_of_day().total_milliseconds());

  return date + (milliseconds / 24.0 / 60.0 / 60.0 / 1000.0);
}

Please comment if you know a more straight-forward way to achieve this transformation, especially using Boost. Syntactically, the code could be simplified using C++11 auto, but I’ve spelt out the types explicitly throughout because I found it helpful to see which parts of the boost library are being used.

1 Comment

Filed under C++, C++ Code, Programming

Agile and cargo cults

Thought provoking blog post from Mike Hadlow on the way Agile is often hijacked by non-technical managers with little talent.

Somehow, over the decade or so since the original agile manifesto, agile has come to mean ‘management agile’. It’s been captured by management consultants and distilled as a small set of non-technical rituals that emerged from the much larger, richer, but often deeply technical set of agile practices.

I agree with this from one of the many comments:

The most successful teams I have seen made use of “tech leads” who were engineers capable of running a project.

Leave a comment

Filed under Programming

ACCU Meetup: Machine Learning for Fun and Profit

I went to the ACCU London Meet-up on 20th March: Burkhard Kloss on Machine Learning for Fun and Profit.

The core message was that Machine learning is an easy technique to invoke these days, especially with 3rd party libraries on hand like Numpy, Sci-kit Learn. The speaker has used these from Python and demonstrated programs to classify the XOR problem and a data set on Iris flowers dating from Victorian times.

Leave a comment

Filed under Meetup, Programming

C++ Quiz

This C++ quiz is quite fun – not all of the questions require C++ 11, but some do.

Leave a comment

Filed under C++, Programming

Surge in popularity of F#

InfoWorld reports that F# is becoming ever more popular. It has risen from 69th to 12th in the Tiobe Programming Community Index.

I’ve been writing F# code for over 3 years and I get the appeal – there are some algorithms that are so much easier to implement in F# than in C++ (particularly when they can be neatly expressed with the additional data structures that F# provides, like discriminated unions).

Leave a comment

Filed under Programming

Apple Security Bug

Nice analysis of the Apple security bug on Slate.

Preventing bugs like these is one of the biggest challenges of software engineering, and this incident should make it pretty damn clear why. A single extra line of code compromised the security of millions and millions, and no one caught it for more than a year.

The obvious question is: why no unit tests?

Leave a comment

Filed under Programming

Complaint-Driven Design

Plenty of good ideas here from Jeff Atwood. I love the passion he shows for his new venture:

Have your team and yourself start using that minimum viable product, every day, all day long. This is way more than mere software development: it’s your whole life. If you aren’t living in the software you’re building, each day, every day, all day … things are inevitably going to end in tears for everyone involved.

Now, need a customer service mindset for this approach, and a band of dedicated, cross-discipline engineers prepared to roll up their sleeves and get things done. But if you have that spirit and belief, the project can succeed.

The only thing I’ve ever seen work is getting down deep and dirty in the trenches with your users, communicating with them and cultivating relationships. That’s how you suss out the rare 10% of community feedback that is amazing and transformative. That’s how you build a community that gives a damn about what you’re doing – by caring enough to truly listen to them and making changes they care about.

Leave a comment

Filed under Programming, Soft skills