Monday, September 20, 2010

Vim Recording Example

Problem:

Given a file with exactly two lines for each IP (for a defined set of IPs) as follows:

<router ip="a.b.c.d" id="">
<router ip="p.q.r.s" id="">
a.b.c.d,23
p.q.r.s,24
...
...

Need to fill the xml attribute 'id' with matching value.

Vim Recording to rescue:

1. Go to a.b.c.d
2. Record in register 'c'
qc
3. search for comma
/\<,\>
4. Go to next word.
w
5. Copy the text after comma (which is the value of id) to buffer 'x':
"xy$
6. Step back to comma
b
5. Copy the text before comma to buffer 'z':
"zy0
6. Search for z-buffer-contents in file.
/\
To copy z buffer contents:
Ctrl-R z
7. Search for next instance
n
8. copy the value of id from x register to id=""
/id="
3w
"xp
9. search for next instance. (original instance)
n
10. Next line.
j
11. beginning of line.
0

Apply to 1000 lines:
1000@c

Done.

Tuesday, June 15, 2010

Changing parsed-variable order in boost.spirit

Problem Statement:

You have defined a structure in which you want to save result of a spirit's rule. There is another rule in your grammar which has exactly same data fields as the above rule but in a different order. How do you reuse your structure?


For this example lets use following structure:

struct Date
{
int month;
int day;
int year;
};

Now we convert it into a boost fusion sequence

BOOST_FUSION_ADAPT_STRUCT(
Date,
(int, month)
(int, day)
(int, year)
)

This is US date format.

we define this rule to parse US dates:
date_rule %= int_ >> lit(':') >> int_ >> lit(':') >> int_;

and we put the parsed result in our Date structure:
Date date;
phrase_parse(begin, end, date_rule, space, date);

now how to parse UK format dates? (day:month:year) ?

there are two options;
1) use Date structure to parse and swap the fields around.
2) define a new rule which does the swapping by itself.

here I describe option 2.

uk_date_format = int_ [at_c<1>(_val) = _1]
>> lit (':') >> int_[at_c<0>(_val) = _1]
>> lit (':') >> int_[at_c<2>(_val) = _1];

phrase_parse(begin, end, uk_date_format, space, date);

(at_c is used to access phoenix tuple.)

Tuesday, May 18, 2010

Wednesday, May 12, 2010

building gcc4.5 on linux

1) Download gcc sources:
svn co svn://gcc.gnu.org/svn/gcc/branches/gcc-4_5-branch
2) Download other resources required:
wget ftp://gcc.gnu.org:21/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2
wget ftp://gcc.gnu.org:21/pub/gcc/infrastructure/mpc-0.8.1.tar.gz
wget ftp://gcc.gnu.org:21/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2
wget ftp://ftp.gnu.org/gnu/autoconf/autoconf-2.64.tar.gz (might be needed)
3) build gmp, mpfr and mpc:
a) cd gmp-4.3.2
./configure --prefix=/usr/local
make
sudo make install
b) cd mpfr-2.4.2
./configure --prefix=/usr/local
make
sudo make install
c) cd mpc-0.8.1
./configure --prefix=/usr/local
make
sudo make install
NOTE: if it fails with 'relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC'
Make sure that you are using /usr/local/lib/libmpfr.so

4) cd gcc-4_5-branch
a) autoconf (it may ask for upgrading to 2.64)
b) LD_RUN_PATH=/usr/local/lib
export LD_RUN_PATH
LDFLAGS="-L/usr/local/lib -R/usr/local/lib"
export LDFLAGS
c) configure gcc:
./configure --prefix=/usr/local --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++ --disable-dssi --enable-plugin --with-cpu=generic --host=x86_64-redhat-linux --build=x86_64-redhat-linux --with-gmp=/usr/local/ --with-mpfr=/usr/local/ --with-mpc=/usr/local
d) make
e) sudo make install

Friday, April 9, 2010

CPU you selected does not support x86-64 instruction set

When does this occur:
It happens in case you have missed or incorrectly specified the target architecture.
Look for something like -m32 or -m64 in your compiler flags. It should be present and reflect target system.

Tuesday, February 9, 2010

Delete all pointers in a STL container

#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_object.hpp>

#include <algorithm>
#include <vector>
#include <iostream>

using namespace boost::phoenix;
using namespace boost::phoenix::arg_names;

class A
{
   public:
   ~A() { std::cout << "A deleted\n"; }
};

int main()
{
     std::vector vec;
     vec.push_back(new A);
     vec.push_back(new A);

     std::for_each (vec.begin(), vec.end(), delete_ (arg1));
}

Friday, January 29, 2010

git cloning of svn repository's subdirectory

If you are interested in following a subdirectory of some svn repository, but do not want git to be bothered about previous history:

git svn clone -r HEAD --no-minimize-url url-goes-here

will do the job for you.

You can update to latest version of upstream (svn) repo

git svn rebase

Probably you do not want to mess with this branch, so check-out another branch to do your stuff and merge in local master later.

git checkout -b local_stuff