Compiling GtkGlExt on Windows

September 21st, 2008

I was developing a 3D sample application wit Gtkmm and GtkGlExtmm. I needed to provide Windows binaries and that literally yesterday.

The developers of Gtkmm provide a all inclusive SDK and runtime installer, so that was not an issue. They actually took over maintenance for the installer and updated it to 2.12. It used to be a third party product that used in parts the glade for windows installer for Gtk+ and was always a version old. The problem was that GtkGlExt and GtkGlExtmm is not bundled with that installer, unlike other things like libxml++ are bundled with it.

On the side of GtkGlExt, there was no real help, since the binaries provided for Windows are outdated and only for GtkGlExt and not GtkGlExtmm. So I had to install the two libraries from source.

The last time I seriously developed under Windows I used Cygwin and cross built to mingw32 (native Windows) target. The reason was that Cygwin provides all necessary tools in a sufficiently recent version to bootstrap any Automake/Autoconf project and proceeded replacements for POSIX only features.

So after installing the Cygwin with the bare bones development tools, I started to build GtkGlExt. Of course it failed to build the test program in configure. I disabled the test and got it almost to compile. I fixed on odd rule in a Makefile and it finally worked fine. The same was for GtkGlExtmm. Bot took forever to complete and needed a bit of touching up. Finally I got onto my actual program. It compiled fine and then crashed with a segmentation fault. At that point my hart skipped a beat.

The reason for the test to fail, seemed to have something to do with my general setup. The first odd thing was that I needed the cygwin.dll, though I had cross built to mingw32. Although this is a common problem with C++ programs, it normally is not an issue. So I installed gdb and painfully stepped through the program, only to find out that I crashed in some trivial code in Glib::RefPtr, Glibmm’s smart pointer class. This smelled of version and compiler settings mismatch; actually It stank. At that point confused and irritated, I did not know how to really fix the problem.

I looked at the Gtk and co. binaries and found out that they where perfectly mingw32 and had zero cygwin. I then looked at my program and it needed cygwin. Since I did not have any POSIX code, the need should not have been. I figured out that It must have not used mingw32 for everything.

Since I only wanted to compile GtkGlExt and my program, I figured I did not need anything that MinGW and MSys did not provide. So I decided to install MinGW with MSys.

Configuring GtkGlExt went fine, even the test programs worked. This spurred some hope that I would get it done before this universe ends. The compile of GtkGlExt failed because some code generation scripts used from Glib needed perl. So I installed perl and cyrpt from the MSys packages and finally all went fine.

At first GtkGlExtmm did not work that fine. The reason was that there was a stray character in all CXXFLAGS within the Makefiles. Suddenly I remembered the other reason why I used cygwin last time. Well I found the stray SOH (Start of Heading) character and replaced it with a space. Now all worked fine.

I finally got my program to compile and run. Boy was I relived as this workd. The best part is I now have a sane build envirnement on my Windows PC for Gtk, something I pushed off for a relative long while.

HOWTO Compile GtkGlExt on Windows

You need:

  1. Install MinGW. This should be stright forward, but check that g++ and make are installed.
  2. Install MSys. Again simple installer.
  3. Copy the perl and crypt package to a place you find again, like C:\msys\1.0\home\username.
  4. Open a MSys shell and type cd /
  5. Untar perl: tar -xjf /home/usename/perl-5.6.1-MSYS-1.0.11-1.tar.bz2
  6. Untar crypt: tar -xjf /home/usename/crypt-1.1-1-MSYS-1.0.11-1.tar.bz2
  7. Copy GtkGlExt and GtkGlExtmm source to a place find again. C:\msys\1.0\home\username.
  8. In the MSys shell go the place with your sources: cd /home/username
  9. Set the environement variable PKG_CONFIG_PATH to msys local path: export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
  10. Untar GtkGlExt: tar -xzf gtkglext-1.2.0.tar.gz
  11. Enter the GtkGlExt source tree: cd gtkglext-1.2.0
  12. Configure, build and install GtkGlExt: ./configure && make && make install
  13. Exit GtkGlExt source tree: cd ..
  14. Untar GtkGlExtmm: tar -xzf gtkglextmm-1.2.0.tar.gz
  15. Enter the GtkGlExtmm source tree: cd gtkglextmm-1.2.0
  16. Configure, build and install GtkGlExtmm: ./configure && make && make install

