Showing posts with label tricks. Show all posts
Showing posts with label tricks. Show all posts

Tuesday, 29 September 2015

Tangram for Breakfast

True, you can use software to play Tangram, but it's not as satisfying as the real life hands-on version.

Amazon sells a splendid wooden version for £2.69:

It's the perfect way of (gently) waking your brain up in the morning -- coffee is good, but a Tangram puzzle + coffee is superior.  Also works great if you're stuck on a coding problem -- solve the tangram, and the solution to the other thingy just pops up in your mind a lot of the time.

Try some cats, or a paradox.


Sunday, 9 August 2015

Adhoc profiling

./YourProgram | ts -i "%.s"

is a quick way of profiling your code.  Does of course not beat the real thing(tm), but for most purposes, this should give you a rough idea where your code spends it's time.

(You may need to install 'moreutils')

Some hacking music... because otherwise, there is just too much margin left in this blog post.


Wednesday, 27 May 2015

Put some colour in your C code

I like colours.

Colours make things easier to spot, especially if you have to look at a few metres of test output bumpf --- looking for something blue or pink is much quicker.

So, I made myself a little color.h file that lives in /usr/include/ where it is available anywhere I need some colour in my code.

You will probably notice the colour names --- I took the liberty of renaming 'green' to 'moss', 'bold green' to just 'green' and 'magenta' to 'purple' and so on.  It's less to type, more descriptive, and, also functions as a small cheat map to understand the weird names the fashionistas in your life use to describe colour shades.

You can find the source for color.h here, note that the test code is present in the file in a comment, ready to be copied into a *.c file.

Enjoy!

                                                 

Monday, 25 May 2015

A fake switch

This post is about a trick from the Dalvik VM Internals video, which was mentioned in the Coursera Android course that is currently running. (maybe the next million years will be more exciting?)
                                         
The claim was that this is the one case where goto isn't evil around minute 33:31 and that it performs better than switch.  Well, does it?  And if so, by how much?

This was the example:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#define DISPATCH()  \
    { goto *op_table[*((s)++) - 'a']; }

static void interp_old(const char* s)
{ 
   static void *op_table[] = { 
   &&op_a, &&op_b, &&op_c, &&op_d, &&op_e };
   DISPATCH();
   op_a: printf("Hell"); DISPATCH();
   op_b: printf("o "); DISPATCH();
   op_c: printf("wo"); DISPATCH();
   op_d: printf("rld!\n"); DISPATCH();
   op_e: return;
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
            
Switch is pretty switched on, so let's see how this compares!

The code that does the comparison can be found here.  Results(on my ancient 32 bit Dell):

With gcc -g:
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 
Using default value of 100000.  Usage: ./dispatch #
Sample size = 100000 chars * 100000 iterations. 
Dispatch method:                         0.002152 
Dispatch shuffled method:                0.002144
Goto switch method:                      0.002641
Goto switch method 2:                    0.002351
For loop Switch method:                  0.002667
For loop Switch shuffled method:         0.002660 
Recursive switch method:                 0.010798
Recursive shuffled Switch method:        0.006913
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

With gcc -O3:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Using default value of 100000.  Usage: ./dispatch #
Sample size = 100000 chars * 100000 iterations.
Dispatch method:                         0.001083
Dispatch shuffled method:                0.001108
Goto switch method:                      0.001338
Goto switch method 2:                    0.001381
For loop Switch method:                  0.001465
For loop Switch shuffled method:         0.001385
Recursive switch method:                 0.001437
Recursive shuffled Switch method:        0.001385
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

So, there is a small difference, but it's really not that big a deal, unless you really are pressed for time and space.

The ever useful gcc manual has an entry about fun with local labels which will tell you more. Still, building this by hand is tedious, even if emacs is your best friend.  

What if you have a huge array of a struct that maps variable A to variable B and also has an optional function pointer F?  What you need is a 'fake_switch' generator, here is my version.  It should be easily adjustable for your taste. 

Goto it ;-)

Ps.: For extra fun, try this C to Assembler translator.  I really like the hover feature they built, but another thing that would be useful is showing the size of the generated code.
                                                                                                                                                                                    

