среда, 20 марта 2013 г.

Incorrect characters in the list of audio devices in Java

Everyone knows, that audio devices are listed in Java with AudioSystem.getMixerInfo(). Mac users know, that the only sound device they have for Macintosh is Built-in Input, Built-In output and Java Sound Audio Engine, version 1.0. And only the ones who use non-latin symbols in OS, know about the problem with this function: Cyrillic letters looks like a habracadabra with it. I’m not sure about Japanese ones, has to be checked still..

Like this:



It happens because getMixerInfo() returns device data in Windows-1252 charset, but tries to output it in Windows-1251. We have to help it somehow

It might be cool to write a wrapper for javax.sound.sampled.Mixer.Info, but toString() and getName() methods in this class are final. Let’s use helper functions:

public class Shared {
 private static String OS = null;
 public static String getOsName()
 {
  if(OS == null)
   OS = System.getProperty("os.name");
     return OS;
 }
 public static boolean isWindows()
 {
    return getOsName().startsWith("Windows");
 }
 public static String toLocalString(Object info)
 {
  if(!isWindows())
   return info.toString();
  String defaultEncoding = Charset.defaultCharset().toString();
  try
  {
   return new String(info.toString().getBytes("windows-1252"), defaultEncoding);
  }
  catch(UnsupportedEncodingException ex)
  {
   return info.toString();
  }
 }
}

Then we use Shared.toLocalString(item.getName()) for every such an instance. All of the titles are ok now:


вторник, 19 марта 2013 г.

grep support for \d

A long time ago, when every PC had own OS, it was easy to remember, which RegEx are from POSIX standart, and which appeared only in Perl-style. Now it’s almost forgotten, and C# regexp classes don’t support POSIX character classes (like [:digit:]).

That’s why the debugging of a simple bash script is so difficult. grep is older then a lot of his users, he remembers all of these times and needs -P for Perl mode and ‘\d’ for digits support.

That’s how

ls | grep -e "[0-9][0-9].[0-9][0-9].md"

becames:

ls | grep -Pe "\d{2}.\d{2}.md"

Free and open-source software licenses

There’re plenty of free and open-source software licenses and not all of them are as simple as Do What the Fuck You Want to Public License. And even if you collected all 4 freedoms and 650 pokemons, it isn’t enough.

The main thing a developer has to know about free and open-source licenses – some of them let the code be linked from code in another language, and some of them don’t.

For example, if you use something licensed as GNU GPL. your project has to be under GNU GPL too. In other hand, if you use BSD license, you can sell it to Apple to make OS X from it.

Please, check the license before using something from GitHub or Google Code.

Google Code allows only OSI-Approved licenses. You have to remember this table to be safe using projects from it:

LicenseLink with code using a different licenseRelease changes under another license
Apache License++
Artistic License+!
Eclipse Public+-
GNU GPL 3--
GNU GPL 2--
GNU Lesser+-
Mozilla Public+!
New BSD++
Microsoft Public License*+-
+ – allowed
- – not allowed
! – limited
* Microsoft Public License isn’t included in official list on Google Code, but is approved by OSI too and is very popular on Codeplex.


Git: How to rename a GitHub repo

GitHub allows to rename your repos. I really like this solution.

Install curl if you haven’t and use terminal (or cygwin under windows):

user=MyUserName
pass=MyPassword
newName='{"name": "NewNameForRepo"}'
oldName="MyRepo"
curl -u "$user:$pass" -X PATCH -d "$newName" https://api.github.com/repos/$user/$oldName

четверг, 7 марта 2013 г.

QT: error: cannot find -lQtMultimedia

The QtMultimedia library is not available in Ubuntu. If you’re trying to create in Qt a project that uses it, it will fail with following error:

error: cannot find -lQtMultimedia

To fix it, you have to install qtmobility and use multimedia items from this lib. Install qtmobility-dev and libdeclarative-multimedia. Then remove QT += multimedia from .pro file and add INCLUDEPATH to QtMobility and QtMultimediaKit directories.