Now you are done. You can find the binaries in C:\msys\1.0\local\bin, along with all other stuff required to build your application.

Binary GtkGlExt

Alternativly you can use my build result and downlaod gtkglextmm-120-win32.tar.gz.

CMS in 10 Minutes with Django

July 16th, 2008

Install Django

Fist you need to install Django. I will not repeat the documentation on Django. So you should read the Django’s Installation Guide.

There is to note that I am using the bleeding edge version from their subversion repository. This is done in anticipation of their 1.0 release, which is scheduled for beginning of September. You can use the 0.95 version, but It lacks some juicy bits.

Create a Project

All Django project (website) starts the same way:

django-admin.py startproject rioki

This will create a template, but usable website.

Embedded Web Server

On of the nifty features is that Django comes with a embedded web server that can (and only should) be used for development. This feature really cuts down development time.

To use it launch the server (in the project directory):

./manage.py runserver

You will now find your website under http://127.0.0.1:8000/.

Well it actually is only a page that tells you have not “configured any urls”. We will get to that soon.

Configuration

Well the first step is to configure your site. This is a simple process, open settings.py in your favorite text editor and edit the file as appropriate.

Mine looks about so:

from os.path import join, dirname

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Sean "Rioki" Farrell', 'i.dont.think@so.com'),
)

MANAGERS = ADMINS

DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = join(dirname(__file__), 'database')

TIME_ZONE = 'Europe/Berlin'

LANGUAGE_CODE = 'en-us'

SITE_ID = 1

USE_I18N = False

MEDIA_ROOT = join(dirname(__file__), 'media')

MEDIA_URL = '/media/'

ADMIN_MEDIA_PREFIX = '/admin_media/'

SECRET_KEY = 'Mine, not yours...'

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.doc.XViewMiddleware',
)

ROOT_URLCONF = 'rioki.urls'

TEMPLATE_DIRS = (
    join(dirname(__file__), 'templates')
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
)

I used a nifty trick for the different paths. For security and stability reasons, you are to give absolute paths for the values such as MEDIA_ROOT. Well I use join(dirname(file), ‘media’), which makes it relative to the settings.py file. As a result I can move the entire package around and always be right and an absolute path.

Create The Database

Now is a good time to create the initial database:

./manage.py syncdb

Well this will load the initial tables and ask you if you want to create a super user.

The Admin Backend

One of the most powerful features is the admin backend. You might not see that in this post.

To use the admin you need to add 'django.contrib.admin‘ to the INSTALLED_APPS. You need to rerun syncdb, wich you must almost always do after you add something to INSTALLED_APPS.

./manage.py syncdb

In addition uncomment lines for the admin in urls.py. You urls.py should about look like that:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^admin/', include('django.contrib.admin.urls')),
)

You can see the admin interface under http://127.0.0.1:8000/admin/

Here you can see one of the nice features of Django: Instant pretty URLs. This comes from the fact that all incoming requests are matched against a table of regular expressions.

You might want to change the Sites entry from example.com to your website, but that’s all for now.

Static Content: Flatpages

Django comes with a module for static content, that is django.contrib.flatpages.

To use it add 'django.contrib.flatpages‘ to INSTALLED_APPS and 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware‘ to MIDDLEWARE_CLASSES.

Rerun syncdb:

./manage.py syncdb

Now you can add some pages via the admin. I added ”/” for the portal page and ”/imprint/” for the imprint. You can find the pages you created at http://127.0.0.1:8000/ and http://127.0.0.1:8000/imprint/.

Well actually, you can’t. It is still complaining about a missing template. Let’s create that.

Create the file templates/flatpages/default.html:

<html>
    <head>
        <title>{{ flatpage.title }}</title>
    </head>
    <body>
        <h1>{{ flatpage.title }}</h1>
        {{ flatpage.content }}
    </body>
</html>

As you can see the content is copied verbatim. You got two solutions you either write the content HTML or use one of the built-in markup languages.

Markup

Django comes bundled with 3 markup languages, that is Textile, Markdown and ReST. To enable those you need to add 'django.contrib.markup‘ to your INSTALLED_APPS. And change the template to look like this:

{% load markup %}
<html>
    <head>
        <title>{{ flatpage.title }}</title>
    </head>
    <body>
        <h1>{{ flatpage.title }}</h1>
        {{ flatpage.content|textile }}
    </body>
</html>

As you can see we are using textile.

Serving Media Files

