Artikel getaggt mit OS X

Binary Search NSArray

Though CFArray comes with binary search capability, NSArray does not – at least not within the iPhone SDK. The indexOfObject:inSortedRange:options:usingComparator: can’t be found.

Plus the CFArrayBSearchValues doesn’t tell you whether the key actually is part of the list or not. That’s what the Java JDK does, so let’s implement some category methods

-(NSInteger)binarySearch:(id)key;
-(NSInteger)binarySearch:(id)key usingSelector:(SEL)comparator;
-(NSInteger)binarySearch:(id)key usingSelector:(SEL)comparator inRange:(NSRange)range;
-(NSInteger)binarySearch:(id)key usingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context;
-(NSInteger)binarySearch:(id)key usingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context inRange:(NSRange)range;
-(NSInteger)binarySearch:(id)key usingDescriptors:(NSArray *)sortDescriptors;
-(NSInteger)binarySearch:(id)key usingDescriptors:(NSArray *)sortDescriptors inRange:(NSRange)range;

ourselves, like

-(NSInteger)binarySearch:(id)key usingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context inRange:(NSRange)range
{
    NSLogD(@"[NSArray(MroBinarySearch) binarySearch:%@ usingFunction:]", key);
    if(self.count == 0 || key == nil || comparator == NULL)
        return [self binarySearch:key usingSelector:nil inRange:range];
 
//	check overflow?
    NSInteger min = range.location;
    NSInteger max = range.location + range.length - 1;
 
    while (min < = max)
    {
        // http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
        const NSInteger mid = min + (max - min) / 2;
        switch (comparator(key, [self objectAtIndex:mid], context))
        {
            case NSOrderedSame:
                return mid;
            case NSOrderedDescending:
                min = mid + 1;
                break;
            case NSOrderedAscending:
                max = mid - 1;
                break;
        }
    }
    return -(min + 1);
}

See the full interface + implementation + testcase without html encoding dirt at github.

Tags: , , , , , , , , ,

NSDateFormatter case sensitive trap

Though NSDateFormatter behaves slightly different than documented, the following might even be correct, as strange as it might look (mind the last two lines):

-(void)testNSDateFormatterTrap
{
    NSDateFormatter *lower = [[[NSDateFormatter alloc] init] autorelease];
    lower.dateFormat = @"yyyy-MM-dd HH:mm:SS ZZZ";
 
    NSDateFormatter *upper = [[[NSDateFormatter alloc] init] autorelease];
    upper.dateFormat = @"YYYY-MM-dd HH:mm:SS ZZZ";
 
    lower.timeZone = upper.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
 
    NSDate *d = [lower dateFromString:@"1970-01-01 00:00:00 +0000"];
    STAssertEqualObjects(@"1970-01-01 00:00:00 +0000", [lower stringFromDate:d], @"lower iso wrong");
    STAssertEqualObjects(@"1970-01-01 00:00:00 +0000", [upper stringFromDate:d], @"upper iso wrong");
 
    d = [d addTimeInterval:(-60*60)];
 
    STAssertEqualObjects(@"1969-12-31 23:00:00 +0000", [lower stringFromDate:d], @"lower iso wrong");
    STAssertEqualObjects(@"1970-12-31 23:00:00 +0000", [upper stringFromDate:d], @"upper iso wrong");
}

The Unicode Format Pattern Documentation explains the difference of the upper- and lowercase year format – but frankly I don’t get the “Year of week of year” idea.

But that subtracting one hour in fact adds almost a whole year – that’s odd to me.

So I rather stay away from the uppercase form – be it correct or buggy.

Seen with iPhone SDK 3.1.2 and XCode 3.2.1 on Snow Leopard.

Update:

I think I got it! Uppercase YYYY makes sense only in combination with a calendar week – and not months or quarters.

Look at January 1st 2010. It belongs to calendar week 53 of 2009. Week 1/2010 starts on Jan 4th.

Tags: , , , , , ,

Upgrade to Snow Leopard

  1. Mac OS X 10.6 Snow Leopard (partition 25G, HFS+ Journaled, Upper/Lower)
  2. Mac OS X 10.5 Leopard (partition 25G, HFS+ Journaled, Upper/Lower)
  3. partition userspace 100G, HFS+ Journaled, Upper/Lower
    $ cat /etc/fstab
    # mount partition "userspace" as /Users
    UUID=D016E3FD-E322-3006-A8F5-D2348C6A5B7B	/Users	hfs	rw,auto
  4. create user “mig”
  5. TimeMachine restore Users + Settings
  6. delete user “mig”
  7. manually copy user “Shared”
  8. iPhone SDK 3.1.2 + XCode 3.2.1
  9. modgenerator 1.5 plus trick
    $ ln -s /Developer/usr/bin/momc /Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc
  10. git 1.6.5.2, manually add to .bash_profile:
    export PATH=$PATH:/usr/local/git/bin
  11. Macports / http://trac.macports.org/wiki/Migration
    $ sudo port selfupdate
    $ sudo port install bcpp pwgen wget lftp fortune optipng graphviz ragel imagemagick
    $ sudo port clean --all installed
    $ sudo port -f uninstall inactive
  12. graphviz-2.25.20091129.0545.pkg
  13. Textwrangler 3.0 (2538)
  14. Gimp 2.6.7
  15. NeoOffice 3.0.1
  16. Evernote 1.5.2 (62233)
  17. TrueCrypt 6.3a
  18. Dropbox v0.6.570
  19. Skype 2.8.0.722
  20. Firefox 3.5,5
  21. Miro Video Player 2.5.3 (775f9134)
  22. wxMaxima-0.8.3 & Maxima-5.19.2.dmg
  23. Twain SANE Scanner Support

