Thursday, January 14, 2010

ls using boost

Want to use boost and get list of files matching a mask. The same list you would obtain running ls in the command prompt?

The idea is to have something like this:


BOOST_FOREACH( const std::string& fname, ls( "./*.cpp" ))
std::cout << fname << std::endl ;


So here is the solution. One function and one helper function (to_regex_copy()):


std::string to_regex_copy( std::string mask )
{
std::string rv = boost::regex_replace( boost::regex_replace( boost::regex_replace( mask, boost::regex( "\\." ), "\\\\." ), boost::regex( "\\?" ), "\\." ), boost::regex( "\\*" ), "\\.*" ) ;
return rv ;
}

std::vector< std::string > ls( const char* pcszMask )
{
namespace fs = boost::filesystem ;
boost::filesystem::path path ;
boost::regex mask ;
if ( fs::is_directory( pcszMask ))
{
path = pcszMask ;
mask = ".*" ;
}
else
{
path = fs::path( pcszMask ).remove_filename() ;
mask = to_regex_copy( fs::path( pcszMask ).filename()) ;
}

std::vector< std::string > rv ;
try
{
for ( fs::directory_iterator p(path), e; p!=e; ++p)
{
if ( boost::regex_match( p->filename(), mask ))
rv.push_back( p->filename()) ;
}
}
catch( ... )
{
}

return rv ;
}

Tuesday, January 12, 2010

Profiling shared library on Linux using sprof

It's not as easy as one would/should expect. (Read here about valgrind if you don't want to use sprof.)

First - FORGET ABOUT gprof. gprof is used for the applications but will NOT WORK for shared libraries. Sorry about that. I learned about it the hard way. What you need to use is sprof.

Second - sprof may make you bite your fingers if your glibc library is not up-to-date. It will be displaying the nasty message: "sprof: failed to load shared object"
If this is what you see then you HAVE TO update your glibc. Read about the bug: here. It may not be as simple to upgrade as well... If I have time I will write how to do it through the .iso file (if you have one.)

Third - Yes, as it is not enough, there is an other surprize! The -pg option works well for gprof, but screws up things for sprof! You need to remove this option in order to get proper results from the shared libraries. Otherwise you will get only zeroes as the execution time for the functions.

So, at the end, what to do?

Assuming you have the patch installed or you have glibc-2.5-34 or newer version installed do as follows (the best, in my opinion, way, assuming you have write access to the current directory.)

1) Compile your shared library (libmylib.so) in debug (-g) mode. No -pg.
2) export LD_PROFILE_OUTPUT=`pwd`
3) export LD_PROFILE=libmylib.so
4) rm -f $LD_PROFILE.profile
4) execute your program that loads libmylib.so
5) sprof PATH-TO-LIB/$LD_PROFILE $LD_PROFILE.profile -p >log
6) See the log.

I hope it will help you.

Friday, October 9, 2009

ssh, scp password-less connection

Below is possibly the simplest way it can be accomplished.
Simply enter username@ip-address, when asked after running this one-liner below:
printf "enter username@ip-address: " ; read UIP ; ssh $UIP "mkdir .ssh 2>/dev/null ; echo $(cat $HOME/.ssh/id_rsa.pub) >>.ssh/authorized_keys ; chmod 700 .ssh ; chmod 600 .ssh/authorized_keys" ; echo "hostname=$(ssh $UIP hostname)"
You should be done.
If everything was okay you should see the hostname command result run on the target.
From this moment, you can run your commands on the remote host by executing like this:
ssh username@ip-address "your command"
or copying files like that:
scp myfile username@ip-address:

Thursday, September 25, 2008

How do I find the name of the current shell that I am working in

There is a command that will work on all platforms I tested (HP-UX, SunOS, Linux, MacOS) and shells (sh,ksh,bash,csh,zsh)

ps -p $$ | grep $$ | awk '{ print $NF }'

Be aware that the shell name can be bash or -bash

Tuesday, June 24, 2008

At least one secondary cursor must be specified to DB->join error 22

Never found this problem on the Internet? Does it mean nobody except me run into it?

I have tested BerkeleyDB BTree 4.7.25 (from Oracle now) and when running a simple test described in their doc I got the following when opening (creating) a database:

At least one secondary cursor must be specified to DB->join error 22

