Skip to content


Starting a learning series on C/C++

Recently started coding various things in C/C++ again, coming from Python to any language after a while is a funny process haha, errors about missing { and ; hahahaha

Anyway, I’m going to use this series as an opportunity to refresh a lot of the fundamentals of the C language first, then moving onto C++. Over time I think a lot of the nuances of the language gets forgotten.

Posted in Rant. Tagged with .

Ubuntu 10.04 broken gconftool-2, Segmentation Fault

Continuing on from the broken Ubuntu installation, for some reason some packages are failing with messages like “Subprocess installed post-installation script returned error exit status 245”

Looking in the post-installation scripts in /var/lib/dpkg/info/[PACKAGE].postinst there is a call to gconf-schemas and if you run that manually, dmesg will report gconftool-2 segfault.

The problem is that if the LOCALE is not set to English UTF8, gconftool-2 will randomly segfault on certain schemas (personally I think this was a huge error that somehow got through testing). The solution is pretty simple… either change the locale to en_US.UTF8 for any new bash shell with bashrc through:

export LC_ALL=en_US.UTF8
export LANG=en_US.UTF8
export LANGUAGE=en_US.UTF8

To check that locales are now correct, use

locale -a

Or, since gconf-schemas is a python wrapper for gconftool-2, go change the code of gconf-schemas:
1. Find where gconf-schemas is

which gconf-schemas

2. Edit the code, find where the script defines the variable ‘env’ for launching a subprocess, and add new dictionary entries (append to the end, dont forget to add a , to the current dict):

'LANG':'en_US.UTF8',
'LC_ALL':'en_US.UTF8',

Now resume any installation, these errors should go away

Posted in Linux. Tagged with , , , .

Ubuntu 10.04 i686, Badly broken dpkg

Recently had to fix a really badly broken dpkg database. I think this could also be useful for if you have gotten to a state where you just want to reinstall every single package currently on the machine…

I guess the instructions are not just for 10.04, but you will need to find the appropriate pacakges

To start, download the following packages from the Ubuntu Package Search:

  • dpkg
  • debconf
  • apt
  • apt-utils
cp -rf /var/lib/dpkg/info /var/lib/dpkg/info.back
rm -rf /var/lib/dpkg/info
dpkg --force-depends -i [[DPKG]]
dpkg --force-depends -i [[DEBCONF]]
dpkg --force-depends -i [[APT]]
dpkg --force-depends -i [[APT-UTILS]]

apt-get update
apt-get --reinstall install ucf
dpkg -l | grep ii | awk '{print "apt-get --reinstsall -y install", "$2"}' > /tmp/inst
chmod +x /tmp/inst
/tmp/inst

Posted in Linux. Tagged with , , , , .

Finding the version and architecture of a Ubuntu installation

I don’t deal with Ubuntu much, so note to self:

To find version of the installation:

cat /etc/issue

To find the architecture:

uname -m

i686, i386 are 32bit
x86_64 are 64bit

Posted in Linux. Tagged with , , , .

Matching repetition of a word/pattern using Regex

Just had to match a string pattern that is like this: -30 29 334 -5 9 33 -31 -289 667 (for example, there is a trailing space)

The regex:

^(?:[\-0-9]+\ ){9}$

In words, the regex matches start of line (^), followed by at least one character that is – or 0-9, followed by a space. The match is in a non-capture group (?: … ) and the pattern in the non-capture group is to be repeated 9 times, followed by the end of the line ($)

Posted in Technology. Tagged with , .

Redirecting System.out to somewhere else (Java)

At times you want the output of all the System.out.println and other System.out print stuff in a log file or in a UI window somewhere. To do this, you need to wrap PrintStream, and call System.setOut and System.setErr

Example Wrapper class that provides the tee function:

public class LogCapture extends PrintStream {
   
    private File outFile;
    private PrintStream filePrint;
    private boolean canLog = false;
   
    public LogCapture(PrintStream original, String logFile) {
        super(original);
        outFile = new File(logFile);
        try {
            if (!outFile.exists())
                outFile.createNewFile();
            filePrint = new PrintStream(outFile);
            if(outFile.canWrite())
                canLog = true;
            else
                canLog = false;
        } catch (Exception e) {
            e.printStackTrace();
            canLog = false;
        }
    }
   
    @Override
    public void write(byte buf[], int off, int len) {
        try {
            super.write(buf, off, len);
            if (canLog)
                filePrint.write(buf, off, len);
        } catch (Exception e) {
        }
    }
   
    @Override
    public void flush() {
        super.flush();
        filePrint.flush();
    }
}

To use:
[cc lang=”java”]
LogCapture logger = new LogCapture(System.out, logFile);
System.setOut(logger);
System.setErr(logger);

Posted in Java. Tagged with , , , .

QtJambi – Disable the X button on a Dialog window (the X on top right corner)

Was looking for a way to hide the X (close) button on the top right of a Qt Dialog, this is the way to do it:

this.setWindowFlags(Qt.WindowType.Window, Qt.WindowType.CustomizeWindowHint, Qt.WindowType.WindowTitleHint);

Posted in Java, Qt. Tagged with , , , .

Disabled annoying Macbook startup sound

Finally decided that I don’t want to hear the macbook pro’s start up BONGGGG sound anymore. The solution is pretty straightforward. Download a programmed called StartNinja, mirrored here

StartNinjaInstaller.dmg

Posted in Rant, Technology. Tagged with , .

Qt Jambi – Using QTableWidget

Quick example to show how to use the QTableWidget:

// this line of code gets a list of headers from a array I had in the code
List<String> headers = ais.get(firstKey).getStatsHeaders();
// adds an extra header
headers.add(0, "Device");
// populate all the header labels
ui.tblStats.setHorizontalHeaderLabels(headers);

// insert a new item
QTableWidgetItem item = new QTableWidgetItem(name);
ui.tblStats.insertRow(0);
ui.tblStats.setItem(0, 0, item);

Posted in Java, Qt. Tagged with , , , , .

Qt Jambi – Updating UI elements from another thread

Generally using Signal slots seem to be the best approach, but for quick and dirty way to just update something on the UI, we can use QApplication.invokeLater and invokeAndWait methods. An example code snippet is below.

QApplication.invokeAndWait(new Runnable() {
    public void run() {
        QTableWidgetItem item = new QTableWidgetItem("hello");
        ui.tblStats.setItem(0,1, item);
    }
});

Posted in Java, Qt. Tagged with , , , , , .