My Byte of Code Blog. Tips and observations on creating software with Objective-C, C/C++, Python, Cocoa and Boost on Mac.

Saturday, November 20, 2010

Parse CSV File with Embedded New Lines Using Boost Tokenizer and C++

In my previous post, Parse CSV File With Boost Tokenizer in C++, I have shown how to use Boost Tokenizer to parse CSV files. The algorithm expected that the file contains one record per line.

However, CSV syntax allows for quoted fields to contain embedded line breaks (quoting wikipedia - should be enough to demonstrate this example). The example code from my previous post could not handle those embedded breaks.

As someone asked in comments if the code can handle them, here is a code that fixes that problem and handles embedded line breaks in quoted fields.

Wednesday, February 24, 2010

Parse CSV File With Boost Tokenizer In C++

Often data required by application are available in CSV formatted files. In c++ it is easy to read file line by line. All that is left is to extract fields from each line and insert them into datastructure stored in memory. Boost Tokenizer is a package that provides a way to easilly break a string or sequence of characters into sequence of tokens, and provides standard iterator interface to traverse the tokens. I will show simple way of using Boost Tokenizer to parse data from CSV file.

Tuesday, February 23, 2010

My FizzBuzz Solution In C++

If you are not sure if I can write simple code here is my FizzBuzz c++ solution:

#include <iostream>
using namespace std;

int main()
{
   for (int i = 1; i < 101; ++i)
   {
       if      (i % 3 == 0 && i % 5 == 0) cout << "FizzBuzz";
       else if (i % 3 == 0)               cout << "Fizz";
       else if (i % 5 == 0)               cout << "Buzz";
       else                               cout << i;
       cout << endl;
   }
}

Monday, February 08, 2010

Representing bidirectional relationship with boost::bimap

In C++, std maps and hash-maps are perfect choice for storing key-value pairs of elements. We use key to lookup related value(s). Quite often I manipulate key-value relationships where both keys and values are unique and usually there is a one-to-one relationships between keys and values. Example of this is a translation tables between two sets of symbols (strings).

I ended up coding my own class with two internal maps, or hash-maps, plus getters and setters to populate and access values for two way lookups.

A while ago I stumbled upon boost's bimap. Here is an example how to use it.

Friday, February 05, 2010

Use vim to generate and execute bash commands

I have been using vim almost exclusively for file editing on any operating system that I use, whether Mac OS X, Linux or Windows. Here is an example how to use vim to build and execute bash commands using vim's regular expressions.

Saturday, January 16, 2010

Markdown and Pygments to syntax highlight code in Blogger

I wanted to use Markdown for posting on Blogger. As I am planning to post source code examples I wanted to be able to do syntax highlighting of the code.

I've found python-markdown which is Markdown implementation in Python. Furthermore, some searching returned Pygments as an option to generate html syntax highlighting from plain source code snippets and I was considering to use Beautiful Soup to extract relevant code blocks from processed Markdown output and replace them with syntax formatted html. There is a python-markdown extension CodeHilite which adds code syntax highlighting support to Python-Markdown using Pygments.

Thursday, January 07, 2010

About My Byte of Code Blog

Blog about developing beautiful, cool and functional software.

Posts will mostly deal with Objective-C, Cocoa, C/C++, possibly some Python and other languages, frameworks and technologies that one may come across.

I will try to document my Mac OS and iPhone development experience but may occasionally mention useful stuff I came across developing Linux and Windows network applications or any tools or code snippets required to earn living.

I am hoping for having enough time to post great deal of code examples.