Thursday, November 17, 2011

Fix for iTunes' "Network connection timeout" error

I have been trying to update my iPhone to iOS 5.0 for a few days now. Every time the software download would fail one third way through with "Network connection timeout" on Win7 x64 host.

It took me a while to dig out the solution that worked for me:

  1. Upgrade to the latest version of iTunes.
  2. Flush DNS Cache.
    Start command prompt and run:
    c:\>: ipconfig /flushdns 
     
  3. Disable FireWall and Anti-Virus software
If none of that worked for you, you might need to download the software update file (IPSW) manually from Apple site directly. Google for "download-iphone-firmware-ipsw."

The folder to place downloaded update is:

c:\Users\USERNAME\AppData\Roaming\Apple Computer\iTunes\iPhone Software Updates\

The filename of the recent update for me was:

     iPhone3,3_5.0.1_9A405_Restore.ipsw 

Tuesday, September 13, 2011

Technical Drawings with Dia

Dia is an excellent multi-platform diagram drawing tool originally written by Alexander Larsson. Unlike Visio, it works well on Windows and Linux, and best part -- it is free.

I have been using it for a good part of the last 10 years and always enjoy its functionality.

The most unintuitive aspect of embedding Dia drawings into your Microsoft Word document or PowerPoint presentation is how to export the drawing to appropriate picture format.

Before you start working on your drawing with Dia, change drawing setting to these:
  • File|Page Setup to:
    1. Paper Size: Letter
    2. Scaling: 45
  • Under View menu item:
    1. Select AntiAliased
    2. Unselect Snap To Objects
Now, proceed with your drawing diagram. You can disregard the fact that it might span over multiple pages. When you are finished, go back to File|Page Setup and change Scaling of it to Fit to: 1 by 1. This will rescale your drawing to fit in a page. The reason for that is the physical constraint of Microsoft Word document which is page-oriented.

Now you are ready to export your drawing from Dia internal XML format to one of the compressed graphics formats such as JPEG, GIF, or PNG.
Open 'Export Diagram' dialog via File|Export. We are going to use PNG as graphics file format as it works well in the documents and on the web across all imaginable platforms.

The trick is to select the right PNG format out of all available which are:
  • Cairo PNG (*.png)
  • Cairo PNG (with alpha) (*.png)
  • Pixbuf[png] (*.png)
  • PNG (anti-aliased) (*.png)
The right format would be the last available, PNG (anti-aliased).

When selected, press 'Save' button and then a little dialog 'diaw.exe' pops up to let you specify PNG Export Options. This is very important as you can set Image width of your final picture.

For Microsoft Word document I set Image widht to 800 and let the dialog adjust the height.

For PowerPoint presentation, set it to 1200 and also remember to have your document orientation set to Landscape in the File|Page Setup we did at the beginning.

When you import the picture in your Word document, you can resize it to better fit the flow of your text.

Friday, June 17, 2011

Formatting Source Code with astyle

astyle is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages. It is cross-platform and gets the job done pretty well.

I use a combination of these switches to get code formatted to my liking:

#!/bin/sh
#
# Name: indent++
#
astyle \
    --style=1tbs \
    --indent=tab=4 \
    --indent-switches \
    --indent-preprocessor \
    --convert-tabs \
    --break-blocks \
    --break-closing-brackets \
    --unpad-paren \
    --pad-header \
    --pad-oper \
    --pad-paren-out \
    --add-brackets \
    --align-pointer=type \
    --align-reference=type < $file > $file.indented

mv $file $file.orig
mv $file.indented $file

Wednesday, March 9, 2011

How to change text font size in Microsoft MSDN 2008 Document Explorer

MSDN Library that comes with Visual Studio 2008 shows its content in Microsoft Document Explorer. The Explorer is a index/search engine with embedded Internet Explorer that shows the text.

One annoying thing that I found is the size of the text fonts. They appear really tiny on my Dell 2009W Wide Screen monitor that sports 1680x1050 resolution.

The Font Resize button on Document Explorer's own toolbar has no effect on the text font size. It took me some digging to learn how to control the font size:

  1. Start MSDN Library. Wait for Microsoft Document Explorer to start up.
  2. Click on Tools->Options. The Options dialog comes up. Note that its Environment/'Fonts and Colors' setting has no effect on the size of the text fonts.
  3. Instead, select Help/'Web Browser', and click on 'Internet Explorer Options..." button.
  4. Choose 'General' tab and click on 'Accessibility' button in 'Appearance' section. 
  5. In 'Accessibility' dialog that pops up under 'Formatting' section enable:
    • Ignore font styles specified on webpage
    • Ignore font sizes specified on webpage 
  6. Confirm to all the changes

