Imagine a situation: You are using git (maybe you like it, or maybe you just have to deal with it because people on the other end do that), you are starting to get familiar with it, make some changes to some source code, then want to synchronize with upstream.
You know that your changes are nothing dramatic, a line here, a word there.
So you go ahead and type git pull, expecting the merge to go seamlessly.
BAM! a conflict! (as it happens, upstream decided to throw out the file and put something completely different in its place)
No biggie, you say, i don't care about my changes, just give me their version. svn revert path/to/file would solve that in a whim.
First of all, you need to locate the file. "git status" won't tell you. Instead, type git commit.
It will say that there are conflicts and list them.
For each conflicting file, git checkout --theirs path/to/file will take the remote version. Similarly, --ours throws away remote changes.
Then just git add the files as usual, or simply go ahead and git commit -a. Done, voila!
March 29, 2010
vim folding makes me happy
When I'm not working with NetBeans and Java, I'm working with vim and python. Vim is unquestionably the best text editor of all time, but this post is not meant as an evangelism. This is aimed at those of you who are already using it.
Maybe you know that vim can do folding. That means that you type "zc" and the piece of code under your cursor neatly folds itself into one line (very much like in NetBeans/Visual Studio/Eclipse etc.), then type "zo" to unfold this line to the whole code. Alternately, use "za" to toggle.
This usually works either manually (you create folds with "zc" and "zf" and whatnot) or via a specialized filetype plugin that will create folds from, for example, sections delimited by curly braces.
It can also work based on indentation. Which happens to work very well for sanely formatted code, and especially for python, where sane formatting is part of syntax. Simplest way to get to this is to simply set foldmethod=indent in your .vimrc.
But this has its own share of drawbacks, namely, the autofolds do not contain a leading line.
For example, in this code:
the autofolder will collapse the three do_stuff()s into one line labeled "+--- 3 lines:do_stuff(1)". I would like to collapse all four lines into one saying "if something()"
To accomplish this, you have to use a different method. This is what I added to my .vimrc (update: escaped the '<' sign that disappeared when rendering html):
You also probably want to have all folds open by default:
And to make things ultimately convenient, remap <SPACE> key to open/close a fold in normal mode
and create a new fold from selection
There! Happy faces all around.
(note that this will mess up your foldlevel math, because there will be many foldlevels without corresponding folds. so don't expect serious use of zm/zr. me, i don't care.)
Maybe you know that vim can do folding. That means that you type "zc" and the piece of code under your cursor neatly folds itself into one line (very much like in NetBeans/Visual Studio/Eclipse etc.), then type "zo" to unfold this line to the whole code. Alternately, use "za" to toggle.
This usually works either manually (you create folds with "zc" and "zf" and whatnot) or via a specialized filetype plugin that will create folds from, for example, sections delimited by curly braces.
It can also work based on indentation. Which happens to work very well for sanely formatted code, and especially for python, where sane formatting is part of syntax. Simplest way to get to this is to simply set foldmethod=indent in your .vimrc.
But this has its own share of drawbacks, namely, the autofolds do not contain a leading line.
For example, in this code:
if something():
do_stuff(1)
do_stuff(2)
do_stuff(3)
the autofolder will collapse the three do_stuff()s into one line labeled "+--- 3 lines:do_stuff(1)". I would like to collapse all four lines into one saying "if something()"
To accomplish this, you have to use a different method. This is what I added to my .vimrc (update: escaped the '<' sign that disappeared when rendering html):
setlocal foldmethod=expr
setlocal foldexpr=(getline(v:lnum)=~'^$')?-1:((indent(v:lnum)<indent(v:lnum+1))?('>'.indent(v:lnum+1)):indent(v:lnum))
set foldtext=getline(v:foldstart)
set fillchars=fold:\ "(there's a space after that \)
You also probably want to have all folds open by default:
set foldlevelstart=999
And to make things ultimately convenient, remap <SPACE> key to open/close a fold in normal mode
nnoremap <space> za
and create a new fold from selection
vnoremap <space> zf
There! Happy faces all around.
(note that this will mess up your foldlevel math, because there will be many foldlevels without corresponding folds. so don't expect serious use of zm/zr. me, i don't care.)
February 1, 2010
few notes on Samsung's IO
J2ME implementation on new Samsung phones has a few peculiarities. Yes, they're doing everything right by the spec - but they do it just slightly differently than most of the others.
First of all, InputStream.read() method, the one that reads single character, seems to be rather slow. When you say something like:
then the phone will do what you want, ssssssssllllllllllloooooooooowwwwwwwwwwwwllllllyyyyyyy.
Instead, make use of InputStream.available(), for example like this:
Now you've seen how Samsung reads from streams in large chunks. As it turns out, the chunks aren't as large as they could be.
Let's say you want to read a chunk of data from a file. Consider this:
Can you spot the problem? Of course, the read(byte[]) method doesn't guarantee that it fills the buffer. But interestingly enough, on a vast majority of phones, it will actually do that when you're reading from a file.
Not on a Samsung.
So remember, kids, always check the return value of read(...) calls. Or, if you're using DataInput, as shown here, just readFully(...).
First of all, InputStream.read() method, the one that reads single character, seems to be rather slow. When you say something like:
while (true) {
int ch = istream.read();
if (ch == -1) throw new IOException ("end of stream");
if (ch == '\n') break;
}
then the phone will do what you want, ssssssssllllllllllloooooooooowwwwwwwwwwwwllllllyyyyyyy.
Instead, make use of InputStream.available(), for example like this:
int av = istream.available();The else is there for a reason. Two reasons, actually. First, some phones won't tell you what is available. So you should try to read anyway. Second, if nothing is actually available at that moment, read() will block and hopefully provide opportunity for other threads to run. (That is, if your phone implementation isn't completely retarded. Which, unfortunately, some can be.)
if (av > 0) {
byte[] buf = new byte[av];
istream.read(buf);
} else {
int ch = istream.read();
}
Now you've seen how Samsung reads from streams in large chunks. As it turns out, the chunks aren't as large as they could be.
Let's say you want to read a chunk of data from a file. Consider this:
DataInputStream dis = filehandle.openDataInputStream();
int length = dis.readInt();
byte[] buf = new byte[length];
dis.read(buf);
Can you spot the problem? Of course, the read(byte[]) method doesn't guarantee that it fills the buffer. But interestingly enough, on a vast majority of phones, it will actually do that when you're reading from a file.
Not on a Samsung.
So remember, kids, always check the return value of read(...) calls. Or, if you're using DataInput, as shown here, just readFully(...).
December 14, 2009
stuttering music in dosbox
If you have a slower computer (although i'm extremely reluctant to call my P4@2.8GHz "slower", it's a fact that my work computer with a Core2 just feels that much snappier) and want to play DOS games in dosbox, you might experience this:
The overall game performance is good, your CPU is not even fully taxed, sounds generally work as expected - all nice and fun, except for the MIDI music. Which crackles and skips and stutters and is generally unpleasant enough that you want to turn it off.
Fear not, for i have found a cure!
Open your dosbox.conf (you don't have one? head over to google and find yourself a nice set of instructions on how to create it and where to place it), locate the [mixer] section and increase the prebuffer value. I set mine to 50 (default seems to be 10) and the music is smooth like a cleanly polished watermelon. Or something.
The overall game performance is good, your CPU is not even fully taxed, sounds generally work as expected - all nice and fun, except for the MIDI music. Which crackles and skips and stutters and is generally unpleasant enough that you want to turn it off.
Fear not, for i have found a cure!
Open your dosbox.conf (you don't have one? head over to google and find yourself a nice set of instructions on how to create it and where to place it), locate the [mixer] section and increase the prebuffer value. I set mine to 50 (default seems to be 10) and the music is smooth like a cleanly polished watermelon. Or something.
August 2, 2009
openwig news
Version 0.3.92 is underway.
Biggest spectacle of this is cartridge saving and loading. A few moments ago i have successfully stored and restored a game of Wherigo Player Tutorial.
Technical details about the solution will follow in a separate article.
And that's pretty much it. There's the usual bunch of random bugfixes, minor improvements and extended Wherigo functionality (did i tell you that you can now see a zone map?), but probably nothing to write home about.
Oh, and i switched back from NetBeans' default proguard 4.2 (obfuscator/optimizer) to older 3.9. The new one is too aggressive in optimizing and still has some bugs. I identified and reported one, but it's not yet fixed, so i can't really continue the search. And i don't need the stress of hunting bugs that aren't there.
Biggest spectacle of this is cartridge saving and loading. A few moments ago i have successfully stored and restored a game of Wherigo Player Tutorial.
Technical details about the solution will follow in a separate article.
And that's pretty much it. There's the usual bunch of random bugfixes, minor improvements and extended Wherigo functionality (did i tell you that you can now see a zone map?), but probably nothing to write home about.
Oh, and i switched back from NetBeans' default proguard 4.2 (obfuscator/optimizer) to older 3.9. The new one is too aggressive in optimizing and still has some bugs. I identified and reported one, but it's not yet fixed, so i can't really continue the search. And i don't need the stress of hunting bugs that aren't there.
tidbits
S40 Nokias do weird things when you put a StringItem on a form, give it a label but no text. It will prevent users from scrolling that form.
S60 Nokias do some weird stuff too, but only sometimes. Best not to do this.
SonyEricssons, on the other hand, retain image size on ImageItem when you setImage(null). You have to set a dummy 1x1 image first (or instead).
And last but not least, with certain bluetooth devices (GPSr's, in this case), Nokia phone would perform a bluetooth service search, but return error instead of the service.
When you're searching for a serial port service (UUID 0x1101) and get an error instead, just take "btspp://" + RemoteDevice.getBluetoothAddress() + ":1" as a connection url and you have a solid chance of connecting successfully.
(That means BT address + channel 1. On single-purpose devices, you'll most likely find the only service on channel 1. Makes sense, eh?)
S60 Nokias do some weird stuff too, but only sometimes. Best not to do this.
SonyEricssons, on the other hand, retain image size on ImageItem when you setImage(null). You have to set a dummy 1x1 image first (or instead).
And last but not least, with certain bluetooth devices (GPSr's, in this case), Nokia phone would perform a bluetooth service search, but return error instead of the service.
When you're searching for a serial port service (UUID 0x1101) and get an error instead, just take "btspp://" + RemoteDevice.getBluetoothAddress() + ":1" as a connection url and you have a solid chance of connecting successfully.
(That means BT address + channel 1. On single-purpose devices, you'll most likely find the only service on channel 1. Makes sense, eh?)
June 12, 2009
thoughts on Nokia class loading mechanism
situation:
Imagine you have your basic midlet class (say gui.Midlet). This class references another class (gps.InternalProvider) which implements a specific interface (gps.LocationService). Note that there is no reference to the actual class gps.InternalProvider, except for instantiation, and that is pretty well hidden in a function under several ifs and switches. Everything else is done through the interface.
Class gps.InternalProvider references classes from an api (JSR-179) that might or might not be present on your target device (S40 Nokia). It uses them rather extensively, but of course, it can't use them until it's instantiated. (no static codeblocks, nothing like that)
issue:
When such midlet is started on such device, it instantly dies with NoClassDefFoundError on the JSR-179 classes. It doesn't even start, I'm pretty convinced that none of my code is executed.
Definitely not the part that would instantiate the offending classes.
solution:
Create a distraction. Add a class (let's call it gps.InternalProviderRedirector) that has only one static method:
thoughts:
This is easy to grasp intuitively (for me, at least), but in some situations, that is not enough. So i studied the JVM specification, especially the parts about class loading and linking, and tried to come up with a scientific explanation to this phenomenon. Here's my best effort - note that it is only an educated guess and might not relate to reality in any way.
The spec says that when you are loading a class, you get symbolic references to all classes in use. Then, in the linking step, you can (but don't have to) resolve those symbolic references by trying to load the referenced classes.
I say that Nokia does this. That means that when linking the Midlet class, the class file for InternalProvider (or InternalProviderRedirector) is already loaded.
Then, either in the initialization phase or when the first code from a class is run, Nokia JVM attempts to link all the referenced classes. That means that those classes now try to load and resolve their symbolic references.
When you start the midlet, the class Midlet is loaded. Then it's linked, triggering loading of InternalProvider. And then it's instantiated, triggering linking of InternalProvider. That triggers loading of JSR-179 classes, which are not present, so the instantiation itself fails.
When you insert InternalProviderRedirector into the chain, InternalProvider is never linked (only loaded), so JSR-179 is never loaded. And all is well.
Imagine you have your basic midlet class (say gui.Midlet). This class references another class (gps.InternalProvider) which implements a specific interface (gps.LocationService). Note that there is no reference to the actual class gps.InternalProvider, except for instantiation, and that is pretty well hidden in a function under several ifs and switches. Everything else is done through the interface.
Class gps.InternalProvider references classes from an api (JSR-179) that might or might not be present on your target device (S40 Nokia). It uses them rather extensively, but of course, it can't use them until it's instantiated. (no static codeblocks, nothing like that)
issue:
When such midlet is started on such device, it instantly dies with NoClassDefFoundError on the JSR-179 classes. It doesn't even start, I'm pretty convinced that none of my code is executed.
Definitely not the part that would instantiate the offending classes.
solution:
Create a distraction. Add a class (let's call it gps.InternalProviderRedirector) that has only one static method:
public static LocationService instantiate() {Then instead of doing this directly in the Midlet class, call InternalProviderRedirector.instantiate(). Magically, it will work.
return new InternalProvider();
}
thoughts:
This is easy to grasp intuitively (for me, at least), but in some situations, that is not enough. So i studied the JVM specification, especially the parts about class loading and linking, and tried to come up with a scientific explanation to this phenomenon. Here's my best effort - note that it is only an educated guess and might not relate to reality in any way.
The spec says that when you are loading a class, you get symbolic references to all classes in use. Then, in the linking step, you can (but don't have to) resolve those symbolic references by trying to load the referenced classes.
I say that Nokia does this. That means that when linking the Midlet class, the class file for InternalProvider (or InternalProviderRedirector) is already loaded.
Then, either in the initialization phase or when the first code from a class is run, Nokia JVM attempts to link all the referenced classes. That means that those classes now try to load and resolve their symbolic references.
When you start the midlet, the class Midlet is loaded. Then it's linked, triggering loading of InternalProvider. And then it's instantiated, triggering linking of InternalProvider. That triggers loading of JSR-179 classes, which are not present, so the instantiation itself fails.
When you insert InternalProviderRedirector into the chain, InternalProvider is never linked (only loaded), so JSR-179 is never loaded. And all is well.
Subscribe to:
Posts (Atom)