During debug we are using the builtin server. That has the consequence that media files are not explicitly served. To fix this we we extend urls.py file:

from django.conf.urls.defaults import *
from django.conf import settings

urlpatterns = patterns('',
    (r'^admin/', include('django.contrib.admin.urls')),
)

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT})
    )

As you can see, the media files are only server through Django when in debug mode. In production mode, on the production website media is server through apache. This is a obvious performance issue.

Design

Well so far we are “done”, except that the site looks really crappy. I took the design from this site, as it will replace it and just adapted it to Django.

I created the site rump templates/default.html:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title>{% block title %}Rioki{% endblock %}</title>
        <link rel="stylesheet" type="text/css" href="/media/design/default.css" />
    </head>
    <body>
        <div class="page">
            <div class="header">
                {% block header %}
                    <a href="/">Start</a> |
                    <a href="/blog/">Blog</a> |
                    <a href="/about/">About</a> |
                    <a href="/proejcts/">Projects</a>
                {% endblock %}
            </div>
            <div class="sidebar">
                {% block sidebar %}
                    <h3>Sidebar:</h3>
                    <ul>
                        <li><a href="http://www.libqgl.org">libQGL</a></li>
                        <li><a href="http://www.sf.net/projects/lunar-exodus">Lunar Exodus</a></li>
                        <li><a href="/stl_algorithm_shortcuts">STL Algorithm Shortcuts</a></li>
                        <li><a href="/tstring">std::tstring</a></li>
                        <li><a href="/blog:itx_rock_my_web_app">ITX Rock my Web App</a></li>
                        <li><a href="http://www.motivator-generator.com">Motivator Generator</a></li>
                    </ul>
                {% endblock %}
            </div>
            <div class="content">
                {% block content %}No content here.{% endblock %}
            </div>
            <div class="footer">
                {% block footer %}
                    © 2006-2008 Sean Farrell<br/>
                    <a href="/about/">Imprint</a> |
                    <a href="/contact/">Contact</a>
                {% endblock %}
            </div>
        </div>
    </body>
</html>

I also put CSS style sheet and the images into /media/design.

This is nice, but the flatpages need the design. To do this I altered the template for the flatpages to be the following:

{% extends 'default.html' %}
{% load markup %}

{% block title %}{{ flatpage.title }}{% endblock %}

{% block content %}
    <h1>{{ flatpage.title }}</h1>
    {{ flatpage.content|textile }}
{%endblock %}

And viola, the beauty of reuse.

Done?

As far as it comes to static content, we are done. To be honest for many sites this is enough; but not for mine.

Firs of all we need the blog. This site is 90% blog, so it will really miss. I could realize it via flatpages, but that is slightly awkward.

An other issue is that media files need to be uploaded to the server via some file transfer process, I want to be able to do that via the admin, since I may not have access to the server directly.

As you can see the menus are hardwired to the template, this is Ok if they don’t change often. (Which might be the case here…) But I want to be able to change them at any time via the admin.

Finally textile is okish, but I don’t like it too much. The other markup languages are worse. I have once tried to implement the dokuwiki syntax in python, let’s see if that works out as template tag.

What needs to be done:

  • blog
  • handling of media files via admin
  • handling of menu via admin
  • integrate dokuml

Experiments with Heightmaps

June 14th, 2008

Don’t ask me how I got the idea but I started experimenting with height maps. It is incredible how much one can do with very little effort.

Heightmap

Heightmap

I riped off a few heightmaps from the internet. Here you can see a mountain range.

The results are quire nice. The only problem comes from the fact that images only have 256 distinct height values. The result can be seen in the valley, the different levels are distinctly visible. But the computation of the lighting is still slightly broken. Either there is a real bug or it is a case of “gourand just won’t cut it”. Grourand shading, the default for openGL, is quite good but in many cases where the geometry is complex the lighting samples are not sufficient.

The shading / lighting problem can be solved in many ways, but on obvious solution is per pixel lighting. The visual artifacts in the plane can be solved with a smoothing filter, but I fear that that will destroy the features in the mountains.

The rendering performance is not great, since a relative trivial implementation was used. I am planing to implement ROAM. Also just reducing the samples in areas with little change can obviously help.

Terrain

Terrain

Terrain

Terrain

Terrain

Terrain

ITX Rock my Web App

April 28th, 2008