This should effectively change the size of the text fonts in the Document Explorer.

Thursday, February 10, 2011

Doxygen tips-n-tricks


Doxygen documentation parser written by Dimitri van Heesch (big thanks!) has been a part of my professional tool chest for a good part of the last ten years. Along with Graphvis (layered drawings of directed graphs), a graph visualization toolkit from AT&T and Lucent Bell Labs, it provides a quick and intuitive way of documenting your source code. Doxygen output also helped me tremendously to navigate through a maze and learn quickly the inner workings of every new project I came across in the past.

First, you need to create a configuration file, Doxyfile, either by running doxywizard GUI or from command-line with 'doxygen -g' command.

Adjust Configuration File

First thing you would want to adjust is project name/number, document encoding and output directory.

PROJECT_NAME = Granule
PROJECT_NUMBER = 1.4.0

OUTPUT_DIRECTORY = dox
DOXYFILE_ENCODING = UTF-8
TAB_SIZE = 4
FULL_PATH_NAMES = NO
HIDE_UNDOC_CLASSES = NO
HIDE_IN_BODY_DOCS = YES 
EXTRACT_ALL = YES
RECURSIVE = YES

If it is a "C" project, optimize for "C":

OPTIMIZE_OUTPUT_FOR_C = YES

For C++ project,

BUILTIN_STL_SUPPORT = YES
TEMPLATE_RELATIONS = YES
CLASS_GRAPH = YES
UML_LOOK = YES
GRAPHICAL_HIERARCHY = YES


You need to tell doxygen the file types to parse:

FILE_PATTERNS = *.h *.cpp

One thing I like about doxygen is its ability to shed some light on the function call chain in a program. For every function, it can optionally list all callers as well as call chain.

CALL_GRAPH = YES
CALLER_GRAPH = YES
REFERENCED_BY_RELATION = YES

COLLABORATION_GRAPH = YES
SOURCE_BROWSER = YES
INLINE_SOURCES = YES

ALPHABETICAL_INDEX = YES
GENERATE_TREEVIEW = YES   (side pane) 
SEARCHENGINE = YES

GENERATE_HTML = YES
HTML_OUTPUT = html
HTML_FILE_EXTENSION = .html
HTML_HEADER =
HTML_FOOTER = 
HTML_STYLESHEET = 
HTML_ALIGN_MEMBERS = YES
DISABLE_INDEX = NO

HAS_DOT = YES
DOT_IMAGE_FORMAT = png
DOT_FONTNAME = FreeSans

Cross-Referencing with Tag Files

If your project is an collection of disjointed libraries each within the boundary of its own package, you might want to enable cross-referencing with tagging feature of doxygen.

To better illustrate the concept, I will use my own software as an example. My flashcards program, Granule, internally relies on libassa (a collection of reusable shared code). In my Linux development environment, I have the following code arrangement:

Library

/path/to/libs/libassa/assa/
|
|-/dox/
|    \--html/
|         \--index.html
|--{*}.h
|--{*}.cpp
|--Makefile
\--Doxygen-lib

Application

/path/to/apps/granule/src
|
|--/dox/
|    \--html/
|         \--index.html
|--{*}.h
|--{*}.cpp
|--Makefile
\--Doxygen-app

The two paths are distinctly different. In order for the HTML documentation generated by Doxygen-app include references to the documentation generated by Doxygen-lib, we need
  • configure Doxygen-lib to generate its tag file
  • configure Doxygen-app to include external tag file(s) to resolve external references
With this in mind, let's look at the syntax of both configuration files:

--- Doxygen-lib ---

GENERATE_TAGFILE = dox/html/libassa.tag


--- Doxygen-app --

TAGFILES = /path/to/libs/libassa/assa/dox/html/libassa.tag=/path/to/libs/libassa/assa/dox/html


BTW, the path can be either absolute (as show in the example above) or relative. As you can see for yourself, the syntax of the TAGFILES parameter is a bit awkward by any standards. I would prefer instead an option of an environment variable to resolve the location of tag files which is more suited for a team of collaborative developers.

