Archive for the ‘Programming’ Category

Comment system for photographs

Sunday, July 27th, 2008

Well I finally got around to adding the ability to comment on every photograph.

Feel free to give it a go.

Neil

scanf + printf with doubles

Wednesday, July 23rd, 2008

I recently had a problem that I was using printf with %E and doubles, but when I did a scanf to read the values back it they always failed.

I couldn’t figure out the problem, till I realised that %E, scientific notation, isnt supposed to be used for doubles.

Its very surprising that printf works one way, but the scanf doesnt work on the way back.

I wrote this little test program. It clearly shows that you should always is &lf when reading and writing doubles.

#include <stdio.h>
 
int main(int argc, char** argv) {
 
  const char * str = "-1.234E003";
  double d = 0;
 
  // check scanf 
  printf("  scanf\n");
  sscanf(str, "%E", &d);
  printf("%%E = %lfE\n", d);
 
  d = 0;
  sscanf(str, "%G", &d);
  printf("%%G = %lf\n", d);
 
  d = 0;
  sscanf(str, "%f", &d);
  printf("%%f = %lf\n", d);
 
  d = 0;
  sscanf(str, "%lf", &d);
  printf("%%lf = %lf\n", d);
 
  // now check printf
  printf("  printf\n");
  printf("%%E = %E\n", d);
  printf("%%G = %G\n", d);
  printf("%%f = %f\n", d);
  printf("%%lf = %lf\n", d);
 
  return 0;
}

When you compile this and run it you get this

  scanf
%E = 0.000000E
%G = 0.000000
%f = 0.000000
%lf = -1234.000000
  printf
%E = -1.234000E+003
%G = -1234
%f = -1234.000000
%lf = -1234.000000

Clear the printf works fine but the scanf cant cope with the doubles.

I’m not sure whether its documented what should happen when you use the incorrect printf or scanf variables (e.g %d) with the incorrect argument type (i.e float not a integer).

Firefox tab at the bottom

Thursday, June 12th, 2008

I’ve always liked how Opera used to put the tab bar at the bottom.  I’ve always configured firefox to be the same too.

I recently updated to Firefox 3 rc3 and I lost this setting.  After a little searching I found that you simply need to add

/* Puts the tabbar at the bottom of firefox */
#content > tabbox { -moz-box-direction: reverse; }

to the following file.

C:\Documents and Settings\<username>\Application Data\Mozilla\
Firefox\Profiles\xxxxxx.default\chrome\userChrome.css

You may need to create the file as it doesn’t exist by default.

Once you restart firefox, the tabs are at the bottom again.

emacs line wrapping

Thursday, April 24th, 2008

By default emacs has line wrap enabled for the whole window. I.e if you only have 1 buffer open then it will line wrap at the edge of the window.

If you open many buffers though there is no line wrapping on each buffer.

If you add the following to your .emacs file it will make side by side buffers behave like the full window.

(setq truncate-partial-width-windows nil)

I dont like line wrap, especially when coding so I set the following

(setq default-truncate-lines t)

I then use f12 to toggle the line wrap by adding the following

(global-set-key [f12] 'toggle-truncate-lines)

So my whole line-wrap.el file is

;; See http://www.delorie.com/gnu/docs/elisp-manual-21/elisp_620.html
;; and http://www.gnu.org/software/emacs/manual/elisp.pdf

;; disable line wrap
(setq default-truncate-lines t)

;; make side by side buffers function the same as the main window
(setq truncate-partial-width-windows nil)

;; Add F12 to toggle line wrap
(global-set-key [f12] 'toggle-truncate-lines)

See 38.3 Truncation or the emacs manual for more information.

emacs refresh F5 key

Thursday, April 17th, 2008

I use this little addition to my .emacs file to support reloading the current file I’m editing.

(defun refresh-file ()
  (interactive)
  (revert-buffer t t t)
  )

(global-set-key [f5] 'refresh-file)

You can also use CTRL-x CTRL-v to do a “Find alternative file” and choose the same file that you are currently editing. F5 is a little quicker though.