Tuesday, 5 May 2015

How to grab every mp3 from a Feedburner page

My little Apple finally died[1] and the major loss was my collection of the entire History of Rome podcast, by Mike Duncan.  Oh noes!

Sadly, Feedburner doesn't provide a big zip file of all the episodes, which is understandable, since it would be huge.

I could hunt around for a browser extension that does a mass download, but this is quicker, safer and way more fun:

Save the above file to your 'history_of_rome' directory as 'hist.xml'

Preferably overnight(if your 'broadband' comes via the phone line...), in the bash shell do this:

$ grep -e "enclosure url=" hist.xml | sed -e 's/[^"]*"//' -e 's/".*//' | xargs wget

And hopefully, if all goes well, in the morning you'll have this:

FINISHED --2015-05-05 02:06:31--
Total wall clock time: 2h 26m 38s
Downloaded: 191 files, 2.0G in 2h 24m 49s (240 KB/s)

~/history_of_rome>

You say that this podcast is a little bit short for such a long story?  Well, there's lots more out there, but this is my second favourite series:



[1] And, after all this, I found that the Apple actually wasn't broken, but the USB port I plugged in for a recharge was.  ;-D

Friday, 17 April 2015

I installed tree and found a useful forest

Today I was idly reading some random unixy stuff, and, came across the 'tree' command.

I had to install it for Ubuntu, but, it definitely was well worth it.

It's s useful if you are connecting with a shell and need to navigate a forest, and also makes a neat html file from any tree you like, which looks good in lynx, or in any web browser for that matter.

The tree gives you the big picture, with tree -d -H . > tree.html


