Posts

Singletons

For a long time now I've been looking for a nice singleton implementation that meets my needs. I want a singleton that: Can't be created more than once. Can't be freed until the program terminates. Can be used as a common base class for all the program's singletons. There are plenty of implementations around that meet requirements 1 and 2, but most of those can't be overridden (usually because they retain a reference to the singleton object in a private global variable or a class var field). Then I found some code by Yoav Abrahami in his article on Delphi3000.com that can be used as a base class. The code was written for a old version of Delphi and used a TStringList to record instances of each singleton class created and only permits one instance of each class. I've adapted Yoav's code to use features of Delphi 2010 to avoid the need for TStringList and use of private global variables. I've also taken advantage of Delphi 2010's new c...

CodeSnip problems, problems, problems

Arrgh! I've been having a lot of problems with the CodeSnip program's database update code - it's been working for some and not for others. All this has happened since I converted the program to Unicode and compiled with Delphi 2010. I'm posting this for two reasons: To try to explain to long suffering users what has been going on with program lately - it's not usually this flaky. To forewarn anyone about to fall down the same hole as I have. The problem never raised its head on my system, but did on some that don't use the Windows-1252 code page. The problem was caused by a checksum failure, which in turn was caused by the downloaded data being converted to the wrong code page before the checksum got calculated: result - bang - bad checksum. My code relies on Indy components to translate downloaded content into text - and Indy and I make different assumptions about how this should be done. And I'm not sure either of us was correct, but they ar...

Delphi Tips News

Image
There's now another new RSS feed that provides news of changes to the Delphi Tips section of DelphiDabbler.com. Subscribe .

URL Decoding revisited

Time to complete the set. So far in this series I have presented URIEncode , URIDecode and URIEncodeQueryString . So here's the missing piece of the jigsaw: URIDecodeQueryString . This routine decodes a query string that has been "query-string-encoded". If you look at URL Encoding revisited you'll see that a query string is encoded with normal URI encoding, except that space characters are encoded using '+' characters instead of '%20' . So, to decode a query string we first need to replace literal '+' characters with spaces. Because the string is still URI encoded we should replace ocurrences of '+' with '%20' , not the actual space character. Once this is done we are left with a standard URI encoded string which we decode as normal. Here's the code: function URIDecodeQueryString ( const Str : string ) : string ; begin Result := URIDecode ( ReplaceStr ( Str , '+' , '%20' ) ) ; end ; ...

URL Decoding

To complement the code of my URL Encoding post , I've now developed a URIDecode routine. It attempts to decode URIs that were percent-encoded according to RFC 3986 . It also allows for some malformed percent-encoded URIs, i.e. those that contain characters outside the RFC's "unreserved" character set. Here's the code. An explanation follows. function URIDecode ( const Str : string ) : string ; // Counts number of '%' characters in a UTF8 string function CountPercent ( const S : UTF8String ) : Integer ; var Idx : Integer ; // loops thru all octets of S begin Result := 0 ; for Idx := 1 to Length ( S ) do if S [ Idx ] = cPercent then Inc ( Result ) ; end ; var SrcUTF8 : UTF8String ; // input string as UTF-8 SrcIdx : Integer ; // index into source UTF-8 string ResUTF8 : UTF8String ; // output string as UTF-8 ResIdx : Integer ; // index into result UT...

URL Encoding revisited

In my previous post I covered URI encoding part of a URI or URL. What I didn't cover was the almost trivial case of encoding a query string. The only difference is that spaces in a query string are converted to reserved '+' characters that are not then percent encoded. The only complication arises when the query string to be encoded already contains '+' symbols. They must be percent-encoded so they are not confused with the symbols that are used to replace spaces. In the original version of this post I completely misunderstood this and presented code that percent-encoded both literal and space-replacement '+' symbols. Unfortunately percent-encoding at this stage also encodes spaces as %20 . We fix this by simply replacing each occurence of %20 with a '+' symbol. Here's the code: it's a very simple function that encodes a query string value passed as a UTF-8 string parameter: function URIEncodeQueryString ( const S : UTF8St...

CodeSnip Program and Database News

Image
I've set up a new RSS feed that helps users of the Code Snippets Online Database and the CodeSnip Snippets Repository Program keep up to date with changes and updates. Subscribe .