February 2012

14:32
Sun
26
Feb 2012

NoConsole - My C# Project

Some time ago I started a new personal project. It is called NoConsole. I uploaded it to SourceForge, with C# source code in Git repository, under GNU GPL. The idea (and name) comes from my aversion to console-based programs. I want to create a framework which enables easy development of GUI over command line tools.

Probably the most interesting part is C# scripting. .NET Framework includes C# compiler, so user doesn't have to install any development environment to be able to compile C# source code into managed EXE or DLL files. So if my program is coded in C#, there was no purpose in including any other scripting language. Scripts that control this program are just CS files. They are compiled into DLL-s on first use. They can execute arbitrary code, but the library shipped with NoConsole simplifies some some common tasks and enables integrating with host application:

The project is in early stage of development. Much can be added or changed. But I already use it to simplify some of the work I do, using my scripts I didn't share here.

Comments (5) | Tags: productions gui .net | Author: Adam Sawicki | Share

22:49
Fri
10
Feb 2012

How to Make Visual Studio Debugger not Step Into STL

It is annoying when you debug your C++ code in Visual Studio, want to step into your function, but the debugger enters source code of some STL container or string. For example, in the following code you will first enter std::basic_string constructor, then std::vector operator[] and then body of MyFunction.

MyFunction(std::string("abc"), myVector[0]);

It turns out there is a way to disable stepping into certain code, using regular expressions. To do this:

  1. Run Registry Editor (regedit.exe).
  2. Navigate to the key appropriate to your version of Visual Studio or Visual C++. "VCExpress" is for free Visual C++ Express, while "VisualStudio" is for commercial Visual Studio. Number is product version. Additional "Wow6432Node" should be used when working in 64-bit Windows. For example:
    • Visual C++ 2005 Express in 32-bit Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VCExpress\8.0\NativeDE\StepOver
    • Visual Studio 2005 Professional in 32-bit Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\NativeDE\StepOver
    • Visual Studio 2008 Professional in 64-bit Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\NativeDE\StepOver (for version 2008 it's 9.0 not 8.0, the article linked below is wrong!)
    • Visual Studio 2010 Professional in 64-bit Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\NativeDE\StepOver
  3. Create new String Value with any name and a value containing regular expression to match against identifiers you want to exclude, like "std\:\:.+" for all identifiers from STL namespace (including members of std::string, std::vector and so on). I assume you know the syntax of regular expressions.
  4. In Visual up to 2008, it starts working after you start new debug session (e.g. with F5). In Visual 2010, you have to restart whole IDE.

Here is the full story:

In May 2007 I asked the question on forum.warsztat.gd and written this blog entry (in Polish). Now I've also found this article: How to Not Step Into Functions using the Visual C++ Debugger, Andy Pennell's Blog and this StackOverflow question: Is there a way to automatically avoiding stepping into certain functions in Visual Studio?

From that I've learned that Visual Studio 6.0 used autoexp.dat file, while new versions use Windows registry.

Rules entered in registry can be suffixed with case-insensitive "=NoStepInto" (which is the default) or "=StepInto".

Aside from regular expression syntax you can use additional special codes: \cid (identifier), \funct (function name), \scope (class or namespace, like myns::CClass::), \anything (any string) and \oper (C++ operator).

Double backslashes you can meet on some web pages come from the REG file format, where backslash must be additionally escaped, like "\\:". If you enter regular expression straight into regedit, it should be "\:".

Comments (4) | Tags: debugger visual studio c++ | Author: Adam Sawicki | Share

22:05
Wed
08
Feb 2012

Optimizing Subqueries in MySQL

I'm not SQL guru, but I have to use databases from time to time, whether at home or at work, for desktop or (mostly) web applications. Some time ago I discovered a trick which greatly optimizes performance of complex queries that contain subqueries. I used it again recently when coding new comments administration panel for this website.

In my previous job I had a case when after several months of work my system gathered so much data that it couldn't generate report in any reasonable time. After applying this optimization reports from same data were generated instantly.

I know for these who deal with databases every day this "trick" or "optimization" is probably very basic, but if you - just like me - do only simple database tasks, it can be new to you. Here are the details:

Let's say we code a blog CMS and we have two tables:

CREATE TABLE posts (
    post_id int(11) NOT NULL AUTO_INCREMENT,
    title varchar(128) NOT NULL,
    content text NOT NULL,
    time datetime NOT NULL,
    PRIMARY KEY (post_id),
    KEY time_k (time)
);

CREATE TABLE comments (
    comment_id int(11) NOT NULL AUTO_INCREMENT,
    post_id int(11) NOT NULL,
    author varchar(32) NOT NULL,
    text text NOT NULL,
    time datetime NOT NULL,
    PRIMARY KEY (comment_id),
    KEY post_id_fk (post_id),
    CONSTRAINT post_id_fk FOREIGN KEY (post_id)
        REFERENCES posts (post_id) ON DELETE CASCADE ON UPDATE CASCADE
);

Read full entry > | Comments (16) | Tags: sql | Author: Adam Sawicki | Share

17:38
Sat
04
Feb 2012

Algorithms for Managing Tree Structure

What are the ways of storing a tree data structure in memory? I mean a tree with arbitrary number of child nodes. Probably first thing that comes to our minds - as high level programmers - is to dynamically allocate each node as object of some class that would store its data and a collection of pointers to its children. Opposite approach would be to store all nodes in some array - a continuous piece of memory, where each node would refer to other nodes by index or something. Third approach - something in between in terms of sophistication, as well as efficiency - is to have dynamically allocated nodes, but not to store a collection of all child nodes. It is a good idea to employ kind of linked list here. I'd like to describe such data structure and basic algorithms for manipulating it.

The idea is that each node stores - besides its data - 5 pointers (possibly null) to some other nodes: parent, previous sibling, next sibling, first child and last child. It could be seen as equivalent to doubly linked list. It has much more pointers that is needed to be able to traverse whole tree, but thanks to this you can traverse such tree in any order, as well as insert and remove nodes at any point, with best possible time complexity. A node with Parent == null is considered a tree root. Here is a picture of a single node and example tree:

Read full entry > | Comments (5) | Tags: algorithms .net | Author: Adam Sawicki | Share

18:05
Thu
02
Feb 2012

CPrintStream - Polymorphic Printf

Constructing and sending somewhere (like to the console or to file) a string created ("formatted") from multiple values of different types (strings, numbers) is a common task in every programming language. C implements it using printf function, which is very convenient, although not type safe. In C++ it is recommended to use streams from standard library, but they are slower, as well as much less pleasant to code comparing to the functions that use formatting string. Let's say you have a numeric variable and you want to print it as 8-digit hexadecimal value, like "0x00001234". Which option looks better?

#include <cstdlib>
#include <iostream>
#include <iomanip>

unsigned u = 0x1234;

// Option 1
printf("0x%08X\n", u);

// Option 2
std::cout << "0x" << std::setw(8) << std::setfill('0') << std::hex << u << std::endl;

I choose option 1 :) In the world of GUI applications, printing text messages may seem not so common, but sometimes it it useful, e.g. for logging debug information. Recently I thought: Why not connect the C-style printing with what is good in C++: classes and polymorphism? That's how I coded simple CPrintStream class and some derived classes, which I'd like to share here:

PrintStream.hpp
PrintStream.cpp

Abstract base class CPrintStream defines method printf(const char* format, ...) for printing a formatted message to some kind of target. Derived classes implemented this as printing to system console (CConsolePrintStream), file (CFilePrintStream) or a buffer in memory (CMemoryPrintStream).

Here are some implementation details. First problem I had to overcome was that the variable argument list ... cannot be passed from function to function. It has to be resolved to a value of type va_list using va_start and va_end functions. Fortunately this va_list object can be passed as argument to another function and all functions that take ... (like printf, fprintf) have their equivalents taking va_list (like vprintf, vfprintf). So my CPrintStream::printf method just converts ... to va_list and passes it to another method called vprintf, and only this one is pure virtual to be overriden in derived classes.

class CPrintStream {
public:
    virtual ~CPrintStream() = 0 { }
    void printf( const char* format, ... );
    virtual void vprintf( const char* format, va_list argList ) = 0;
};

CConsolePrintStream does its job using vprintf function from standard C library - the va_list equivalent of printf. CFilePrintStream uses fopen_s to open a file of type FILE* and does printing with vfprintf function - the va_list equivalent of fprintf.

Finally, the CMemoryPrintStream uses std::vector<char> as buffer for raw binary data. I believe that's the best choice as: 1. STL vector can be easily resized, 2. char is the type recommended to represent raw bytes, 3. vector stores data in continuous memory that you can safely access by writing &myVector[index], which is not guaranteed in std::string. How do I store formatted string in memory? I use vsprintf_s - the va_list equivalent of sprintf. But before this, I calculate required length with _vscprintf - a function I've learned about recently. I believe using it is much better than sprintf-ing to some buffer of constant length, e.g. char temp[1024], like many programmers do.

Comments (1) | Tags: c++ | Author: Adam Sawicki | Share

STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2014 Adam Sawicki
Copyright © 2004-2014 Adam Sawicki