(OMG, I'll be whistling this all day long now.  And so will you ;-D )

Wednesday, 25 February 2015

Your own malloc function, the very lazy way

The problem:  You want your own malloc function, and there are gazillions of them in the code base you're working on.  You could automate editing this lot, but littering the place with lots of custom xmallocs does not appeal, plus, there is are 3rd party libraries and you don't really want to mess with those.

The solution:  3 different ways of solving.  And: How to coax the linker into compliance for the wrap.




My patch, that puts it all together, using CMake.

Saturday, 20 December 2014

No-one expects the...

... unexpected!

I pasted some text into an open C comment and whilst closing the comment, I found that '/' does 'undo'.

Wot?

Aha, the ctrl key was stuck and it turns out that C-/ is short for M-x undo or C-x u.


YEEHAW!

Wednesday, 13 August 2014

Sometimes, to fake is better than to make

#! /bin/bash                                                         
# Compile a test file without those noisy -Wunused* flags, but       
# everything else that may be useful.                                
# 
# Usage: compileCTest <name of your file  without the '.c'>  
       
# give the executable an easy to spot and mass-delete friendly name
executable="$1-exe"

# remove the old executable if it exists                             
if [ -e $executable ]
then                                                                 
    rm $executable;                                                  
fi                                                                                                            
echo ""
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo ""

# all flags from -Wall and -Wextra minus the -Wunused flags
runGCC='gcc -g -std=c99 -Waddress -Warray-bounds -Wchar-subscripts -Wenum-compare -Wimplicit-int -Wimplicit-function-declaration -Wformat -Wmain -Wmaybe-uninitialized -Wmissing-braces -Wnonnull -Wparentheses -Wpointer-sign -Wreturn-type -Wsequence-point -Wsign-compare -Wstrict-aliasing -Wstrict-overflow=1 -Wswitch -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wvolatile-register-var -Wclobbered -Wempty-body -Wignored-qualifiers -Wmissing-field-initializers -Wmissing-parameter-type -Wold-style-declaration -Woverride-init -Wsign-compare -Wtype-limits -Wuninitialized $1.c -o $1-exe -lm -lgmp'

eval $runGCC                                                         

# check that compile resulted in executable and if so, run it  
if [ -e $executable ]                 
then                                                                 
# add a couple of dividing lines to make this easier to read 
echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=";
./$executable;
echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=";
fi         

Thursday, 29 May 2014

A script for Exaile: bedtime-story.sh

As much as I like Exaile, it sadly lacks the option of turning a recording off after n minutes.

But, who needs a feature for the Exaile GUI when a script will suffice?

So, set your story up to play in the GUI, then use this:

bedtime-story.sh:

# turn the screen off
xset dpms force off

sleep $@ > /dev/null 2>&1

if [ $? = 1 ]
then
    # turn the screen on
    xset dpms force on
    echo "Fade out Exaile playback after a defined time, using the computer's volume control."
    echo ""
    echo "Usage: bedtime-story NUMBER[smh]..."
    echo ""
    echo "Each argument is a number followed by a unit."
    echo "The units are: s (seconds), m (minutes), h (hours)"
    echo "Example: bedtime-story 20m 10s"
    echo "The screen will be turned off, move the mouse or press a key to turn it back on."
fi

# grab current spot and volume, then gently fade and pause
c=$(exaile --current-position)
v=$(pactl list | grep Volume | cut -d ":" -f 3 | cut -d "%" -f 1 |\
 sed -n '1p' | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')
for((i=$v;i>=0;i--)) do
    sleep 1s
    pactl set-sink-volume 0 ${i}%
done
exaile --play-pause

# return volume to former setting
pactl set-sink-volume 0 ${v}%
echo "The current track was faded out beginning at $c."

Now I can go to sleep in style, whilst listening to the Internet's longest history podcasts. (in parts).

Wednesday, 28 May 2014

Console Mio!

I figured out my concentration/reading problem -- the solution is to use lynx in the console.

I know, it's very retro, but, ever so easy on the eyes.

To set your Ubuntu console up, do this:

sudo dpkg-reconfigure console-setup

select: UTF8
next select: Combined - Latin; Slavic Cyrillic; Greek     
then select: VGA
Finally, select: 32x16 

For extra happiness, set your mouse up with gpm:  sudo apt-get install gpm

Now type alt-ctrl F1 [or F2-F6] and browse like it's 1980.

To get back to X, do alt-ctrl F7


Monday, 6 January 2014

μηδέν άγαν {nothing in excess}

One of my (very patient) mentors made the following very true observation about me:

By the way, designing features based on speculation is usually not a good idea. (...)  Maybe there's a bug report in our tracker requesting such functionality? If not, perhaps you don't need to worry too much about that scenario.

I have a habit of assuming the worst case and then try to create something that (hopefully) solves for the entire range of problems/issues that might occur.

This sounds like it's a good thing, and sometimes it is, at other times, it isn't, because the sky is not about to fall.[1]

I've been thinking why that is so. and my best guess is that this probably a function of OO coding on the MUD, where anything and everything can cause surprises, because things interact whilst interactively interacting.

If you've ever replaced 20+ players lives, stats, xp points and equipment because your fun toy caused a chain reaction that you hadn't considered... you get extra careful after the umpteenth time ;-D

Contact me for a great deal on a used pair of bullet proof sandals.


-=-=-=-=-=-=-=-

[1] From the Omega game:
(you walk into the establishment)
Rampart Healers. Member RMA.
a: Heal injuries (50 crowns)
b: Cure disease (250 crowns)
ESCAPE: Leave these antiseptic alcoves.
(you hit ESCAPE)
OK, but suppose you have Acute Satyriasis?

Thursday, 21 November 2013

Why I don't use a smart phone

Of course I own a smartphone.  Just that it's not a phone (I have an (ancient)(lightweight) Nokia for this task) but my Apple only does 3 things that are useful to me:

1) work as a timer and reminder
2) enable me to take cooking recipes I find on the web into my kitchen
3) play the odd mp3 over the small speaker as a bedtime story

Other than that, the screen is too small to comfortably read or do anything on, and whilst I could get a bigger model, the small one I have is already too heavy to lug about as is, and, a larger screen would still be too small.

Don't you get bored just sitting around, say, waiting for Godot or on other occasions?

Nope, because my brain is a far more useful and interesting device than a smartphone, and if I really am in idle input mode, I have a tiny little Samsung U5 so I can listen to audio books.  This little toy weighs 28 grams and I can tuck it anywhere into my clothes.

Also, I quite like just sitting somewhere and looking at the world pass by, giving my mind the free run of my brain to grep for interesting things in my thoughts I've been unaware of so far.