When working on web applications, it is obvious to separate the design of a website from its core logic. Not only can different people edit the bits, it also ensures a certain design consistency is created. There are many ways to use template but one of the most prominent is PEAR’s IT/ITX. But even those have a huge potential for reuse that is not obvious.

A Subtle Introduction

If you check out the PEAR module HTML_Template_IT you will see that using it is straight forward.

Let’s write a friendly greeting program. It will get a title and message via GET or POST and display it. The simple implementation is something like:

<?php
    if (isset($_REQUEST["title"]))
    {
        $title = $_REQUEST["title"];
    }
    else
    {
        $title = "Greeting";
    }
    if (isset($_REQUEST["message"]))
    {
        $title = $_REQUEST["message"];
    }
    else
    {
        $title = "No message specified.";
    }
?>
<html>
    <head>
        <title><?php echo $title ?></title>
    </head>
    <body>
        <div align="center">
            <?php echo $message ?>
        </div>
    </body>
</html>

Now the problem is that the HTML is entangled with the code. If you have a similar page with totally different logic you end up duplicating your design. If you ever have to change something you have to do it everywhere, not to mention site wide design changes.

We will now rewrite the program to use IT template. For this we will first write the template.

<html>
    <head>
        <title>{title}</title>
    </head>
    <body>
        <div align="center">
            {message}
        </div>
    </body>
</html>

This is quite simple the {title} and {message} are place holders that are substituted. To do this we change the application as follows.

<?php

require_once "HTML/Template/IT.php";

if (isset($_REQUEST["title"]))
{
    $title = $_REQUEST["title"];
}
else
{
    $title = "Greeting";
}
if (isset($_REQUEST["message"]))
{
    $title = $_REQUEST["message"];
}
else
{
    $title = "No message specified.";
}

$template = new HTML_Template_IT("./tpl");

$template->loadTemplateFile("message.tpl", true, true);

$template->setVariable("title", $title);
$template->setVariable("message", $message);

$template->show();

The code is quite self explanatory. You create a HTML_Template_IT object and then load the template with loadTemplateFile. The variable substitution is done with setVariable and the compiled template is printed with show.

Now we can consistently show different messages from different applications. The problem now comes when you consider site wide design changes. In this case you have to go to the next level and use ITX.

The Next Level

Now let’s consider you have a finished design and you convert it to a template.

<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
    <head>
        <title>{title} - MySite</title>
        <meta name="author" content="{author}">
        <meta name="description" content="{description}">
        <meta name="keywords" content="{keywords}">
        <link rel="stylesheet" type="text/css" href="{url}/tpl/style.css" />
    </head>
    <body>
        <div class="page">
            <div class="header"></div>
            <div class="main">
                {main}
            </div>
            <div class="footer">
                &copy; 2008 Jon Doe |
                <a href="{url}/contact.php">Contact</a>
            </div>
        </div>
    </body>
</html>

This the basic outline every page gets and is the basic design of the site. Please note that placeholders for menus and such where omitted in the example.

This design is nice, only how do I get the different content there. Well there is the simple solution, put it all into main place holder. Since we want to reuse it we encapsulate all the template handling into a Page object.

The Page object is fairly simple:

<?php
require_once 'HTML/Template/ITX.php';
require_once 'inc/config.php';

class Page
{
    var $template;

    function Page($title)
    {
        $this->template = new HTML_Template_ITX(get_template_path());
        $this->template->loadTemplatefile('main.tpl', true, true);

        $this->template->setVariable('url', get_config('site', 'url'));
        $this->template->setVariable('title', $title);

        $this->template->setVariable('author', get_config('site', 'author'));
        $this->template->setVariable('description', get_config('site', 'description'));
        $this->template->setVariable('keywords', get_config('site', 'keywords'));
    }

    function set_main($value)
    {
        $this->template->setVariable('main', $value);
    }

    function show()
    {
        $this->template->show();
    }
}

The Page class basically loads the template and sets the common values. These values are pulled via get_config but you can do it in any way you like.

The class is used as follows:

<?php
require_once "inc/Page.php";

if (isset($_REQUEST["title"]))
{
    $title = $_REQUEST["title"];
}
else
{
    $title = "Greeting";
}
if (isset($_REQUEST["message"]))
{
    $title = $_REQUEST["message"];
}
else
{
    $title = "No message specified.";
}

$page = new Page($title);
$page->set_main("<div align=\"center\">{$message}</div>");
$page->show();