However, it gets the job done and that is what's important at the end of the day.

Happy coding!

Friday, February 4, 2011

How to reassign a USB mount point

The minute you plug in a USB device in your computer, Window operating system assigns a drive letter (K:, J:, etc.) to the drive. Sometimes, the assigned drive letter conflicts with another network drive such as share drive. As a result, your USB is not visible or accesible by the Windows explorer.

To fix this problem, run diskmgmt.msc utility, find the drive in question and change its drive letter to something else that would not conflict with visible drives.
  • Plug-in your USB device.
  • On Windows desktop, click on Start -> run
  • Type in diskmgmt.msc and click OK to bring up the Disk Manager:
  • Find the USB removable media in the list you wish to change the drive letter for.
  • Right-click on the drive entry, select Change drive letter/path, and change the drive letter.
  • Unplug your USB device.
  • Reboot the machine, plug the USB device back in and verify that it appears in the Windows Explorer and that device is accessible.

Friday, January 21, 2011

How to add Purify to automake project

This post describes how to add Purify support to the Automake project.

In order to instrument your automake project with Purify, you would need to perform following modifications to your project files:
  • add SUPPORT_PURIFY m4 macro to the project files
  • modify configure.ac
  • modify $rootdir/Makefile.am
  • add Purify-specific bootstrap script

Add m4 macro to test Purify installation


Under $rootdir create macros directory and add support_purify.m4 file:

AC_DEFUN([SUPPORT_PURIFY],
[
dnl Name: support-purify.m4
dnl
dnl Description: Definition of SUPPORT_PURIFY m4 macro.
dnl
dnl

AC_ARG_ENABLE(purify-linker,
[AC_HELP_STRING([--enable-purify-linker],
[Augment the linker with purify.])],
[enable_purify_linker=$withval],
[enable_purify_linker=no])

if test "x$enable_purify_linker" = xyes ; then
  echo "Option enabled --enable-purify-linker=yes"
  AC_DEFINE([USE_PURIFY_LINKER],[],[Link-time support for Purify.])
  use_purify_linker=yes
else
  use_purify_linker=no
fi

dnl ====================================================
dnl Specify location of PURIFY

AC_MSG_CHECKING(for purify)

AC_ARG_WITH(purify,
[ --with-purify=PATH Specify the prefix where purify is installed],
,
test "$purify_PREFIX" && with_purify="$purify_PREFIX")

test "$with_purify" &&
test ! "$with_purify" = no &&
purify_PREFIX="$with_purify"

AC_MSG_RESULT($purify_PREFIX)
AC_SUBST(purify_BIN)

echo "purify_BIN is $purify_BIN"

dnl ====================================================
dnl Specify options for Purify

AC_ARG_WITH(purify-options,
[ --with-purify-options=ARG manually set PURIFY options to ARG],
PURIFY_OPTIONS=$with_purify_options,
PURIFY_OPTIONS="-follow-child-processes=yes -windows=no -cache-dir=${HOME}/tmp -always-use-cache-dir=yes -view-file=${HOME}/tmp/%v-%p.pv -messages=batch")

AC_SUBST(PURIFY_OPTIONS)

dnl ====================================================
dnl Augment linker

if test "x$enable_purify_linker" = xyes ; then
echo "augment the linker"
AUX_LINKER="${purify_BIN} ${PURIFY_OPTIONS}"
echo "AUX_LINKER = $AUX_LINKER"
fi

AC_SUBST(AUX_LINKER)

CCLD="$AUX_LINKER $CXX"
CXXLD="$AUX_LINKER $CXX"

AC_SUBST(CXXLD)
AC_SUBST(CCCLD)

echo "In macro SUPPORT PURIFY: CXX = $CXX"
echo "In macro SUPPORT PURIFY: CXXLD = $CXXLD"
dnl ====================================================
dnl End of Purify macro definition
]
)

Modify configure.ac


m4_include([macros/support_purify.m4])
SUPPORT_PURIFY

Modify $topdir/Makefile.am


Add 'bootstrap-dev-purify' and 'macros/support_purify.m4' to your EXTRA_DIST target.

Purify-specific bootstrap script


Modify your bootstrap script to enable Purify instrumentation:
autogen.sh
configure --prefix=$INSTALLDIR --enable-purify-linker=yes
make

Conclusion


