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

Sunday, January 02, 2011

Build Xcode project from within Vim

To be able to compile Xcode project from Vim with :mak I set makeprg option to use xcodebuild instead of Makefile.

I set makeprg for objective-c files in local ftplugin for objective-c ~/.vim/ftplugin/objc.vim:

set makeprg=xcodebuild\ -activetarget\ -activeconfiguration

Now when I compile from Vim with :mak, the current active build and configuration gets compiled and you can iterate in Vim over any errors or warnings the way you would using Makefile:

:cope - Open a window to show the current list of errors
:cn   - Skip to next error in the list
:cp   - Skip to previous error in the list
:clo  - Close current window

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.