This is quite ok. But you can see the issue coming, you need to assemble tons of HTML bits to make a large page.

Explicit Message Page

Since we want the most explicit solution, we will create a Message_Page. To do this we start with a template.

<!-- BEGIN message -->
<div align="center">{message}</div>
<!-- END message -->

Well this is the missing bit. Its not much, but messages look this way.

The Message_Page is a s follows:

<?php

#include "Page.php"

class Message_Page extends Page
{
    function Message_Page($title)
    {
        $this->Page($title);

        $this->template->addBlockFile('main', 'main', 'message.tpl');
        $this->template->setCurrentBlock('message');
    }

    function set_message($text)
    {
        $this->template->setVariable('message',   $message);
    }

    function show()
    {
        $this->template->parseCurrentBlock('message');
        Page::show();
    }
}

Well this is where it gets interesting, we insert one template into an other by calling addBlockFile. The template message.tpl is inserted into the main placeholder and converted to a block. We add a setCurrentBlock, parseCurrentBlock pair, so that the variable scoping works properly.

Easy Forms

This seems slightly overkill, but consider the following example.

You want also to have forms on your page. The simple way is to build one template with the entire form and substitute that. But there is a nifty solution, built the form from template bits.

Here is the template:

<!-- BEGIN form -->
<h1>{title}</h1>
<!-- BEGIN instructions -->
<p>{instructions}</p>
<!-- END instructions -->
<form method="{method}" action="{action}">
    <table>
        <!-- BEGIN input -->
        <tr>
            <td valign="top">{name}</td>
            <td><input type="text" name="{variable}" value="{value}" size="{size}" /></td>
        </tr>
        <!-- END input -->
        <!-- BEGIN text -->
        <tr>
            <td valign="top">{name}</td>
            <td><textarea name="{variable}" cols="{cols}" rows="{rows}">{value}</textarea></td>
        </tr>
        <!-- END text -->
    </table>
    <input type="submit" value="{submit}">
</form>
<!-- END form -->

How this template works, we will see with the Form_Page class:

<?php

require_once 'Page.php';

class Form_Page extends Page
{
    function Form_Page($title, $method, $action)
    {
        $this->Page($title);

        $this->template->addBlockFile('main', 'main', 'form.tpl');

        $this->template->setCurrentBlock('form');
        $this->template->setVariable('title',    $title);
        $this->template->setVariable('method',   $method);
        $this->template->setVariable('action',   $action);
    }

    function set_instructions($text)
    {
        $this->template->setVariable('instructions', $text);
    }    

    function add_input($name, $variable, $value = '', $size = 50)
    {
        $this->template->setCurrentBlock('input');    

        $this->template->setVariable('name',     $name);
        $this->template->setVariable('variable', $variable);
        $this->template->setVariable('value',    $value);
        $this->template->setVariable('size',     $size);

        $this->template->parseCurrentBlock('input');
    }

    function add_text($name, $variable, $value = '', $cols = 50, $rows = 10)
    {
        $this->template->setCurrentBlock('text');    

        $this->template->setVariable('name',     $name);
        $this->template->setVariable('variable', $variable);
        $this->template->setVariable('value',    $value);
        $this->template->setVariable('cols',     $cols);
        $this->template->setVariable('rows',     $rows);

        $this->template->parseCurrentBlock('text');
    }

    function set_submit_text($text)
    {
        $this->template->setVariable('submit', $text);
    }

    function show()
    {
        $this->template->parseCurrentBlock('form');
        Page::show();
    }
}

Well basically it is the same as with Message_Page, the template is substituted into the main placeholder and common values are used. The difference comes with add_text and add_input.

Since blocks can be reused, multiple calls to add_input, will add multiple fields. The following application shows the Form_Page in action by using a contact form.

<?php
require_once 'inc/Form_Page.php';

$page = new Form_Page('Contact', 'POST', 'contact.php');

$instructions = 'If you whish to contact us, please us the following form.' .
                'We will reply via e-mail in the comming days.';
$page->set_instructions($instructions);

$page->add_input('Name', 'name');
$page->add_input('E-Mail', 'email');
$page->add_text('Text', 'text');
$page->set_submit_text('Send');

$page->show();

Static Content

Every web application has static content, to simplify access to this, I created a Static_Page class. This page will load the content from a text file and add it verbatim into to main placeholder.

<?php

require_once 'Page.php';
require_once 'inc/config.php';