Now you can run 'bootstrap-dev-purify' script to let automake test Purify on your system and if successful, instrument your project with Purify-enabled object code.

Tuesday, January 11, 2011

Safe sprintf() C++ style

Scanning the legacy "C" code, often I find code snippets similar to the following code:

#include <stdio.h>

static const char msg_buf [1024];

static void log (const char* msg) {  /* log the message */ }
/* ... some place later */

int i = 43;
const char* msg = "Safety first"

sprintf (msg_buf, "%s : %d\n", msg, i);
log (msg_buf);

Any static code analysis tool such as (cppcheck, flawfinder, RATS, etc.) will mark call to sprintf() as unsafe on the ground that it doesn't check for buffer overflow.

However, if you can compile your program with C++ compiler, the easiest fix to such a problem would be to use ostringstream C++ standard library string buffer instead:

#include <sstream>

static void log (const char* msg) 
{  
   /* log the message */ 
}

/* Add a wrapper version of log() function
* that accepts stream string as an argument
* and clears up buffer after the message
* has been logged.
*/ 
static void log (const std::ostringstream& msg)
{
    log (msg.str().c_str());
    msg.str (""); 
}

namespace {
    std::ostringstream msg_buf;
}

/* ... some place later */

int i = 43;
const char* msg = "Safety first."

msg_buf << msg << " " << i << std::endl;

log (msg_buf);  /* overloaded */ 


Note that we added a wrapper log() function to help us hide the syntax and to clean up the buffer after the message has been logged.

Formatting Options

It takes time to get used to C++ stream formatting.
For example, the following code snippet prints an integer number as upper case hex to the buffer:

#include <stdio.h>

int i = 43; 
char msg_buf[1024];

snprintf (msg_buf, 1024, "Print hex number : %0X.", i);

To achieve equivalent result with C++ stream, you need to insert a number of stream manipulators first.

std::hex manipulator specifies that integers should be treated as hexadecimal (base 16) values.

std::showbase manipulator specifies that the base of a number is to be output ahead of the number (a leading 0 for octals; a leading 0x or 0X for hexadecimals). This setting is reset with stream manipulator noshowbase.

std::setfill (char) manipulator pads empty space of the justified field with char character. By default, the filler is a whitespace.

std::uppercase manipulator converts the stream to upper case.

std::width (d) manipulator specifies the minimum widht of the data field.

#include <sstream>
#include <iomanip>

int i = 43;
std::ostringstream msg_buf;

msg_buf << "Hex number : "
        << std::hex << std::showbase 
        << std::setfill('0') << std::uppercase
        << i << ".";


For example, to print a date in YYYYDDMM format, the "C" code might look like this:

#include <stdio.h>

int year  = 2011;
int day   = 14;
int month = 1;
char msg_buf[1024];

snprintf (msg_buf, 1024,
          "Today's date: %04d%02d%02d", 
          year, day, month);

We can easily rework the solution in a safe C++ manner:

#include <sstream>
#include <iomanip>

int year  = 2011;
int day   = 14;
int month = 1;

std::ostringstream msg_buf;

msg_buf << std::setw(4)
        << year
        << std::setw(2) << std::setfill('0') 
        << day
        << std::setw(2) << std::setfill('0') 
        << month;

Tuesday, January 4, 2011

Qt4 SDK packages Fedora Core 14

I took me some cycles to figure out which RPM packages to install under Fedora Core 14 running GNOME desktop to begin development with Qt4 libraries.

Here is the bare minimum:
  • qt-4.7.1-7.fc14.i686
  • qt-x11-4.7.1-7.fc14.i686
  • qt-devel-4.7.1-7.fc14.i686
And documentation, examples, etc:
  • qt-doc-4.7.1-7.fc14.noarch
  • qt-demos-4.7.1-7.fc14.i686
  • qt-examples-4.7.1-7.fc14.i686
To develop with Qt4 IDE:
  • qt-creator-2.1.0-4.rc1.fc14.i686 

To develop with WebKit:
  • qt-webkit-4.7.1-7.fc14.i686
  • qt-webkit-devel-4.7.1-7.fc14.i686
To develop for smart phones, netbooks, and non-mobile personal computers:
  • qt-mobility-1.0.1-3.fc14.i686
  • qt-mobility-devel-1.0.1-3.fc14.i686
  • qt-mobility-doc-1.0.1-3.fc14.noarch