If you do not have idle time for your brain to run riot in, it kills your creativity -- 'boredom' is the best inspiration because your mind hates a void and will find a way of entertaining you with ideas, the moment you give your mind time and space to talk to you.


It's a different kind of apple... :)

Sunday, 10 November 2013

Making sure I stay lucky



After nearly losing all my work yesterday, I've made this little script to run in the Emacs compile buffer before the make command.  This runs svn diff every time I compile and puts the result into a separate directory.

#!/bin/sh
CURRENT_DIR=`pwd` 
DATE_MARKER=$(date +%Y%m%d%T) 
SECURITY_PATCH_DIR=/home/g/security_patches/ 
WHAT=`echo "${CURRENT_DIR##*/}"`
EXT=.patch
SECURITY_DIFF=${SECURITY_PATCH_DIR}${WHAT}${DATE_MARKER}${EXT}
SVN=/home/g/InstalledSVN1.9/subversion
$SVN diff > $SECURITY_DIFF 

Wednesday, 6 November 2013

My day/week/month is made!

Julian Foad said:

"Your BRANCH-README file <http://svn.apache.org/repos/asf/subversion/branches/invoke-diff-cmd-feature/BRANCH-README> is a fantastic window into the branch, much more detailed and helpful than the rest of us ever write. I can easily see exactly what the whole feature does, how it's structured, and what your plans are, without having been paying close attention. I'm mentioning this mainly so anybody else contemplating taking a look at this knows this is a good place to start reading. And I'm pleased to see you have taken care to adhere to our coding style. "

It's the most wonderful compliment I've heard in years :)

{And, to be honest, writing this BRANCH-README helped me a lot in getting things done and keep me focused on what I was (and am) trying to do.  It was danielsh who suggested that I should write one (ironically I initially omitted it, despite fixing a part of the hacking guide entry for this most excellent advice.)}

Don't delay, write _your_ BRANCH-README today!

Thursday, 24 October 2013

How to use svn diff to create a list of visitable files with line numbers in the Emacs compile buffer


Here is how to use the svn diff output within the emacs compile buffer to generate a handy list of files with line numbers that you can click on to visit every bit of code you've changed:

M-x compile

svn diff |\
perl -ne '/Index: (.*)$/ && ($name = $1);\
/^@@.*\+(\d+),/ && print "./$name:$1:diff\n";'  


Monday, 21 October 2013

Merging the trunk to branch, the lazy way

#!/bin/sh                                                                                                                                 
${SVN_INSTALLATION?"Please set the environment variable SVN_INSTALLATION."}
SVN=$SVN_INSTALLATION
BRANCH_DIR=`pwd`
tmp=`$SVN info 2>&1 | grep SVN_ERR_WC_NOT_WORKING_COPY`
if [ -n "$tmp" ]; then
    echo "Error: The branch directory does not contain a SVN repository." >&2;
    exit;
fi
TRUNK_DIR=$1
REL_DIR=$2
if [ -z "$TRUNK_DIR" ]; then
    echo "Usage: mergeBranch <trunk directory>" >&2;
    exit;
fi
if [ ! -d "$TRUNK_DIR" ]; then
    echo "Error: Trunk directory not found." >&2;
    exit;
fi
cd $TRUNK_DIR
tmp='$SVN info 2>&1 | grep SVN_ERR_WC_NOT_WORKING_COPY'
if [ -z "$tmp" ]; then
    echo "Error: The trunk directory does not contain a SVN repository." 1>&2;
    cd $BRANCH_DIR
    exit;