After hours of searching here is what I found - the standard installation that comes the btree with is precompiled with a different Visual Studio that has a problem with VS 6.0 I was using. Even though I was able to download the dll (the release mode because the debug mode was wrong - shipped with improper MS nondebug libraries) the opening was returning this wrong message.

Only after opening the .dsw that came with the sources, and rebuilding it with VS 6.0 all went okay.

I hope it will help someone...

Friday, June 6, 2008

New download of the cvs repository on Windows

I have moved to a new laptop, and I had to get all the data from the cvs repository to this new one.
I wasn't even expecting so many problems with the WinCVS application to have!
No, it's not that WinCVS is bad. It's great. The point is that It gives so many options to accomodate so many users behaviours that at someome moment these options became a burden!

So I have finally decided to describe, step-by-step all the operations one needs to do in order to download new repository to his/her place.
This list is for cvs windows only.
I may to do this for Linux/Unix as well some time.
If you want to install WinCVS then, please, search on the Internet, wait for my new blog, or write to me in the comments.


RUN: <<<WinCVS>>>

MENU: <<<Admin/Login...>>>
EDIT: <<<CVSROOT:>>> - avoid using <<<...>>> It's overly confusing. It is better to enter the path:
:pserver;username=USERNAME;hostname=HOSTNAME:REPOSITORY
EXAMPLE: :pserver;username=john;hostname=our.server.org:/Projects

BUTTON: <<<OK>>>
The CVS will ask you for the password which will be stored at your machine
in an encrypted form and will be used for communication every time you
access the repository, but until you click MENU: <<<Admin/Logout>>>
MENU: <<<Remote/Checkout module...>>>
DIALOG: <<<Checkout settings>>>
EDIT: <<<Module name and path on the server:>>>
ProjectAlpha
EDIT: <<<Local folder to checkout to:>>>
CHANGE IT so it points at the ROOT directory where the ProjectAlpha directory will be created.
EXAMPLE: c:\JohnsProject\ (yes, add the backslash at the end!)

NOTE: If you DO NOT WANT to create the ProjectAlpha\ directory in the c:\JohnsProject one then you need to do:
CHECKBOX: <<<Check out into directory:>>> Turn in ON
EDIT: <<<Check out into directory:>>>
JohnsProject
COMBOBOX: <<<CVSROOT:>>>
Drop down menu and select the last one that you have enteted a few steps above.
BUTTON: <<<OK>>>
Off you go!



-------------------------------------
After this operation, you will have your selected directory (c:\JohnsProject\ProjectAlpha, or c:\JohnsProject only) populated with the data.
from the server.

Thursday, June 5, 2008

Sending an email from PHP (on Windows too.)

Yey, finally got something that really works.
It's not my stuff, but I was able to found it after a long time search!
Here is the link to sourceForge.net, to the page that hosts php Mailer for PHP 5 for Windows, and looks like for Unix too. If the link does not work then search for the word phpMailer.

The easiest way to install the stuff is to do as follows (example on Windows, with assumption that you have your php installed in the c:\php directory.)

> mkdir c:\php\mailer
Unzip phpMailer_v2.1.zip into c:\php\mailer
> notepad.exe c:\php\php.ini
Add at the very end:
include_path=".;c:\php\mailer"

Youre done!

Now, example:
>
<?php
require_once( "class.phpmailer.php" ) ;

$mail = new PHPMailer() ;
$mail->IsSMTP() ;
$mail->Host = "smtp.myserver.com:587" ;
$mail->SMTPAuth = true ;
$mail->Username = "username" ;
$mail->Password = "password" ;
$mail->From = "me@myserver.com" ;
$mail->FromName = "It is me!" ;
$mail->AddAddress( "testaddress@theirserver.com", "Poor Receiver" ) ;
$mail->AddReplyTo( "me@myserver.com", "do-not-reply" ) ;
$mail->AddAttachment( "c:\\mydog.jpg", "Isn't she cute?" ) ;
$mail->Subject = "Sorry for this test email!" ;
$mail->IsHTML( true ) ;
$mail->Body = file_get_contents( "c:\\myemail.html" ) ;
$mail->AltBody = "non-HTML mail client? Where are you from?" ;

if( !$mail->Send())
printf( "Sending Error: " . $mail->ErrorInfo . "\n" ) ;
else
printf( "Message has been sent successfully.\n" ) ;
?>