Remaining issues:

  1. SenTestingKit error highlighting not working.

Tags: , , , , , , , , ,

Sane Batch Scan Workaround

Obwohl scanimage aus dem SANE Werkzeugkasten in der (aktuellen?) OS X Version

$ scanimage --version
scanimage (sane-backends) 1.0.21cvs; backend version 1.0.21

(installiert nach http://blog.mro.name/2008/12/scanner-mustek-1200-cu-unter-mac-os-x-105/) beim Versuch per “batch” mehrere Seiten zu scannen jeweils nach Seite 1 aus der Kurve fliegt:

$ scanimage --batch-start=45 --batch-count=16 --batch --batch-prompt \
--format=pnm --mode Color --resolution 300
...
Scanning page 46
scanimage: sane_start: Invalid argument

und das auch ein bekanntes Problem zu sein scheint, gibt’s einen Workaround per selbstgebautem Shell-Script:

#!/bin/sh
# http://blog.mro.name/2009/07/sane-batch-scan-workaroun/
pre=scan-`date "+%Y-%m-%dT%H:%M"`
echo "To create a pdf, call after scanning the pages:"
echo "    convert $pre*.png -despeckle -density 100x100 -compress JPEG -quality 50 -page a4 $pre.pdf"
for ((i=0;i < 100;i++))
do
	echo "Place document no. $i on the scanner."
	echo "Press &lt;RETURN&gt; to continue, &lt;q&gt; + &lt;RETURN&gt; to quit."
	echo \\a
	read key
	if [[ $key == "q" || $key == "Q" ]] ; then
		break
	fi
	dst=$pre-page$(printf %03d $i)
	scanimage --mode Gray --resolution 300 --buffer-size=1024 --progress > $dst.pnm
	# background conversion:
	(convert $dst.pnm -level 15,85% -density 300x300 -type grayscale \
		-resample 100x100 $dst.png && rm $dst.pnm) &
done
ls -l $pre*

é voilá.

Um das ganze dann noch in ein PDF moderater Größe zu verwandeln genügt (ImageMagick z.B. via darwinport vorausgesetzt):

$ time convert *.png -despeckle -density 100x100 \
-compress JPEG -quality 75 -page a4 out.pdf
 
real	3m39.867s
user	2m55.069s
sys	0m9.628s

.

Tags: , , , , , ,

iTunes Mediathek aus dem Home Verzeichnis raus

Damit das Home-Verzeichnis meines Macbooks mehr Arbeit und weniger Ballast enthält nehme ich die MP3s raus:

Den Rest des Eintrags lesen. »

Tags: ,

HFS Filesystem Party

Vorgestern Nacht hat – möglicherweise im Zusammenhang mit einem unaufmerksam durchgeführten Java Update – das Filesystem meines Macbooks einen Schuß bekommen. Folge: Die Maschine bootet, bootet, bootet – und schaltet sich kommentarlos aus (sic!). Alles sehr ergonomisch – ohne verwirrende Fehlermeldungen.

Den Rest des Eintrags lesen. »

Tags: , , , , ,

Transfer Phone Numbers from gammu to the Mac Adressbook

ok, make yourself comfortable, fasten your seatbelt and stop smoking. This will be quite a journey:

Den Rest des Eintrags lesen. »

Tags: , , , , , ,

lftp OSX dmg

Da’s für lftp zwar einen Darwinport gibt, aber kein Disk Image (was unbedarfte Seelen durchaus von der Installation abhalten kann), habe ich ein solches gebaut und hier zum

Download lftp-3.7.6-1.dmg

verlinkt. Leider ist das Binary nicht statisch gelinkt, drum hat das ganze wenig Sinn. Details siehe unten im “Nachtrag”.

Wie und wo genau das sich dann installiert weiß ich nicht – ich selbst benutze ja den Darwinport

Zur Benutzung nur 2 kurze Anmerkungen:

Ich finde es bequem (wenn auch von Sicherheitsseite nicht die reine Lehre) die FTP Zugangsdaten zu den meistbenutzten Servern in der $HOME/.netrc abzulegen. Vorsicht: die Datei muß folgende Rechte haben (bzw. nicht haben):

$ chmod 600 $HOME/.netrc

Außerdem habe ich folgende Einstellungen in der $HOME/.lftp/rc Konfiguration:

set ftp:list-options -a
set net:limit-total-rate 0,15360&

Viel Spaß damit!

Nachtrag

Leider funktioniert das so nicht, da das Binary nicht statisch gelinkt ist – was leider nicht geht:

Noch dazu sind die dynamisch gelinkten Macport Libs sämtlich auch als Teil von OSX selbst vorhanden. Sehr ärgerlich.

Tags:

Scanner Mustek 1200 CU unter Mac OS X 10.5

Ein aktueller Mac mag den alten Scanner zwar nicht von Haus aus, läßt sich aber dazu überreden: Scanner Mustek 1200 CU unter Mac OS X 10.5 verweist auf TWAIN SANE Interface for MacOS X:

  1. libusb
  2. SANE backends
  3. SANE Preference Pane
  4. TWAIN SANE Interface

Tags: , , , ,