Archive for August, 2009

Clean/truncate filenames before writing them to a CD/DVD

Sunday, August 23rd, 2009

I’m an old-fashioned IT guy: in filenames I prefer the standard ASCII character set, no spaces, no weird international characters and no meter long names. Before writing a directory to a DVD:

<?php
function cdRename($path, $maxlength) {
$pi = pathinfo($path);
$ext = isset($pi['extension']) ? $pi['extension'] : FALSE;
$fn = $pi['filename'];
$fn = substr($fn, 0, $maxlength);
$fn = str_ireplace(//iso 8859-1 only :((
array('á', 'à', 'ã', 'â', 'é', 'è', 'î', 'í', 'ú', 'u', 'ö', 'õ', 'ô'),
array('a', 'a', 'a', 'a', 'e', 'e', 'i', 'i', 'u', 'u', 'o', 'o', 'o'), $fn);
$npath = $pi['dirname'] . DIRECTORY_SEPARATOR . $fn . ($ext ? '.'.$ext : '');
if ($npath != $path) rename($path, $npath);
}
$it = new RecursiveDirectoryIterator('./');
// RecursiveIteratorIterator - 0: LEAVES_ONLY, 1: SELF_FIRST, 2: CHILD_FIRST
foreach (new RecursiveIteratorIterator($it, 2) as $path) cdRename($path, 170);
?>

This is a PHP cli (shell) script, it truncates filenames (without touching the extension) and removes some international characters, which may pose problems when transferred to ntfs/fat/samba/ext3 etc. Unfortunately PHP is still a crap and if you save this script in UTF-8 then it will not work (at least it did not work for me on ntfs and php 5.2.6), so so far one can only replace iso 8859-1 characters; hope this will change with version 6. The script can also rename directories, since the iterator takes children items first – hope someone finds this useful.

Firefox 3.5 not-so-awesome bar

Saturday, August 22nd, 2009

Since the 3.0 series (released more than a year ago) the url bar is driving me nuts: sometimes it takes 7-10 seconds to come up with the first possible url and locks the browser until then; I really don’t like tweaking the about:config page, but I did do that for this one; now it’s okay – still, it’s a shame: we have canvas, html 5 video, css 3 selectors, new gradient crap is coming up, yet the ui still has some unfixed problems…

EDIT: false alert. Still sucks; maybe if I delete all my bookmarks AND empty the history each and every day… but wait, what’s the point in having this awesome location bar then?

Java verbosity

Thursday, August 20th, 2009

I have a massive (butt ugly, huge etc.) XML file, which I want to read into the memory, modify some of the nodes, then save it back into XML. It’s not that I like PHP, because I don’t, just to show how ridiculous things are:

PHP

$doc->save($fileName)

JAVA

private void saveToFile(Document doc, String fileName) {
StringWriter writer = new StringWriter();
Transformer transformer;
try {
transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
writer.close();
String flat = writer.toString();
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write(flat);
fw.flush();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}

I didn’t write proper exception handling in the above examples of course and I’m sure Java gurus out there can write better and nicer code than me, so let’s say, this is just a useless rant.

Writing Jedit macros in Javascript

Tuesday, August 18th, 2009

Jedit, an extensible and powerful editor written in java, supports macros written in beanshell, which is very nice, but since Javascript is the lingua franca of the internet, why not write macros in Javascript?

This is already possible with the JavaScriptShell plugin, but I have two problems with that approach: one is that I cannot access plugin objects (like via import in bsh, or importPackage/importClass in javascript) the other is that js files show up with full path in the macro list, which is pretty ugly for me (yes, for me; maybe it is okay for the rest of the world).

An uglier, but pretty flexible approach would be using the java script engine from a bsh proxy macro:

//evaluates javascript from jsmacro files
void eval_js_macro(View view, String filename) {
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
ScriptEngineManager manager = new ScriptEngineManager ();
ScriptEngine engine = manager.getEngineByName ("js");
engine.put("textArea", view.getTextArea());
engine.put("buffer", view.getTextArea().getBuffer());
engine.put("engine", engine);
context = file_get_contents(DOTJEDITPATH + "/startup/startup.js");
context += file_get_contents(DOTJEDITPATH + "/macros_js/" + filename);
engine.eval(context);
}

I put this function in my startup.bsh, where my path definitions are (read about it in my previous post here) and where some useful functions are stored (like the file_get_contents above, read about it here). The macro parses the startup.js file, which has been used by the JavaScriptShell plugin (til I had that installed), and I myself have some useful wrapper functions there.

In case you want to access other elements (like plugin objects or whatever you’re missing) you can add them to the javascript context with the engine.put command.

Also note that the org.gjt.sp.jedit namespace is perfectly okay in javascript, so the var Macros = org.gjt.sp.jedit.Macros; javascript command is okay and will work as expected.

Revisting Arch Linux

Saturday, August 15th, 2009

Just some random rants; Blender’s working, accelerated video is working: cool. Firefox (on 64bit) is fast as hell, Blender renderin speed much better and the desktop feels really snappy (even with the powersaving cpu governor). Now, some rants:

XFCE4: right now, I have no menu editor; I know they’re working on it, but it’s not that cool. Also I have two file manager icons on the control panel. They’re the same; not a big deal though.

KDE 4.3: pretty stable this time, still damn buggy. Internal composite manager is a waste; sometimes it dies, sometimes it kills xorg, sometimes it gave me a black screen, sometimes it slowed down… The panel gets corrupted even with no composite, the kashew icon has some broken text when moved around… and so on. Btw when can I have the gradient background option back?

Krusader: I’m a dual pane file manager fan; the user menu’s been fixed, many things have not been (like the action menu show/hide inversion). I fall back to Worker: still no antialiased fonts. Yikes! XFree86 calling from the nineties!

Logitech shiny-mice (mine is a cordless MX8): uses evdev, cool. I can “map” keyboard events to button presses, but can not assign a button event to many buttons at once (I like the middle button on the middle + thumb left + thumb right; it makes my fingers hurt less).

Wings 3D devel: from AUR… well, who cares? Probably because of evdev no mouse button clicks are picked up (or actually holding a mouse button). It’s pretty hard to do 3d without camera movements – and I’m puzzled here, because I really don’t want to fall back to the plain old xorg driver. I bought this mouse for the buttons (and to lessen joint pain in my fingers).

Wine: I’m on x86_64 and wine is not officially supported on 64 bit Arch. Should I fall back to AUR again or should I mess with chroot and schroot? Now I really don’t feel like doing either of those…

Virtualbox: hardware acceleration still buggy (yeah, I know it’s experimental) + only the left and the right mouse buttons work. Not a big problem, but still, it’s quite annoying with graphics software.