fi
cd $TRUNK_DIR;
$SVN up
tmp=`$SVN info | grep "Last Changed Rev:"`
TRUNK=${tmp##*: }
cd $BRANCH_DIR
tmp=`$SVN info | grep "Last Changed Rev:"`
BRANCH=${tmp##*: }
$SVN up
WHAT=${BRANCH_DIR##*/}
echo "On the $WHAT branch: trunk revision $TRUNK merged into branch revision $BRANCH." > log.mesg
$SVN merge $REL_DIR

Monday, 30 September 2013

Sqlite compiler spam delenda est.


Because sqlite produces ~50000 extra letters of bumpf per compile that often obscures actually important compiler messages, I've not been as diligent as I should have been about reading compiler messages.  A lot the time I get lucky and it doesn't matter, but I also got caught out by that.

Here is how to get rid of all the noise and make your compiler output useful once again:

First we take a snapshot of the 'native' compiler messages that come with the trunk:

make 1>stdout.report 2>stderr.constant;
sort --unique stderr.constant > stderr.unique;
grep -v -F sqlite stderr.unique

This removes the sqlite warnings and shows you only the current warning messages that are actually ~/trunk related.

Any subsequent compiles that are started with the line below will use the generated stderr.unique file to filter output and remove every compiler message that is 'native' to the trunk, leaving just the compiler messages that pertain to your code:

make 2>&1 >stdout.report | tee stderr.report | grep -v -F -f stderr.unique >&2 2>/dev/null

You can also type this line when you invoke the emacs compile buffer and your C-x ` will continue to work.

Update:

Daniel Shahaf kindly mailed me his solution:

When I ran into this problem, I solved it differently: by building sqlite3 as a shared library and pointing configure to that.  Details  here:

https://svn.apache.org/repos/infra/infrastructure/buildbot/aegis/buildmaster/master1/projects/subversion.conf

# Built from the amalgamation with:
# s=/home/buildslave18/slave18/tools/sqlite; sudo rm -rf $s && sudo mkdir -p $s/include $s/lib && sudo cp sqlite3.h $s/include && sudo gcc -W -Wall -Wextra -Werror -fPIC -shared -o $s/lib/libsqlite3.so sqlite3.c -ldl -lpthread
# NOTE: this doesn't create a .la file for libtool, so libtool won't add this library to rpath of binaries compiled against it, so we have to set LD_LIBRARY_PATH below.
    '--with-sqlite=/home/buildslave18/slave18/tools/sqlite',

That approach is used by the warnings bot:
http://ci.apache.org/builders/svn-warnings

I suppose there's more than one way to skin a cat.

Cheers,

Daniel
(who can do './autogen.sh >/dev/null && ./configure -q && make -s' and
get no output and no warnings)

Tuesday, 17 September 2013

I found find. Now I'm lost.

I used find before quite a lot, but it never occurred to me to type 'man find'.

Well, I just did and found the software equivalent to this:




30 pages of man file.  Is there anything find doesn't do?

I'm going to have to grow a second brain I think.

Friday, 30 August 2013

Another failed patch

Whilst doing the long overdue merge of trunk into my invoke-diff-cmd branch, I spotted a deprecation warning that looked like a minor oversight, and so, I fixed it, svn compiled, the tests passed and ... the patch turned out to be a no-can-do, because APR_ARRAY_PUSH doesn't care what type you stuff in there.

I thought it did, because of this:

#define APR_ARRAY_PUSH(ary,type) (*((type *)apr_array_push(ary))) 

which *looks* like it's type-safe at least if you don't know what you're really looking at (and if you haven't bothered to check the underlying code to see what it actually does).  It of course does no such thing -- so, what does this trick that does seemingly nothing actually do?

Ask on #svn-dev and ye shall receive:

[19:32] <breser> gbb: The reason it takes a type is to avoid warnings.  The apr_array_push() function returns a void *.  Technically there's no issue with putting any pointer in a void pointer, however some  compilers will warn you about that since you may not be doing it intentionally.                                                        
                                                                      
[19:33] <breser> gbb: The array code doesn't care what the pointer is  to, it's up to the user of APR Arrays to keep their types straight.   
                                                                      
[19:34] <breser> gbb: In this case the fact that the array code is  taking a type (and casting) is hiding the errors/warnings you would have otherwise seen.                                                                                                                     
[20:30] <Bert> breser, gbb: Apr arrays can be of different kinds than just pointers. In some cases we have arrays of specific struct kinds  (e.g. for property changes in the diff handling) 

Thank you Ben and Bert, it certainly did clear things up, and made me look up the 'long answer' --- the very short answer of course is that C isn't C++, and so doesn't have type-safe templates.  Caveat coder!