Boost tokenize for the win
21 May 2009 - Archived in tokenize, tokens, token, string, boost, c++String manipulation has historically been a major shortfalling of the C++ language, or at least it was until boost came along. Something as simple as spliting a string up on a delimeter is now made so much easier thanks to the boost::tokenizer and boost string algorithms.
#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
int main(int argc, char** argv)
{
string text = "token, test string";
char_separator<char> sep(", ");
tokenizer<char_separator<char>> tokens(text, sep);
for (tokenizer<char_separator<char> >::iterator it = tokens.begin(); it != tokens.end(); ++it)
{
cout << *it << "." << endl;
}
}
And my personal favourite is to push the result of the tokenization into a vector/list/stl container.
#include#include #include #include
#include using namespace std; using namespace boost; int main(int argc, char** argv) { string text = “token, test string”; list tokenList; split(tokenList, text, is_any_of(”, “), token_compress_on); (list ::iterator it = tokenList.begin(); it != tokenList.end(); ++it) { cout << *it << "." << endl; } }
So awesome!
Stolen from the lazy programmer and reposted here for my own reference.
Linux fstab and UUIDs
19 April 2009 - Archived in uuid, fstab, mount, linuxWhen I upgraded to Ubuntu Intrepid Ibex I installed it clean on a new harddrive that I installed alongside my old Hardy hard drive (that was totally unintentional - but hilarious). I then mounted the old hard drive, which was much bigger, via fstab so that I could have access to more storage and my older files, but then a funny thing started happening… occasionally when booting the mount would fail and upon inspection it was because the hard drives labeling had been swapped around. One HD is SATA and the other older one is IDE, when I was setting things up the SATA drive was hda and the IDE was given hdb, the problem was that on subsequent boots this would be reversed !!! I’m not sure if this is a linux thing or a bios thing but either way it was screwing up my system so I needed a solution, thankfully this was very simple - all i had to do was find the UUID of the harddrive in question using one of the following two commands
ls -las /dev/disk/by-uuid/
blkid /dev/hda1
Then it was as simple as editing my /etc/fstab to replace the reference to /dev/hd[ab]1 with a reference to the uuid and I was done
/dev/hda1 /data ext3 defaults,errors=remount-ro 0 1
to
UUID=21819e49-70fa-477e-9937-bd04e0550aab /data ext3 defaults,errors=remount-ro 0 1
Thanks to f241vc15 for pointing me in the right direction.
SVN command line
31 March 2009 - Archived in conflict, automatic, version control, subversion, svn, resolutionHave been wondering for a while now how to force the svn command line to resolve conflicts, didn’t really see anything on google until I came across this: http://svnbook.red-bean.com/en/1.5/svn.ref.svn.html
Turns out there is a standard switch which I hadn’t found! The accept switch “specifies an action for automatic conflict resolution. Possible actions are postpone, base, mine-full, theirs-full, edit, and launch“. So to just use the local version (safest):
svn up --accept mine-full directory
Ruby operator precedence of ‘&&’ and ‘and’
6 March 2009 - Archived in and, operator precedence, rubyWow that a heck of a post title isn’t it… well the up shoot of this post is to point out the non-intuitive (to me anyway) difference in the precedence of the two AND operators in ruby. The operator ‘&&’ has a much higher precedence than ‘and’, more importantly ‘and’ has a lower precedence than the assignment operator ‘=’. So what does this mean….
a = 'test' b = nil both = a && b # both == nil both = a and b # both == 'test' both = (a and b) # both == nil
In the second case the statement evaluates as
(both = a) and b # the result of this statement would be false, but it's never used or assigned.
So the upshoot is ALWAYS use ‘&&’ to avoid unintentional failure!
(Example borrowed from PJHyett.)
Debugging ruby backticks
20 February 2009 - Archived in back ticks, execute, backticks, kernel, ruby, system, command lineStolen from Dale’s blog… reposted here for my own reference. Also this link is good for a reference on executing shell commands from ruby.
I needed to knock up a Ruby script recently that orchestrated some external processes using backticks. However something was going wrong and I need to temporarily peek into the inputs and outputs of the external programs. Ruby bend-ability to the rescue:
module Kernel
alias_method :real_backticks, :'`'
def `(cmd)
puts "`#{cmd}` => #{output = real_backticks(cmd)}"
output
end
end
SVN undelete of a directory
6 February 2009 - Archived in version control, subversion, restore, undelete, svnSomewhere way back in the history of our project someone (read Partro) decided that we didn’t need a pesky little data folder anymore and decided that it should be deleted. Turns out he was pretty much right because it wasn’t until about 200 revisions later that I noted its absence, so the question was how to get it back again. The first thing I did was to throw the output of the change list into a text file so that I could identify which revision contained the delete
i=250 while [ $i -lt 604 ] do echo $i >> /home/dev/bdk_changes.txt svnlook changed bdk -r$i >> ~/bdk_changes.txt i=`expr $i + 1` done
(apparently the same thing can be done with svn log –verbose > ~/a.text.file). Then after finding that the offending revision was 403 I wanted to copy the directory from revision 402 into my working copy; according to the svn manual this is accomplished using
svn copy –revision 402 http://svn.server/repos/path/to/deleted_file ./deleted_file
this didn’t exactly work for me tho as I got the following error
D:BDK>svn copy –revision 402 http://svn.vlc/svn/bdk/trunk/data . svn: ‘/svn/bdk/!svn/bc/617/trunk/data’ path not found
well duh of course you can’t find the data directory at revision 617, that is why I want revision 402 (like I specified)… oh well lets try the following
D:BDK>svn copy http://svn.vlc/svn/bdk/trunk/data@402 . A dataPTImporter A dataPTImporterPtRoutes.csv Checked out revision 402. A data
woot! I now have my directory back again!
Ruby file loading
10 December 2008 - Archived in IO, file, rubyJust coz I always forget them, three good ways to load files into memory in ruby
File.open(filename, 'r') do |f| f.readlines end
IO.readlines(filename).each do |line| end
lines = IO.read(filename).split("n")
thanks to Partro for the outline along with the following three points
- File.open gives you a file handle you can stream from
- while the IO methods read it all in at once
- read gives you a single string of the contents, readlines splits it on line endings
vim paste indenting
3 November 2008 - Archived in vimYou know how it goes, you want to paste some code into a file that you are editing, but for some reason each line is indented from the previous line until there is no room left on your screen! The solution is to add the following to your ~/.vimrc
set pastetoggle=<F6>
then you can use F6 to turn autoindent on and off at will ![]()
Right hand margin in Visual Studio 2005, 2008
11 September 2008 - Archived in registry, right, margin, visual studioI don’t like having lines longer than 100 characters long in my source code. Many editors will allow you to specify a right hand margin so that you can see when a line is longer and hence wrap it sensibly to the next line. To do this in Visual Studio, however, requires a bit more work. As described in this blog post, you have to edit the registry to make Visual Studio display nice dotted vertical lines at specific column:
* Open “HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Text Editor” key
* Create new string value “Guides”
* Set its value to something like “RGB(192,192,192) 119″ (columns in registry are zero-based)
* Restart Visual Studio
This works in VS2005 as well, just change 9.0 to 8.0 in the reg location.
Visual Studio Find in Files
15 August 2008 - Archived in no files, Find, MSVC, 2005, visual studioSo a number of times I’ve found that Visual Studio 2005’s find utility is borked. You know how it goes you press Ctrl-Shift-F and expect it to search through your solution looking for that little snippet of code you know you’ve written before and suddenly you get
Find all "vector< MyClass>“, Match case, Subfolders, Find Results 1, “Entire Solution” No files were found to look in. Find was stopped in progress.
what? Turns out the solution is to use the little known short cut “Ctrl+Scroll Lock”, the only purpose of which is to toggle the usability of the find function, press it once and it’s borked, press it again and it works. Magic!
UPDATE Also sometimes when the above doesn’t work you can press ALT + Break…. sigh