class Text_Page extends Page
{
    function Text_Page($title, $text_file)
    {
        $this->Page($title);    

        $text = $this->read_text($text_file);
        $this->template->setVariable('main', $text);
    }

    function read_text($text_file)
    {
        $content_file = get_data_path() . $text_file;
        $fh = fopen($content_file, "r") or die("Could not open $content_file.");
        $content = fread($fh, filesize($content_file));
        fclose($fh);
        return $content;
    }
}

A static page would use it as follows:

<?php 

require_once 'inc/Static_Page.php';

$template = new Static_Page('Example', 'example.txt');
$template->show();

PHP and MySQL

ecx.images-amazon.com_images_i_51rlqgd8q5l._sl160_.jpg

PHP and MySQL

Well I must addmit that I did not come to the idea fully on my own. The concept with recycling templates and building forms from blocks in templates was taken from the very good book PHP and MySQL from O’Reilly.

But I cleaned up the concept and code, since I think the author is a crappy programmer, although the concepts are nice. To state a example he reuses the templates by inheriting from the HTML_Template_ITX class. Well sorry, inheritance is not for code reuse and a page definitely is not a template.

Links


std::tstring

February 18th, 2008

If you are using Microsoft Visual Studio the use of the Microsoft Foundation Clases (MFC) is often seen as a must. But MFC is a relative large library and many modules don’t use much of it. There are cases where the only piece of MFC is CString. But linking against MFC just for CString is superfluous.

Since the standard C++ library is always bundled with every compiled program the use of std::string and std::wstring is a good alternative. Conversion from and to CString is trivial, if you know the current settings.

The problem is that many functions defined by the WinAPI and MFC are dependent if Unicode is used or not. Depending on the settings either char or wchar_t us used. So code is independent to this setting TCHAR can be used. It will then be defined to the one or the other.

In the one case you can plainly convert to std::string in the other std::wstring. But which do you use in your code. This gets worse if you receive and return the strings; character set conversion is out of the question.

The Microsoft Solution

If you read about this in the Microsoft Developer Network (MSDN) you get a plain solution: Use the preprocessor.

#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif

This solution follows the same as many proposed by Microsoft. But if you give me a Euro for every #undef I needed to write in my code, bacause a macro from the WinAPI tainted my code, I would be rich.

Definition

Since we are programming C++, it is wise to solve the problem with the means of the language instead of the using a preprocessor hack. The definition of tstring follows the definition of string and wstring. tstring is a variant of the template basic_string with TCHAR. To add a little type safety and portability I defined tchar_t following the idea of wchar_t.

namespace std
{
    typedef TCHAR tchar_t;
    typedef basic_string<tchar_t> tstring;
}

tchar_t and tstring where defined in the namespace std, to show that it actually just is a special form of basic_string and fully compatible to other standard code.

Streams

Well now that you have your string class dependent on TCHAR, there is the problem of streams. Streams are also parameterized for char or wchar_t. But here the solution is straight forward. Define your own types based on ”basic_streamtype”

namespace std
{
    typedef basic_ios<tchar_t>              tios;
    typedef basic_streambuf<tchar_t>        tstreambuf;
    typedef basic_istream<tchar_t>          tistream;
    typedef basic_ostream<tchar_t>          tostream;
    typedef basic_iostream<tchar_t>         tiostream;
    typedef basic_stringbuf<tchar_t>        tstringbuf;
    typedef basic_istringstream<tchar_t>    tistringstream;
    typedef basic_ostringstream<tchar_t>    tostringstream;
    typedef basic_stringstream<tchar_t>     tstringstream;
    typedef basic_filebuf<tchar_t>          tfilebuf;
    typedef basic_ifstream<tchar_t>         tifstream;
    typedef basic_ofstream<tchar_t>         tofstream;
    typedef basic_fstream<tchar_t>          tfstream;
}

Conversion To and From CString

Conversion to and from CString, LPTCSTR or LPTSTR is plain simple. The most important aspect is that no character conversion is necessary.

Conversion from CString to std::tstring:

CString     cstr(_T("test"));
std::string std_str(cstr);

Conversion from std::tstring to CString:

std::string std_str(_T("test"));
CString     cstr(std_str.c_str());

Conclusion

Just because other modules use TCHAR, LPTSTR, LPTCSTR or CString does not mean you can’t use C++ standard classes. You only have to create your own variants.