Recently modified blog posts
News and other things I find interesting
RSS feeds and ATOM feeds

Django tagging
Last modified: Sunday, July 18, 2010
I added tags support for my blog posts via using the Django tagging library. As you can see each blog post now has any number of tags associated with them.
Overall Django tagging works a lot like Django comments in that you can attach tags to any model in your project.
To implement Django tagging inside my site, I did need to make a slight model change to have a tags field in my blog news item table. Apparently this field is only used for caching and so that it shows up in the Django administration. When you modify your model in Django administration a new text field appears labeled Tags that you can enter in space separated tags for your model.
I made each tag into a link which bring you to all posts on the site about the specified tag. Also you can find a Tags link on the right hand bar which displays the list of tags in use and how many posts each tag has.
The Django tagging framework has many other features which I didn't use, but from what I used it's a great and very easy to use library.
2 books I'm reading...
Last modified: Saturday, July 17, 2010
I'm currently reading:
- Gray Hat Python: Python Programming for hackers and reverse engineers by Justin Seitz
- C# in depth 2nd edition by John Skeet
I'm about half way through both books. They're both great books so far.
Forward declaring enums in C++
Last modified: Saturday, July 17, 2010
Forward declaring things in C++ is very useful because it dramatically speeds up compilation time. You can forward declare several things in C++ including: struct, class, function, etc...
But can you forward declare an enum in C++?
No you can't.
But why not allow it? If it were allowed you could define your enum type in your header file, and your enum values in your source file. Sounds like it should be allowed right?
Wrong.
In C++ there is no default type for enum like there is in C# (int). In C++ your enum type will be determined by the compiler to be any type that will fit the range of values you have for your enum.
What does that mean?
It means that your enum's underlying type cannot be fully determined until you have all of the values of the enum defined. Which mans you cannot separate the declaration and definition of your enum. And therefore you cannot forward declare an enum in C++.
The ISO C++ standard S7.2.5:
The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration. It is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than
intunless the value of an enumerator cannot fit in anintorunsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0. The value ofsizeof()applied to an enumeration type, an object of enumeration type, or an enumerator, is the value ofsizeof()applied to the underlying type.
You can determine the size of an enumerated type in C++ by using the sizeof operator. The size of the enumerated type is the size of its underlying type. In this way you can guess which type your compiler is using for your enum.
What if you specify the type of your enum explicitly like this:
enum Color : char { Red=0, Green=1, Blue=2};
assert(sizeof Color == 1);
Can you then forward declare your enum?
No. But why not?
Specifying the type of an enum is not actually part of the current C++ standard. It is a VC++ extension. It will be part of C++0x though.
Learning Cocoa with Objective C on XCode
Last modified: Saturday, July 17, 2010
I've been starting to learn programming on my apple computer. The cool thing is that all dev tools on Mac are free unlike in windows. The bad part is that it is quite a bit different from programming Win32 :(. I guess maybe i should be using Carbon (Which is more for porting applications from C++). Apple claims that they will be updating both Cocoa and Carbon as time goes on equally.
Tags: apple c++ cocoa objective-c xcode
Add a new commentSlow compilation time in C/C++
Last modified: Saturday, July 17, 2010
Compilation in C/C++ is a very big operation due to C/C++'s complex grammar. Source files typically residing in .cpp are always only compiled one time; however, header files typically residing in .h files are compiled once per compiler execution. Each header file needs to be recompiled because there could be different effects made from the preprocessor.
Since an individual header file is often compiled many times, header compilation as a whole can make up a large part of your total C/C++ compilation time.
Two of ways you can do to reduce this portion of compilation time is:
- Forward declarations
- Precompiled headers
Forward declarations
Extensively using forward declarations at all times will give you the biggest performance in compilation time.
Forward declaration means to declare something without defining it in a header file. Include the header file instead in the source file where it will be compiled and parsed only once.
c.h:
class C
{
public:
C()
{
}
};
d.h:
class C; //<--- This is a forward declaration
class D
{
public:
D()
{
}
C c;
};
Notice that d.h does not include c.h even know it uses a class declared in c.h
main.cpp:
#include "c.h"
#include "d.h"
int main(int argc, char **argv)
{
D d;
return 0;
}
In main.cpp it is important that you include c.h before d.h; otherwise, the compiler will complain about C being an undefined type.
Note you can also perform forward declarations with template types:
template <typename T>
class CMyClass;
Precompiled headers
Precompiled headers allow you to speed up compile time when compiling C++ source code. You typically put anything in a precompiled header that doesn't change often or ever such as the standard library includes or boost includes.
Precompiled headers are available for most C++ compilers including GCC and Visual C++. Both of those implementations are similar.
Only 1 precompiled header can be included per compilation, so therefore at a minimum per file. But in a single project you can have several different precompiled headers.
In Visual C++ the compiled headers have an extension of .pch and in GCC they have an extension of .gch.
In GCC you compile headers just like any other file but you put the output inside a file with a suffix of .gch.
So for example if you precompile stdafx.h you will have a precompiled header that will be automatically searched for called stdafx.h.gch anytime you include stdafx.h
stdafx.h:
#include <string>
#include <stdio.h>
a.cpp:
#include "stdafx.h"
int main(int argc, char**argv)
{
std::string s = "Hi";
return 0;
}
Then compile as:
> g++ -c stdafx.h -o stdafx.h.gch
> g++ a.cpp
> ./a.out
Your compilation will work even if you remove stdafx.h after step 1.
Tags: c c++ g++ visual-studio
Add a new comment | 1 comment(s)
|
Jack on
Wednesday, July 28, 2010 (04:07:30) says: |
How email works?
Last modified: Saturday, July 17, 2010
Here's a quick tutorial on how email works, SMTP, POP3, IMAP, Webmail, ...
What is a Standard?
A standard is a set of rules that are followed by all developers around the world. Some standards include HTTP, SMTP, POP3, …
There is official documentation that describes each individual standard and most standards have been around for 0 to 30 years.
Each standard document is a very detailed explanation of what the standard is and how it works. Typically a standard has an RFC number associated with it, but there are many different types of standards.
Protocols
SMTP and POP3 are ‘standards’. Each standard describes a different protocol. A protocol is any kind of communication between 2 or more computers.
What is SMTP?
SMTP is the ‘standards’ protocol that is used to send email. Your computer uses SMTP to send email. See RFC 821, August 1982
What is POP3?
POP3 is the ‘standards’ protocol that is used to receive email. Your computer uses POP3 to receive email. POP3 is also referred to as simply POP. See RFC 1939, May 1996.
POP3 typically will connect to the mail server and download messages to your computer. It can then optionally delete the message from the server (which it is usually setup to do).
How Email works
- User A wants to send an email to user B.
- User A writes up an email and presses send.
- User A’s computer, uses SMTP communication to send the email to User A’s (Yes A, not B) SMTP server.
- User A’s SMTP server, sends the email to user B’s SMTP server using SMTP communication.
- User B when he feels like it, contacts his SMTP server and uses POP3 to download the messages.
Some important notes:
The only way to send email is to use SMTP. (Actually you can also use MAPI and some other things but let's not get into that)
The only way to receive email is to use POP3. (Actually there is also IMAPv4, but we'll pretend that POP3 is the only way)
How Email Applications work:
SMTP communication is present on your computer, no matter what email client you use. Any time an email is sent out, your computer uses SMTP to send the email. It doesn’t matter if you're using Eudora, Outlook, Outlook Express, Mozilla Thunderbird, or a custom made program. All programs use SMTP to send emails.
By using standards you are guaranteed that, even know user A uses Outlook, and user B uses Eudora, and they both have different SMTP servers both of the users will be able to communicate.
What is HTTP?
Before I can get to what web mail is, you first need to know what HTTP is. HTTP is just another standard protocol. But HTTP is meant to download files and web pages, unlike SMTP which is meant to send emails. See HTTP 1.1 RFC 2616, June 1999.
What is web mail?
Web mail is an online web page that allows you to send and receive emails using HTTP.
But wait a minute, didn’t I just say that the ONLY way to send email was using SMTP?
Yes! What the web page does, is provide you with a form that you fill out. Your computer doesn’t know that it is any different from a form that you fill out to enter your credit card information, or a form that you fill out to enter your home address, or a form that you fill out to sign into another web site. All your computer knows is that you are filling out a form.
When you press the send button, your web browser sends the form to the server. The server knows that this form is for email though. So the server interprets the form and extracts the needed information. The HTTP server then uses SMTP to send the message. Because the only way that a message is going to get from User A to User B is using SMTP.
What the web browser has done is fooled you into thinking that you are sending an email. But what’s really happening, is that your web browser is filling out a form, and then the web server is using SMTP to send your email.
Can you give me a web mail walk through ?
- User A wants to send an email to User B, User A is going to use web mail.
- User A uses his browser to type in an internet address (for example: www.hotmail.com).
- User A’s computer uses HTTP to contact the server and ask for the web page that is used for web mail in this case.
- The server responds (using HTTP) to User A’s computer with a web page that gives him options to compose mail, check mail, …
- User A clicks on the compose a message link. Again User A’s computer uses HTTP to contact the server.
- The server responds (using HTTP) to User A’s computer with the web page (which contains a form) that allows User A to compose a message.
- User A fills in the web page and presses send. The page is sent back to the server using HTTP.
- In the background, unknown to User A, the web server uses SMTP to send the email to User B. Why? Because the only way to send an email is to use SMTP
- The server responds (using HTTP) to User A’s computer with a web page that says the email was sent.
How does the web server use SMTP?
Since SMTP is a standard protocol it uses SMTP in the same way any program would use SMTP. See the section ‘How email works’.
What is IMAPv4?
I mentioned IMAPv4 earlier. IMAPv4 is a second method used by email clients to retrieve your emails. IMAPv4 is also referred to as more simply IMAP. IMAPv4 is more complex than POP3, but gives you the ability to work on your email from multiple computers. If you use more than one computer, and you'd like to access your email from both computers, IMAP is the way to go.
IMAP stores all of its data on the mail server. In that way each mail client from each different computer can be in sync. When you read an email from one computer, your work computer will also see that the message is read. Since data is stored on the server, IMAP email accounts are typically more expensive.
Tags: email http imap pop3 smtp
Add a new commentSTL mini tutorial - strings, vectors, and maps
Last modified: Saturday, July 17, 2010
Vectors, strings, and maps are all part of the C++ standard template library (STL). You can use them in any C++ program. It doesn't matter what operating system you're programming on, if it has C++ support, then it has STL support.
Before using this tutorial you should have a good grasp at C++ templates, as well as everyting that you should know before templates. A great tutorial for beginners is at cplusplus.com
To use STL you need the following includes:
//If you want to use maps
#include <map>
//If you want to use vectors
#include <vector>
//If you want to use strings
#include <string>
And you will need to always prefix string, vector and map with std:: unless you put this:
using namespace std;
Here is an example on how to use STL strings:
string s = “hi”;//s holds "hi"
s += “ Brian”;//s now holds “hi Brian”
int iSize = s.size();//iSize holds 8
char *p = s.c_str();//p holds a pointer to an array of characters with a 0 at the end.
All character arrays, when holding strings, have a 0 at the end, this is very important. They don’t have the character “0” at the end, but they have an ascii value of 0 at the end. This zero is used to indicate the end of the char array. When you call the string's c_str() method, it will append a 0 for you at the end of the char array that it returns. You could have also used s.data(), which means the exact same thing as s.c_str().
Many people don't know this, but strings can also store binary data. By binary data I just mean any data that is non text. I.e. there may be zero's intermixed througout the string.
So for example:
string s;
//The size of pBinaryData is uiLen1; copy a new string object into s that holds the binary data from pBinaryData
s = string(pBinaryData, uiLen1);
s.append(pMoreBinaryData, uiLen2);
assert(s.size() == uiLen1 + uiLen2);
In the example above s.c_str() will now hold the binary data from both calls. You no longer have to worry about forgetting to delete binary data that you allocate. If you created pBinaryData and pMoreBinaryData on the heap, you can delete[] them after the call to append. Anything that you put in a string will have it's own memory, you don't need to worry about freeing it.
Here is an example on how to use STL vectors:
You use a vector to store a list of values. A value can be a number, string, or any other class object that you create. This list can grow to any number of elements, unlike an array.
vector<string> v;
v.push_back(“hi”);
v.push_back(“hi2”);
v.push_back(“hi3”);
assert(v.size() == 3);
for(int a = 0; a < v.size(); ++a)
{
string s = v[a];
s += “Brian”;
}
//The above "for loop" is pointless obviously :). It will go through each element in the vector, and copy the vector element's data to a variable "s"
// When I go s+= "Brian" it won’t modify the vector contents in any way. Because when you access each elemetn v[a], it will create a copy of the string.
// After the for loop, the vector is left unchanged.
Notice how I wrote vector
Vector of your own class objects:
class Cat
{
public:
string str1;
string str2;
string str3;
int x;
};
vector<Cat> vMyCats;
Cat minou;
minou.str1 = “hi”;
minou.str2 = “hi2”;
minou.str3 = “hi3”;
minou.x = 5;
vMyCats.push_back(minou);
assert(vMyCats.size() == 1);
//vMyCats holds a vector of Cat objects. Each cat has 3 strings and 1 integer.
Here is an example on how to use STL maps:
You use a map to store a lookup table. i.e. to associate something with something else.
map<string, string> myMap;
myMap.insert(pair<string,string>(“firstName”, “brian”);
myMap.insert(pair<string,string>(“lastName”, “bondy”);
myMap.insert(pair<string,string>(“age”, “24”);
string strFirstName = myMap[“firstName”];
assert(strFirstName == "brian");
You can use a map with other things then a string: map
FROM can be any type, for example int, Cat, string,
TO can be any type, for example int, Cat, string
The type you use for the FROM you use in the brackets to do lookups
The type you use in the TO is the value that’s associated with it.
Windows 64Bit AMD and Intel processor rundown
Last modified: Saturday, July 17, 2010
I just purchased a new 64-Bit AMD computer and installed Windows 64-Bit AMD for the first time.
Here’s a short rundown on what I found out during my first night of using it.
There are 2 main types of 64-bit processors, IA64 and x64, and they run different versions of windows 64-bit.
That means that every driver built for Windows 64-Bit needs to be compiled twice.
The IA64 processor was called the Itanium. It was a huge flop because of problems with the way they did the caching. It was replaced with the Itanium 2, which has a better design for cache. Itanium 2 does not mean dual processors, but instead it means the 2nd design of Itanium (version 2). It is referred to the Itanium 2 CPU. It is now referred to as IA64 only.
With x64 you can install both Windows 32 bit or Windows 64 bit. With IA64 you can only install the IA64 Windows. You can run 32-bit apps on all variants. For the 64-bit Windows versions you can run the applications through an emulator that works transparently called Windows 32 on Windows 64 (WOW64)
The same does not hold for 32-bit drivers though. WOW64 is only for user mode applications.
On 64-bit machines there are actually 2 different registries. A 32-bit registry and a 64-bit registry. They are treated as different registries by 32-bit and 64-bit applications but the 32-bit registry is just a subkey for the 64-bit registry. Windows will automatically route an application to the proper registry depending on if it’s 32-bit or 64-bit.
Because of WOW64, any application that was compiled before on a 32-bit machine, will still work in 64-bit Windows. This isn't because it supports it direclty, but because windows is using WOW64 to translate the 32-bit calls to 64-bit ones. If however a program depends on a driver, or if it integrates with the windows shell, then that program will no longer work. I believe IPC is also translated on the fly by WOW64.
Some other useful information.
These 2 directories are both for 64-bit applications/drivers only. Pretty great naming! :), but I'm sure it will save many headaches for inf file authors.
C:\Windows\System32\C:\Program Files\
These 2 directories are for 32-bit apps and drivers only, they use 32-bit emulation
C:\Windows\SysWOW64\C:\Program Files (x86)\
So developing for 64-bit machines can get a little tricky if you actually want it to run as a 64-bit program. If it's running as a 32-bit program, then your ints will still be 4 bytes, and everything else you hold true will also still hold.
Right now, when I want to make a 64-bit program, I use a makefile instead of using Visual Studio directly. You can use different NSIS .exes to generate the different installers. I suggest to build each project with 3 different versions of NSIS. Then if you want to, you can build a 32-bit installer wrapper that combines each of your installers.
Archived old front page
Last modified: Saturday, July 17, 2010
I archived the old front page. You can access that page here
Tags: site
Add a new commentWhat's my IP?
Last modified: Saturday, July 17, 2010
Added a page to the Other section that allows you to get your external IP address. You can access that page here.
Tags: site
Add a new comment
